-
Notifications
You must be signed in to change notification settings - Fork 5
/
detection.py
74 lines (66 loc) · 2.07 KB
/
detection.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
from openalpr import Alpr
import cv2
import sys
import os
import time
cam = cv2.VideoCapture(1)
cv2.namedWindow("test")
time.sleep(1)
alpr = Alpr("eu", "openalpr.conf", "runtime_data")
if not alpr.is_loaded():
print("Error loading OpenALPR")
sys.exit(1)
alpr.set_top_n(20)
#alpr.set_default_region("md")
i = 0
counter_nothing = 0
plates_list = []
confidences = []
while True:
ret, frame = cam.read()
cv2.imshow("test", frame)
cv2.imwrite(str(i)+ ".jpg", frame)
results = alpr.recognize_file(str(i)+".jpg")
#for plate in results["results"]:
# if plate not in plates:
# plates.append(plate["plate"])
# confidences.append(plate["confidence"])
# print(confidences[plates.index(plate["plate"])])
# confidences[plates.index(plate["plate"])] = confidences[plates.index(plate["plate"])] + plate["confidence"]
plates = results["results"]
if len(plates) > 0:
for candidate in plates[0]['candidates']:
if candidate["plate"] not in plates_list:
plates_list.append(candidate["plate"])
confidences.append(candidate["confidence"])
continue
confidences[plates_list.index(candidate["plate"])] += candidate["confidence"]
if (not plates):
counter_nothing += 1
else:
counter_nothing = 0
if (counter_nothing == 50):
plates_list = []
confidences = []
counter_nothing = 0
try:
ind = confidences.index(max(confidences))
print(str(plates_list[ind]) + " " + str(confidences[ind]))
except:
pass
os.remove(str(i)+".jpg")
#print(results)
k = cv2.waitKey(1)
i = i + 1
i = 0
for plate in results['results']:
i += 1
print("Plate #%d" % i)
print(" %12s %12s" % ("Plate", "Confidence"))
for candidate in plate['candidates']:
prefix = "-"
if candidate['matches_template']:
prefix = "*"
print(" %s %12s%12f" % (prefix, candidate['plate'], candidate['confidence']))
# Call when completely done to release memory
alpr.unload()