加入收藏 | 设为首页 | 会员中心 | 我要投稿 PHP编程网 - 黄冈站长网 (http://www.0713zz.com/)- 数据应用、建站、人体识别、智能机器人、语音技术!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

PHP OCR实战:用Tesseract从图像中读取文字

发布时间:2016-10-17 21:38:30 所属栏目:PHP教程 来源:站长网
导读:副标题#e# OpticalCharacterRecognition(OCR)即光学字符辨识是把打印文本转换成一个数字表示的过程。它有各种各样的实际应用从数字化印刷书籍、创建收据的电子记录,到车牌识别甚至破解基于图像的验证码。 498)this.width=498;' onmousewheel = 'javascript

现在我们可以如下使用:

  1. $text = $tesseract->recognize(); 
  2. $number = findPhoneNumber($text, 'GB'); 

我们需要给谷歌电话库提供一个提示来说明这个号码是哪个国家的。你也可以改成你自己的国家。

我们把所有的这些打包在一个新的路由中:

  1. $app->post('/identify-telephone-number', function(Request $request) use ($app) { 
  2.  
  3.   // Grab the uploaded file 
  4.   $file = $request->files->get('upload'); 
  5.  
  6.   // Extract some information about the uploaded file 
  7.   $info = new SplFileInfo($file->getClientOriginalName()); 
  8.  
  9.   // Create a quasi-random filename 
  10.   $filename = sprintf('%d.%s', time(), $info->getExtension()); 
  11.  
  12.   // Copy the file 
  13.   $file->move(__DIR__.'/../uploads', $filename); 
  14.  
  15.   // Instantiate the Tessearct library 
  16.   $tesseract = new TesseractOCR(__DIR__ . '/../uploads/' . $filename); 
  17.  
  18.   // Perform OCR on the uploaded image 
  19.   $text = $tesseract->recognize(); 
  20.  
  21.   $number = findPhoneNumber($text, 'GB'); 
  22.  
  23.   return $app->json( 
  24.     [ 
  25.       'number'     =>  $number, 
  26.     ] 
  27.   ); 
  28.  
  29. }); 

我们现在有简单的API的基础—-也就是JSON响应-—我们可以用来作为一个简单的移动应用的后端,这款应用可以用来从一幅图中添加联系人,打电话。

总结

(编辑:PHP编程网 - 黄冈站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读