加入收藏 | 设为首页 | 会员中心 | 我要投稿 云计算网_宿迁站长网 (https://www.0527zz.com/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

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

发布时间:2016-01-23 13:28:48 所属栏目:PHP教程 来源:网络整理
导读:Tesseract是一个能实现OCR的开源项目。你能在*Nix系统,Mac系统和Windows系统上运行这个项目,但是只要使用一个库,我们就能在PHP项目中使用它了。本教程的目的是教你如何使用。

第一步是用Composer来安装依赖文件:

  1. composer require silex/silex twig/twig thiagoalessio/tesseract_ocr:dev-master 

然后建立三个文件夹:

  1. public 
  2. - uploads 
  3. - views 

我们需要上传表单(viewsindex.twig):

  1. <html> 
  2.   <head> 
  3.     <title>OCR</title> 
  4.   </head> 
  5.   <body> 
  6.  
  7.     <form action="" method="post" enctype="multipart/form-data"
  8.       <input type="file" name="upload"
  9.       <input type="submit"
  10.     </form> 
  11.  
  12.   </body> 
  13. </html> 

需要一个结果展示页面(viewsresults.twig)::

  1. <html> 
  2.   <head> 
  3.     <title>OCR</title> 
  4.   </head> 
  5.   <body> 
  6.  
  7.     <h2>Results</h2> 
  8.  
  9.     <textarea cols="50" rows="10">{{ text }}</textarea> 
  10.  
  11.     <hr> 
  12.  
  13.     <a href="/">← Go back</a> 
  14.  
  15.   </body> 
  16. </html> 

现在建立skeleton Silex app (publicindex.php):

  1. <php 
  2.  
  3. require __DIR__.'/../vendor/autoload.php'
  4.  
  5. use SymfonyComponentHttpFoundationRequest; 
  6.  
  7. $app = new SilexApplication(); 
  8.  
  9. $app->register(new SilexProviderTwigServiceProvider(), [ 
  10.   'twig.path' => __DIR__.'/../views'
  11. ]); 
  12.  
  13. $app['debug'] = true; 
  14.  
  15. $app->get('/'function() use ($app) { 
  16.  
  17.   return $app['twig']->render('index.twig'); 
  18.  
  19. }); 
  20.  
  21. $app->post('/'function(Request $requestuse ($app) { 
  22.  
  23.     // TODO 
  24.  
  25. }); 
  26.  
  27. $app->run(); 

如果你在浏览器访问这个应用,你应该能看到一个文件上传表单。如果你在使用Homestead Improved Vagrant,你可以通过如下链接访问该应用。

  1. http://homestead.app/ 

(编辑:云计算网_宿迁站长网)

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