def detect(model, input_dir, output_dir): lp_threshold = .5 imgs_paths = glob('%s/*car.jpg' % input_dir) print 'Searching for license plates using WPOD-NET' for i, img_path in enumerate(imgs_paths): print '\t Processing %s' % img_path bname = splitext(basename(img_path))[0] Ivehicle = cv2.imread(img_path) ratio = float(max(Ivehicle.shape[:2])) / min(Ivehicle.shape[:2]) side = int(ratio * 288.) bound_dim = min(side + (side % (2 ** 4)), 608) print "\t\tBound dim: %d, ratio: %f" % (bound_dim, ratio) Llp, LlpImgs, _ = detect_lp(model, im2single(Ivehicle), bound_dim, 2 ** 4, (240, 80), lp_threshold) if len(LlpImgs): Ilp = LlpImgs[0] Ilp = cv2.cvtColor(Ilp, cv2.COLOR_BGR2GRAY) Ilp = cv2.cvtColor(Ilp, cv2.COLOR_GRAY2BGR) s = Shape(Llp[0].pts) cv2.imwrite('%s/%s_lp.png' % (output_dir, bname), Ilp * 255.) writeShapes('%s/%s_lp.txt' % (output_dir, bname), [s])
def detect_lp(self, img, filename, i): ratio = float(max(img.shape[:2])) / min(img.shape[:2]) side = int(ratio*288.) bound_dim = min(side + (side % (2**4)), 608) Llp, LlpImgs, _ = detect_lp(self.wpod_net, im2single(img), bound_dim, 2**4, (240, 80), self.lp_threshold) if len(LlpImgs): Ilp = LlpImgs[0] Ilp = cv2.cvtColor(Ilp, cv2.COLOR_BGR2GRAY) Ilp = cv2.cvtColor(Ilp, cv2.COLOR_GRAY2BGR) s = Shape(Llp[0].pts) cv2.imwrite('%s/%s-lp-%s.png' % (self.output_dir, filename, i), Ilp*255.) # LP OCR R, idxs, boxes, confidences, class_ids = self.lp.detect_objects(Ilp*255., filename) print('\t\t%d lps found' % len(R)) if len(R): L = dknet_label_conversion(R, 240, 80) L = nms(L, .45) L.sort(key=lambda x: x.tl()[0]) lp_str = ''.join([chr(l.cl()) for l in L]) if (len(lp_str) <= 3): return "No license found" return lp_str return "No license found"
def plate_detect(wpod_net, lp_threshold, Icars, Lcars): print ('Searching for license plates using WPOD-NET') Ilps = [] Llps = [] for i, (Icar, Lcar) in enumerate(zip(Icars, Lcars)): print ('\t Processing car %s' % i) ratio = float(max(Icar.shape[:2]))/min(Icar.shape[:2]) side = int(ratio*288.) bound_dim = min(side + (side%(2**4)),608) # print ("\t\tBound dim: %d, ratio: %f" % (bound_dim,ratio)) Icar = Icar.astype('uint8') Llp,LlpImgs,_ = detect_lp(wpod_net,im2single(Icar),bound_dim,2**4,(240,80),lp_threshold) if len(LlpImgs): print ("\t\tLP found") Ilp = LlpImgs[0] Ilp = cv2.cvtColor(Ilp, cv2.COLOR_BGR2GRAY) Ilp = cv2.cvtColor(Ilp, cv2.COLOR_GRAY2BGR) s = Shape(Llp[0].pts) #cv2.imwrite('%s/car%s_lp.png' % (plate_img_dir, i),Ilp*255.) #writeShapes('%s/car%s_lp.txt' % (plate_img_dir, i),[s]) Ilps.append(Ilp*255.) Llps.append(s) else: Ilps.append(np.nan) Llps.append(np.nan) return Ilps, Llps
def LPDection(carImagePath): wpod_net = load_model(wpod_net_path) print carImagePath Ivehicle = cv2.imread(carImagePath) ratio = float(max(Ivehicle.shape[:2])) / min(Ivehicle.shape[:2]) side = int(ratio * 288.) bound_dim = min(side + (side % (2**4)), 608) Llp, LlpImgs, _ = detect_lp(wpod_net, im2single(Ivehicle), bound_dim, 2**4, (240, 80), lp_threshold) if len(LlpImgs): bname = basename(splitext(carImagePath)[0]) dname = dirname(carImagePath) Ilp = LlpImgs[0] cv2.imwrite('%s/%s_lp_raw.png' % (dname, bname), Ilp * 255.) Ilp = cv2.cvtColor(Ilp, cv2.COLOR_BGR2GRAY) Ilp = cv2.cvtColor(Ilp, cv2.COLOR_GRAY2BGR) s = Shape(Llp[0].pts) LPImagePath = '%s/%s_lp.png' % (dname, bname) cv2.imwrite(LPImagePath, Ilp * 255.) LPTextPath = '%s/%s_lp.txt' % (dname, bname) writeShapes(LPTextPath, [s]) return LPImagePath else: return ""
def predict(self, img, bname): output_dir = "output/tmp" # bname = splitext(basename(img_path))[0] # Ivehicle = cv2.imread(img_path) Ivehicle = cv2.normalize(src=img, dst=None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U) ratio = float(max(Ivehicle.shape[:2]))/min(Ivehicle.shape[:2]) side = int(ratio*288.) bound_dim = min(side + (side % (2**4)), 608) # print("\t\tBound dim: {}, ratio: {}".format(bound_dim, ratio)) Llp, LlpImgs, _ = detect_lp(self.wpod_net, im2single(Ivehicle), bound_dim, 2**4, (240, 80), self.lp_threshold) if len(LlpImgs): Ilp = LlpImgs[0] Ilp = cv2.cvtColor(Ilp, cv2.COLOR_BGR2GRAY) Ilp = cv2.cvtColor(Ilp, cv2.COLOR_GRAY2BGR) s = Shape(Llp[0].pts) # cv2.imwrite('{}/{}_lp.png'.format(output_dir, bname), Ilp * 255.) # writeShapes('{}/{}_lp.txt'.format(output_dir, bname), [s]) Ilp = cv2.normalize(src=(Ilp * 255.), dst=None, alpha=0, beta=255, norm_type=cv2.NORM_MINMAX, dtype=cv2.CV_8U) return Ilp, s return None, None
def augment_sample(I,pts, dim_w, dim_h, cls): maxsum,maxangle = 50,np.array([20.,20.,25.]) angles = np.random.rand(3)*maxangle if angles.sum() > maxsum: angles = (angles/angles.sum())*(maxangle/maxangle.sum()) I = im2single(I) iwh = getWH(I.shape) # codigo original # whratio = random.uniform(2., 4.) # wsiz = random.uniform(dim * .2, dim * 1.) # carro if cls=='0': whratio = random.uniform(2.75, 3.25) wsiz = random.uniform(dim_w * .2, dim_w * 0.7) else: whratio = random.uniform(0.7, 1.2) wsiz = random.uniform(dim_w * .2, dim_w * 0.7) # whratio = 1. # wsiz = random.uniform(dim_w*.2, dim_w*1.) # moto # whratio = random.uniform(0.7, 1.2) # wsiz = random.uniform(dim_w * .3, dim_w * 0.7) hsiz = wsiz/whratio dx = random.uniform(0.,dim_w - wsiz) dy = random.uniform(0.,dim_h - hsiz) pph = getRectPts(dx,dy,dx+wsiz,dy+hsiz) pts = pts*iwh.reshape((2,1)) T = find_T_matrix(pts2ptsh(pts),pph) H = perspective_transform((dim_w,dim_h),angles=angles) H = np.matmul(H,T) Iroi,pts = project(I,H,pts,dim_w, dim_h) # hsv_mod = np.random.rand(3).astype('float32') # hsv_mod = (hsv_mod - .5)*.3 # hsv_mod[0] *= 360 # Iroi = hsv_transform(Iroi,hsv_mod) Iroi = np.clip(Iroi,0.,1.) pts = np.array(pts) # if random.random() > .5: # Iroi,pts = flip_image_and_pts(Iroi,pts) tl,br = pts.min(1),pts.max(1) llp = Label(0,tl,br) return Iroi,llp,pts
def lp_detector(Ivehicle, wpod_net): lp_threshold = .2 ratio = float(max(Ivehicle.shape[:2])) / min(Ivehicle.shape[:2]) side = int(ratio * 288.) bound_dim = min(side + (side % (2**4)), 608) Llp, LlpImgs, _ = detect_lp(wpod_net, im2single(Ivehicle), bound_dim, 2**4, (240, 80), lp_threshold) if len(LlpImgs): Ilp = LlpImgs[0] cv2.imwrite("lp1.png", Ilp * 255.) return Ilp * 255
def detectar_placa(self, cropped_car_image_ndarray, generic_detected_vehicle_class, top_left_car): height, width = cropped_car_image_ndarray.shape[:2] ratio = float(max(cropped_car_image_ndarray.shape[:2])) / min( cropped_car_image_ndarray.shape[:2]) side = int(ratio * 288.) bound_dim = min(side + (side % (2**4)), 608) lp_threshold = .4 Llp, LlpImgs, _ = detect_lp(self.wpod_net, im2single(cropped_car_image_ndarray), bound_dim, 2**4, (240, 80), lp_threshold, generic_detected_vehicle_class) detected_plates_list = [] if len(LlpImgs): for indice_bbox, Ilp in enumerate(LlpImgs): # Ilp = LlpImgs[0] llp_img_np_bmp = Ilp.astype('float32') * 255. llp_img_np_bmp = llp_img_np_bmp.astype(np.uint8) llp_img_pil_bmp = Image.fromarray(llp_img_np_bmp) # llp_img_pil_bmp_resized = llp_img_pil_bmp.resize((WIDTH_PLATE_CHAR_RECOG, HEIGHT_PLATE_CHAR_RECOG), # Image.NEAREST) # llp_img_np_bmp_resized = np.array(llp_img_pil_bmp_resized) license_plate_bouding_box_relativo = Llp[indice_bbox] probability = license_plate_bouding_box_relativo._Label__prob top_left_rl_tp = tuple( license_plate_bouding_box_relativo._Label__tl.tolist()) top_left_x = int(top_left_rl_tp[0] * width) top_left_y = int(top_left_rl_tp[1] * height) top_left_abs_frame_x = top_left_x + top_left_car[0] top_left_abs_frame_y = top_left_x + top_left_car[1] bottom_right_rl = license_plate_bouding_box_relativo._Label__br.tolist( ) bottom_right_x = int(bottom_right_rl[0] * width) bottom_right_y = int(bottom_right_rl[1] * height) detected_plates_list.append({ 'plate_ndarray': llp_img_np_bmp, 'top_left_bbox': (top_left_x, top_left_y), 'top_left_abs_frame': (top_left_abs_frame_x, top_left_abs_frame_y), 'bottom_right_bbox': (bottom_right_x, bottom_right_y), 'probability': probability, 'generic_detected_vehicle_class': generic_detected_vehicle_class }) if len(detected_plates_list) > 0: sorted(detected_plates_list, key=lambda i: i['probability'], reverse=True) return detected_plates_list[0] return None
def augment_sample(I,pts,dim): maxsum,maxangle = 120,np.array([80.,80.,45.]) angles = np.random.rand(3)*maxangle if angles.sum() > maxsum: angles = (angles/angles.sum())*(maxangle/maxangle.sum()) I = im2single(I) # /255.0 归一化 iwh = getWH(I.shape) #得到宽高 whratio = random.uniform(2.,4.) # 随即取一个2-4的值作为宽高比 wsiz = random.uniform(dim*.2,dim*1.) # 宽取0.2*208 到 208之间 hsiz = wsiz/whratio dx = random.uniform(0.,dim - wsiz) dy = random.uniform(0.,dim - hsiz) #下面涉及到整个变换 # In the first 3 lines, the original corner points are transformed into a rectangular bounding box with aspect ratio # varying between 2:1 and 4:1. In other words, T matrix rectifies the LP with a random aspect ratio. Then, pph = getRectPts(dx,dy,dx+wsiz,dy+hsiz) pts = pts*iwh.reshape((2,1)) #将点恢复到真实坐标值 T = find_T_matrix(pts2ptsh(pts),pph) #in the next two lines, a perspective transformation with random rotation (H) is combined with T to #generate the final transformation. H = perspective_transform((dim,dim),angles=angles) H = np.matmul(H,T) Iroi,pts = project(I,H,pts,dim) hsv_mod = np.random.rand(3).astype('float32') hsv_mod = (hsv_mod - .5)*.3 hsv_mod[0] *= 360 Iroi = hsv_transform(Iroi,hsv_mod) Iroi = np.clip(Iroi,0.,1.) pts = np.array(pts) if random.random() > .5: Iroi,pts = flip_image_and_pts(Iroi,pts) tl,br = pts.min(1),pts.max(1) llp = Label(0,tl,br) return Iroi,llp,pts
def augment_sample(I,pts,dim): maxsum,maxangle = 120,np.array([80.,80.,45.]) angles = np.random.rand(3)*maxangle if angles.sum() > maxsum: angles = (angles/angles.sum())*(maxangle/maxangle.sum()) # chuyển qua ma trận float32, chia cho 255 I = im2single(I) # Lấy w, h dạng ma trận iwh = getWH(I.shape) whratio = random.uniform(2.,4.) wsiz = random.uniform(dim*.2,dim*1.) hsiz = wsiz/whratio dx = random.uniform(0.,dim - wsiz) dy = random.uniform(0.,dim - hsiz) pph = getRectPts(dx,dy,dx+wsiz,dy+hsiz) # Lấy tọa độ thật pts = pts*iwh.reshape((2,1)) T = find_T_matrix(pts2ptsh(pts),pph) H = perspective_transform((dim,dim),angles=angles) H = np.matmul(H,T) Iroi,pts = project(I,H,pts,dim) hsv_mod = np.random.rand(3).astype('float32') hsv_mod = (hsv_mod - .5)*.3 hsv_mod[0] *= 360 Iroi = hsv_transform(Iroi,hsv_mod) Iroi = np.clip(Iroi,0.,1.) pts = np.array(pts) if random.random() > .5: Iroi,pts = flip_image_and_pts(Iroi,pts) # lấy giá trị tọa độ top-left, bot-right tl,br = pts.min(1),pts.max(1) llp = Label(0,tl,br) return Iroi,llp,pts
def lpd(img, boxes, frame_count): print("inside-1") output_dir = 'output' detected_cars_dir="detected_cars" detected_plates_dir="detected_plates" lp_threshold = .5 # lp_model="data/lp-detector/wpod-net_update1.h5" # wpod_net = load_model(lp_model) plates=[] for i, box in enumerate(boxes): if min(box) >0: # print(box) print("inside-2") bname = 'frame{}_{}.png'.format(frame_count,i) Ivehicle=img[box[0]:box[2], box[1]:box[3]] cv2.imwrite("%s/%s/%s" %(output_dir,detected_cars_dir,bname),Ivehicle) # print(Ivehicle.shape[:2]) ratio = float(max(Ivehicle.shape[:2]))/min(Ivehicle.shape[:2]) side = int(ratio*288.) bound_dim = min(side + (side%(2**4)),608) # print "\t\tBound dim: %d, ratio: %f" % (bound_dim,ratio) Llp,LlpImgs,_ = detect_lp(self.wpod_net,im2single(Ivehicle),bound_dim,2**4,(240,80),lp_threshold) if len(LlpImgs): print("inside-3") Ilp = LlpImgs[0] Ilp = cv2.cvtColor(Ilp, cv2.COLOR_BGR2GRAY) Ilp = cv2.cvtColor(Ilp, cv2.COLOR_GRAY2BGR) s = Shape(Llp[0].pts) # print(Llp[0].pts) plates.append(Llp[0].pts) # cv2.imwrite('%s/%s_lp.png' % (output_dir,bname),Ilp*255.) cv2.imwrite("%s/%s/%s" % (output_dir,detected_plates_dir,bname),Ilp*255.) # writeShapes('%s/%s_lp.txt' % (output_dir,bname),[s]) return plates
def detect_license_plate(Ivehicle, lp_threshold, wpod_net): try: ratio = float(max(Ivehicle.shape[:2]))/min(Ivehicle.shape[:2]) side = int(ratio*288.) bound_dim = min(side + (side%(2**4)),608) #print "\t\tBound dim: %d, ratio: %f" % (bound_dim,ratio) Llp,LlpImgs,_ = detect_lp(wpod_net,im2single(Ivehicle),bound_dim,2**4,(240,80),lp_threshold) if len(LlpImgs): Ilp = LlpImgs[0] Ilp = cv2.cvtColor(Ilp, cv2.COLOR_BGR2GRAY) Ilp = cv2.cvtColor(Ilp, cv2.COLOR_GRAY2BGR) return Ilp except: pass
def detectPlates(self, img, box, frame_count, i): # print("inside-1") plates = [] if min(box) > 0: # print(box) # print("inside-2") Ivehicle = img[box[0]:box[2], box[1]:box[3]] # cv2.imwrite("%s/%s/%s" % # (output_dir, detected_cars_dir, bname), Ivehicle) # print(Ivehicle.shape[:2]) ratio = float(max(Ivehicle.shape[:2])) / min(Ivehicle.shape[:2]) side = int(ratio * 288.) bound_dim = min(side + (side % (2**4)), 608) # print "\t\tBound dim: %d, ratio: %f" % (bound_dim,ratio) #dt_plates_start = time.time() Llp, LlpImgs, _ = detect_lp(self.wpod_net, im2single(Ivehicle), bound_dim, 2**4, (240, 80), self.lp_threshold) #dt_plates_end = time.time() # print("Time taken for for internal plate detection", # dt_plates_end-dt_plates_start) if len(LlpImgs): # print("inside-3") Ilp = LlpImgs[0] #Ilp = cv2.cvtColor(Ilp, cv2.COLOR_BGR2GRAY) #Ilp = cv2.cvtColor(Ilp, cv2.COLOR_GRAY2BGR) #s = Shape(Llp[0].pts) # print(Llp[0].pts) plates.append(Llp[0].pts) # cv2.imwrite('%s/%s_lp.png' % (output_dir,bname),Ilp*255.) # cv2.imwrite("%s/%s/%s" % # (output_dir, detected_plates_dir, bname), Ilp*255.) # writeShapes('%s/%s_lp.txt' % (output_dir,bname),[s]) return plates
def find_lp_one_img(img_path, lp_trans_net, out_dir, lp_thd): st = time.time() print '\t Processing %s' % img_path bname = splitext(basename(img_path))[0] Ivehicle = cv2.imread(img_path) ratio = float(max(Ivehicle.shape[:2])) / min(Ivehicle.shape[:2]) side = int(ratio * 288.) bound_dim = min(side + (side % (2**4)), 608) print "\t\tBound dim: %d, ratio: %f" % (bound_dim, ratio), Llp, LlpImgs, _ = detect_lp(lp_trans_net, im2single(Ivehicle), bound_dim, 2**4, (240, 80), lp_thd) lp_img = "" lp_label_f = "" if len(LlpImgs): Ilp = LlpImgs[0] Ilp = cv2.cvtColor(Ilp, cv2.COLOR_BGR2GRAY) Ilp = cv2.cvtColor(Ilp, cv2.COLOR_GRAY2BGR) s = Shape(Llp[0].pts) lp_img = '%s/%s_lp.png' % (out_dir, bname) lp_label_f = '%s/%s_lp.txt' % (out_dir, bname) cv2.imwrite(lp_img, Ilp * 255.) writeShapes(lp_label_f, [s]) print "runtime: %.1fs" % (time.time() - st) return lp_img, lp_label_f
print('Searching for license plates using WPOD-NET') for i, img_path in enumerate(imgs_paths): print('\t Processing %s' % img_path) start = datetime.datetime.now() bname = splitext(basename(img_path))[0] Ivehicle = cv2.imread(img_path) ratio = float(max(Ivehicle.shape[:2]))/min(Ivehicle.shape[:2]) side = int(ratio*288.) bound_dim = min(side + (side % (2**4)), 608) print("\t\tBound dim: %d, ratio: %f" % (bound_dim, ratio)) Llp, LlpImgs, _ = detect_lp(wpod_net, im2single( Ivehicle), bound_dim, 2**4, (240, 80), lp_threshold) if len(LlpImgs): Ilp = LlpImgs[0] Ilp = cv2.cvtColor(Ilp, cv2.COLOR_BGR2GRAY) Ilp = cv2.cvtColor(Ilp, cv2.COLOR_GRAY2BGR) s = Shape(Llp[0].pts) cv2.imwrite('%s/%s_lp.png' % (output_dir, bname), Ilp*255.) writeShapes('%s/%s_lp.txt' % (output_dir, bname), [s]) stop = datetime.datetime.now() print(stop-start) except: traceback.print_exc()
for i, img_path in enumerate(onlyfiles): print('\t Processing %s' % img_path) logger.info('\t Processing %s' % img_path) bname = splitext(basename(img_path))[0] Ivehicle = cv2.imread(img_path) ratio = float(max(Ivehicle.shape[:2])) / min(Ivehicle.shape[:2]) side = int(ratio * 288.) bound_dim = min(side + (side % (2**4)), 608) #print("\t\tBound dim: %d, ratio: %f" % (bound_dim, ratio)) #logger.info("\t\tBound dim: %d, ratio: %f" % (bound_dim, ratio)) Llp, LlpImgs, elapsed_time = detect_lp(wpod_net, im2single(Ivehicle), bound_dim, 2**4, (240, 80), lp_threshold) inference_times.append(elapsed_time) if len(LlpImgs): Ilp = LlpImgs[0] Ilp = cv2.cvtColor(Ilp, cv2.COLOR_BGR2GRAY) Ilp = cv2.cvtColor(Ilp, cv2.COLOR_GRAY2BGR) s = Shape(Llp[0].pts) cv2.imwrite('%s/%s_lp.png' % (output_dir, bname), Ilp * 255.) writeShapes('%s/%s_lp.txt' % (output_dir, bname), [s]) print("Model size after gzip is : {} bytes".format( get_gzipped_model_size(tflite_path)))
def validar_lp_model(entrada_diretorio_validacao, diretorio_saida, wpod_net): print('iniciando validacao modelo') lp_threshold = .4 imgs_paths = glob('%s/*.jpg' % entrada_diretorio_validacao) # if not os.path.exists(diretorio_saida): # os.makedirs(diretorio_saida) print('Searching for license plates using WPOD-NET') indicadores_validacao = IndicadoresValidacao() for i, img_path in enumerate(imgs_paths): # print('\t Processing %s' % img_path) bname_image_file = splitext(basename(img_path))[0] name_file_gt = bname_image_file + '.txt' name_file = basename(img_path) Ivehicle = cv2.imread(img_path) height, width = Ivehicle.shape[:2] ratio = float(max(Ivehicle.shape[:2])) / min(Ivehicle.shape[:2]) # side = int(ratio*288.) # bound_dim = min(side + (side%(2**4)),608) side = int(ratio * 288.) bound_dim = min(side + (side % (2**4)), 608) # print("\t\tBound dim: %d, ratio: %f" % (bound_dim,ratio)) ground_truth_frame = [] ground_truth_car = [] ground_truth_moto = [] class_object = None gt_img_path = img_path.replace('.jpg', '.txt') with open(gt_img_path) as f: lines = f.readlines() for linha in lines: if len(linha) < 5: continue indicadores_validacao.labeled_samples_total += 1 pontos = linha.split(',')[1:9] class_object = int(linha.split(',')[-2]) Llp, LlpImgs, _ = detect_lp(wpod_net, im2single(Ivehicle), bound_dim, 2**4, (240, 80), lp_threshold, class_object) lista_preds_frame = [] pontos = [ int(float(ponto) * width) if indice < 4 else int(float(ponto) * height) for indice, ponto in enumerate(pontos) ] # ground_truth = [int(float(pontos[0]) * width), int(float(pontos[4]) * height), # int(float(pontos[2]) * width), int(float(pontos[6]) * height), 0, 0, 0] x_points = pontos[0:4] * width y_points = pontos[4:8] * height # top_left_plate = int(float(pontos[0]) * width), int(float(pontos[4]) * height) top_left_plate_x = min(x_points) top_left_plate_y = min(y_points) bottom_right_plate_x = max(x_points) bottom_right_plate_y = max(y_points) top_left_plate = top_left_plate_x, top_left_plate_y # bottom_right_plate = int(float(pontos[2]) * width), int(float(pontos[6]) * height) bottom_right_plate = bottom_right_plate_x, bottom_right_plate_y # gt = [xmin, ymin, xmax, ymax, class_id, difficult, crowd] ground_truth_frame.append([ top_left_plate[0], top_left_plate[1], bottom_right_plate[0], bottom_right_plate[1], 0, 0, 0 ]) if class_object == 0: indicadores_validacao.labeled_samples_total_car += 1 ground_truth_car.append([ top_left_plate[0], top_left_plate[1], bottom_right_plate[0], bottom_right_plate[1], class_object, 0, 0 ]) else: indicadores_validacao.labeled_samples_total_moto += 1 ground_truth_moto.append([ top_left_plate[0], top_left_plate[1], bottom_right_plate[0], bottom_right_plate[1], class_object, 0, 0 ]) if len(LlpImgs): for indice_bbox, Ilp in enumerate(LlpImgs): # Ilp = LlpImgs[0] Ilp = cv2.cvtColor(Ilp, cv2.COLOR_BGR2GRAY) # Ilp = cv2.cvtColor(Ilp, cv2.COLOR_GRAY2BGR) license_plate_bouding_box_relativo = Llp[indice_bbox] probability = license_plate_bouding_box_relativo._Label__prob top_left_rl_tp = tuple( license_plate_bouding_box_relativo._Label__tl.tolist()) top_left_x = int(top_left_rl_tp[0] * width) top_left_y = int(top_left_rl_tp[1] * height) bottom_right_rl = license_plate_bouding_box_relativo._Label__br.tolist( ) bottom_right_x = int(bottom_right_rl[0] * width) bottom_right_y = int(bottom_right_rl[1] * height) plate_width = bottom_right_x - top_left_x plate_height = bottom_right_y - top_left_y red_color = (255, 0, 0) bbox_color = red_color # car_rgb_np = cv2.cvtColor(Ivehicle, cv2.COLOR_BGR2RGB) car_rgb_np = Ivehicle thickness = 1 # [xmin, ymin, xmax, ymax, class_id, confidence] if top_left_x > 0 and top_left_y > 0 and bottom_right_x > 0 and bottom_right_y > 0: preds_frame = [ int(top_left_x), int(top_left_y), int(bottom_right_x), int(bottom_right_y), class_object, probability ] lista_preds_frame.append(preds_frame) cv2.rectangle(car_rgb_np, (top_left_x, top_left_y), (bottom_right_x, bottom_right_y), bbox_color, thickness) # filled s = Shape(Llp[0].pts) # cv2.imwrite('%s/%s_lp_car.png' % (output_dir,bname), car_rgb_np) # cv2.imwrite('%s/%s_lp.png' % (output_dir,bname),Ilp*255.) # writeShapes('%s/%s_lp.txt' % (diretorio_saida,bname_image_file),[s]) else: print('imagem sem placa detectacada %s ' % img_path) lista_preds_frame.append([0, 0, 0, 0, class_object, 0]) # metric_fn.add(np.array(lista_preds_frame), np.array([[0, 0, 0, 0, 0, 0, 0]])) metric_fn.add(np.array(lista_preds_frame), np.array(ground_truth_frame)) # if is_exibir_gt and len(LlpImgs): # for bbox_gt in ground_truth_frame: # cv2.rectangle(car_rgb_np, (bbox_gt[0], bbox_gt[1]), (bbox_gt[2], bbox_gt[3]), bbox_color_gt, thickness) # filled # cv2.imwrite('%s/%s_lp_car.png' % (diretorio_saida, bname_image_file), car_rgb_np) true_positive_frame, false_positive_frame, false_negative_frame, true_positive_prob_frame, false_positive_prob_frame = evaluate_precision_recall( ground_truth_frame, lista_preds_frame, 0.5) indicadores_validacao.true_positive += true_positive_frame indicadores_validacao.false_positive += false_positive_frame indicadores_validacao.false_negative += false_negative_frame indicadores_validacao.true_positive_probability.extend( true_positive_prob_frame) indicadores_validacao.false_positive_probability.extend( false_positive_prob_frame) if class_object == 0: indicadores_validacao.true_positive_car += true_positive_frame indicadores_validacao.false_positive_car += false_positive_frame indicadores_validacao.false_negative_car += false_negative_frame else: indicadores_validacao.true_positive_moto += true_positive_frame indicadores_validacao.false_positive_moto += false_positive_frame indicadores_validacao.false_negative_moto += false_negative_frame print('calculando mAP') mAP_pascal = metric_fn.value(iou_thresholds=0.5, recall_thresholds=np.arange(0., 1.1, 0.1))['mAP'] print('VOC PASCAL mAP:' + str(mAP_pascal)) mAP_pascal_all_points = metric_fn.value(iou_thresholds=0.5)['mAP'] print('VOC PASCAL mAP in all points:' + str(mAP_pascal_all_points)) mAP_coco = metric_fn.value(iou_thresholds=np.arange(0.5, 1.0, 0.05), recall_thresholds=np.arange(0., 1.01, 0.01), mpolicy='soft')['mAP'] print('COCO mAP: ' + str(mAP_coco)) indicadores_validacao.imprmir_ftn_all() indicadores_validacao.imprimir_precision_recall_all() # indicadores_validacao.imprimir_probabilidades() return mAP_pascal, mAP_pascal_all_points, mAP_coco, indicadores_validacao
Icar = crop_region(Iorig, label) # 截取车辆区域 Icar 车辆 label 车辆坐标信息 Icar = Icar.astype('uint8') Lcars.append(label) draw_label(Iorig, label, color=YELLOW, thickness=3) # lp detector print('Searching for license plates using WPOD-NET') ratio = float(max(Icar.shape[:2])) / min(Icar.shape[:2]) side = int(ratio * 288.) bound_dim = min(side + (side % (2**4)), 608) print("\t\tBound dim: %d, ratio: %f" % (bound_dim, ratio)) Llp, LlpImgs, elapse = detect_lp(wpod_net, im2single(Icar), bound_dim, 2**4, (240, 80), lp_threshold) print('\tld detection used %d s time' % elapse) if len(LlpImgs): # 检测的车牌图像信息 Ilp = LlpImgs[0] Ilp = cv2.cvtColor(Ilp, cv2.COLOR_BGR2GRAY) Ilp = cv2.cvtColor(Ilp, cv2.COLOR_GRAY2BGR) s = Shape(Llp[0].pts) # 车牌坐标信息 #cv2.imwrite('%s/%s_lp.png' % (output_dir,bname),Ilp*255.) #writeShapes('%s/%s_lp.txt' % (output_dir,bname),[s]) lpts = Llp[0].pts * label.wh().reshape(
def validar_lp(entrada_diretorio_validacao, nome_arquivo_gt, diretorio_saida, wpod_net): print('iniciando validacao') caminho_completo_arquivo = os.path.abspath( os.path.join(entrada_diretorio_validacao, nome_arquivo_gt)) annotation_tree = ET.parse(caminho_completo_arquivo) tag_raiz = annotation_tree.getroot() qtd_imagens_rotuladas = 0 qtd_imagens = 0 # [xmin, ymin, xmax, ymax, class_id, difficult, crowd] dict_bbox_gt = {} for indice_tag_image, image_tag in enumerate(tag_raiz.findall('image')): nome_frame = image_tag.get('name') boxes = image_tag.findall('polygon') lista_bbox_gt_frame = [] for box in boxes: if 'plate' in box.get('label'): # forma de obtencao do bbox qdo o shape e retangulo # bbox_plate = [int(float(box.get('xtl'))), int(float(box.get('ytl'))), int(float(box.get('xbr'))), int(float(box.get('ybr'))), 0, 0, 0] # forma de obtencao do bbox qdo o shape e poligon 4 pontos pontos_str = box.get('points').split(';') pontos = [point_str_to_tuple(p) for p in pontos_str] ponto_top_left, ponto_bottom_right = get_pontos_extremos( pontos) bbox_plate = [ ponto_top_left[0], ponto_top_left[1], ponto_bottom_right[0], ponto_bottom_right[1], 0, 0, 0 ] lista_bbox_gt_frame.append(bbox_plate) print(image_tag.tag, image_tag.attrib, box.get('label')) dict_bbox_gt[nome_frame] = np.array(lista_bbox_gt_frame) lp_threshold = .4 imgs_paths = glob('%s/*.jpg' % entrada_diretorio_validacao) print('Searching for license plates using WPOD-NET') for i, img_path in enumerate(imgs_paths): print('\t Processing %s' % img_path) bname = splitext(basename(img_path))[0] name_file = basename(img_path) Ivehicle = cv2.imread(img_path) height, width = Ivehicle.shape[:2] ratio = float(max(Ivehicle.shape[:2])) / min(Ivehicle.shape[:2]) side = int(ratio * 288.) bound_dim = min(side + (side % (2**4)), 608) print("\t\tBound dim: %d, ratio: %f" % (bound_dim, ratio)) Llp, LlpImgs, _ = detect_lp(wpod_net, im2single(Ivehicle), bound_dim, 2**4, (240, 80), lp_threshold) ground_truth_frame = dict_bbox_gt[name_file] if len(LlpImgs): lista_preds_frame = [] for indice_bbox, Ilp in enumerate(LlpImgs): # Ilp = LlpImgs[0] Ilp = cv2.cvtColor(Ilp, cv2.COLOR_BGR2GRAY) Ilp = cv2.cvtColor(Ilp, cv2.COLOR_GRAY2BGR) license_plate_bouding_box_relativo = Llp[indice_bbox] probability = license_plate_bouding_box_relativo._Label__prob top_left_rl_tp = tuple( license_plate_bouding_box_relativo._Label__tl.tolist()) top_left_x = int(top_left_rl_tp[0] * width) top_left_y = int(top_left_rl_tp[1] * height) bottom_right_rl = license_plate_bouding_box_relativo._Label__br.tolist( ) bottom_right_x = int(bottom_right_rl[0] * width) bottom_right_y = int(bottom_right_rl[1] * height) plate_width = bottom_right_x - top_left_x plate_height = bottom_right_y - top_left_y red_color = (255, 0, 0) bbox_color = red_color # car_rgb_np = cv2.cvtColor(Ivehicle, cv2.COLOR_BGR2RGB) car_rgb_np = Ivehicle thickness = 1 # [xmin, ymin, xmax, ymax, class_id, confidence] if top_left_x > 0 and top_left_y > 0 and bottom_right_x > 0 and bottom_right_y > 0: preds_frame = [ int(top_left_x), int(top_left_y), int(bottom_right_x), int(bottom_right_y), 0, probability ] lista_preds_frame.append(preds_frame) cv2.rectangle(car_rgb_np, (top_left_x, top_left_y), (bottom_right_x, bottom_right_y), bbox_color, thickness) # filled s = Shape(Llp[0].pts) # cv2.imwrite('%s/%s_lp_car.png' % (output_dir,bname), car_rgb_np) # cv2.imwrite('%s/%s_lp.png' % (output_dir,bname),Ilp*255.) writeShapes('%s/%s_lp.txt' % (diretorio_saida, bname), [s]) metric_fn.add(np.array(lista_preds_frame), ground_truth_frame) else: gt_frame = dict_bbox_gt[name_file] # metric_fn.add(np.empty(()), gt_frame) if is_exibir_gt and len(LlpImgs): for bbox_gt in ground_truth_frame: cv2.rectangle(car_rgb_np, (bbox_gt[0], bbox_gt[1]), (bbox_gt[2], bbox_gt[3]), bbox_color_gt, thickness) # filled cv2.imwrite('%s/%s_lp_car.png' % (diretorio_saida, bname), car_rgb_np) print('calculando mAP') # compute PASCAL VOC metric mAP_pascal = metric_fn.value(iou_thresholds=0.5, recall_thresholds=np.arange(0., 1.1, 0.1))['mAP'] print('VOC PASCAL mAP:' + str(mAP_pascal)) # compute PASCAL VOC metric at the all points mAP_pascal_all_points = metric_fn.value(iou_thresholds=0.5)['mAP'] print('VOC PASCAL mAP in all points:' + str(mAP_pascal_all_points)) # compute metric COCO metric mAP_coco = metric_fn.value(iou_thresholds=np.arange(0.5, 1.0, 0.05), recall_thresholds=np.arange(0., 1.01, 0.01), mpolicy='soft')['mAP'] print('COCO mAP: ' + str(mAP_coco)) return mAP_pascal, mAP_pascal_all_points, mAP_coco
# print('\t Processing %s' % img_path) bname = splitext(basename(img_path))[0] # A3 = time.clock() Ivehicle = cv2.imread(img_path) # A3 = time.clock()-A3 # print(A3) ratio = float(max(Ivehicle.shape[:2])) / min( Ivehicle.shape[:2]) #resizing ratio side = int(ratio * 288.) bound_dim = min(side + (side % (2**4)), 608) # print("\t\tBound dim: %d, ratio: %f" % (bound_dim,ratio)) # A1 = time.clock() - A1 # print(A1) with torch.no_grad(): Llp, LlpImgs, runtime = detect_lp(model, im2single(Ivehicle), bound_dim, 2**4, (240, 80), lp_threshold) # print(runtime) # A2 = time.clock() if len(LlpImgs): Ilp = LlpImgs[0] Ilp = cv2.cvtColor(Ilp, cv2.COLOR_BGR2GRAY) Ilp = cv2.cvtColor(Ilp, cv2.COLOR_GRAY2BGR) s = Shape(Llp[0].pts) cv2.imwrite('%s/%s_lp.png' % (output_dir, bname), Ilp * 255.) writeShapes('%s/%s_lp.txt' % (output_dir, bname), [s]) A2 = time.clock() - A1 print(A2)
f2 = open('plate.txt', 'a') wpod_net_path = 'lp-detector/wpod-net.h5' wpod_net = load_model(wpod_net_path) for i in range(146): bname = imgs_paths + str(i) + '.png' print('\t Processing %s' % bname) #bname = splitext(basename(img_path))[0] img = cv2.imread(bname) if min(img.shape[:2]) != 0: ratio = float(max(img.shape[:2])) / min(img.shape[:2]) side = int(ratio * 288.) bound_dim = min(side + (side % (2**4)), 608) Llp, LlpImgs, _ = detect_lp(wpod_net, im2single(img), bound_dim, 2**4, (240, 80), 0.5) if len(LlpImgs): print('%d,%d,%d,%d' % (int(Llp[0].pts[0][0] * img.shape[1]), int(Llp[0].pts[1][0] * img.shape[0]), int(Llp[0].pts[0][2] * img.shape[1]), int(Llp[0].pts[1][2] * img.shape[0])), file=f1) Ilp = LlpImgs[0] cv2.imwrite('./carplate/%s.png' % (i), Ilp * 255.) chars = HyperLPR_plate_recognition(Ilp * 255) if (len(chars) != 0): f2.write(chars[0][0]) # write the car plates else: f2.write('0')
def validar_lp_model(entrada_diretorio_validacao, diretorio_saida, wpod_net): print('iniciando validacao modelo') lp_threshold = .4 imgs_paths = glob('%s/*.jpg' % entrada_diretorio_validacao) print('Searching for license plates using WPOD-NET') for i, img_path in enumerate(imgs_paths): print('\t Processing %s' % img_path) bname_image_file = splitext(basename(img_path))[0] name_file_gt = bname_image_file + '.txt' name_file = basename(img_path) Ivehicle = cv2.imread(img_path) height, width = Ivehicle.shape[:2] ratio = float(max(Ivehicle.shape[:2])) / min(Ivehicle.shape[:2]) side = int(ratio * 288.) bound_dim = min(side + (side % (2**4)), 608) print("\t\tBound dim: %d, ratio: %f" % (bound_dim, ratio)) Llp, LlpImgs, _ = detect_lp(wpod_net, im2single(Ivehicle), bound_dim, 2**4, (240, 80), lp_threshold) if len(LlpImgs): lista_preds_frame = [] for indice_bbox, Ilp in enumerate(LlpImgs): # Ilp = LlpImgs[0] Ilp = cv2.cvtColor(Ilp, cv2.COLOR_BGR2GRAY) Ilp = cv2.cvtColor(Ilp, cv2.COLOR_GRAY2BGR) license_plate_bouding_box_relativo = Llp[indice_bbox] probability = license_plate_bouding_box_relativo._Label__prob top_left_rl_tp = tuple( license_plate_bouding_box_relativo._Label__tl.tolist()) top_left_x = int(top_left_rl_tp[0] * width) top_left_y = int(top_left_rl_tp[1] * height) bottom_right_rl = license_plate_bouding_box_relativo._Label__br.tolist( ) bottom_right_x = int(bottom_right_rl[0] * width) bottom_right_y = int(bottom_right_rl[1] * height) plate_width = bottom_right_x - top_left_x plate_height = bottom_right_y - top_left_y red_color = (255, 0, 0) bbox_color = red_color # car_rgb_np = cv2.cvtColor(Ivehicle, cv2.COLOR_BGR2RGB) car_rgb_np = Ivehicle thickness = 1 # [xmin, ymin, xmax, ymax, class_id, confidence] if top_left_x > 0 and top_left_y > 0 and bottom_right_x > 0 and bottom_right_y > 0: preds_frame = [ int(top_left_x), int(top_left_y), int(bottom_right_x), int(bottom_right_y), 0, probability ] lista_preds_frame.append(preds_frame) cv2.rectangle(car_rgb_np, (top_left_x, top_left_y), (bottom_right_x, bottom_right_y), bbox_color, thickness) # filled s = Shape(Llp[0].pts) # cv2.imwrite('%s/%s_lp_car.png' % (output_dir,bname), car_rgb_np) # cv2.imwrite('%s/%s_lp.png' % (output_dir,bname),Ilp*255.) writeShapes( '%s/%s_lp.txt' % (diretorio_saida, bname_image_file), [s]) gt_img_path = img_path.replace('.jpg', '.txt') ground_truth_frame = [] with open(gt_img_path) as f: lines = f.readlines() for linha in lines: pontos = linha.split(',')[1:9] ground_truth = [ int(float(pontos[0]) * width), int(float(pontos[4]) * height), int(float(pontos[2]) * width), int(float(pontos[6]) * height), 0, 0, 0 ] ground_truth_frame.append(ground_truth) metric_fn.add(np.array(lista_preds_frame), np.array(ground_truth_frame)) if is_exibir_gt and len(LlpImgs): for bbox_gt in ground_truth_frame: cv2.rectangle(car_rgb_np, (bbox_gt[0], bbox_gt[1]), (bbox_gt[2], bbox_gt[3]), bbox_color_gt, thickness) # filled cv2.imwrite( '%s/%s_lp_car.png' % (diretorio_saida, bname_image_file), car_rgb_np) print('calculando mAP') # compute PASCAL VOC metric mAP_pascal = metric_fn.value(iou_thresholds=0.5, recall_thresholds=np.arange(0., 1.1, 0.1))['mAP'] print('VOC PASCAL mAP:' + str(mAP_pascal)) # compute PASCAL VOC metric at the all points mAP_pascal_all_points = metric_fn.value(iou_thresholds=0.5)['mAP'] print('VOC PASCAL mAP in all points:' + str(mAP_pascal_all_points)) # compute metric COCO metric mAP_coco = metric_fn.value(iou_thresholds=np.arange(0.5, 1.0, 0.05), recall_thresholds=np.arange(0., 1.01, 0.01), mpolicy='soft')['mAP'] print('COCO mAP: ' + str(mAP_coco)) return mAP_pascal, mAP_pascal_all_points, mAP_coco
def detect_lp_nums(self, img): if self.wpod_net is None: return [], -1 h, w, c = img.shape ratio = float(max(img.shape[:2]))/min(img.shape[:2]) side = int(ratio*288.) bound_dim = min(side + (side%(2**4)), 608) lp_locs, lp_imgs, _ = detect_lp(self.wpod_net, im2single(img), bound_dim, 2**4, (240, 80), self.det_thresh) if len(lp_locs) == 0: if self.roi_det_enabled and self.prev_locs != None: rect, ang = self.utils.getLPCoordinates(self.prev_locs.pts, w, h) if self.utils.isLPGood(rect) and h-rect[1] > 80: w1 = int(rect[2] * 2.0); h1 = int(rect[3] * 4.0); x1 = int(rect[0] + (rect[2]/2.0) - (w1/2.0)) y1 = int(rect[1] + (rect[3]/2.0) - (h1/2.0)) if x1 + w1 > w: w1 = w - x1 if y1 + h1 > h: h1 = h - y1 if x1 >= 0 and y1 >=0 and w1 > 0 and h1 > 0: temp_img = img[y1:y1+h1, x1:x1+w1] if temp_img.shape[0] > 0 and temp_img.shape[1] > 0: ratio = float(max(temp_img.shape[:2]))/min(temp_img.shape[:2]) side = int(ratio*288.) bound_dim = min(side + (side%(2**4)), 608) lp_locs, lp_imgs, _ = detect_lp(self.wpod_net, im2single(temp_img), bound_dim, 2**4, (240, 80), self.det_thresh) if len(lp_locs) > 0: self.prev_locs = lp_locs[0] #print "Possible Detection: ", len(lp_locs) for lp in lp_locs: #r, _ = self.utils.getLPCoordinates(lp.pts, w1, h1) #if self.utils.isLPGood(r): # cv2.rectangle(temp_img, (r[0], r[1]), (r[0]+r[2], r[1]+r[3]), (0, 0, 255), 2) for l in range(len(lp.pts[0])): lp.pts[0][l] = (x1 + (lp.pts[0][l]*w1))/w lp.pts[1][l] = (y1 + (lp.pts[1][l]*h1))/h break else: self.prev_locs = None #cv2.imshow("roi", temp_img) else: self.prev_locs = None else: self.prev_locs = lp_locs[0] detected_lps = [] lp_status = 0 for i in range(len(lp_imgs)): lp_str = "" rect, ang = self.utils.getLPCoordinates(lp_locs[i].pts, w, h) if self.utils.isLPGood(rect) == False: continue if self.ocr_net is not None: #im_p = lp_imgs[i] im = img[rect[1]:rect[1]+rect[3], rect[0]:rect[0]+rect[2]] im = cv2.cvtColor(im, cv2.COLOR_BGR2GRAY) im_c = tuple(np.array(im.shape[1::-1]) / 2) rmat = cv2.getRotationMatrix2D(im_c, ang, 1) im_r = cv2.warpAffine(im, rmat, im.shape[1::-1], flags=cv2.INTER_LINEAR) tmp_img_path = "./.temp.png" cv2.imwrite(tmp_img_path, im_r) res = detect(self.ocr_net, self.ocr_meta, bytes(tmp_img_path, encoding='utf-8'), thresh=self.rec_thresh) if len(res) > 4 and self.utils.isLPGood(rect): lp_str = self.utils.getLPSorted(res) if self.cons_lp_enabled: lp_str, lp_status = self.consolidator.consolidate_lp([lp_str, res]) detected_lps.append((lp_str, rect)) return detected_lps, lp_status
for img_path in file: print('\t Processing %s' % img_path) img_path = os.path.join(imgs_paths, img_path) bname = splitext(basename(img_path))[0] Ivehicle = cv2.imread(img_path) im = cv2.resize(Ivehicle, (900, 500)) cv2.imshow("asd", im) #cv2.waitKey(0) ratio = float(max(Ivehicle.shape[:2])) / min( Ivehicle.shape[:2]) side = int(ratio * 288.) bound_dim = min(side + (side % (2**4)), 608) print("\t\tBound dim: %d, ratio: %f" % (bound_dim, ratio)) Llp, LlpImgs, _ = detect_lp(wpod_net, im2single(Ivehicle), bound_dim, 2**4, (240, 80), lp_threshold) print(len(LlpImgs)) if (len(LlpImgs)): for i in LlpImgs: cv2.imshow("12", i) cv2.waitKey(0) # if len(LlpImgs): # Ilp = LlpImgs[0] # Ilp = cv2.cvtColor(Ilp, cv2.COLOR_BGR2GRAY) # Ilp = cv2.cvtColor(Ilp, cv2.COLOR_GRAY2BGR) # # s = Shape(Llp[0].pts) # # cv2.imwrite('%s/%s_lp.png' % (output_dir,bname),Ilp*255.)
def pipeline(img): ''' Pipeline function for detection and tracking ''' global frame_count global tracker_list global max_age global min_hits global track_id_list global debug global avg_fps frame_count += 1 #print("") #print(frame_count) #print("") start = time.time() img_dim = (img.shape[1], img.shape[0]) # YOLO detection for vehicle yolo_start = time.time() z_box = yolo_det.get_detected_boxes(img) #z_box_cpy= z_box yolo_end = time.time() # Lpd #print("Time taken for yolo detection is", yolo_end-yolo_start) track_start = time.time() if debug: print('Frame:', frame_count) x_box = [] if debug: for i in range(len(z_box)): img1 = helpers.draw_box_label(img, z_box[i], box_color=(255, 0, 0)) cv2.imshow("frame", img1) k = cv2.waitKey(10) if k == ord('e'): cv2.destroyAllWindows() sys.exit(-1) # plt.show() if len(tracker_list) > 0: for trk in tracker_list: x_box.append(trk.box) matched, unmatched_dets, unmatched_trks \ = assign_detections_to_trackers(x_box, z_box, iou_thrd=0.3) if debug: print('Detection: ', z_box) print('x_box: ', x_box) print('matched:', matched) print('unmatched_det:', unmatched_dets) print('unmatched_trks:', unmatched_trks) # Deal with matched detections if matched.size > 0: for trk_idx, det_idx in matched: z = z_box[det_idx] tmp_trk = tracker_list[trk_idx] tmp_trk.features.append(extract_feature(img, z)) z = np.expand_dims(z, axis=0).T tmp_trk.kalman_filter(z) xx = tmp_trk.x_state.T[0].tolist() xx = [xx[0], xx[2], xx[4], xx[6]] x_box[trk_idx] = xx tmp_trk.box = xx tmp_trk.hits += 1 tmp_trk.no_losses = 0 # Deal with unmatched detections if len(unmatched_dets) > 0: for idx in unmatched_dets: z = z_box[idx] if len(unmatched_trks) > 0: min_score = 10000000 tmp_idx = -1 for trk_idx in unmatched_trks: trk = tracker_list[trk_idx] #print(len(trk.features)) if len(trk.features) == 0: continue score = trk.feature_match(extract_feature( img, z)) ## find closest feature match if score < min_score: min_score = score tmp_idx = trk_idx if min_score < feature_thresh and tmp_idx != -1: z = np.expand_dims(z, axis=0).T tmp_trk = tracker_list[tmp_idx] tmp_trk.kalman_filter(z) xx = tmp_trk.x_state.T[0].tolist() xx = [xx[0], xx[2], xx[4], xx[6]] x_box[trk_idx] = xx tmp_trk.box = xx tmp_trk.hits += 1 tmp_trk.no_losses = 0 continue #new_boxes.append(z) z = np.expand_dims(z, axis=0).T tmp_trk = tr.Tracker() # Create a new tracker x = np.array([[z[0], 0, z[1], 0, z[2], 0, z[3], 0]]).T tmp_trk.x_state = x tmp_trk.predict_only() xx = tmp_trk.x_state xx = xx.T[0].tolist() xx = [xx[0], xx[2], xx[4], xx[6]] tmp_trk.box = xx tmp_trk.id = track_id_list.popleft( ) # assign an ID for the tracker tracker_list.append(tmp_trk) x_box.append(xx) # Deal with unmatched tracks*100 if len(unmatched_trks) > 0: for trk_idx in unmatched_trks: tmp_trk = tracker_list[trk_idx] tmp_trk.no_losses += 1 tmp_trk.predict_only() xx = tmp_trk.x_state xx = xx.T[0].tolist() xx = [xx[0], xx[2], xx[4], xx[6]] tmp_trk.box = xx x_box[trk_idx] = xx # The list of tracks to be annotated img_vis = img.copy() good_tracker_list = [] #print(img_dim) good_boxes = [] for trk in tracker_list: if ((trk.hits >= min_hits) and (trk.no_losses <= max_age)): good_tracker_list.append(trk) good_boxes.append(trk.box) #for trk in good_tracker_list: selected_ids = nms(torch.FloatTensor(np.array(good_boxes)), torch.FloatTensor([1.0] * len(good_boxes)), 0.45) for idx in selected_ids: trk = good_tracker_list[idx] x_cv2 = trk.box idx = trk.id if debug: print('updated box: ', x_cv2) print() # Draw the bounding boxes on the img_vis = helpers.draw_box_label(img_vis, x_cv2, idx) if frame_count % 5 == 0: y1_temp, x1_temp, y2_temp, x2_temp = x_cv2 w_temp = x2_temp - x1_temp h_temp = y2_temp - y1_temp if w_temp * h_temp < 400 or w_temp <= 0 or h_temp <= 0 or min( x_cv2) < 0: continue plates = [] #print(x_cv2) dt_start = time.time() Ivehicle = img[y1_temp:y2_temp, x1_temp:x2_temp] ratio = float(max(Ivehicle.shape[:2])) / min(Ivehicle.shape[:2]) side = int(ratio * 288.) bname = 'frame{}_{}.png'.format(frame_count, idx) bound_dim = min(side + (side % (2**4)), size) # print "\t\tBound dim: %d, ratio: %f" % (bound_dim,ratio) #dt_plates_start = time.time() Llp, LlpImgs, _ = detect_lp(wpod_net, im2single(Ivehicle), bound_dim, 2**4, (240, 80), lp_threshold) if len(LlpImgs): plates = [Llp[0].pts] cv2.imwrite("%s/%s" % (detected_plates_dir, bname), LlpImgs[0] * 255.) plate_string = _lpr.plates_ocr(LlpImgs[0] * 255.) for plate in plates: x1 = (plate[0][0] * w_temp + x1_temp).astype('int') y1 = (plate[1][0] * h_temp + y1_temp).astype('int') x2 = (plate[0][1] * w_temp + x1_temp).astype('int') y2 = (plate[1][1] * h_temp + y1_temp).astype('int') x3 = (plate[0][2] * w_temp + x1_temp).astype('int') y3 = (plate[1][2] * h_temp + y1_temp).astype('int') x4 = (plate[0][3] * w_temp + x1_temp).astype('int') y4 = (plate[1][3] * h_temp + y1_temp).astype('int') plate = np.array([[x1, y1], [x2, y2], [x3, y3], [x4, y4]], np.int32) plate = plate.reshape((-1, 1, 2)) cv2.polylines(img_vis, [plate], True, (255, 0, 0), 4) cv2.putText(img_vis, plate_string, (x1, y1), cv2.FONT_HERSHEY_SIMPLEX, 1.1, (0, 0, 255), 2) cv2.imwrite("%s/%s" % (detected_cars_dir, bname), img_vis[y1_temp:y2_temp, x1_temp:x2_temp]) track_end = time.time() # images # dt_start = time.time() print("Time taken to track the boxes is", track_end - track_start) end = time.time() fps = 1.0 / (end - start) #dt_fps = 1.0/(dt_dr+yolo_end-yolo_start) avg_fps += fps cv2.putText(img_vis, "FPS: {:.4f}".format(fps), (int(0.8 * img_dim[0]), 100), cv2.FONT_HERSHEY_SIMPLEX, 1.1, (255, 255, 0), 4) #cv2.putText(img_vis, "Detect FPS: {:.4f}".format( # dt_fps), (10, 100), cv2.FONT_HERSHEY_SIMPLEX, 1.1, (255,255, 0), 4) # Book keeping deleted_tracks = filter(lambda x: x.no_losses > feature_tp, tracker_list) for trk in deleted_tracks: track_id_list.append(trk.id) tracker_list = [x for x in tracker_list if x.no_losses <= feature_tp] if debug: print('Ending tracker_list: ', len(tracker_list)) print('Ending good tracker_list: ', len(good_tracker_list)) return img_vis