Ejemplo n.º 1
0
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])
Ejemplo n.º 2
0
    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
Ejemplo n.º 3
0
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 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"
Ejemplo n.º 5
0
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
Ejemplo n.º 6
0
	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
Ejemplo n.º 7
0
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
Ejemplo n.º 8
0
            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()
        sys.exit(1)
    sleep(10)
    sys.exit(0)
Ejemplo n.º 9
0
                    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(
                            2, 1) + label.tl().reshape(2, 1)
                        lptspx = lpts * np.array(Iorig.shape[1::-1],
                                                 dtype=float).reshape(2, 1)
                        draw_losangle(Iorig, lptspx, RED, 3)

                        Ilp = Ilp * 255.
                        Ilp = Ilp.astype('uint8')  # 十分重要,不做这个转换,就无法识别
                        '''
						cv2.imshow("img", Ilp)
						cv2.waitKey(3000)
						cv2.imwrite('%s/%s_new_lp.png' % (output_dir,bname),Ilp)	
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 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
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
Ejemplo n.º 13
0
            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)
            for i, lpt in enumerate(LlpImgs):
                #cv2.imwrite('a%s/%s_lp.png' % (output_dir,bname),lpt)
                if len(LlpImgs):
                    Ilp = lpt
                    Ilp = cv2.cvtColor(Ilp, cv2.COLOR_BGR2GRAY)
                    Ilp = cv2.cvtColor(Ilp, cv2.COLOR_GRAY2BGR)

                    s = Shape(Llp[i].pts)

                    cv2.imwrite('%s/%s%d_lp.png' % (output_dir, bname, i),
                                Ilp * 255.)
                    writeShapes('%s/%s%d_lp.txt' % ('points', bname, i), [s])

    except:
        traceback.print_exc()
        sys.exit(1)

    sys.exit(0)