This Tutorial covers about how to convert image to text using Python+ OpenCv + OCR. Which includes installing opencv for python, install and config tesseract ocr .
Technologies using: Tesseract is an optical character recognition engine for various operating systems. It is free software, released under the Apache License, Version 2.0, and development has been sponsored by Google since 2006. Tesseract is considered one of the most accurate open source OCR engines currently available.
OpenCV was built to provide a common infrastructure for computer vision applications and to accelerate the use of machine perception in the commercial products. Being a BSD-licensed product, OpenCV makes it easy for businesses to utilize and modify the code. The library has more than 2500 optimized algorithms, which includes a comprehensive set of both classic and state-of-the-art computer vision and machine learning algorithms. These algorithms can be used to detect and recognize faces, identify objects, classify human actions in videos, track camera movements, track moving objects, extract 3D models of objects, produce 3D point clouds from stereo cameras, stitch images together to produce a high resolution image of an entire scene, find similar images from an image database, remove red eyes from images taken using flash, follow eye movements, recognize scenery and establish markers to overlay it with augmented reality, etc. OpenCV has more than 47 thousand people in their user community and an estimated number of downloads exceeding 7 million. The library is used extensively in companies, research groups and by governmental bodies.
sudo apt-get updatesudo apt-get install libleptonica-dev sudo apt-get install tesseract-ocr tesseract-ocr-devsudo apt-get install libtesseract-devbrew install tesseractdownload binary from https://github.com/UB-Mannheim/tesseract/wiki. then add
pytesseract.pytesseract.tesseract_cmd = 'C:\Program Files (x86)\Tesseract-OCR\tesseract.exe' to your script.
pip install tesseractpip install tesseract-ocrreferences: https://pypi.org/project/pytesseract/ (INSTALLATION section) and https://github.com/tesseract-ocr/tesseract/wiki#installation
import cv2import numpy as nptry: from PIL import Imageexcept ImportError: import Imageimport pytesseract src_path = src_path def get_string(img_path):# read image with openCvimg = cv2.imread(img_path)# Convert to GrayScaleimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)# Apply dilation and erosion to remove some noisekernel = np.ones((1,1), np.uint8)img = cv2.dilate(img, kernel, iterations=1)img = cv2.erode(img, kernel, iterations=1)cv2.imwrite(src_path + "removed_noise.png", img)#Apply threshold to get image with only black and whiteimg = cv2.adaptiveThreshold (img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)cv2.imwrite(src_path + "thres.png",img)#Recognize text with tesseract for pythonresult = pytesseract.image_to_string(Image.open(src_path + "thres.png"))# os.remove(temp)return resultprint("---------Start Recognize text from image---------")print get_string(src_path+img_path)print("--------Done-----------")