-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
executable file
·273 lines (231 loc) · 7.54 KB
/
main.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
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
from flask import Flask, Response, render_template, request
import cv2
import requests
from datetime import datetime
import json
app = Flask(__name__)
cap = cv2.VideoCapture(0)
frameWidth = 640
frameHeight = 480
nPlateCascade = cv2.CascadeClassifier(
"static/russian_numplate.xml")
minArea = 200
color = (255, 0, 255)
cap.set(3, frameWidth)
cap.set(4, frameHeight)
cap.set(10, 110)
count = 0
listObj1 = []
registered_vehicles = []
d = {
"region": None,
"state": None,
"numberplate": None,
"time": None,
"vehicle-of-building": None,
"phone-number": None
}
register_a_veh = [None, None, None]
def read_json2():
global registered_vehicles
with open("reg-vehicles.json") as fp2:
registered_vehicles = json.load(fp2)
read_json2()
@app.route("/", methods=["GET", "POST"])
def index():
global count
if request.method == "POST":
if request.form.get("enter"):
print("enter")
count = 1
return render_template('home.html')
def api1():
global d
regions = ['in'] # Change to your country
with open('n.png', 'rb') as fp: # location can be changed
response = requests.post('https://api.platerecognizer.com/v1/plate-reader/', data=dict(regions=regions),
files=dict(upload=fp), headers={'Authorization': 'Token add-your-key-here'})
# plate_number = response.json()['results'][0]['plate']
d["numberplate"] = response.json()['results'][0]['plate']
d["numberplate"] = d["numberplate"].upper()
d["time"] = str(datetime.now())
print("Number Plate = " + d["numberplate"])
# read_json2()
for i in registered_vehicles:
if i[0] == d["numberplate"]:
print("yes")
d["vehicle-of-building"] = "Yes"
d["phone-number"] = i[1]
break
else:
continue
else:
d["vehicle-of-building"] = "No"
state_list = {"DL": "Delhi", "MH": "Maharashtra", "HP": "Himachal Pradesh", "TN": "Tamil Nadu",
"HR": "Harayana", "UP": "Uttar Pradesh", "GJ": "Gujarat", "RJ": "Rajasthan", "WB": "West Bengal",
"MP": "Madhya Pradesh", "KA": "Karanataka", "KL": "Kerala", "AP": "Andhra Pradesh", "UK": "Uttrakhand",
"OD": "Odhisha", "PB": "Punjab", "GA": "Goa", "JH": "Jharkhand", "AS": "Assam", "BR": "Bihar"}
for i in state_list:
if d["numberplate"][:2] == i:
d["state"] = state_list[i]
d["region"] = i
break
else:
continue
write_to_json()
@app.route("/num", methods=["GET", "POST"])
def res():
return render_template('res.html', json_object=d)
@app.route("/all_veh", methods=["GET", "POST"])
def all_veh():
read_json1()
return render_template('all_veh.html', lst=listObj1, len=len(listObj1))
@app.route("/reg_veh", methods=["GET", "POST"])
def reg_veh():
return render_template('reg_veh.html', reg=registered_vehicles)
@app.route("/register")
def register():
return render_template('register.html')
@app.route("/registered", methods=["POST"])
def registered():
global register_a_veh
register_a_veh[0] = request.form["num"]
register_a_veh[1] = request.form["phone"]
register_a_veh[2] = request.form["address"]
write_to_json2()
read_json2()
if suc:
return render_template('error.html')
else:
return render_template('done.html')
@app.route("/deleted", methods=["POST"])
def delete():
global num_to_delete
l1 = len(registered_vehicles)
num_to_delete = request.form["num1"]
delete_from_json()
read_json2()
if len(registered_vehicles) == l1:
return render_template('error.html')
else:
return render_template('deleted.html')
@app.route("/updated", methods=["POST"])
def update():
global num_to_update
global num1_to_update
global phone_to_update
global address_to_update
num_to_update = request.form["num2"]
num1_to_update = request.form["num3"]
phone_to_update = request.form["phone2"]
address_to_update = request.form["address2"]
update_json()
read_json2()
if suc1:
return render_template("error.html")
else:
return render_template("updated.html")
def read_json1():
global listObj1
with open("sample.json") as fp1:
listObj1 = json.load(fp1)
def write_to_json():
listObj = []
with open("sample.json") as fp:
listObj = json.load(fp)
listObj.append(d)
with open("sample.json", 'w') as json_file:
json.dump(listObj, json_file,
indent=4,
separators=(',', ': '))
def write_to_json2():
global suc
suc = False
listObj2 = []
with open("reg-vehicles.json") as fp3:
listObj2 = json.load(fp3)
if register_a_veh[0] != "" and register_a_veh[1] != "" and register_a_veh[2] != "":
listObj2.append(register_a_veh)
else:
suc = True
pass
with open("reg-vehicles.json", 'w') as json_file:
json.dump(listObj2, json_file,
indent=4,
separators=(',', ': '))
def delete_from_json():
listObj3 = []
with open("reg-vehicles.json") as fp4:
listObj3 = json.load(fp4)
for i in listObj3:
if i[0] == str(num_to_delete):
listObj3.remove(i)
print("deleted")
break
else:
continue
with open("reg-vehicles.json", 'w') as json_file:
json.dump(listObj3, json_file,
indent=4,
separators=(',', ': '))
def update_json():
global suc1
suc1 = False
listObj4 = []
with open("reg-vehicles.json") as fp5:
listObj4 = json.load(fp5)
for i in listObj4:
if i[0] == str(num_to_update):
if num1_to_update != "":
i[0] = num1_to_update
if phone_to_update != "":
i[1] = phone_to_update
if address_to_update != "":
i[2] = address_to_update
else:
pass
print("updated")
break
if num1_to_update == "" and phone_to_update == "" and address_to_update == "" and num_to_update == "":
suc1 = True
else:
continue
with open("reg-vehicles.json", 'w') as json_file:
json.dump(listObj4, json_file,
indent=4,
separators=(',', ': '))
def gen(cap):
global count
while True:
ret, img = cap.read()
if not ret:
break
k = cv2.waitKey(2)
imgGray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
numberPlates = nPlateCascade.detectMultiScale(imgGray, 1.1, 10)
for (x, y, w, h) in numberPlates:
area = w * h
if area > minArea:
print("DETECTED")
img = cv2.rectangle(
img, (x, y), (x + w, y + h), (255, 0, 255), 2)
imgRoi = img[y:y + h, x:x + w]
if count == 1:
img_name = "n.png"
cv2.imwrite(img_name, imgRoi)
print("saved")
count = 0
api1()
else:
continue
ret, jpeg = cv2.imencode('.jpg', img)
frame = jpeg.tobytes()
yield (b'--frame\r\n'
b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
@app.route('/video_feed')
def video_feed():
global cap
return Response(gen(cap),
mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__':
app.run(host='0.0.0.0', port=1234, threaded=True)