-
Notifications
You must be signed in to change notification settings - Fork 0
/
two_stage_lp.py
169 lines (147 loc) · 6.67 KB
/
two_stage_lp.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
"""
@author: sotiris
"""
import os
import pickle
import cv2
import argparse
import glob as glob
import numpy as np
from utilities import denormalize, match, draw_img_bboxes
def two_stage_lp(img_paths, settings_car, settings_lp):
from imageai.Detection.Custom import CustomObjectDetection
detector_car = CustomObjectDetection()
detector_car.setModelTypeAsYOLOv3()
detector_car.setModelPath(settings_car["model_path"])
detector_car.setJsonPath(settings_car["config_path"])
detector_car.loadModel()
detector_lp = CustomObjectDetection()
detector_lp.setModelTypeAsYOLOv3()
detector_lp.setModelPath(settings_lp["model_path"])
detector_lp.setJsonPath(settings_lp["config_path"])
detector_lp.loadModel()
crop_image = lambda img, bb: img[bb[1]:bb[3] + 1, bb[0]:bb[2] + 1]
detections_image = []
for in_img_path in img_paths:
detections = []
img = cv2.imread(in_img_path)
tmp = detector_car.detectObjectsFromImage(input_image=in_img_path,
output_type="array",
minimum_percentage_probability=\
settings_car["threshold"])[1]
rois = [(crop_image(img, det["box_points"]), det["box_points"]) \
for det in tmp if det["name"] in ("car","truck","motorbike","bus")]
for i, roi in enumerate(rois):
tmp = detector_lp.detectObjectsFromImage(input_image=roi[0],
input_type="array",
output_type="array",
minimum_percentage_probability=\
settings_lp["threshold"])[1]
for det_lp in tmp:
det_lp["box_points"] = (np.asarray(det_lp["box_points"]) +\
np.tile(roi[1][:2], 2)).tolist()
detections += tmp
detections_image.append(detections)
return detections_image
def make_inputs(anno_dir, img_dir):
anno_file_paths = glob.glob(os.path.join(anno_dir, "*.txt"))
in_img_list = []
bboxes = []
for anno_file_path in anno_file_paths:
in_img_list +=\
[os.path.join(img_dir,os.path.split(os.path.splitext(anno_file_path)[0])[-1]+".jpg")]
anno_file = open(anno_file_path)
objects = anno_file.readlines() # read all lines into a list
img = cv2.imread(in_img_list[-1]) # read image
if img is None:
print("corrupted image file")
in_img_list.pop()
continue
else:
bboxes.append([denormalize(obj, img.shape[:-1])
for obj in objects if obj.split()[0] in ('15')])
return in_img_list, bboxes
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Infer two models in parallel')
parser.add_argument('--anno_dir',
dest='anno_dir',
type=str,
required=True,
help="path to annotation directory"
)
parser.add_argument('--img_dir',
dest='img_dir',
type=str,
required=True,
help="path to images directory"
)
parser.add_argument('--config',
nargs=2,
dest='cfg_list',
required=True,
help="a list of paths to json configuration files"
)
parser.add_argument('--model',
nargs=2,
dest='model_list',
required=True,
help="a list of paths to the model files"
)
parser.add_argument('--out_dir',
dest='out_dir',
type=str,
required=False,
default=os.getcwd(),
help="path to the output directory"
)
parser.add_argument('--vehicle_thresh',
dest='vehicle_thresh',
type=float,
required=False,
default=0.3,
help="vehicle detection threshold"
)
parser.add_argument('--lp_thresh',
dest='lp_thresh',
type=float,
required=False,
default=0.5,
help="lp detection threshold"
)
args = parser.parse_args()
anno_dir = os.path.abspath(args.anno_dir)
img_dir = os.path.abspath(args.img_dir)
out_dir = os.path.abspath(args.out_dir)
vehicle_thresh = 100*args.vehicle_thresh
lp_thresh = 100*args.lp_thresh
input_paths, bboxes = make_inputs(anno_dir, img_dir)
for i in range(len(args.model_list)):
assert os.path.isfile(args.model_list[i]), "Not a valid model file %s"\
% (args.model_list[i])
assert os.path.isfile(args.cfg_list[i]), "Not a valid model file %s"\
% (args.cfg_list[i])
for i in range(len(args.model_list)):
assert os.path.isfile(args.model_list[i]), "Not a valid model file %s"\
% (args.model_list[i])
assert os.path.isfile(args.cfg_list[i]), "Not a valid model file %s"\
% (args.cfg_list[i])
settings_orig = {"model_path": os.path.abspath(args.model_list[0]),
"config_path": os.path.abspath(args.cfg_list[0]),
"threshold": vehicle_thresh}
settings_cus = {"model_path": os.path.abspath(args.model_list[1]),
"config_path": os.path.abspath(args.cfg_list[1]),
"threshold": lp_thresh}
out_dir_path = os.path.abspath(os.path.join(os.getcwd(), out_dir))
if not os.path.isdir(out_dir_path):
os.mkdir(out_dir_path)
detections_lp = two_stage_lp(input_paths, settings_orig, settings_cus)
mdict_list = []
for i in range(len(input_paths)):
draw_img_bboxes(input_paths[i], detections_lp[i], out_dir_path)
mdict = dict( )
mdict["file_path"] = input_paths[i]
mdict["matches"] = match(detections_lp[i], bboxes[i])
mdict["det"] = detections_lp[i]
mdict["gt"] = [ dict({"bbox": bbox, "class": r"LicensePlate"}) for bbox in bboxes[i]]
mdict_list.append(mdict)
pickle.dump(mdict_list, open(r"two_stage_mdict_list.pkl", "wb"))