def detect_lp(output_dir, loaded_models, Iorig_name): imgs_paths = glob('%s/%s_*car.png' % (output_dir, Iorig_name)) print('Searching for license plates...') for i, img_path in enumerate(imgs_paths): bname = splitext(basename(img_path))[0] lp_net, lp_meta, lp_threshold = loaded_models[1] R, _ = detect(lp_net, lp_meta, img_path.encode('utf-8'), thresh=lp_threshold) if len(R): Iorig = cv2.imread(img_path) WH = np.array(Iorig.shape[1::-1], dtype=float) Llp = [] for i, r in enumerate(R): cx, cy, w, h = (np.array(r[2]) / np.concatenate( (WH, WH))).tolist() tl = np.array([cx - w / 2., cy - h / 2.]) br = np.array([cx + w / 2., cy + h / 2.]) label = Label(0, tl, br) Ilp = crop_region(Iorig, label) Llp.append(label) cv2.imwrite('%s/%s_lp.png' % (output_dir, bname), Ilp) lwrite('%s/%s_lp.txt' % (output_dir, bname), Llp) else: print('No license plate found')
def detect_vehicle(img_path, output_dir, loaded_models, bname): vehicle_net, vehicle_meta, vehicle_threshold = loaded_models[0] R, _ = detect(vehicle_net, vehicle_meta, img_path.encode('utf-8'), thresh=vehicle_threshold) R = [r for r in R if r[0].decode(encoding='utf-8') in ['car']] print('%d cars found' % len(R)) if len(R): Iorig = cv2.imread(img_path) WH = np.array(Iorig.shape[1::-1], dtype=float) Lcars = [] for i, r in enumerate(R): cx, cy, w, h = (np.array(r[2]) / np.concatenate((WH, WH))).tolist() tl = np.array([cx - w / 2., cy - h / 2.]) br = np.array([cx + w / 2., cy + h / 2.]) label = Label(0, tl, br) Icar = crop_region(Iorig, label) Lcars.append(label) cv2.imwrite('%s/%s_%dcar.png' % (output_dir, bname, i), Icar) lwrite('%s/%s_cars.txt' % (output_dir, bname), Lcars)
def find_vehicle_one_img(img_path, veh_net, veh_meta, out_dir, veh_thd): st = time.time() print '\tScanning %s' % img_path bname = basename(splitext(img_path)[0]) R, _ = dn.detect(veh_net, veh_meta, img_path, thresh=veh_thd) # R: [name, prob, [x center, y center, width, height]] R = [r for r in R if r[0] in ['car', 'bus']] out_img = [] out_label_f = "" if len(R): Iorig = cv2.imread(img_path) WH = np.array(Iorig.shape[1::-1], dtype=float) Lcars = [] for i, r in enumerate(R): cx, cy, w, h = (np.array(r[2]) / np.concatenate((WH, WH))).tolist() tl = np.array([cx - w / 2., cy - h / 2.]) br = np.array([cx + w / 2., cy + h / 2.]) label = Label(0, tl, br) Icar = crop_region(Iorig, label) Lcars.append(label) p_img = '%s/%s_%dcar.png' % (out_dir, bname, i) cv2.imwrite(p_img, Icar) out_img.append(p_img) out_label_f = '%s/%s_cars.txt' % (out_dir, bname) lwrite(out_label_f, Lcars) print '\t\t%d cars found, runtime: %.1fs' % (len(R), time.time() - st) return out_img, out_label_f
def vehicle_detection(img_path, output_dir): try: if not isdir(output_dir): makedirs(output_dir) bname = basename(splitext(img_path)[0]) plates = [] # Vehicle detection R, _ = detect(vehicle_net, vehicle_meta, img_path, thresh=vehicle_threshold) R = [r for r in R if r[0] in ['car', 'bus', 'motorbike']] # print '\t\t%d cars found' % len(R) if not len(R): return ("", plates) Iorig = cv2.imread(img_path) WH = np.array(Iorig.shape[1::-1], dtype=float) Lcars = [] for i, r in enumerate(R): cx, cy, w, h = (np.array(r[2]) / np.concatenate((WH, WH))).tolist() tl = np.array([cx - w / 2., cy - h / 2.]) br = np.array([cx + w / 2., cy + h / 2.]) label = Label(0, tl, br) Icar = crop_region(Iorig, label) Lcars.append(label) carImagePath = '%s/%s_%dcar.png' % (output_dir, bname, i) cv2.imwrite(carImagePath, Icar) #print("CarImagePath: ", carImagePath) # LP detection LPImagePath = LPDection(carImagePath) if LPImagePath: lp_str = OCRDection(LPImagePath) if lp_str: plates.append(lp_str) lwrite('%s/%s_cars.txt' % (output_dir, bname), Lcars) # draw yellow box around the cars and red box around license plates genOutput(img_path, output_dir, bname) return ('%s/%s_output.png' % (output_dir, bname), plates) except: traceback.print_exc() return ("", plates)
def detect_vehicle(self, img, filename): data = [] copy = img R, boxes, confidences, class_ids = self.vehicle.detect_objects(copy, str(filename).split('.')[0] + '.txt') if len(R): WH = np.array(copy.shape[1::-1], dtype=float) Lcars = [] for i, r in enumerate(R): cx, cy, w, h = (np.array(boxes[i])/np.concatenate((WH, WH))).tolist() tl = np.array([cx - w/2., cy - h/2.]) br = np.array([cx + w/2., cy + h/2.]) label = Label(0, tl, br) Lcars.append(label) data.append((cx, cy, w, h)) cv2.imwrite('%s/%s-car.png' % (self.output_dir, filename), copy) lwrite('%s/%s-car.txt' % (self.output_dir, filename), Lcars) return data
print('\t\t%d cars found' % len(R)) if len(R): Iorig = cv2.imread(img_path) WH = np.array(Iorig.shape[1::-1], dtype=float) Lcars = [] for i, r in enumerate(R): cx, cy, w, h = (np.array(r[2]) / np.concatenate( (WH, WH))).tolist() tl = np.array([cx - w / 2., cy - h / 2.]) br = np.array([cx + w / 2., cy + h / 2.]) label = Label(0, tl, br) Icar = crop_region(Iorig, label) Lcars.append(label) cv2.imwrite('%s/%s_%dcar.png' % (output_dir, bname, i), Icar) lwrite('%s/%s_cars.txt' % (output_dir, bname), Lcars) except: traceback.print_exc() sys.exit(1) sys.exit(0)
Iorig = cv2.imread(img_path) WH = np.array(Iorig.shape[1::-1], dtype=float) Lcars = [] for i, r in enumerate(R): cx, cy, w, h = (np.array(r[2]) / np.concatenate( (WH, WH))).tolist() tl = np.array([cx - w / 2., cy - h / 2.]) br = np.array([cx + w / 2., cy + h / 2.]) label = Label(0, tl, br, category=r[0].decode("utf-8")) Icar = crop_region(Iorig, label) Lcars.append(label) cv2.imwrite( # '%s/%s_%dcar.png' % (output_dir, bname, i), Icar) '%s/%s_car_%d.png' % (output_dir, bname, i), Icar) lwrite('%s/%s_cars.txt' % (output_dir, bname), Lcars, write_category_names=True) except: traceback.print_exc() sys.exit(1) sys.exit(0)
R, _ = detect(lp_net, lp_meta, img_path.encode('utf-8'), thresh=lp_threshold) #R = [r for r in R if r[0] in ['lp']] if len(R): Iorig = cv2.imread(img_path) WH = np.array(Iorig.shape[1::-1], dtype=float) Llp = [] for i, r in enumerate(R): cx, cy, w, h = (np.array(r[2]) / np.concatenate( (WH, WH))).tolist() tl = np.array([cx - w / 2., cy - h / 2.]) br = np.array([cx + w / 2., cy + h / 2.]) label = Label(0, tl, br) Ilp = crop_region(Iorig, label) Llp.append(label) cv2.imwrite('%s/%s_lp.png' % (output_dir, bname), Ilp) lwrite('%s/%s_lp.txt' % (output_dir, bname), Llp) except: traceback.print_exc() sys.exit(1) sys.exit(0)