-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathocr1.py
More file actions
81 lines (75 loc) · 3.29 KB
/
ocr1.py
File metadata and controls
81 lines (75 loc) · 3.29 KB
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
import cv2
import numpy as np
import pytesseract
import re
class Rekognition:
def __init__(self, data):
self.dataObj = data
pytesseract.pytesseract.tesseract_cmd = "D:\Software\Programming\Teserract_OCR\\tesseract.exe"
# path = "C:\\Users\\soham\\PycharmProjects\\pythonProject\\Resources\\indimg6.jpeg"
# self.dataObj.plateImage = cv2.imread(path)
# path1 = "C:\\Users\\soham\\PycharmProjects\\pythonProject\\Resources\\img.png"
# self.dataObj.photo = cv2.imread(path1) # Store cv2 image
self.dataObj.numPlate = self.detectLicense()
print(self.dataObj.numPlate)
def showOriginal(self):
cv2.imshow("Original", self.dataObj.plateImage)
cv2.waitKey(0)
def show(self, img, name=""):
cv2.imshow(name, img)
cv2.waitKey(0)
def process_Image(self):
img = cv2.resize(self.dataObj.plateImage, (440, 160))
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
# self.show(thresh,"Threshold")
# input()
output = cv2.connectedComponentsWithStats(thresh, 4, cv2.CV_32S)
(numLabels, labels, stats, centroids) = output
for i in range(0, numLabels):
if i == 0:
text = "examining component {}/{}".format(i + 1, numLabels)
else:
text = "examining component {}/{}".format(i + 1, numLabels)
# print("[INFO] {}".format(text))
x = stats[i, cv2.CC_STAT_LEFT]
y = stats[i, cv2.CC_STAT_TOP]
w = stats[i, cv2.CC_STAT_WIDTH]
h = stats[i, cv2.CC_STAT_HEIGHT]
area = stats[i, cv2.CC_STAT_AREA]
# print("width = ", w, " height = ", h)
(cX, cY) = centroids[i]
output = img.copy()
cv2.rectangle(output, (x, y), (x + w, y + h), (0, 255, 0), 3)
cv2.circle(output, (int(cX), int(cY)), 4, (0, 0, 255), -1)
componentMask = (labels == i).astype("uint8") * 255
# show our output image and connected component mask
# cv2.imshow("Output", output)
# input()
# cv2.imshow("Connected Component", componentMask)
# input()
# while cv2.waitKey(4000) & 0xFF == ord('q'):
# break
mask = np.zeros(gray.shape, dtype="uint8")
for i in range(1, numLabels):
x = stats[i, cv2.CC_STAT_LEFT]
y = stats[i, cv2.CC_STAT_TOP]
w = stats[i, cv2.CC_STAT_WIDTH]
h = stats[i, cv2.CC_STAT_HEIGHT]
area = stats[i, cv2.CC_STAT_AREA]
kw = w > 15 and w < 100
kh = h > 50 and h < 160
# ka = area>2000 and area<16000
if all((kw, kh)):
# print("[INFO] keeping connected component '{}'".format(i))
componentMask = (labels == i).astype("uint8") * 255
mask = cv2.bitwise_or(mask, componentMask)
return mask
def detectLicense(self):
res = self.process_Image()
self.show(res,"Masked-Characters")
cv2.waitKey(2000)
print("sd")
numplate = pytesseract.image_to_string(res).replace(" ", "")
numplate = re.sub("[^0-9a-zA-Z]", "", numplate) # in this string
return numplate