Hướng dẫn how to download tesseract in python - cách tải xuống tesseract trong python

Python-Tesseract là một công cụ nhận dạng ký tự quang học [OCR] cho Python. Đó là, nó sẽ nhận ra và đọc văn bản được nhúng trong hình ảnh.

Python-Tesseract là một gói cho động cơ Google Tesseract-acr. Nó cũng hữu ích như một tập lệnh gọi độc lập cho Tesseract, vì nó có thể đọc tất cả các loại hình ảnh được hỗ trợ bởi các thư viện hình ảnh Gối và Leptonica, bao gồm JPEG, PNG, GIF, BMP, TIFF và những người khác. Ngoài ra, nếu được sử dụng làm tập lệnh, Python-Tesseract sẽ in văn bản được công nhận thay vì ghi nó vào một tệp.

CÁCH SỬ DỤNG

Bắt đầu nhanh

Lưu ý: Hình ảnh thử nghiệm được đặt trong thư mục thử nghiệm/dữ liệu của REPO GIT.tests/data folder of the Git repo.

Sử dụng thư viện:

from PIL import Image

import pytesseract

# If you don't have tesseract executable in your PATH, include the following:
pytesseract.pytesseract.tesseract_cmd = r''
# Example tesseract_cmd = r'C:\Program Files [x86]\Tesseract-OCR\tesseract'

# Simple image to string
print[pytesseract.image_to_string[Image.open['test.png']]]

# In order to bypass the image conversions of pytesseract, just use relative or absolute image path
# NOTE: In this case you should provide tesseract supported images or tesseract will return error
print[pytesseract.image_to_string['test.png']]

# List of available languages
print[pytesseract.get_languages[config='']]

# French text image to string
print[pytesseract.image_to_string[Image.open['test-european.jpg'], lang='fra']]

# Batch processing with a single file containing the list of multiple image file paths
print[pytesseract.image_to_string['images.txt']]

# Timeout/terminate the tesseract job after a period of time
try:
    print[pytesseract.image_to_string['test.jpg', timeout=2]] # Timeout after 2 seconds
    print[pytesseract.image_to_string['test.jpg', timeout=0.5]] # Timeout after half a second
except RuntimeError as timeout_error:
    # Tesseract processing is terminated
    pass

# Get bounding box estimates
print[pytesseract.image_to_boxes[Image.open['test.png']]]

# Get verbose data including boxes, confidences, line and page numbers
print[pytesseract.image_to_data[Image.open['test.png']]]

# Get information about orientation and script detection
print[pytesseract.image_to_osd[Image.open['test.png']]]

# Get a searchable PDF
pdf = pytesseract.image_to_pdf_or_hocr['test.png', extension='pdf']
with open['test.pdf', 'w+b'] as f:
    f.write[pdf] # pdf type is bytes by default

# Get HOCR output
hocr = pytesseract.image_to_pdf_or_hocr['test.png', extension='hocr']

# Get ALTO XML output
xml = pytesseract.image_to_alto_xml['test.png']

Hỗ trợ cho các đối tượng Mảng OpenCV/Numpy

import cv2

img_cv = cv2.imread[r'//digits.png']

# By default OpenCV stores images in BGR format and since pytesseract assumes RGB format,
# we need to convert from BGR to RGB format/mode:
img_rgb = cv2.cvtColor[img_cv, cv2.COLOR_BGR2RGB]
print[pytesseract.image_to_string[img_rgb]]
# OR
img_rgb = Image.frombytes['RGB', img_cv.shape[:2], img_cv, 'raw', 'BGR', 0, 0]
print[pytesseract.image_to_string[img_rgb]]

Nếu bạn cần cấu hình tùy chỉnh như OEM/PSM, hãy sử dụng từ khóa cấu hình.config keyword.

# Example of adding any additional options
custom_oem_psm_config = r'--oem 3 --psm 6'
pytesseract.image_to_string[image, config=custom_oem_psm_config]

# Example of using pre-defined tesseract config file with options
cfg_filename = 'words'
pytesseract.run_and_get_output[image, extension='txt', config=cfg_filename]

Thêm cấu hình sau, nếu bạn gặp lỗi TessData như: Lỗi mở tệp dữ liệu

# Example config: r'--tessdata-dir "C:\Program Files [x86]\Tesseract-OCR\tessdata"'
# It's important to add double quotes around the dir path.
tessdata_dir_config = r'--tessdata-dir ""'
pytesseract.image_to_string[image, lang='chi_sim', config=tessdata_dir_config]

Chức năng

  • Get_litages Trả về tất cả các ngôn ngữ hiện được hỗ trợ bởi Tesseract OCR. Returns all currently supported languages by Tesseract OCR.

  • get_tesseract_version trả về phiên bản Tesseract được cài đặt trong hệ thống. Returns the Tesseract version installed in the system.

  • Image_TO_String trả về đầu ra không biến đổi dưới dạng chuỗi từ xử lý OCR Tesseract Returns unmodified output as string from Tesseract OCR processing

  • Image_to_boxes trả về kết quả chứa các ký tự được nhận ra và ranh giới hộp của chúng Returns result containing recognized characters and their box boundaries

  • Image_TO_DATA Trả về kết quả chứa các ranh giới hộp, tâm sự và thông tin khác. Yêu cầu Tesseract 3.05+. Để biết thêm thông tin, vui lòng kiểm tra tài liệu Tesseract TSV Returns result containing box boundaries, confidences, and other information. Requires Tesseract 3.05+. For more information, please check the Tesseract TSV documentation

  • Image_TO_OSD Trả về kết quả chứa thông tin về định hướng và phát hiện tập lệnh. Returns result containing information about orientation and script detection.

  • Image_TO_ALTO_XML Trả về kết quả ở dạng Tesseract từ Alto XML. Returns result in the form of Tesseract’s ALTO XML format.

  • Run_and_get_output trả về đầu ra thô từ Tesseract OCR. Cung cấp thêm một chút kiểm soát các tham số được gửi đến Tesseract. Returns the raw output from Tesseract OCR. Gives a bit more control over the parameters that are sent to tesseract.

Thông số

Image_TO_DATA [Image, Lang = none, config = '', NICE = 0, output_type = output.String, thời gian chờ = 0, pandas_config = none]config='', nice=0, output_type=Output.STRING, timeout=0, pandas_config=None]

  • Đối tượng hình ảnh hoặc Chuỗi - PIL Hình ảnh/mảng numpy hoặc đường dẫn tệp của hình ảnh được xử lý bởi Tesseract. Nếu bạn vượt qua đối tượng thay vì đường dẫn tệp, pytesseract sẽ ngầm chuyển đổi hình ảnh thành chế độ RGB. Object or String - PIL Image/NumPy array or file path of the image to be processed by Tesseract. If you pass object instead of file path, pytesseract will implicitly convert the image to RGB mode.

  • Lang String - Chuỗi mã ngôn ngữ Tesseract. Mặc định là Eng nếu không được chỉ định! Ví dụ cho nhiều ngôn ngữ: Lang = 'Eng+FRA' String - Tesseract language code string. Defaults to eng if not specified! Example for multiple languages: lang='eng+fra'

  • Chuỗi cấu hình - bất kỳ cờ cấu hình tùy chỉnh bổ sung nào không có sẵn thông qua hàm pytesseract. Ví dụ: config = '-PSM 6' String - Any additional custom configuration flags that are not available via the pytesseract function. For example: config='--psm 6'

  • Số nguyên đẹp - Sửa đổi mức độ ưu tiên của bộ xử lý cho lần chạy Tesseract. Không được hỗ trợ trên Windows. Nice điều chỉnh sự độc đáo của các quy trình giống như Unix. Integer - modifies the processor priority for the Tesseract run. Not supported on Windows. Nice adjusts the niceness of unix-like processes.

  • Thuộc tính lớp output_type - Chỉ định loại đầu ra, mặc định là chuỗi. Đối với danh sách đầy đủ của tất cả các loại được hỗ trợ, vui lòng kiểm tra định nghĩa của lớp pytesseract.output. Class attribute - specifies the type of the output, defaults to string. For the full list of all supported types, please check the definition of pytesseract.Output class.

  • Số nguyên thời gian chờ hoặc float - Thời gian tính bằng giây để xử lý OCR, sau đó, pytesseract sẽ chấm dứt và nâng cao thời gian chạy. Integer or Float - duration in seconds for the OCR processing, after which, pytesseract will terminate and raise RuntimeError.

  • Pandas_config Dict - Chỉ dành cho loại đầu ra.DataFrame. Từ điển với các đối số tùy chỉnh cho pandas.read_csv. Cho phép bạn tùy chỉnh đầu ra của Image_TO_DATA. Dict - only for the Output.DATAFRAME type. Dictionary with custom arguments for pandas.read_csv. Allows you to customize the output of image_to_data.

CLI sử dụng:

pytesseract [-l lang] image_file

CÀI ĐẶT

Prerequisites:

  • Python-Tesseract đòi hỏi Python 3.6+

  • Bạn sẽ cần Thư viện hình ảnh Python [PIL] [hoặc cái nĩa gối]. Theo Debian/Ubuntu, đây là gói hình ảnh Python hoặc Python3.python-imaging or python3-imaging.

  • Cài đặt Google Tesseract OCR [Thông tin bổ sung Cách cài đặt động cơ trên Linux, Mac OSX và Windows]. Bạn phải có khả năng gọi lệnh Tesseract là Tesseract. Ví dụ, nếu đây không phải là trường hợp vì Tesseract không phải là đường dẫn của bạn, bạn sẽ phải thay đổi biến Tesseract_cmd, pytesseract.pytesseract.tesseract_cmd. Theo Debian/Ubuntu, bạn có thể sử dụng gói Tesseract-acr. Cho người dùng Mac OS. Vui lòng cài đặt gói Homebrew Tesseract.pytesseract.pytesseract.tesseract_cmd. Under Debian/Ubuntu you can use the package tesseract-ocr. For Mac OS users. please install homebrew package tesseract.

    Lưu ý: Trong một số trường hợp hiếm hoi, bạn có thể cần phải cài đặt thêm TessConfigs và cấu hình từ Tesseract-acr/Tessconfigs nếu gói cụ thể của HĐH không bao gồm chúng.tessconfigs and configs from tesseract-ocr/tessconfigs if the OS specific package doesn’t include them.

Kiểm tra trang gói PyTesseract để biết thêm thông tin.

pip install pytesseract

Hoặc nếu bạn đã cài đặt Git:

pip install -U git+//github.com/madmaze/pytesseract.git
git clone //github.com/madmaze/pytesseract.git
cd pytesseract && pip install -U .
conda install -c conda-forge pytesseract

Kiểm tra

Để chạy bộ thử nghiệm dự án này, cài đặt và chạy TOX. Đảm bảo rằng bạn đã cài đặt Tesseract và trên đường dẫn của bạn.tox. Ensure that you have tesseract installed and in your PATH.

pip install tox
tox

GIẤY PHÉP

Kiểm tra tệp giấy phép bao gồm trong kho/phân phối Python-Tesseract. Kể

Người đóng góp

  • Ban đầu được viết bởi Samuel Hoffstaetter

  • Juarez Bochi

  • Matthias Lee

  • Lars Kistner

  • Ryan Mitchell

  • Emilio Cecchini

  • John Hagen

  • Darius Morawiec

  • Eddie Bedada

  • Uğurcan Akyüz

Làm cách nào để cài đặt Tesseract?

Các bước cài đặt..
Bước 1-Tải xuống và cài đặt từ liên kết Tesseract-onr-W64-setup-v4.0.0.20181030.exe. ....
Bước 2 - Trong các biến môi trường hệ thống, thêm đường dẫn cài đặt Python như được hiển thị bên dưới [hoặc sao chép đường dẫn nơi bạn đã cài đặt Python].

Làm thế nào để bạn thực hiện Tesseract trong Python?

Tìm hiểu cách nhập gói pytesseract vào các tập lệnh Python của bạn.Sử dụng OpenCV để tải một hình ảnh đầu vào từ đĩa.Chuyển hình ảnh vào động cơ OCR Tesseract thông qua thư viện pytesseract.Hiển thị kết quả văn bản OCR trên thiết bị đầu cuối của chúng tôi.Use OpenCV to load an input image from disk. Pass the image into the Tesseract OCR engine via the pytesseract library. Display the OCR'd text results on our terminal.

Làm cách nào để tải xuống tesseract

Truy cập //github.com/tesseract-oc/tesseract/releases và tải xuống tệp .zip.5. Tiếp theo, truy cập //github.com/tesseract-oc/tessdata_best và chọn [các] tệp ngôn ngữ bạn cần nếu bạn đang làm việc với tài liệu ngôn ngữ không phải là tiếng Anh [xem hình ảnh bên dưới]. zip file. 5. Next, go to //github.com/tesseract-ocr/tessdata_best and select the language file[s] you need if you are working with non-English language material [see image below].

Làm cách nào để tải xuống pytesseract trong pycharm?

Cách cài đặt pytesseract trong Python..
Chạy lệnh này trong Anaconda CMD PIP Cài đặt pytesseract ..
Trước tiên hãy truy cập trang web hoặc chỉ đơn giản là tải xuống phiên bản 64bit ..
Bây giờ bấm đúp vào nhấp chuột vào thiết lập ..
Bây giờ bấm vào tiếp theo cho đến khi bạn đạt đến giai đoạn này [ảnh bên dưới] ..

Bài Viết Liên Quan

Chủ Đề