-
Notifications
You must be signed in to change notification settings - Fork 0
/
Spiffy.py
82 lines (63 loc) · 2.32 KB
/
Spiffy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# function to preprocess image like by finding biggest contour and cropping it then using various filters with the goal of making text as sharp as possible and then removing any noise that apperared in the process
import cv2
import numpy as np
import pytesseract
import os
import easyocr
def thresholding(img):
# convert to grayscale
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# blur the image
blur = cv2.GaussianBlur(gray, (5, 5), 0)
# apply adaptive thresholding
thresh = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 11, 2)
# invert the image
invert = cv2.bitwise_not(thresh)
# extract the contours by cropping the image starting from biggest countour to next 5 countours
# dilation and erosion to remove noise from invert
kernel = np.ones((1,1), np.uint8)
dilate = cv2.dilate(invert, kernel, iterations=1)
erode = cv2.erode(dilate, kernel, iterations=1)
return erode
# function to preprocess the image
def preprocess(img):
# get the contour of the image
thresh = thresholding(img)
# display thresh
# cv2.imshow('thresh', thresh)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
return thresh
def easyocr(img):
# easy ocr thresh image
text1=easyocr.Reader(['en']).readtext(img)
# take only the phrases from easy ocr output
text1 = [i[1] for i in text1] # type: ignore
# join the phrases
text1 = ' '.join(text1)
# print the text
print(text1)
# add text to original image
cv2.putText(img, text1, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
list = [text1, img]
return list
if __name__ == '__main__':
# read the image
img = cv2.imread('OCR_Test_Images/GVK-One.jpg')
# preprocess the image
thresh = preprocess(img)
# easy ocr thresh image
text1=easyocr.Reader(['en']).readtext(img)
# take only the phrases from easy ocr output
text1 = [i[1] for i in text1] # type: ignore
# join the phrases
text1 = ' '.join(text1)
# print the text
print(text1)
# add text to original image
cv2.putText(img, text1, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
# display the image
cv2.imshow('original', img)
cv2.imshow('thresh', thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()