9951 explained code solutions for 126 technologies


tesseract-ocrHow can I increase the accuracy of tesseract OCR?


  1. Improve Tesseract OCR Training Data

The accuracy of Tesseract OCR can be improved by providing it with better training data. This can be done by creating a custom training dataset that contains samples of the type of text you want to recognize. The training dataset should include a variety of fonts, sizes, and styles.

  1. Adjust Tesseract OCR Parameters

Tesseract OCR has several parameters that can be adjusted to improve its accuracy. These parameters include the threshold, page segmentation mode, and language.

  1. Pre-process Images

Pre-processing images can also help improve the accuracy of Tesseract OCR. This includes techniques such as image binarization, deskewing, noise removal, and contrast adjustment.

  1. Example Code Block
import cv2
import pytesseract

# Read the image
img = cv2.imread('image.png')

# Pre-process the image
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]

# Run Tesseract OCR
text = pytesseract.image_to_string(thresh, lang='eng', config='--psm 6')

# Print the recognized text
print(text)
  1. Code Parts Explanation

    • cv2.imread('image.png'): Reads the image from the file.
    • cv2.cvtColor(img, cv2.COLOR_BGR2GRAY): Converts the image to grayscale.
    • cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)[1]: Applies a threshold to the image to binarize it.
    • pytesseract.image_to_string(thresh, lang='eng', config='--psm 6'): Runs Tesseract OCR on the image.
    • print(text): Prints the recognized text.
  2. Relevant Links

Edit this code on GitHub