Now that officially first evaluation is finished and not to mentioned but I passed, here is my 2nd report for Image Processing (Image to text).
Project- Poor man’s rekognition
Mentor- Johannes Lochter
Organization- CCExtractor Development
Org admin- Carlos Fernandez
May 27 — June 1 = complete use-cases 1,2 and 3 (completed)
June 3 — June 15 = complete use-cases 4 (completed)
June 17 — June 19 =1st report (completed)
June 20 — July 3=complete use-case 5(completed)
July 4 — July 6=2nd report(completed)
Well, first evaluation is officially over and I have completed my part with all of my dedication and full determination. So in this article, I will only be discussing my last use-case and that is extraction of text from an image file.
Use-case 5
Extraction of text from an image.
Code 1.
Extraction of text from an image is a subpart of image processing and is called OPTICAL CHARACTER RECOGNITION (OCR). I have used Tesseract which is an OCR engine developed by Google. It supports Unicode and has the ability to recognize more than 100 languages. I personally think it is super cool ✌.
Requirements:
- Pytesseract
- Pillow
I have added multiple libraries to experiment but one can only import this 2 libraries and everything will be smooth.
Let’s jump to the coding part
#import libraries
import cv2
import numpy as np
import pytesseract
from PIL import Imageimg = Image.open('4.png')pytesseract.pytesseract.tesseract_cmd = 'C:/Users/Faiz Khan/Desktop/ddd/text recognition/Tesseract-OCR/tesseract'result = pytesseract.image_to_string(img)with open('reader.txt', mode='w') as file:
file.write(result)
Here I have basically read the texts from an image and have made a .txt file of the result.
Code 2.
This is a second which was my main code but I was unable to properly process it because of some error but have thought to complete it as soon as I get time. Here I did perform filters in order to get a clear image.
Requirements:
- OpenCV
- Numpy
- Tesseract
- Pillow
Let’s jump to the coding part.
import cv2
import numpy as np
from PIL import Image
from pytesseract import image_to_string# Path of working folder on Disk
src_path = "C:/Users/Faiz Khan/Desktop/ddd/text recognition"def get_string(img_path):# Read image with opencv
img = cv2.imread('C:/Users/Faiz Khan/Desktop/ddd/text recognition/2.png')
# Convert to gray
img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Apply dilation and erosion to remove some noise
kernel = np.ones((1, 1), np.uint8)
img = cv2.dilate(img, kernel, iterations=1)
img = cv2.erode(img, kernel, iterations=1)
# Write image after removed noise
cv2.imwrite(src_path + "removed_noise.png", img) # Apply threshold to get image with only black and white
img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 2)
# Write the image after apply opencv to do some ...
cv2.imwrite(src_path + "thres.png", img)
# Recognize text with tesseract for python
result = pytesseract.image_to_string(Image.open(src_path + "thres.png"))
return resultprint(get_string(src_path + "cool.jpg") )
Much more can be done here.
Also if anyone is able to figure out the errors and solution to the above-mentioned code, please let me know.
The amazing journey so far.