Exemple #1
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 ""
Exemple #2
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])
Exemple #3
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
                    shapes[selected].changeClosest(
                        disp.getMouseCenterRelative())

                if key in key_delete_selected_shape:
                    print 'Delete closest vertex'
                    del shapes[selected]
                    pt = disp.getMouseCenterRelative()
                    selected = selectClosest(shapes, pt)

                if key == key_shift_pts:
                    shapes[selected].shiftPts()

            if key == key_create_new_shape:
                print 'Create new shape'
                shapes.append(ShapeDisplay())
                selected = len(shapes) - 1

            if key == key_select_closest_shape:
                print 'Select closest'
                pt = disp.getMouseCenterRelative()
                selected = selectClosest(shapes, pt)

        if key == key_exit:
            sys.exit()

        if key in ([key_previous] + key_next):
            writeShapes(lab_file, shapes)
            curr_image += 1 if key in key_next else -1
            curr_image = max(curr_image, 0)
            continue
Exemple #5
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)
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
Exemple #8
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)