TesseractOCR PHP Code Examples HotExamples

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 15

PHP TesseractOCR Code Examples

This page contains top rated real world PHP examples of TesseractOCR extracted from
open source projects. You can rate examples to help us improve the quality of
examples

FREQUENTLY USED METHODS

recognize (14) (https://hotexamples.com/examples/-/TesseractOCR/recognize/php-


tesseractocr-recognize-method-examples.html)
setLanguage (7) (https://hotexamples.com/examples/-/TesseractOCR/setLanguage/php-
tesseractocr-setlanguage-method-examples.html)
setWhitelist (5)
(https://hotexamples.com/examples/-/TesseractOCR/setWhitelist/php-tesseractocr-
setwhitelist-method-examples.html)
setTempDir (4) (https://hotexamples.com/examples/-/TesseractOCR/setTempDir/php-
tesseractocr-settempdir-method-examples.html)
convertImageToTif (1)
(https://hotexamples.com/examples/-/TesseractOCR/convertImageToTif/php-
tesseractocr-convertimagetotif-method-examples.html)
executeTesseract (1)
(https://hotexamples.com/examples/-/TesseractOCR/executeTesseract/php-tesseractocr-
executetesseract-method-examples.html)
generateConfigFile (1)
(https://hotexamples.com/examples/-/TesseractOCR/generateConfigFile/php-
tesseractocr-generateconfigfile-method-examples.html)
generateWhitelist (1)
(https://hotexamples.com/examples/-/TesseractOCR/generateWhitelist/php-
tesseractocr-generatewhitelist-method-examples.html)
readOutputFile (1)
(https://hotexamples.com/examples/-/TesseractOCR/readOutputFile/php-tesseractocr-
readoutputfile-method-examples.html)
removeTempFiles (1)
(https://hotexamples.com/examples/-/TesseractOCR/removeTempFiles/php-tesseractocr-
removetempfiles-method-examples.html)

EXAMPLE #1

3  
 (https://hotexamples.com/examples/-/TesseractOCR/-/php-tesseractocr-class-
examples.html#0xbfe59f4d44142b155e99d33953ad400676fe7ded150a79e9fbcf589b2ca

Show file File: Templates_model.php Project: nus-mtp/bill-egoat

169 public function ocr($billID, $template)


170 {
171 $amountImgFileDirectory = "images/detection_result/";
172 $dueDateImgFileDirectory = "images/detection_result/";
173 $amountImgFileName = "croppedAmt.jpg";
174 $dueDateImgFileName = "croppedDueDate.jpg";
175 //connect to mysql and getting the coordinate data
176 require_once 'TesseractOCR.php';
177 $this->billdb->select('billFilePath');
178 $this->billdb->where('billID', $billID);
179 $query1 = $this->billdb->get('bills');
180 //$this->billdb->query("SELECT billFilePath from bills where billID =
" . $billID);
181 $ini_filename = $query1->result()[0]->billFilePath;
182 $im = imagecreatefromjpeg($ini_filename);
183 list($width, $height) = getimagesize($ini_filename);
184 $query2 = $this->templatedb->query("SELECT coordinateLabelX,
coordinateLabelY, coordinateLabelX2, coordinateLabelY2 FROM dat
185 $row = $query2->row(0);
186 $x1 = $row->coordinateLabelX;
187 $y1 = $row->coordinateLabelY;
188 $x2 = $row->coordinateLabelX2 - $row->coordinateLabelX;
189 $y2 = $row->coordinateLabelY2 - $row->coordinateLabelY;
190 // Scale Up coordinates
191 $x1 = $x1 * $width;
192 $y1 = $y1 * $height;
193 $x2 = $x2 * $width;
194 $y2 = $y2 * $height;
195 //cropping the image using the coordinate data
196 $to_crop_array = array('x' => $x1, 'y' => $y1, 'width' => $x2, 'height' =>
$y2);
197 if ($template != 0) {
198 $thumb_im = imagecrop($im, $to_crop_array);
199 } else {
200 $thumb_im = $im;
201 }
202 imagejpeg($thumb_im, $amountImgFileDirectory . $amountImgFileName, 100);
203 //run OCR on the cropped section
204 $tesseract = new TesseractOCR($amountImgFileDirectory . $amountImgFileName);
205 $tesseract->setLanguage('eng');
206 $amount = $tesseract->recognize();
207 $amount = preg_replace("/[^0-9,.]/", "", $amount);
208 $query3 = $this->templatedb->query("SELECT coordinateLabelX,
coordinateLabelY, coordinateLabelX2, coordinateLabelY2 FROM dat
209 $row = $query3->row(0);
210 $x1 = $row->coordinateLabelX;
211 $y1 = $row->coordinateLabelY;
212 $x2 = $row->coordinateLabelX2 - $row->coordinateLabelX;
213 $y2 = $row->coordinateLabelY2 - $row->coordinateLabelY;
214 // Scale Up coordinates
215 $x1 = $x1 * $width;
216 $y1 = $y1 * $height;
217 $x2 = $x2 * $width;
218 $y2 = $y2 * $height;
219 //cropping the image using the coordinate data
220 $to_crop_array = array('x' => $x1, 'y' => $y1, 'width' => $x2, 'height' =>
$y2);
221 if ($template != 0) {
222 $thumb_im = imagecrop($im, $to_crop_array);
223 } else {
224 $thumb_im = $im;
225 }
226 imagejpeg($thumb_im, $dueDateImgFileDirectory . $dueDateImgFileName, 100);
227 //run OCR on the cropped section
228 $tesseract = new TesseractOCR($dueDateImgFileDirectory .
$dueDateImgFileName);
229 $tesseract->setLanguage('eng');
230 $duedate = $tesseract->recognize();
231 $amount = strtok($amount, " ");
232 $day = strtok($duedate, " ");
233 $month = strtok(" ");
234 $year = strtok(" ");
235 str_replace(array(",", "."), "", $day);
236 str_replace(array(",", "."), "", $month);
237 str_replace(array(",", "."), "", $year);
238 if (ctype_alpha($day)) {
239 $temp = $day;
240 $day = $month;
241 $month = $temp;
242 }
243 switch ($month) {
244 case 'Jan':
245 case 'January':
246 $month = "01";
247 break;
248 case 'Feb':
249 case 'February':
250 $month = "02";
251 break;
252 case 'Mar':
253 case 'March':
254 $month = "03";
255 break;
256 case 'Apr':
257 case 'April':
258 $month = "04";
259 break;
260 case 'May':
261 $month = "05";
262 break;
263 case 'Jun':
264 case 'June':
265 $month = "06";
266 break;
267 case 'Jul':
268 case 'July':
269 $month = "07";
270 break;
271 case 'Aug':
272 case 'August':
273 $month = "08";
274 break;
275 case 'Sep':
276 case 'September':
277 $month = "09";
278 break;
279 case 'Oct':
280 case 'October':
281 $month = "10";
282 break;
283 case 'Nov':
284 case 'November':
285 $month = "11";
286 break;
287 case 'Dec':
288 case 'December':
289 $month = "12";
290 break;
291 }
292 $data = array('totalAmt' => $amount, 'billDueDate' => $year . "-" .
$month . "-" . $day);
293 $this->billdb->where('billID', $billID);
294 $this->billdb->update('bills', $data);
295 /* remove the cropped images once the check is complete.
296
297 $command = escapeshellcmd('rm -f ' .
$amountImgFileDirectory . $amountImgFileName);
298
299 shell_exec($command);
300 $command = escapeshellcmd('rm -f ' .
$dueDateImgFileDirectory . $dueDateImgFileName);
301
302 shell_exec($command);
303 */
304 return $ini_filename;
305 }

EXAMPLE #2

1  

 (https://hotexamples.com/examples/-/TesseractOCR/-/php-tesseractocr-class-
examples.html#0xa8c913b267ef00e37e191f1d82f67f313aa71a0665525cd5dbd6520048

Show file File: webminer-drive.php Project: kjenney/php-webminer

359 /**
360 * Get Captcha Value and input in box
361 */
362 function captcha($imglocation, $typeinbox)
363 {
364 $html = $this->getSource();
365 $tidy = tidy_parse_string($html)->html()->value;
366 $searchqp = htmlqp($tidy, 'body');
367 $captchaurl = $searchqp->branch($imglocation)->attr('src');
368 $saveimg = '/tmp/mycaptcha.png';
369 file_put_contents($saveimg, file_get_contents($captchaurl));
370 $tesseract = new TesseractOCR($saveimg);
371 $crackedvalue = $tesseract->recognize();
372 $this->driver->findElement(WebDriverBy::CssSelector($typeinbox))-
>sendKeys($crackedvalue);
373 }

EXAMPLE #3

1  

 (https://hotexamples.com/examples/-/TesseractOCR/-/php-tesseractocr-class-
examples.html#0xa5fa3e97b86497d9f66a0c6a31baccb37b240d26cb66c3d3a36a7e5a1a

Show file File: ocr.inc.php Project: edualm/SDIS-EuroMillions-Server

6 function numbersForFileNamed($fileName)
7 {
8 $tesseract = new \TesseractOCR(BASE_PATH . 'inbox/' . $fileName);
9 $tesseract->setWhitelist(range(0, 9));
10 return preg_split('/[ \\n]/', $tesseract->recognize());
11 }

EXAMPLE #4

0  
 (https://hotexamples.com/examples/-/TesseractOCR/-/php-tesseractocr-class-
examples.html#0x77b581349a7c73836fff4dda0353623a659b6bbec2966731bd963b3992

Show file File: ocr.php Project: pinekta/MyTestSample

<?php
1
2 require_once 'vendor/autoload.php';
3 for ($i = 1; $i <= 10; $i++) {
4 $tesseract = new TesseractOCR("img/image{$i}.jpg");
5 $tesseract->setLanguage('eng');
6 $tesseract->setWhitelist(range('a', 'z'), range(0, 9));
7 echo $tesseract->recognize() . "\r";
8 }

EXAMPLE #5

-1  

 (https://hotexamples.com/examples/-/TesseractOCR/-/php-tesseractocr-class-
examples.html#0x3c0a19958937a39b9111744a038f4536d49fdbce41d0e0d5c8fe177256

Show file File: ImageController.php Project: lilikokalova/diplomna-rabota

36 public function tesseract($image_path)


37 {
38 require_once 'D:\\xampp\\htdocs\\ocr\\vendor\\thiagoalessio\\
tesseract_ocr\\TesseractOCR\\TesseractOCR.php';
39 $tesseract = new TesseractOCR(public_path() . '/images/' .
$image_path);
40 $text = $tesseract->recognize();
41 Session::put('trans', $text);
42 return $text;
43 }
EXAMPLE #6

-1  
 (https://hotexamples.com/examples/-/TesseractOCR/-/php-tesseractocr-class-
examples.html#0x28fceed3b8b517bb1725f55b53fc7f1a98a034d53bcfcab533fd3bd93c62

Show file File: CodeRead.php Project: nikhilsamarsingh/CodeRead

14 public function recognizeText($imageFile)


15 {
16 require_once "TesseractOCR/TesseractOCR.php";
17 require_once "Repositories/CR_File.php";
18 // Recognize text from image
19 $tesseract = new TesseractOCR($imageFile->filePath);
20 $tesseract->setWhitelist(range('A', 'Z'), range('a', 'z'), range(0, 9),
'_-.,;"#<>()%{}[]= ');
21 $txt = $tesseract->recognize();
22 // Save text file
23 // public/output/code_filename.ext
24 $codeFilePath = "public/output/code_" . $imageFile->fileName . $this-
>LANGUAGES[$this->language];
25 $recognizedCodeFile = new CR_File($codeFilePath);
26 $recognizedCodeFile->write($txt);
27 return $recognizedCodeFile;
28 }

EXAMPLE #7

-1  

 (https://hotexamples.com/examples/-/TesseractOCR/-/php-tesseractocr-class-
examples.html#0xb9d0d874b6daf30e7f0eae3753f8ec65baf0406f0d455cae0f0bc64fb04e

Show file File: class.tesseract_ocr.php Project: PseudoAj/mfcs

20 function generateConfigFile($arguments)
21 {
22 $configFile = mfcs::config('mfcstmp') . '/tesseract-ocr-config-' .
rand() . '.conf';
23 exec("touch {$configFile}");
24 $whitelist = TesseractOCR::generateWhitelist($arguments);
25 if (!empty($whitelist)) {
26 $fp = fopen($configFile, 'w');
27 fwrite($fp, "tessedit_char_whitelist {$whitelist}");
28 fclose($fp);
29 }
30 return $configFile;
31 }

EXAMPLE #8

-1  

 (https://hotexamples.com/examples/-/TesseractOCR/-/php-tesseractocr-class-
examples.html#0xd48b837dbb7af369b1bf5c794f57da779e2b7e782a33bcbaa75df4c615

Show file File: index.php Project: mehulsbhatt/ocr-php-tesseract-example


44 // Grab the uploaded file
45 $file = $request->files->get('upload');
46 // Extract some information about the uploaded file
47 $info = new SplFileInfo($file->getClientOriginalName());
48 // Create a quasi-random filename
49 $filename = sprintf('%d.%s', time(), $info->getExtension());
50 // Copy the file
51 $file->move(__DIR__ . '/../uploads', $filename);
52 // Instantiate the Tessearct library
53 $tesseract = new TesseractOCR(__DIR__ . '/../uploads/' . $filename);
54 // Perform OCR on the uploaded image
55 $text = $tesseract->recognize();
56 return $app['twig']->render('results.twig', ['text' => $text]);
57 });
58 $app->post('/identify-telephone-number', function (Request $request)
use($app) {
59 // Grab the uploaded file
60 $file = $request->files->get('upload');
61 // Extract some information about the uploaded file
62 $info = new SplFileInfo($file->getClientOriginalName());
63 // Create a quasi-random filename
64 $filename = sprintf('%d.%s', time(), $info->getExtension());
65 // Copy the file
66 $file->move(__DIR__ . '/../uploads', $filename);
67 // Instantiate the Tessearct library
68 $tesseract = new TesseractOCR(__DIR__ . '/../uploads/' . $filename);
69 // Perform OCR on the uploaded image
70 $text = $tesseract->recognize();
71 $number = findPhoneNumber($text, 'GB');
72 return $app->json(['number' => $number]);
73 });
74 $app->run();

EXAMPLE #9

-1  
 (https://hotexamples.com/examples/-/TesseractOCR/-/php-tesseractocr-class-
examples.html#0xa1a884bf4e0750f8a8b87f6042e1b64ebde59da067f73a0bfaff13a22b40

Show file File: files.php Project: PseudoAj/mfcs

732 public static function createOCRTextFile($originalFile, $assetsID,


$filename)
733 {
734 $text = TesseractOCR::recognize($originalFile);
735 if (file_put_contents(self::getSaveDir($assetsID, 'ocr') .
DIRECTORY_SEPARATOR . $filename . '.txt', $text) === FALSE) {
736 return FALSE;
737 }
738 $return['ocr'][] = array('name' => $filename . '.txt', 'path' =>
self::getSaveDir($assetsID, 'ocr', FALSE), 'size' => filesi
739 }
EXAMPLE #10

-1  

 (https://hotexamples.com/examples/-/TesseractOCR/-/php-tesseractocr-class-
examples.html#0x84e02acbaf95fb66a029adcd5f4d86eae6ba5fc4a0a1bf674ce7546db2f

Show file File: DocumentParserWorker.php Project: jinchen891021/paperwork

102 private function parseContent($fileUri, $language)


103 {
104 if (!File::exists($fileUri)) {
105 throw new Exception('Document parsing job #' . $this->job-
>getJobId() . ' received a uri to a file that does not seem to
106 }
107 $tesseract = new TesseractOCR($fileUri);
108 $tesseract-
>setTempDir(Config::get('paperwork.tesseractTempDirectory'));
109 if (isset($language)) {
110 $tesseract->setLanguage($language);
111 }
112 return $tesseract->recognize();
113 }

EXAMPLE #11

-1  
 (https://hotexamples.com/examples/-/TesseractOCR/-/php-tesseractocr-class-
examples.html#0x581d8d39143ca49ec0e6fd54fd30d64237f7d8dc05ef2c1c517cfd1b9e2

Show file File: TesseractOCRTest.php Project: ais-pro/tesseract-ocr-for-php

23 public function testSpecificLanguageRecognition()


24 {
25 $tesseract = new TesseractOCR("{$this->imagesDir}german.png");
26 $tesseract->setLanguage('deu');
27 $this->assertEquals('grüßen in Deutsch', $tesseract->recognize());
28 }

EXAMPLE #12

-1  
 (https://hotexamples.com/examples/-/TesseractOCR/-/php-tesseractocr-class-
examples.html#0x2ba2f94ad1c555b1092eac2bb51733fdd9e80af1daa3a9d3a577df4fb95

Show file File: functions.php Project: schpill/standalone

598 function ocr($img, $lng = 'fre')


599 {
600 $t = new \TesseractOCR($img);
601 $t->setTempDir(CACHE_PATH);
602 // $t->setLanguage($lng);
603 return $t->recognize();
604 }
EXAMPLE #13

-1  

 (https://hotexamples.com/examples/-/TesseractOCR/-/php-tesseractocr-class-
examples.html#0x28d036c4f8268e22890837a357cc879d104a94e5deb6897a847eed004

Show file File: HomeController.php Project: soarmorrow/refercom

129 public function getOCR()


130 {
131 require_once base_path() .
'/vendor/thiagoalessio/tesseract_ocr/TesseractOCR/TesseractOCR.php';
132 $tesseract = new TesseractOCR(public_path() .
'/assets/img/social/fb_login.png');
133 $tesseract->setTempDir(storage_path());
134 $tesseract->setLanguage('eng');
135 //same 3-letters code as tesseract training data packages
136 echo $tesseract->recognize();
137 }

EXAMPLE #14

-1  
 (https://hotexamples.com/examples/-/TesseractOCR/-/php-tesseractocr-class-
examples.html#0x20c042452379c644833ba8aaa48df094b6f3869e977e02d3a82ef7f6db

Show file File: DashboardController.php Project: soarmorrow/refercom


765 /**
766 * Get Ocr uploaded Image Text.
767 *
768 * @return with Success with Text Extracted or Error
769 */
770 public function postUpload()
771 {
772 // Build the input for our validation
773 $input = array('image' => Input::file('image'));
774 // Within the ruleset, make sure we let the validator know that this
775 // file should be an image
776 $rules = array('image' => 'required|mimes:jpeg,png,pdf');
777 // Now pass the input and rules into the validator
778 $validator = Validator::make($input, $rules);
779 // Check to see if validation fails or passes
780 if ($validator->fails()) {
781 // Redirect with a helpful message to inform the user that
782 // the provided file was not an adequate type
783 return Redirect::back()->with('message', 'Error: The provided file
was not an image');
784 } else {
785 $file = Input::file('image');
786 $destinationPath = 'uploads/photos';
787 $image = $file->getClientOriginalName();
788 Input::file('image')->move($destinationPath, $image);
789 require_once base_path() .
'/vendor/thiagoalessio/tesseract_ocr/TesseractOCR/TesseractOCR.php';
790 $tesseract = new TesseractOCR(public_path() . '/' .
$destinationPath . '/' . $image);
791 $tesseract->setTempDir(storage_path());
792 $tesseract->setLanguage('eng');
793 //same 3-letters code as tesseract training data packages
794 $ocr = $tesseract->recognize();
795 return Redirect::route('upload-form')->with('message', 'Success:
File upload was successful')->with('ocr', $ocr);
796 }
797 return Redirect::back()->with('error', 'An error occured');
798 }

EXAMPLE #15

-1  
 (https://hotexamples.com/examples/-/TesseractOCR/-/php-tesseractocr-class-
examples.html#0x1fe9d161164d37933ea867ec57d9cb085143d80868c004b2ba099e6f4

Show file File: ocr.php Project: nus-mtp/bill-egoat


6 function ocr($id, $template)
7 {
8 //connect to mysql and getting the coordinate data
9 require_once 'TesseractOCR/TesseractOCR.php';
10 $link = mysql_connect('localhost:3306', 'root', 'ysAb7cEkjvOa');
11 mysql_select_db('billdb');
12 //$id = 171;
13 //$template = 54;
14 $sql = "SELECT billFilePath from bills where billID = " . $id;
15 $result = mysql_query($sql);
16 $row = mysql_fetch_array($result, MYSQL_NUM);
17 $ini_filename = $row[0];
18 $im = imagecreatefromjpeg($ini_filename);
19 //echo $ini_filename;
20 //echo $id;
21 //echo $template;
22 mysql_select_db('templatedb');
23 $sql = "SELECT coordinateLabelX, coordinateLabelY, coordinateLabelX2,
coordinateLabelY2 FROM datafields WHERE templateID = "
24 $result = mysql_query($sql);
25 $row = mysql_fetch_array($result, MYSQL_NUM);
26 $x1 = $row[0];
27 $y1 = $row[1];
28 $x2 = $row[2] - $row[0];
29 $y2 = $row[3] - $row[1];
30 //cropping the image using the coordinate data
31 $to_crop_array = array('x' => $x1, 'y' => $y1, 'width' => $x2, 'height'
=> $y2);
32 $thumb_im = imagecrop($im, $to_crop_array);
33 imagejpeg($thumb_im, 'images/cropped1.jpg', 100);
34 //run OCR on the cropped section
35 $tesseract = new TesseractOCR('images/cropped1.jpg');
36 $tesseract->setLanguage(eng);
37 $amount = $tesseract->recognize();
38 $sql = "SELECT coordinateLabelX, coordinateLabelY, coordinateLabelX2,
coordinateLabelY2 FROM datafields WHERE templateID = "
39 $result = mysql_query($sql);
40 $row = mysql_fetch_array($result, MYSQL_NUM);
41 $x1 = $row[0];
42 $y1 = $row[1];
43 $x2 = $row[2] - $row[0];
44 $y2 = $row[3] - $row[1];
45 //cropping the image using the coordinate data
46 $to_crop_array = array('x' => $x1, 'y' => $y1, 'width' => $x2, 'height'
=> $y2);
47 $thumb_im = imagecrop($im, $to_crop_array);
48 imagejpeg($thumb_im, 'images/cropped2.jpg', 100);
49 //run OCR on the cropped section
50 $tesseract = new TesseractOCR('images/cropped2.jpg');
51 $tesseract->setLanguage(eng);
52 $duedate = $tesseract->recognize();
53 $amount = strtok($amount, " ");
54 $day = strtok($duedate, " ");
55 $month = strtok(" ");
56 $year = strtok(" ");
57 switch ($month) {
58 case Jan:
59 $month = "01";
60 break;
61 case Feb:
62 $month = "02";
63 break;
64 case Mar:
65 $month = "03";
66 break;
67 case Apr:
68 $month = "04";
69 break;
70 case May:
71 $month = "05";
72 break;
73 case Jun:
74 $month = "06";
75 break;
76 case Jul:
77 $month = "07";
78 break;
79 case Aug:
80 $month = "08";
81 break;
82 case Sep:
83 $month = "09";
84 break;
85 case Oct:
86 $month = "10";
87 break;
88 case Nov:
89 $month = "11";
90 break;
91 case Dec:
92 $month = "12";
93 break;
94 }
95 //echo "<br>" . $amount . "<br>";
96 //echo $year;
97 //echo $month;
98 //echo $day;
99 mysql_select_db('billdb');
100 $sql = "UPDATE bills SET totalAmt = " . $amount . ", billDueDate = '" .
$year . "-" . $month . "-" . $day . "' WHERE billID
101 //echo "<br>" . $sql;
102 $result = mysql_query($sql);
103 mysql_close($link);
104 }
0 Comments HotExamples

1 Sinan Yıldız

Sort by Best
⤤Recommend ⤤ Share

Start the discussion…

Be the
first to comment.

✉ Subscribe d Add Disqus to your siteAdd DisqusAdd 🔒 Disqus' Privacy


PolicyPrivacy PolicyPrivacy

Top Functions (https://hotexamples.com/site/trends?type=php%7Cf) | Top


Classes (https://hotexamples.com/site/trends?type=php%7Cc) | Documentation
(https://phpdoc.hotexamples.com/doc/map) | EN
(https://hotexamples.com/examples/-/TesseractOCR/-/php-tesseractocr-class-
examples.html) | RU

(https://hotexamples.com/ru/examples/-/TesseractOCR/-/php-tesseractocr-class-
examples.html)
(//www.liveinternet.ru/click)

You might also like