Heim  >  Artikel  >  Backend-Entwicklung  >  php实现ocr文字识别

php实现ocr文字识别

巴扎黑
巴扎黑Original
2016-11-10 11:31:312725Durchsuche

 OCR的百度定义 (Optical Character Recognition,光学字符识别)是指电子设备(例如扫描仪或数码相机)检查纸上打印的字符,通过检测暗、亮的模式确定其形状,然后用字符识别方法 将形状翻译成计算机文字的过程;即,针对印刷体字符,采用光学的方式将纸质文档中的文字转换成为黑白点阵的图像文件,并通过识别软件将图像中的文字转换成 文本格式,供文字处理软件进一步编辑加工的技术。

作为一个工程师,在实际编程中,可能需要把图片中的文字显示出来,这就需要用到ocr技术。因为php开发,所以优先选择php,找了php的ocr扩展测试了下,结果发现不可用(地址:http://sourceforge.net/projects/phpocr.berlios)? 网上也看了很多朋友的demo,基本上原理都是对图片分解成0,1矩阵,然后根据特征,转化成相应的字符串。测试几个都是不可行的。然后看到别人说PHP 搞OCR的很少,也不适合,语言效率太低,这种算法需要很高的效率。可以尝试C,MATLAB 等的OCR算法。搞matlab的玩OCR这类偏算法的很多。

无奈才虚学浅,不会C。无意中却发现百度有ocr的api提供:http://apistore.baidu.com/apiworks/servicedetail/146.html。

写了个玩下:

<?php
header("Content-type: text/html; charset=utf-8");
 
function curl($img){
 
$ch = curl_init();
$url =&#39;http://apis.baidu.com/apistore/idlocr/ocr&#39;;//百度ocr api
$header = array(
&#39;Content-Type:application/x-www-form-urlencoded&#39;,
&#39;apikey:69c2ace1ef297ce88869f0751cb1b618&#39;,
);
 
$data_temp = file_get_contents($img);
$data_temp = urlencode(base64_encode($data_temp));
//封装必要参数
$data="fromdevice=pc&clientip=127.0.0.1&detecttype=LocateRecognize&languagetype=CHN_ENG&imagetype=1&image=".$data_temp;
curl_setopt($ch, CURLOPT_HTTPHEADER , $header);// 添加apikey到header
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);// 添加参数
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch , CURLOPT_URL , $url);// 执行HTTP请求
$res = curl_exec($ch);
if($res === FALSE){
echo "cURL Error: ". curl_error($ch);
}
curl_close($ch);
$temp_var = json_decode($res,true);
return $temp_var;
 
}
 
$wordArr = curl(&#39;4.jpg&#39;);
if($wordArr[&#39;errNum&#39;]==0){
var_dump($wordArr);
}else{
echo "识别出错:".$wordArr["errMsg"];
}


Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn