tp6 + 百度身份证识别ocr

本文介绍了如何在ThinkPHP6框架下结合百度AI开放平台的OCR服务,实现身份证识别功能。首先需要在百度AI平台创建OCR应用,然后下载PHP的SDK包,并在TP6框架中引入和配置,最后展示相关代码的使用方法。
摘要由CSDN通过智能技术生成

一、登录百度AI开放平台创建orc应用

1、选择文字识别→创建应用→做好记录后续使用下面三个数据
在这里插入图片描述
2、选择需要操作的文字识别→买入身份识别和银行卡识别(根据自己实际需求选,这个返回的比较全面,有人头像,所以选择这分开的,要是只需要信息,买通用就行)在这里插入图片描述

二、下载sdk包
我使用的框架是php的,thinkphp6。下载包地址:https://ai.baidu.com/sdk#ocr
在这里插入图片描述
在这里插入图片描述

三、tp框架引入sdk包
在项目三方类库vendor中新建一个ocr文件夹,把下载好的sdk包放入到里面:
在这里插入图片描述
四、tp6相关代码使用
1、在app文件夹下,新建一个公用文件夹common 放入两个文件 Ai.php Image.php(用于base64上传)。同时在页面上引入
在这里插入图片描述
2、\new\app\common\Ai.php’

<?php

namespace app\common;

use think\facade\Config;
require_once '../vendor/ocr/AipOcr.php';
class Ai
{
   
    /**
     * phpmailer对象
     */
    protected $client;

    /**
     * 构造函数
     */
    public function __construct()
    {
   
        $config = Config::get('ocr');
        $this->client = new \AipOcr($config['ai_app_id'],$config['ai_api_key'],$config['ai_secret_key']);
    }

    static private function _check_file_path_type_is_url($file_path) {
   
        if (preg_match('/http/', $file_path)) {
   
            return true;
        }
        return false;
    }

    /**
     * 身份证识别
     * @param $file_path //图片地址
     * @param $front //正反
     */
    public function getIdCard($file_path,$front) {
   
        if (self::_check_file_path_type_is_url($file_path)) {
   
            return $this->client->idcard($file_path);
        } else {
   
            $image = file_get_contents($file_path);
            return $this->client->idcard($image,$front);
        }
    }

    /**
     * 银行卡识别
     * @param $file_path //图片地址
     */
    public function getBankCard($file_path) {
   
        if (self::_check_file_path_type_is_url($file_path)) {
   
            return $this->client->bankcard($file_path);
        } else {
   
            $image = file_get_contents($file_path);
            return $this->client->bankcard($image);
        }
    }

    public function request_post($url = '', $param = '') {
   
        if (empty($url) || empty($param)) {
   
            return false;
        }
//        var_dump($param);exit;
        $postUrl = $url;
        $curlPost = $param;
        $curl = curl_init();//初始化curl
        curl_setopt($curl, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($curl, CURLOPT_HEADER</
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值