Site icon บริษัท โค๊ดบี จำกัด

PHP ติดตั้งและใช้งาน Tesseract OCR แปลงรูปภาพเป็นข้อความภาษาไทย

PHP-ติดตั้งและใช้งาน Tesseract-OCR

PHP-ติดตั้งและใช้งาน Tesseract-OCR

&NewLine;<figure class&equals;"wp-block-video"><&sol;figure>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph"><strong>Tesseract<&sol;strong> คือ Engine ที่ใช้สำหรับกระบวนการแปลงสื่อสิ่งพิมพ์ เช่นกระดาษ นิตยาสาร สัญญา หรือข้อมูลอะไรก็ตามที่อยู่ในรูปของเอกสารกระดาษ ให้กลายเป็นเป็นข้อความ เรียกกระบวนการนี้ว่า <strong>Optical Character Recognition<&sol;strong> หรือเรียกสั้น ๆ ว่า OCR <&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph"><strong>Tesseract<&sol;strong> Engine สามารถแปลงข้อมูลในรูปภาพไปเป็นตัวอักษรภาษาต่าง ๆ ทั่วโลก เดิมที่ถูกพัฒนาโดย Hewlett-Packard &lpar; HP &rpar; และเปิดเป็นโอเพ่นซอร์สในปี 2005 จากนั้นในปี 2006 ทีมพัฒนาของ Google ได้นำมาพัฒนาต่อยอดให้มีความสามารถมากขึ้น สามารถเข้าดูได้ที่ <a rel&equals;"noreferrer noopener" href&equals;"https&colon;&sol;&sol;opensource&period;google&sol;projects&sol;tesseract" target&equals;"&lowbar;blank">https&colon;&sol;&sol;opensource&period;google&sol;projects&sol;tesseract<&sol;a><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">ในบทความนี้จะมาแนะนำการติดตั้งและใช้งาน <strong>Tesseract OCR<&sol;strong> ในภาษา PHP บนระบบปฏิบัติการ Mac OS นะครับ สำหรับการทดสอบ เบื้องต้นให้เตรียม Environment ตามด้านล่างนะครับ<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li>ติดตั้ง <a href&equals;"https&colon;&sol;&sol;brew&period;sh&sol;index&lowbar;th">homebrew package manager<&sol;a><&sol;li><li>ติดตั้ง PHP 7&period;2 หรือเวอร์ชั่นที่สูงกว่า<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph"><strong>ติดตั้ง Tesseract OCR<&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li>เปิด terminal ขึ้นมาที่โฟลเด้อ ocr ติดตั้ง <a rel&equals;"noreferrer noopener" href&equals;"https&colon;&sol;&sol;github&period;com&sol;thiagoalessio&sol;tesseract-ocr-for-php" target&equals;"&lowbar;blank">https&colon;&sol;&sol;github&period;com&sol;thiagoalessio&sol;tesseract-ocr-for-php<&sol;a> ผ่าน composer โดยใช้คำสั่งดังนี้ครับ<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code>composer require thiagoalessio&sol;tesseract&lowbar;ocr<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">เมื่อติดตั้งสำเร็จเราจะได้โครงสร้างโปรเจคตามรูปด้านล่าง ลบ compser&period;lock ทิ้งไป<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<figure class&equals;"wp-block-image size-large"><img src&equals;"https&colon;&sol;&sol;www&period;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2020&sol;06&sol;PHP-ติดตั้งและใช้งาน-Tesseract-OCR-1-940x128&period;png" alt&equals;"" class&equals;"wp-image-5038" data-full&equals;"https&colon;&sol;&sol;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2020&sol;06&sol;PHP-ติดตั้งและใช้งาน-Tesseract-OCR-1&period;png" data-full-size&equals;"1268x172" &sol;><&sol;figure>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li>ติดตั้ง <strong>Tesseract OCR<&sol;strong> ใช้ homebrew โดยเปิด Terminal ขึ้นมาและพิมพ์คำสั่งดังนี้<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code>brew install tesseract<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">หลังจากติดตั้ง tesseract เรียบร้อยแล้ว ถ้าเราอยากรู้คำสั่งต่างของ tesseract ให้พิมพ์ tesseract ที่ Terminal <&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph"><strong>tesseract &&num;8211&semi;version<&sol;strong> คำสั่งสำหรับดูว่าเราติดตั้งเวอร์ชั่นอะไรไว้ในเครื่อง <&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph"><strong>tesseract &&num;8211&semi;list-langs<&sol;strong> คำสั่งสำหรับตรวจสอบภาษาที่ tesseract สามารถ recognize ได้<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<figure class&equals;"wp-block-image size-large"><img src&equals;"https&colon;&sol;&sol;www&period;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2020&sol;06&sol;PHP-ติดตั้งและใช้งาน-Tesseract-OCR-2-940x645&period;png" alt&equals;"" class&equals;"wp-image-5041" data-full&equals;"https&colon;&sol;&sol;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2020&sol;06&sol;PHP-ติดตั้งและใช้งาน-Tesseract-OCR-2&period;png" data-full-size&equals;"1394x956" &sol;><&sol;figure>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li>ทดสอบการทำงาน โดยให้สร้างไฟล์ index&period;php ไว้ในโฟลเด้อ ocr จากนั้น ลองหาภาพที่มีข้อความภาษาอังกฤษวางในโฟลเด้อ images ตั้งชื่อไฟล์ว่า eng&period;png ดูตัวอย่างตามภาพ<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<figure class&equals;"wp-block-image size-large"><img src&equals;"https&colon;&sol;&sol;www&period;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2020&sol;06&sol;PHP-ติดตั้งและใช้งาน-Tesseract-OCR-3-940x195&period;png" alt&equals;"" class&equals;"wp-image-5043" data-full&equals;"https&colon;&sol;&sol;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2020&sol;06&sol;PHP-ติดตั้งและใช้งาน-Tesseract-OCR-3&period;png" data-full-size&equals;"1222x254" &sol;><&sol;figure>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">และรูปภาพที่มีข้อความภาษาอังกฤษอยู่ในภาพ ใช้สำหรับทดสอบ <&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<figure class&equals;"wp-block-image size-large"><img src&equals;"https&colon;&sol;&sol;www&period;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2020&sol;06&sol;PHP-ติดตั้งและใช้งาน-Tesseract-OCR-4-940x195&period;png" alt&equals;"" class&equals;"wp-image-5044" data-full&equals;"https&colon;&sol;&sol;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2020&sol;06&sol;PHP-ติดตั้งและใช้งาน-Tesseract-OCR-4&period;png" data-full-size&equals;"964x200" &sol;><&sol;figure>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">คำสั่งใน <strong>index&period;php<&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code>&NewLine;require&lowbar;once "vendor&sol;autoload&period;php"&semi;&NewLine;use thiagoalessio&bsol;TesseractOCR&bsol;TesseractOCR&semi;&NewLine;&NewLine;echo &lpar;new TesseractOCR&lpar;'images&sol;eng&period;png'&rpar;&rpar;&NewLine; ->lang&lpar;'eng'&rpar;&NewLine; ->executable&lpar;'&sol;usr&sol;local&sol;Cellar&sol;tesseract&sol;4&period;1&period;1&sol;bin&sol;tesseract'&rpar;&NewLine; ->run&lpar;&rpar;&semi;&NewLine;<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">ในส่วนของ executable หากเราไม่ทราบว่า tesseract ถูกติดตั้งไว้ที่ path ไหน ให้ใช้คำสั่ง <strong>brew list tesseract<&sol;strong> เพื่อดู path ที่เราติดตั้ง tesseract ได้ ผลลัพธ์คำสั่งตามด้านล่าง<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code>&sol;usr&sol;local&sol;Cellar&sol;tesseract&sol;4&period;1&period;1&sol;bin&sol;tesseract&NewLine;&sol;usr&sol;local&sol;Cellar&sol;tesseract&sol;4&period;1&period;1&sol;include&sol;tesseract&sol; &lpar;19 files&rpar;&NewLine;&sol;usr&sol;local&sol;Cellar&sol;tesseract&sol;4&period;1&period;1&sol;lib&sol;libtesseract&period;4&period;dylib&NewLine;&sol;usr&sol;local&sol;Cellar&sol;tesseract&sol;4&period;1&period;1&sol;lib&sol;pkgconfig&sol;tesseract&period;pc&NewLine;&sol;usr&sol;local&sol;Cellar&sol;tesseract&sol;4&period;1&period;1&sol;lib&sol; &lpar;2 other files&rpar;&NewLine;&sol;usr&sol;local&sol;Cellar&sol;tesseract&sol;4&period;1&period;1&sol;share&sol;tessdata&sol; &lpar;35 files&rpar;<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">ผลลัพธ์ที่ได้เมื่อเรารันคำสั่งใน index&period;php ผ่าน localhost&colon;8080&sol;ocr ตามรูปด้านล่างครับ สำหรับภาพที่ไม่ได้มีความซับซ้อนมาก เช่น ไม่มีพื้นหลัง ไม่มีเส้นตัดทับตัวอักษร ผลลัพธ์แม่นยำ 100&percnt; เลยครับ<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<figure class&equals;"wp-block-image size-large"><img src&equals;"https&colon;&sol;&sol;www&period;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2020&sol;06&sol;PHP-ติดตั้งและใช้งาน-Tesseract-OCR-5-1-940x671&period;png" alt&equals;"" class&equals;"wp-image-5049" data-full&equals;"https&colon;&sol;&sol;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2020&sol;06&sol;PHP-ติดตั้งและใช้งาน-Tesseract-OCR-5-1&period;png" data-full-size&equals;"1232x880" &sol;><&sol;figure>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph"><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph"><strong>ติดตั้ง tesseract ภาษาไทย <&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">ติดตั้ง tesseract ภาษาไทย เบื้องต้นหลังจากเราติดตั้ง tesseract เรียบร้อยแล้ว จะมีภาษาที่สามารถ recognize ได้แค่ 3 ภาษานะครับ ในกรณีที่เราต้องการแปลงข้อมูลในภาพที่เป็นภาษาไทยให้เราติดตั้งภาษาใน <strong>tesseract<&sol;strong> เพิ่ม โดยใช้คำสั่ง <&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code>brew install tesseract-&lt&semi;langcode> สำหรับติดตั้งภาษาใดภาษาหนึ่ง&NewLine;&NewLine;brew install tesseract-lang สำหรับติดตั้งทุกภาษา<&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">ปรับ code ที่ไฟล์ index&period;php นิดหน่อยครับ โดยเพิ่มภาษาไทยเข้าไป<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-code"><code>echo &lpar;new TesseractOCR&lpar;'thai&period;png'&rpar;&rpar; &NewLine;&Tab;->lang&lpar;'tha'&comma;'eng'&rpar;&NewLine; ->executable&lpar;'&sol;usr&sol;local&sol;Cellar&sol;tesseract&sol;4&period;1&period;1&sol;bin&sol;tesseract'&rpar;&NewLine; ->run&lpar;&rpar;&semi; <&sol;code><&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">ผลลัพธ์ที่ได้ สำหรับการแปลงเป็นภาษาไทย<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<figure class&equals;"wp-block-image size-large"><img src&equals;"https&colon;&sol;&sol;www&period;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2020&sol;06&sol;PHP-ติดตั้งและใช้งาน-Tesseract-OCR-6-940x671&period;png" alt&equals;"" class&equals;"wp-image-5053" data-full&equals;"https&colon;&sol;&sol;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2020&sol;06&sol;PHP-ติดตั้งและใช้งาน-Tesseract-OCR-6&period;png" data-full-size&equals;"1232x880" &sol;><&sol;figure>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph"><strong>Tesseract OCR กับ รูปภาพที่ซับซ้อน<&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">ลองทดสอบ Tesseract OCR กับระบบภาพ Captcha หลาย ๆ แบบ ตั้งแต่ง่าย ๆ จนถึงยากมาก &lpar; แบบที่คนเองยังอ่านลำบาก &rpar; <&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph"><strong>รูปแบบตัวเลขล้วน อ่านได้ปกติ<&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<figure class&equals;"wp-block-image size-large"><img src&equals;"https&colon;&sol;&sol;www&period;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2020&sol;06&sol;ocr-แบบง่าย-940x671&period;png" alt&equals;"" class&equals;"wp-image-5058" data-full&equals;"https&colon;&sol;&sol;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2020&sol;06&sol;ocr-แบบง่าย&period;png" data-full-size&equals;"1232x880" &sol;><&sol;figure>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph"><strong>แบบข้อความผสมตัวเลข ยังอ่านได้อยู่<&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<figure class&equals;"wp-block-image size-large"><img src&equals;"https&colon;&sol;&sol;www&period;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2020&sol;06&sol;ocr-ปกติ-940x671&period;png" alt&equals;"" class&equals;"wp-image-5059" data-full&equals;"https&colon;&sol;&sol;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2020&sol;06&sol;ocr-ปกติ&period;png" data-full-size&equals;"1232x880" &sol;><&sol;figure>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph"><strong>แบบเบลอ ๆ เอียง ๆ ยังพออ่านได้<&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<figure class&equals;"wp-block-image size-large"><img src&equals;"https&colon;&sol;&sol;www&period;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2020&sol;06&sol;ocr-ยากขึ้นมาหน่อย-940x671&period;png" alt&equals;"" class&equals;"wp-image-5060" data-full&equals;"https&colon;&sol;&sol;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2020&sol;06&sol;ocr-ยากขึ้นมาหน่อย&period;png" data-full-size&equals;"1232x880" &sol;><&sol;figure>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph"><strong>แบบยากมาก มีเส้นสีเดียวกันทับตัวอักษร อ่านไม่ได้แล้ว<&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<figure class&equals;"wp-block-image size-large"><img src&equals;"https&colon;&sol;&sol;www&period;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2020&sol;06&sol;ocr-ยากมาก-940x671&period;png" alt&equals;"" class&equals;"wp-image-5061" data-full&equals;"https&colon;&sol;&sol;codebee&period;co&period;th&sol;labs&sol;wp-content&sol;uploads&sol;2020&sol;06&sol;ocr-ยากมาก&period;png" data-full-size&equals;"1232x880" &sol;><&sol;figure>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph">ขอบคุณข้อมูลจากหลาย ๆ ที่สำหรับการทดสอบ<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li>https&colon;&sol;&sol;stackoverflow&period;com&sol;questions&sol;38091060&sol;how-to-setup-and-running-tesseract-ocr-for-php-opensource<&sol;li><li>https&colon;&sol;&sol;github&period;com&sol;thiagoalessio&sol;tesseract-ocr-for-php<&sol;li><li>https&colon;&sol;&sol;github&period;com&sol;tesseract-ocr&sol;tesseract&sol;wiki<&sol;li><li>https&colon;&sol;&sol;fonts&period;google&period;com<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<p class&equals;"wp-block-paragraph"><&sol;p>&NewLine;

Exit mobile version