-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathocr.py
86 lines (71 loc) · 3.08 KB
/
ocr.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
83
84
85
86
import cv2
import easyocr
import os
import numpy as np
import imutils
import pytesseract
pytesseract.pytesseract.tesseract_cmd = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
def ocr(img):
# img = cv2.imread(path)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# cv2.imshow ('gray',gray)
# threshold the image using Otsu's thresholding method
thresh = cv2.threshold(gray, 0, 255,
cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
# # apply a distance transform which calculates the distance to the
# # closest zero pixel for each pixel in the input image
# dist = cv2.distanceTransform(thresh, cv2.DIST_L2, 3)
# # normalize the distance transform such that the distances lie in
# # the range [0, 1] and then convert the distance transform back to
# # an unsigned 8-bit integer in the range [0, 255]
dist = cv2.normalize(thresh, None, 0, 1.0, cv2.NORM_MINMAX)
# cv2.imshow('dist2',dist)
# dist = (dist * 255).astype("uint8")
# cv2.imshow('dist3',dist)
# # threshold the distance transform using Otsu's method
dist = cv2.threshold(dist, 0, 255,
cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]
# cv2.imshow('dist4',dist)
# apply an "opening" morphological operation to disconnect components
# in the image
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
opening = cv2.morphologyEx(dist, cv2.MORPH_OPEN, kernel)
# find contours in the opening image, then initialize the list of
# contours which belong to actual characters that we will be OCR'ing
cnts = cv2.findContours(opening.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
chars = []
# loop over the contours
for c in cnts:
# compute the bounding box of the contour
(x, y, w, h) = cv2.boundingRect(c)
# check if contour is at least 35px wide and 100px tall, and if
# so, consider the contour a digit
if w >= 1 and h >= 10:
chars.append(c)
# compute the convex hull of the characters
chars = np.vstack([chars[i] for i in range(0, len(chars))])
hull = cv2.convexHull(chars)
# allocate memory for the convex hull mask, draw the convex hull on
# the image, and then enlarge it via a dilation
mask = np.zeros(img.shape[:2], dtype="uint8")
cv2.drawContours(mask, [hull], -1, 255, -1)
mask = cv2.dilate(mask, None, iterations=5)
# take the bitwise of the opening image and the mask to reveal *just*
# the characters in the image
final = cv2.bitwise_and(opening, opening, mask=mask)
# cv2.imshow("final", final)
# cv2.waitKey()
# reader = easyocr.Reader(['en'])
# text = None
# result = reader.readtext(final)
# for detection in result:
# text = detection[1]
# OCR the input image using Tesseract
text = pytesseract.image_to_string(final, config='--psm 6')
return text
# if __name__ == '__main__':
# root = './'
# path = os.path.join(root, 'runs/detect/exp6/crops/licence-plate/08.jpg')
# print(ocr(path))