1import cv2
2import pytesseract
3
4img = cv2.imread('image.jpg')
5
6# Adding custom options
7custom_config = r'--oem 3 --psm 6'
8pytesseract.image_to_string(img, config=custom_config)
1try:
2 from PIL import Image
3except ImportError:
4 import Image
5import pytesseract
6
7# If you don't have tesseract executable in your PATH, include the following:
8pytesseract.pytesseract.tesseract_cmd = r'<full_path_to_your_tesseract_executable>'
9# Example tesseract_cmd = r'C:\Program Files (x86)\Tesseract-OCR\tesseract'
10
11# Simple image to string
12print(pytesseract.image_to_string(Image.open('test.png')))
13
14# List of available languages
15print(pytesseract.get_languages(config=''))
16
17# French text image to string
18print(pytesseract.image_to_string(Image.open('test-european.jpg'), lang='fra'))
19
20# In order to bypass the image conversions of pytesseract, just use relative or absolute image path
21# NOTE: In this case you should provide tesseract supported images or tesseract will return error
22print(pytesseract.image_to_string('test.png'))
23
24# Batch processing with a single file containing the list of multiple image file paths
25print(pytesseract.image_to_string('images.txt'))
26
27# Timeout/terminate the tesseract job after a period of time
28try:
29 print(pytesseract.image_to_string('test.jpg', timeout=2)) # Timeout after 2 seconds
30 print(pytesseract.image_to_string('test.jpg', timeout=0.5)) # Timeout after half a second
31except RuntimeError as timeout_error:
32 # Tesseract processing is terminated
33 pass
34
35# Get bounding box estimates
36print(pytesseract.image_to_boxes(Image.open('test.png')))
37
38# Get verbose data including boxes, confidences, line and page numbers
39print(pytesseract.image_to_data(Image.open('test.png')))
40
41# Get information about orientation and script detection
42print(pytesseract.image_to_osd(Image.open('test.png')))
43
44# Get a searchable PDF
45pdf = pytesseract.image_to_pdf_or_hocr('test.png', extension='pdf')
46with open('test.pdf', 'w+b') as f:
47 f.write(pdf) # pdf type is bytes by default
48
49# Get HOCR output
50hocr = pytesseract.image_to_pdf_or_hocr('test.png', extension='hocr')
51
52# Get ALTO XML output
53xml = pytesseract.image_to_alto_xml('test.png')
54
1# https://github.com/mindee/doctr
2
3from doctr.models import ocr_predictor
4
5model = ocr_predictor(det_arch='db_resnet50', reco_arch='crnn_vgg16_bn', pretrained=True)