pip install tesseract-ocr code example
Example 1: pytesseract
pip install pytesseract
Example 2: pytesseract configs
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.
Example 3: image processing for OCR using python
1|
2| import cv2
3| import pytesseract
5|
6| image = cv2.imread(sample_image.png’)
7|
8| gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
9|
10|
11|
11|threshold_img = cv2.threshold(gray_image, 0, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
12|
13| cv2.imshow(‘threshold image’, threshold_img)
14|
15| cv2.waitKey(0)
16|
17| cv2.destroyAllWindows()