Ocr in php

Source: Internet
Author: User
: This article describes how to implement ocr in php. For more information about PHP tutorials, see. MORE: http://www.webyang.net/Html/web/article_161.html

The Baidu definition of OCR (Optical Character Recognition, Optical Character Recognition) refers to the Character printed on paper by an electronic device (such as a scanner or digital camera), which is determined by detecting the dark and bright pattern, then, we use the character recognition method to translate shapes into Computer Texts. that is, for printed characters, we use optical methods to convert the texts in paper documents into black and white dot matrix image files, the recognition software converts the text in the image into a text format for further editing and processing by the text processing software.

As an engineer, in actual programming, texts in images may need to be displayed, which requires ocr technology. Because php development, so the first choice of php, find the php ocr Extension test, the results found that unavailable (address: http://sourceforge.net/projects/phpocr.berlios )? I also read demos from many friends online. Basically, the principle is to break down the image into a 0, 1 matrix, and then convert the image into a corresponding string based on the features. Testing is not feasible. Then we can see that PHP is rarely used for OCR and is not suitable. The Language efficiency is too low. This algorithm requires high efficiency. Try OCR algorithms such as C and MATLAB. There are many biased algorithms such as OCR for matlab.

It is helpless to learn less, not C. I accidentally discovered that Baidu has ocr APIs: http://apistore.baidu.com/apiworks/servicedetail/146.html.

I wrote a play:

 
 
  1. Header ("Content-type: text/html; charset = utf-8 ");
  2. Function curl ($ img ){
  3. $ Ch = curl_init ();
  4. $ Url = 'http: // apis.baidu.com/apistore/idlocr/ocr'#//baidu OCR api
  5. $ Header = array (
  6. 'Content-Type: application/x-www-form-urlencoded ',
  7. 'Apikey: 69c2ace1ef297ce88869f0751cb1b618 ',
  8. );
  9. $ Data_temp = file_get_contents ($ img );
  10. $ Data_temp = urlencode (base64_encode ($ data_temp ));
  11. // Encapsulate necessary parameters
  12. $ Data = "fromdevice = pc & clientip = 127.0.0.1 & detecttype = LocateRecognize & languagetype = CHN_ENG & imagetype = 1 & image =". $ data_temp;
  13. Curl_setopt ($ ch, CURLOPT_HTTPHEADER, $ header); // add the apikey to the header
  14. Curl_setopt ($ ch, CURLOPT_POST, 1 );
  15. Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data); // add a parameter
  16. Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
  17. Curl_setopt ($ ch, CURLOPT_URL, $ url); // execute the HTTP request
  18. $ Res = curl_exec ($ ch );
  19. If ($ res = FALSE ){
  20. Echo "cURL Error:". curl_error ($ ch );
  21. }
  22. Curl_close ($ ch );
  23. $ Temp_var = json_decode ($ res, true );
  24. Return $ temp_var;
  25. }
  26. $ WordArr = curl('4.jpg ');
  27. If ($ wordArr ['errnum'] = 0 ){
  28. Var_dump ($ wordArr );
  29. } Else {
  30. Echo "recognition error:". $ wordArr ["errMsg"];
  31. }

After testing a few images, the accuracy is still quite high. Is unrealistic ~


Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

The above section describes how to implement ocr in php, including some content. I hope to help anyone who is interested in PHP tutorials.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.