PHP:基於百度大腦api實現OCR文字識別


有個項目要用到文字識別,網上找了很多資料,效果不是很好,偶然的機會,接觸到百度大腦。百度大腦提供了很多解決方案,其中一個就是文字識別,百度提供了三種文字識別,分別是銀行卡識別、身份證識別和通用文字識別,下面我們來測試下吧。

第一步:下載PHP文字識別demo

下載地址:https://git.oschina.net/jianqingwang/ocr

第二步:申請api

到百度大腦https://ai.baidu.com/tech/ocr/general申請api

 

sa

百度大腦申請api

申請好了之后,創建應用,可以得到2個信息,一分別是AppID,API Key和Secret Key。

打開demo下的DemoAipOcr.php,我們一起配置下接口信息!

<?php

// 引入文字識別OCR SDK
require_once '../AipOcr.php';

// 定義常量
const APP_ID = '寫你的id';
const API_KEY = '寫你的key';
const SECRET_KEY = '寫你的SECRET_KEY';

// 初始化
$aipOcr = new AipOcr(APP_ID, API_KEY, SECRET_KEY);

// 身份證識別
//var_dump($aipOcr->idcard(file_get_contents('idcard.jpg'), true));

// 銀行卡識別 
//var_dump($aipOcr->bankcard(file_get_contents('bankcard.jpg')));

// 通用文字識別
$rescult = $aipOcr->general(file_get_contents('20170413195205.jpg'));
$words = $rescult['words_result'];
var_dump($words[0]);
 //var_dump($aipOcr->general(file_get_contents('20170413195205.jpg')));

上面展示了三種識別系統,想測試銀行卡識別、身份證識別取消相關方法的注釋即可。

原圖:

通用文字識別測試圖片

我們看下我本地圖片20170413195205.jpg的識別效果:

array(12) { [0]=> array(2) { [“location”]=> array(4) { [“left”]=> int(34) [“top”]=> int(13) [“width”]=> int(96) [“height”]=> int(20) } [“words”]=> string(15) “阿迪郵件通” } [1]=> array(2) { [“location”]=> array(4) { [“left”]=> int(770) [“top”]=> int(14) [“width”]=> int(35) [“height”]=> int(19) } [“words”]=> string(6) “首頁” } [2]=> array(2) { [“location”]=> array(4) { [“left”]=> int(848) [“top”]=> int(15) [“width”]=> int(42) [“height”]=> int(18) } [“words”]=> string(9) “送郵件” } [3]=> array(2) { [“location”]=> array(4) { [“left”]=> int(928) [“top”]=> int(14) [“width”]=> int(250) [“height”]=> int(19) } [“words”]=> string(33) “於我們聯系我們注冊登錄” } [4]=> array(2) { [“location”]=> array(4) { [“left”]=> int(456) [“top”]=> int(118) [“width”]=> int(289) [“height”]=> int(27) } [“words”]=> string(40) “我們提供最全的郵件營銷資源!” } [5]=> array(2) { [“location”]=> array(4) { [“left”]=> int(487) [“top”]=> int(176) [“width”]=> int(228) [“height”]=> int(26) } [“words”]=> string(31) “開始使用阿迪郵件營銷!” } [6]=> array(2) { [“location”]=> array(4) { [“left”]=> int(32) [“top”]=> int(331) [“width”]=> int(129) [“height”]=> int(33) } [“words”]=> string(12) “通道齊全” } [7]=> array(2) { [“location”]=> array(4) { [“left”]=> int(423) [“top”]=> int(331) [“width”]=> int(157) [“height”]=> int(34) } [“words”]=> string(15) “不限制內容” } [8]=> array(2) { [“location”]=> array(4) { [“left”]=> int(811) [“top”]=> int(329) [“width”]=> int(127) [“height”]=> int(39) } [“words”]=> string(12) “離線發送” } [9]=> array(2) { [“location”]=> array(4) { [“left”]=> int(31) [“top”]=> int(374) [“width”]=> int(1140) [“height”]=> int(21) } [“words”]=> string(214) “聚合國內外優秀郵件服務商,給用戶提供最全的郵件代發如果客戶選擇國娃服務商,我們不會對內容進行審查,因系統24小時執行郵件推送服務,無需等待,只要提交了郵” } [10]=> array(2) { [“location”]=> array(4) { [“left”]=> int(30) [“top”]=> int(391) [“width”]=> int(1076) [“height”]=> int(26) } [“words”]=> string(195) “服務。只要通過我們,就能使用 Railgun、 Sendmall百為都走國外的郵件服務通道。相比其他服務商,我們堅持件內容,我們系統自動幫你推送到收件人手里!” } [11]=> array(2) { [“location”]=> array(4) { [“left”]=> int(33) [“top”]=> int(410) [“width”]=> int(721) [“height”]=> int(23) } [“words”]=> string(143) “度簡單郵件等國內外的郵件群發服務,無縫發送到世界各做好買水人的角色,不啊參與、干預用戶的發送內容。” } }

好了,趕緊去注冊個賬號,測試下百度ocr文字識別的功能吧!

本人博客地址:PHP:基於百度大腦api實現OCR文字識別


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM