用百度AI的OCR文字识别结合PHP实现了图片的文字识别功能
开发中需要使用上传身份证自动获取身份证信息,百度AI提供此类接口,性价比较高。
1.http://ai.baidu.com/使用免费版文字识别接口,下载sdk。
2.基础代码如下:
// 引入文字识别OCR SDK
require_once '../AipOcr.php';
// 定义常量
const APP_ID = 'xxx';
const API_KEY = 'xxx';
const SECRET_KEY = 'xxx';
// 初始化
$aipOcr = new AipOcr(APP_ID, API_KEY, SECRET_KEY);
// 身份证识别
echo json_encode($aipOcr->idcard(file_get_contents('idcard.jpg'), true), JSON_PRETTY_PRINT);
遗憾的是用sdk中包含的身份证信息进行识别时并未正确获取韦小宝信息,百度黑起自己来也一点不马虎啊~
获取数据结果:
{
log_id: 1651506565,
words_result_num: 5,
image_status: "unknown",
words_result: {
公民身份号码: {
location: {
width: 0,
top: 0,
height: 0,
left: 0
},
words: ""
},
性别: {
location: {
width: 15,
top: 83,
height: 18,
left: 100
},
words: "男"
},
民族: {
location: {
width: 13,
top: 84,
height: 16,
left: 197
},
words: "汉"
},
姓名: {
location: {
width: 64,
top: 40,
height: 22,
left: 99
},
words: "韦小宝"
},
住址: {
location: {
width: 217,
top: 161,
height: 50,
left: 93
},
words: "北京市东城区景山前街4号紫禁城敬事房"
}
}
}