Hãy tưởng tượng trường hợp trích xuất thông tin văn bản từ nhiều nguồn khác nhau cho dự án hiện tại mà bạn đang thực hiện, các nguồn khác nhau mà bạn có thể tìm thấy dữ liệu văn bản để trích xuất là gì? . Nhưng nếu bạn có thể trích xuất dữ liệu văn bản từ hình ảnh thì sao? . Tính năng này được sử dụng trên hầu hết các phần mềm hiện đại như Google Lens, Microsoft Math Calculator, v.v. Nhưng chúng ta có thể sử dụng nó cho mục đích riêng của mình không? . Python cùng với tesseract-OCR có thể được sử dụng để nhận dạng và trích xuất dữ liệu văn bản từ hình ảnh và chuyển đổi nó thành chuỗi. Ở đây chúng tôi sẽ triển khai OCR bằng python
Tesseract-OCR là gì?
Tesseract-OCR là một trong những công cụ Nhận dạng ký tự quang học mạnh nhất đang được điều hành bởi Google. Nó là một công cụ miễn phí mã nguồn mở hỗ trợ tất cả các loại hệ điều hành bao gồm Windows, Linux, macOS, v.v. Nó được xây dựng bằng nhiều chiến lược khác nhau như Thị giác máy tính, Học máy, Xử lý ngôn ngữ tự nhiên. Tesseract-OCR hỗ trợ tất cả các loại tệp hình ảnh bao gồm JPG, PNG, JPEG, BMP, JFIF. khiến nó trở thành một trong những công cụ OCR tốt nhất
Chúng ta có thể sử dụng OCR ở đâu?
OCR có thể hữu ích trong nhiều trường hợp như
- Trích xuất dữ liệu văn bản từ hình ảnh giúp tạo dữ liệu cho máy học và khoa học dữ liệu
- Chuyển đổi từ viết tay thành văn bản kỹ thuật số
- Lấy thông tin từ hộ chiếu và tài liệu
Cài đặt tesseract-OCR và pytesseract
Vì mục đích này, chúng tôi yêu cầu một mô-đun có tên "pytesseract" là trình bao bọc cho tesseract-OCR. Bạn có thể chỉ cần cài đặt pytesseract và tesseract-OCR bằng cách sử dụng pip
$ pip install pytesseract tesseract
Nếu bạn đang sử dụng hệ thống Windows, bạn sẽ cần cài đặt tesseract. tập tin exe riêng biệt. cài đặt nó từ liên kết này
Sau khi cài đặt thành công, đã đến lúc vào phần mã hóa
Ở đây, chúng tôi có một tệp hình ảnh đơn giản.
Bây giờ chúng ta sẽ chèn hình ảnh này vào mã để trích xuất văn bản được viết dưới dạng chuỗi.
import pytesseract
# Path were tesseract is installed
pytesseract.pytesseract.tesseract_cmd = 'C:/Users/Tesseract-OCR/Tesseract'
# Converting image to string
image_to_text = str[pytesseract.image_to_string[r'C:/users/91759/Desktop/greeting.png']]
print[image_to_text]
Output:
Welcome to PyCodeMates
Trước tiên, chúng tôi đã nhập mô-đun "pytesseract" và khởi tạo đường dẫn nơi mô-đun tesseract được cài đặt [Lưu ý rằng mô-đun tesseract được cài đặt trong hệ thống của bạn có thể ở một đường dẫn khác]. Sau đó, tên và vị trí của hình ảnh của chúng tôi được chỉ định để chương trình có thể xác định nó. Cuối cùng, chúng tôi đã nhận được kết quả chính xác "Chào mừng bạn đến với PyCodeMates". Bạn có thể thắc mắc về sự dễ dàng triển khai Nhận dạng ký tự quang học của riêng mình trong ít nhất 5 dòng mã python. Nhưng nếu bạn muốn tìm hiểu thêm về điều này thì đây là một số tài liệu tham khảo
Đọc văn bản từ hình ảnh bằng Tesseract
Cải thiện bài viết
Lưu bài viết
Thích bài viết
- Cập nhật lần cuối. 01 tháng 12 năm 2022
Cải thiện bài viết
Lưu bài viết
Pytesseract hay Python-tesseract là công cụ Nhận dạng ký tự quang học [OCR] dành cho python. Nó sẽ đọc và nhận dạng văn bản trong hình ảnh, biển số xe, v.v. Ở đây, chúng tôi sẽ sử dụng gói tesseract để đọc văn bản từ hình ảnh đã cho.
Chủ yếu, 3 bước đơn giản có liên quan ở đây như hình dưới đây. -
- Đang tải Hình ảnh được lưu từ máy tính hoặc tải xuống bằng trình duyệt và sau đó tải tương tự. [Mọi hình ảnh có văn bản]
- Nhị phân hóa hình ảnh [Chuyển đổi hình ảnh thành nhị phân]
- Sau đó chúng tôi sẽ chuyển hình ảnh qua hệ thống OCR
Thực hiện
Mã python sau thể hiện Bản địa hóa văn bản và đoán chính xác văn bản được viết trong hình ảnh.
Python3
python tesseract.py --image Images/OCR.png2
python tesseract.py --image Images/OCR.png3
python tesseract.py --image Images/OCR.png4
python tesseract.py --image Images/OCR.png5
python tesseract.py --image Images/OCR.png4
python tesseract.py --image Images/OCR.png7
python tesseract.py --image Images/OCR.png4
python tesseract.py --image Images/OCR.png9
python tesseract.py --image Images/OCR.png0
python tesseract.py --image Images/OCR.png1
python tesseract.py --image Images/OCR.png4
python tesseract.py --image Images/OCR.png3
python tesseract.py --image Images/OCR.png4
python tesseract.py --image Images/OCR.png5
python tesseract.py --image Images/OCR.png6
python tesseract.py --image Images/OCR.png7
python tesseract.py --image Images/OCR.png8
python tesseract.py --image Images/OCR.png9
python tesseract.py --image Images/OCR.png20
python tesseract.py --image Images/OCR.png21
python tesseract.py --image Images/OCR.png20
_______423____424____26
python tesseract.py --image Images/OCR.png26
python tesseract.py --image Images/OCR.png20
_______423____429____26____431
python tesseract.py --image Images/OCR.png32
python tesseract.py --image Images/OCR.png8
python tesseract.py --image Images/OCR.png34
python tesseract.py --image Images/OCR.png20
python tesseract.py --image Images/OCR.png36
python tesseract.py --image Images/OCR.png20
python tesseract.py --image Images/OCR.png23
python tesseract.py --image Images/OCR.png39
python tesseract.py --image Images/OCR.png6
python tesseract.py --image Images/OCR.png41
python tesseract.py --image Images/OCR.png20
python tesseract.py --image Images/OCR.png23
python tesseract.py --image Images/OCR.png29
python tesseract.py --image Images/OCR.png6
python tesseract.py --image Images/OCR.png46
python tesseract.py --image Images/OCR.png32
python tesseract.py --image Images/OCR.png48____26____450
python tesseract.py --image Images/OCR.png51
python tesseract.py --image Images/OCR.png52
python tesseract.py --image Images/OCR.png53
python tesseract.py --image Images/OCR.png6______455
python tesseract.py --image Images/OCR.png56
python tesseract.py --image Images/OCR.png57
python tesseract.py --image Images/OCR.png58
python tesseract.py --image Images/OCR.png59
python tesseract.py --image Images/OCR.png6
python tesseract.py --image Images/OCR.png41
python tesseract.py --image Images/OCR.png42
python tesseract.py --image Images/OCR.png43
python tesseract.py --image Images/OCR.png44
python tesseract.py --image Images/OCR.png45
python tesseract.py --image Images/OCR.png46
python tesseract.py --image Images/OCR.png6
python tesseract.py --image Images/OCR.png6
python tesseract.py --image Images/OCR.png41
python tesseract.py --image Images/OCR.png70
python tesseract.py --image Images/OCR.png71
python tesseract.py --image Images/OCR.png72
python tesseract.py --image Images/OCR.png73
python tesseract.py --image Images/OCR.png20
python tesseract.py --image Images/OCR.png75
python tesseract.py --image Images/OCR.png76
python tesseract.py --image Images/OCR.png77
python tesseract.py --image Images/OCR.png46
python tesseract.py --image Images/OCR.png43
python tesseract.py --image Images/OCR.png44
python tesseract.py --image Images/OCR.png45____446
python tesseract.py --image Images/OCR.png6
python tesseract.py --image Images/OCR.png6
python tesseract.py --image Images/OCR.png45
python tesseract.py --image Images/OCR.png70
_______471____448____449
python tesseract.py --image Images/OCR.png32
python tesseract.py --image Images/OCR.png71
python tesseract.py --image Images/OCR.png92
python tesseract.py --image Images/OCR.png93
python tesseract.py --image Images/OCR.png6
python tesseract.py --image Images/OCR.png95____496
python tesseract.py --image Images/OCR.png97
python tesseract.py --image Images/OCR.png98
python tesseract.py --image Images/OCR.png99
python tesseract.py --image Images/OCR.png00
python tesseract.py --image Images/OCR.png6
python tesseract.py --image Images/OCR.png02
python tesseract.py --image Images/OCR.png03
python tesseract.py --image Images/OCR.png04
python tesseract.py --image Images/OCR.png05
python tesseract.py --image Images/OCR.png06
python tesseract.py --image Images/OCR.png07
python tesseract.py --image Images/OCR.png08
python tesseract.py --image Images/OCR.png09
python tesseract.py --image Images/OCR.png10
python tesseract.py --image Images/OCR.png11
python tesseract.py --image Images/OCR.png09
python tesseract.py --image Images/OCR.png13____214
python tesseract.py --image Images/OCR.png15____473____432
Bây giờ, hãy làm theo các bước dưới đây để đọc thành công Văn bản từ một hình ảnh.
- Lưu mã và hình ảnh mà bạn muốn đọc văn bản trong cùng một tệp
- Mở dấu nhắc lệnh. Chuyển đến vị trí lưu tệp mã và hình ảnh
- Thực hiện lệnh bên dưới để xem Đầu ra
ví dụ 1
Thực hiện lệnh bên dưới để xem Đầu ra
python tesseract.py --image Images/OCR.png0
Chúng tôi có Hình ảnh gốc được hiển thị
Tiêu đề
Chúng tôi có hình ảnh thang độ xám được hiển thị. [P. png]
p
đầu ra
ví dụ 2
Thực hiện lệnh bên dưới để xem Đầu ra
python tesseract.py --image Images/OCR.png
Chúng tôi có Hình ảnh gốc được hiển thị
OCR
Chúng tôi có hình ảnh thang độ xám được hiển thị. [P. png]
p
đầu ra
Ghi chú cá nhân của tôi arrow_drop_up
Tiết kiệm