Lời dẫn đầu
Hello mọi người, tiếp nối chuỗi sharing nho nhỏ về chủ đề OCR. Hôm nay, mình xin được đóng góp một ít sự tìm hiểu của mình qua việc dịch một bài viết rất hay về vấn đề sử dụng Tessaract-OCR cho Text Recognition với trợ thủ
Google Colab Link bài tại đây. Và let’s go, mình xin bắt đầu bài sharing hôm nay nhé.
Một số từ khóa
- Text Recognition
- Google Colab
- Tesseract-OCR
- OCR
Nội dung
Installation
Trước tiên, anh em cài đặt pytesseract
. Anh em có thể tìm hiểu thêm về package này trên trang https://pypi.org/project/pytesseract/ .
123 Pytesseract or Python-tesseract is an OCR tool for python that also serves asa wrapper for the Tesseract-OCR Engine
1 2 | !pip install pytesseract |
Nếu anh em sau khi run
gặp phải lỗi dưới đây:
TesseractNotFoundError: /usr/bin/tesseract is not installed or it’s not in your PATH
Lạ nhỉ? Yên tâm, việc cài đặt gặp một chút vấn đề. Lý do là vì trong thực tế, anh em bước đầu cần cài đặt các package khác như Tesseract-ocr và tập cmd trực tiếp kết nối với file.exe. Anh em chạy câu lệnh dưới để có thể cài đặt package tesseract-ocr
.
1 2 | !sudo apt install tesseract<span class="token operator">-</span>ocr |
Anh em nhớ RESTART RUNTIME
để khởi động lại môi trường vừa thiết lập nhé.
Import Libraries
1 2 3 4 5 6 7 | <span class="token keyword">try</span><span class="token punctuation">:</span> <span class="token keyword">from</span> PIL <span class="token keyword">import</span> Image <span class="token keyword">except</span> ImportError<span class="token punctuation">:</span> <span class="token keyword">import</span> Image <span class="token keyword">import</span> cv2 <span class="token keyword">import</span> pytesseract |
Checking folder cài đặt
Trước khi thực thi khối hàm OCR ảnh, anh em có thể check vị trí lưu cục bộ hiện tại của tesseract:
1 2 | !which tesseract |
Kết quả của câu lệnh sẽ cho biết vị trí tesseract:
1 2 3 4 | pytesseract.pytesseract.tesseract_cmd = ( r'/usr/bin/tesseract' ) |
Importing Image
Anh em có thể sử dụng cv2 để import và chỉnh sửa ảnh. Anh em có thể đọc hình ảnh thông qua hàm imread
của OpenCV. Hệ màu được trả về từ cv2
là BGR, do đó anh em lưu ý cần convert sang hệ màu RGB.
1 2 3 4 5 | img_cv <span class="token operator">=</span> cv2<span class="token punctuation">.</span>imread<span class="token punctuation">(</span><span class="token string">r'/content/image.png'</span><span class="token punctuation">)</span> <span class="token comment"># By default OpenCV stores images in BGR format and since pytesseract assumes RGB format,</span> <span class="token comment"># we need to convert from BGR to RGB format/mode:</span> <span class="token comment">#d = cv2.cvtColor(img_cv, cv2.COLOR_BGR2RGB)</span> |
Ngoài cách anh em truyền trực tiếp bởi đường dẫn ảnh, anh em hoàn toàn có thể sử dụng đoạn code dưới để có thể upload file ảnh lên Google Colab
:
1 2 3 | <span class="token keyword">from</span> google<span class="token punctuation">.</span>colab <span class="token keyword">import</span> files uploaded <span class="token operator">=</span> files<span class="token punctuation">.</span>upload<span class="token punctuation">(</span><span class="token punctuation">)</span> |
Tham số trong Tesseract
Câu lệnh chính để thực thi OCR trên ảnh:
1 2 3 4 | <span class="token comment"># Example of adding any additional options</span> custom_oem_psm_config <span class="token operator">=</span> <span class="token string">r'--oem 3 --psm 6'</span> pytesseract<span class="token punctuation">.</span>image_to_string<span class="token punctuation">(</span>image<span class="token punctuation">,</span> config<span class="token operator">=</span>custom_oem_psm_config<span class="token punctuation">,</span> lang <span class="token operator">=</span> <span class="token string">'eng'</span><span class="token punctuation">)</span> |
You can give three important flags for tesseract to work and these are -l , –oem , and –psm.
The -l (lang) flag controls the language of the input text.
The –oem argument, or OCR Engine Mode, controls the type of algorithm used by Tesseract.
The –psm controls the automatic Page Segmentation Mode used by Tesseract.
Tham số lang chính là ngôn ngữ đích mà anh em cần lựa chọn, danh sách các ngôn ngữ mà Tesseract có hỗ trợ được thông tin tại Danh sách ngôn ngữ.
Số lượng tham số chúng ta có thể điều chỉnh bao gồm hai tham số trong config, dưới dây là một số chế độ cho tham số psm
trong Tesseract.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #Page segmentation modes: # 0 Orientation and script detection (OSD) only. # 1 Automatic page segmentation with OSD. # 2 Automatic page segmentation, but no OSD, or OCR. # 3 Fully automatic page segmentation, but no OSD. (Default) # 4 Assume a single column of text of variable sizes. # 5 Assume a single uniform block of vertically aligned text. # 6 Assume a single uniform block of text. # 7 Treat the image as a single text line. # 8 Treat the image as a single word. # 9 Treat the image as a single word in a circle. # 10 Treat the image as a single character. # 11 Sparse text. Find as much text as possible in no particular order. # 12 Sparse text with OSD. # 13 Raw line. Treat the image as a single text line, # bypassing hacks that are Tesseract-specific. |
Tham số -oem có thể tham khảo tại bảng dưới đây:
Nguồn: https://ai-facets.org/tesseract-ocr-best-practices/
Thực thi OCR và output
1 2 3 4 5 | <span class="token comment">#by using print we avoid a single string containing various /n</span> lang <span class="token operator">=</span> <span class="token string">'eng'</span> config <span class="token operator">=</span> <span class="token string">r'--oem 3 --psm 6'</span> extractedInformation <span class="token operator">=</span> pytesseract<span class="token punctuation">.</span>image_to_string<span class="token punctuation">(</span>Image<span class="token punctuation">.</span><span class="token builtin">open</span><span class="token punctuation">(</span><span class="token string">'Screenshot 2022-11-12 101632.jpg'</span><span class="token punctuation">)</span><span class="token punctuation">,</span> lang <span class="token operator">=</span> lang<span class="token punctuation">,</span> config <span class="token operator">=</span> config<span class="token punctuation">)</span> |
Kết quả là 23/10/2000
Tesseract-OCR hoàn toàn được gắn và chạy thành công trên Google Colab. Quá đơn giản đúng không anh em.
Lời cảm ơn
Hy vọng một số thông tin được chia sẻ trong bài dịch sẽ mang lại cho mọi người nhiều điều thú vị và mới mẻ.