-
Notifications
You must be signed in to change notification settings - Fork 7
/
plate.py
executable file
·122 lines (104 loc) · 4.41 KB
/
plate.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
from ctypes import *
import os
import cv2
import darknet
import glob
def convertBack(x, y, w, h): # Convert from center coordinates to bounding box coordinates
xmin = int(round(x - (w / 2)))
xmax = int(round(x + (w / 2)))
ymin = int(round(y - (h / 2)))
ymax = int(round(y + (h / 2)))
return xmin, ymin, xmax, ymax
def cvDrawBoxes(detections, img):
if len(detections) > 0: # If there are any detections
plate_detection = 0
for detection in detections: # For each detection
name_tag = detection[0].decode() # Decode list of classes
if name_tag == 'license_plate': # Filter detections for car class
x, y, w, h = detection[2][0],\
detection[2][1],\
detection[2][2],\
detection[2][3] # Obtain the detection coordinates
xmin, ymin, xmax, ymax = convertBack(
float(x), float(y), float(w), float(h)) # Convert to bounding box coordinates
xmax += xmax*0.05
xmax = int(xmax)
pt1 = (xmin, ymin) # Format Coordinates for Point 1 and 2
pt2 = (xmax, ymax)
save_path = "cropped/"
roi = img[ymin:ymax, xmin:xmax]
cv2.imwrite(os.path.join(save_path, str(cvDrawBoxes.counter) + ".jpg"), roi)
cvDrawBoxes.counter += 1
cv2.rectangle(img, pt1, pt2, (0, 255, 0), 1)
plate_detection += 1
return img
cvDrawBoxes.counter = 0
netMain = None
metaMain = None
altNames = None
def YOLO(image_list):
global metaMain, netMain, altNames
configPath = "./cfg/alpr_tiny_test.cfg"
weightPath = "./alpr_tiny.weights"
metaPath = "./data/alpr.data"
if not os.path.exists(configPath):
raise ValueError("Invalid config path `" +
os.path.abspath(configPath)+"`")
if not os.path.exists(weightPath):
raise ValueError("Invalid weight path `" +
os.path.abspath(weightPath)+"`")
if not os.path.exists(metaPath):
raise ValueError("Invalid data file path `" +
os.path.abspath(metaPath)+"`")
if netMain is None:
netMain = darknet.load_net_custom(configPath.encode(
"ascii"), weightPath.encode("ascii"), 0, 1) # batch size = 1
if metaMain is None:
metaMain = darknet.load_meta(metaPath.encode("ascii"))
if altNames is None:
try:
with open(metaPath) as metaFH:
metaContents = metaFH.read()
import re
match = re.search("names *= *(.*)$", metaContents,
re.IGNORECASE | re.MULTILINE)
if match:
result = match.group(1)
else:
result = None
try:
if os.path.exists(result):
with open(result) as namesFH:
namesList = namesFH.read().strip().split("\n")
altNames = [x.strip() for x in namesList]
except TypeError:
pass
except Exception:
pass
i = 0
while (i < len(image_list)):
# print(os.listdir(), "\n\n\n")
image = cv2.imread(os.path.join("data/plates", image_list[i]))
width = image.shape[1]
height = image.shape[0]
# Create an image we reuse for each detect
darknet_image = darknet.make_image(width, height, 3)
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image_rgb = cv2.resize(image_rgb,
(width, height),
interpolation=cv2.INTER_LINEAR)
darknet.copy_image_from_bytes(darknet_image, image_rgb.tobytes())
detections = darknet.detect_image(netMain, metaMain, darknet_image, thresh=0.3)
image = cvDrawBoxes(detections, image_rgb)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
#cv2.imshow('Output', image)
#cv2.waitKey(0)
i += 1
cv2.destroyAllWindows()
if __name__ == "__main__":
image_list = os.listdir(os.path.join("data", "plates"))
img_list = []
for file in image_list:
if file.endswith(".jpg") or file.endswith(".png"):
img_list.append(file)
YOLO(img_list)