-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprogram.py
More file actions
138 lines (99 loc) · 3.15 KB
/
Copy pathprogram.py
File metadata and controls
138 lines (99 loc) · 3.15 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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import tkinter as tk
from tkinter import filedialog
from PIL import Image
import pytesseract as pyt
import numpy as np
import cv2 as cv
import sys
import imutils
root=tk.Tk()
root.withdraw()
file=filedialog.askopenfilename()
img=cv.imread(file)
imgR=imutils.resize(img,width=500)
original=imgR.copy()
cv.imshow("Resized image after loading", imgR)
cv.waitKey(0)
cv.destroyAllWindows()
newImg=np.zeros(imgR.shape,imgR.dtype)
contrast=float(input("Enter the value of color boosting[1-3]: "))
for y in range(imgR.shape[0]):
for x in range(imgR.shape[1]):
for c in range(imgR.shape[2]):
newImg[y,x,c]=np.clip(contrast*imgR[y,x,c],0,255)
#newImg=cv.bilateralFilter(newImg.copy(),7,15,15)
cv.imshow('after increasing color intensity',newImg)
cv.waitKey(0)
cv.destroyAllWindows()
hsv=cv.cvtColor(newImg,cv.COLOR_BGR2HSV)
# Range of white color.
lower_white = np.array([0,0,180], dtype=np.uint8)
upper_white = np.array([180,38,255], dtype=np.uint8)
imgW=cv.inRange(hsv,lower_white,upper_white)
cv.imshow("white",imgW)
cv.waitKey(0)
cv.destroyAllWindows()
# Range of yellow color.
lower_yellow = np.array([20, 39, 64], dtype=np.uint8)
upper_yellow = np.array([40, 255, 255], dtype=np.uint8)
imgY=cv.inRange(hsv,lower_yellow,upper_yellow)
cv.imshow("yellow",imgY)
cv.waitKey(0)
cv.destroyAllWindows()
combinedImage=cv.bitwise_or(imgW,imgY)
cv.imshow("combined Image",combinedImage)
cv.waitKey(0)
cv.destroyAllWindows()
ker=int(input("Enter the size of kernel[7-15] :"))
rectKernel=cv.getStructuringElement(cv.MORPH_RECT,(ker,ker))
tophat=cv.morphologyEx(combinedImage,cv.MORPH_TOPHAT,rectKernel)
cv.imshow('after filtering',tophat)
cv.waitKey(0)
cv.destroyAllWindows()
contours, hierarchy= cv.findContours(tophat.copy(),cv.RETR_TREE,cv.CHAIN_APPROX_NONE)
contours=sorted(contours, key = cv.contourArea, reverse = True)[:10]
print("length of contours ",len(contours))
NumPltCnt=[]
for c in contours:
epsilon =0.02* cv.arcLength(c, True)
approx = cv.approxPolyDP(c,epsilon,True)
if len(approx) == 4:
NumPltCnt.append(approx)
print("Number of rectangles found :",len(NumPltCnt))
print(NumPltCnt)
if(len(NumPltCnt)==0):
sys.exit("No Number plate found !!.")
box=[]
for c in NumPltCnt:
rect=cv.minAreaRect(c)
x=cv.boxPoints(rect)
box.append(np.int0(x))
#img=cv.drawContours(img,[box],0,(0,255,0),3)
print(box[0])
xCord=[]
yCord=[]
for x in box[0]:
xCord.append(x[0])
yCord.append(x[1])
xmin=min(xCord)
xMax=max(xCord)
ymin=min(yCord)
ymax=max(yCord)
croped=original[ymin:ymax,xmin:xMax]
amtOfTxt=tophat[ymin:ymax,xmin:xMax]
text=pyt.image_to_string(amtOfTxt,lang='eng')
print("The extracted text of number plate is: ",text)
def isMaxWhite(plate):
avg = np.mean(plate)
if (avg >= 115):
return True
else:
return False
if(isMaxWhite(amtOfTxt)):
cv.imshow("Cropped",croped)
cv.waitKey(0)
cv.destroyAllWindows()
original=cv.drawContours(original,NumPltCnt,-1,(0,255,0),3)
cv.imshow("Final",original)
cv.waitKey(0)
cv.destroyAllWindows()