<jupyter_start><jupyter_text>Failure Cases in Tesseract OCRIn this notebook, we will see some instances where Tesseract does not work as expected and provide logical reasons for them. We will see some examples from scanned documents as well as natural camera images.We will discuss how to improve the OCR output in the next notebook.  Install Tesseract<jupyter_code>!apt install libtesseract-dev tesseract-ocr > /dev/null
!pip install pytesseract > /dev/null<jupyter_output>WARNING: apt does not have a stable CLI interface. Use with caution in scripts.<jupyter_text>Import Libraries<jupyter_code>import pytesseract
import cv2
import glob
import matplotlib.pyplot as plt
%matplotlib inline
from IPython.display import Image<jupyter_output><empty_output><jupyter_text>Failure Cases Categories Even though failure cannot be always attributed to one specific reasons, We have listed a few major reasons for failure of OCR using Tesseract and in general. They are:1. **Cluttered Background** - The text might not be visibly clear or it might appear camouflaged with the background.1. **Small Text** - The text might be too small to be detected. 1. **Rotation or Perspective Distortion** - The text might be rotated in the image or the image itself might be distorted.  Test Image 1: Image of a Book<jupyter_code>!wget https://www.dropbox.com/s/uwrdek4jjac4ysz/book2.jpg?dl=1 -O book2.jpg --quiet<jupyter_output><empty_output><jupyter_text>Downloaded Image  Output<jupyter_code>book2_text = pytesseract.image_to_string('book2.jpg')
print(book2_text)<jupyter_output>The Impact of the Highly Improbable<jupyter_text>Observation We saw this example in the last notebook. The major reason is the relatively cluttered background and low contrast of the white text on orange background. Another issue with these kind of images is the variability of text size. **"Black"** is written in extra-large size whereas **"The impact of"** is normal.  Test Image 2: Driving License<jupyter_code>!wget https://www.dropbox.com/s/rdaha84n8jo3bmw/dl.jpg?dl=0 -O dl.jpg --quiet<jupyter_output><empty_output><jupyter_text>Downloaded Image![](https://www.dropbox.com/s/rdaha84n8jo3bmw/dl.jpg?dl=1)  Output<jupyter_code>dl_text = pytesseract.image_to_string('dl.jpg')
print(dl_text)
Image("dl.jpg")<jupyter_output>DRIVER LICENSE

= a

123 486 789 Sein States,
tate

MURPHY,

<NAME>
bomsun scm LO wl
CUO ULeTe Ne F
seraaiin NoNE™ ms |
08/20/2020 BeELZ™ BRO q

  

IVANYWHERE STREET
ITY, DL

500 asosse7eo 67890-1071 ah NA Yuare<jupyter_text>Observation - It is unable to detect small text( Date of Expiry, Address, etc.)- Same issue with cluttered background ( New York on Top Left )  Test Image 3: License Plate<jupyter_code>!wget "https://www.dropbox.com/s/xz24vxrp4uvvnri/license_plate.jpg?dl=0" -O lp1.jpg --quiet<jupyter_output><empty_output><jupyter_text>Downloaded Image  Output<jupyter_code>lp_text = pytesseract.image_to_string('lp1.jpg')
print(lp_text)<jupyter_output>Tiles)<jupyter_text>Observation Even though the text is very clear to us, Tesseract finds it difficult to recognize. The major issue is with contrast.   Street SignsStreet signs are one of the most difficult ones to recognize. Let us see some examples.  Test Image 4<jupyter_code>!wget https://www.dropbox.com/s/uwlnxiihqgni57o/streetsign1.jpg?dl=0 -O streetsign1.jpg --quiet<jupyter_output><empty_output><jupyter_text>Downloaded Image  Output<jupyter_code>ss1_text = pytesseract.image_to_string('streetsign1.jpg')
print(ss1_text)<jupyter_output><jupyter_text>Test Image 5<jupyter_code>!wget https://www.dropbox.com/s/dbkag5gsicxqoqg/streetsign2.jpg?dl=0 -O streetsign2.jpg --quiet<jupyter_output><empty_output><jupyter_text>Downloaded Image  Output<jupyter_code>ss2_text = pytesseract.image_to_string('streetsign2.jpg')
print(ss2_text)<jupyter_output><jupyter_text>Test Image 6<jupyter_code>!wget https://www.dropbox.com/s/cgni28zl1k9sesk/streetsign3.jpg?dl=0 -O streetsign3.jpg --quiet<jupyter_output><empty_output><jupyter_text>Downloaded Image  Output<jupyter_code>ss3_text = pytesseract.image_to_string('streetsign3.jpg')
print(ss3_text)<jupyter_output>
{'fraction_non_alphanumeric': 0.0886291697102508, 'fraction_numerical': 0.021670318967616266, 'mean_word_length': 6.296625222024867, 'pattern_counts': {'":': 0, '<': 51, '<?xml version=': 0, '>': 53, 'https://': 7, 'lorem ipsum': 0, 'www.': 7, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '14', 'dir': 'jupyter-structured-clean-dedup', 'id': '13174', 'license': 'Net-SNMP', 'n_tokens_mistral': 1485, 'n_tokens_neox': 1429, 'n_words': 394, 'path': 'OCR/2_Tesseract_Failure_Cases.ipynb', 'repo_name': 'vince-CV/cv-ocr-LP-recognition', 'size': '4114'}