Exemplo n.º 1
0
def pick_range_of_angle(img_folder, output_folder, hor_degree_threshold,
                        ver_degree_threshold):
    for img_path in read_img_from_dir(img_folder):
        vertices = CCPD_vertices_info(img_path)
        hor_angle, ver_angle = rotation_degrees(*vertices)

        if abs(hor_angle) > hor_degree_threshold or abs(
                ver_angle - 90) > ver_degree_threshold:
            copy(img_path, output_folder)
Exemplo n.º 2
0
    def renew_img_paths(self):
        if self.mixing_train:
            '''for randomization
            CCPD_FR = read_img_from_dir('/home/shaoheng/Documents/Thesis_KSH/training_data/CCPD_FR')
            openALPR_br = read_img_from_dir('/home/shaoheng/Documents/Thesis_KSH/training_data/openALPR_br')
            openALPR_us = read_img_from_dir('/home/shaoheng/Documents/Thesis_KSH/training_data/openALPR_us')
            openALPR_eu = read_img_from_dir('/home/shaoheng/Documents/Thesis_KSH/training_data/openALPR_eu')
            CCPD_FR = sample(CCPD_FR, 200)
            '''
            vernex_origin = read_img_from_dir(
                '/home/shaoheng/Documents/Thesis_KSH/training_data/vernex')
            kr_tilt_vernex = read_img_from_dir(
                '/home/shaoheng/Documents/Thesis_KSH/training_data/kr_tilt_vernex'
            )
            # vernex_origin = sample(vernex_origin, 900)

            self.imgs_paths = vernex_origin + kr_tilt_vernex

        elif not self.mixing_train:
            self.imgs_paths = read_img_from_dir(self.img_folder)
Exemplo n.º 3
0
def mean_size_LP(img_folder, training_dim, total_stride=1):
    imgs_path = read_img_from_dir(img_folder)
    imgs_amount = len(imgs_path)
    W, H = 0., 0.
    for img_path in imgs_path:
        img_size = cv2.imread(img_path).shape  # cv2.imread.shape -> (h, w, ch)
        vertices = CCPD_FR_vertices_info(img_path)
        BBCor = pts_to_BBCor(*vertices)
        BBCor = pixel_to_ratio(img_size, *BBCor)
        w_ratio, h_ratio = BBCor[1][0] - BBCor[0][0], BBCor[1][1] - BBCor[0][1]
        W += w_ratio * training_dim
        H += h_ratio * training_dim
    return (W + H) / 2 / imgs_amount / total_stride
Exemplo n.º 4
0
def vernex_from_datasets():
    in_dir = '/home/shaoheng/Documents/cars_label_FRNet/openALPR_refined/FR/us'
    out_dir = '/home/shaoheng/Documents/Thesis_KSH/training_data/vernex'
    imgs_paths = read_img_from_dir(in_dir)

    for img_path in imgs_paths:
        txt_path = splitext(img_path)[0] + '.txt'
        if not isfile(txt_path):
            continue

        print('processing', img_path)

        # acquiring fr bbcor part
        '''For CCPD, openALPR FR'''
        img = cv2.imread(img_path)
        fr_bbcor = yolo_to_BBCor(img.shape, *yolo_readline(txt_path))
        fr_vertices = BBCor_to_pts(*fr_bbcor)

        # acquiring lp vertices part, different for each dataset
        '''For CCPD'''
        lp_vertices = CCPD_vertices_info(img_path)
        '''For openALPR FR
        lp_vertices = CCPD_FR_vertices_info(img_path)'''

        # acquiring class, front=0, rear=1
        '''For CCPD, openALPR FR'''
        fr_class = yolo_class(txt_path)
        if fr_class == 0:
            class_name = 'front'
        elif fr_class == 1:
            class_name = 'rear'

        # write to new name
        file_name = ''
        # make the file name format similar to CCPD files
        for i, vertex in enumerate(lp_vertices + fr_vertices):
            file_name += str(vertex[0]) + '&' + str(vertex[1])
            if i == 7:
                file_name = file_name + '_' + class_name + '.jpg'
            else:
                file_name += '_'

        print('write to', join(out_dir, file_name))
        cv2.imwrite(join(out_dir, file_name), img)
Exemplo n.º 5
0
def vernex_from_json():
    in_dir = '/home/shaoheng/Documents/Thesis_KSH/samples/kr_tilt'
    out_dir = '/home/shaoheng/Documents/Thesis_KSH/training_data/kr_tilt_vernex'
    imgs_paths = read_img_from_dir(in_dir)

    for img_path in imgs_paths:
        json_path = splitext(img_path)[0] + '.json'
        if not isfile(json_path):
            continue

        print('processing', img_path)

        try:
            w, h, cls, lp_fr_vertices = json_lp_fr(json_path)
        except AssertionError:
            print(traceback.print_exc())
            continue

        img = cv2.imread(img_path)
        # acquiring fr bbcor part
        fr_vertices = lp_fr_vertices[cls]

        # acquiring lp vertices part
        lp_vertices = lp_fr_vertices['lp']

        # acquiring class, front=0, rear=1
        '''For CCPD, openALPR FR'''
        class_name = cls

        # write to new name
        file_name = ''
        # make the file name format similar to CCPD files
        for i, vertex in enumerate(lp_vertices + fr_vertices):
            file_name += str(vertex[0]) + '&' + str(vertex[1])
            if i == 7:
                file_name = file_name + '_' + class_name + '.jpg'
            else:
                file_name += '_'

        print('write to', join(out_dir, file_name))
        cv2.imwrite(join(out_dir, file_name), img)
Exemplo n.º 6
0
    hsvs = np.array([(hsv_from_GIMP_to_cv2(*hsv))for hsv in hsvs])
    hsvs_min = np.array([min(hsvs[:, 0]), min(hsvs[:, 1]), min(hsvs[:, 2])])
    hsvs_max = np.array([max(hsvs[:, 0]), max(hsvs[:, 1]), max(hsvs[:, 2])])
    return hsvs_min, hsvs_max


# segment the img (in cv2.imread format, color space BGR) by bounding of hsv color space
# hsv_lower and hsv_upper -> np.array([h, s, v])
def hsv_segmentation(img, hsv_lower, hsv_upper):
    img = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
    mask = cv2.inRange(img, hsv_lower, hsv_upper)
    img_after_seg = cv2.bitwise_and(img, img, mask=mask)
    return img_after_seg


if __name__ == '__main__':
    path = '/home/shaoheng/Documents/cars_label_FRNet/ccpd_dataset/ccpd_base'
    imgs_paths = read_img_from_dir(path)

    hsv_lower, hsv_upper = eyeballing_hsv()

    for img_path in imgs_paths:
        img = cv2.imread(img_path)
        LP_part = CCPD_vertices_info(img_path)
        img_LP = cut_by_fourpts(img, *LP_part)
        img_after_seg = hsv_segmentation(img, hsv_lower, hsv_upper)

        cv2.imshow('img', img_after_seg)
        cv2.moveWindow('img', 0, 0)
        cv2.waitKey(0)
        cv2.destroyAllWindows()
Exemplo n.º 7
0
def coco_mAP_vernex(output_result_folder, iou_threshold, classify_cal=True):
    data_val_folder = output_result_folder
    imgs_paths_val = read_img_from_dir(data_val_folder)
    total_gt = len(imgs_paths_val)
    class_dict = {'front': 1, 'rear': 2}
    lp_results = []  # -> format [prob, True or False], True for true positive, False for false positive
    fr_results = []  # -> format [True or False, IoU], True for true class, False for false class, IoU for quadrilateral IoU
    res_pres = []

    # read all the prediction result and sort them
    for img_path in imgs_paths_val:
        vertices_lp_gt = vernex_vertices_info(img_path)
        vertices_fr_gt = vernex_front_rear_info(img_path)
        fr_class = vernex_fr_class_info(img_path)

        f = open(splitext(img_path)[0] + '_result.json', 'r')
        data = json.load(f)

        '''for vernex'''
        for lp in data['lps']:

            true_or_false = False  # true positive or false positive
            try:
                # if polygons_iou(lp['vertices_lp'], vertices_lp_gt) >= iou_threshold:
                if IoU(pts_to_BBCor(*lp['vertices_lp']), pts_to_BBCor(*vertices_lp_gt)) >= iou_threshold:
                    true_or_false = True
            except:
                # traceback.print_exc()
                if IoU(pts_to_BBCor(*lp['vertices_lp']), pts_to_BBCor(*vertices_lp_gt)) >= iou_threshold:
                    true_or_false = True

            if true_or_false and classify_cal:
                classify = False
                if class_dict[fr_class] == lp['fr_class']:
                    classify = True
                try:
                    fr_results.append([classify, polygons_iou(lp['vertices_fr'], vertices_fr_gt)])
                except:
                    # traceback.print_exc()
                    fr_results.append([classify, 0])

            lp_results.append([lp['lp_prob'], true_or_false])

        '''for openALPR
        for lp in data['results']:

            true_or_false = False  # true positive or false positive
            lp_vertices = [[s['x'], s['y']] for s in lp['coordinates']]
            try:
                # if polygons_iou(lp_vertices, vertices_lp_gt) >= iou_threshold:
                if IoU(pts_to_BBCor(*lp_vertices), pts_to_BBCor(*vertices_lp_gt)) >= iou_threshold:
                    true_or_false = True
            except:
                # traceback.print_exc()
                if IoU(pts_to_BBCor(*lp_vertices), pts_to_BBCor(*vertices_lp_gt)) >= iou_threshold:
                    true_or_false = True

            lp_results.append([lp['confidence'], true_or_false])'''


        '''for Sighthound
        for lp in data['objects']:

            annotation = lp['licenseplateAnnotation']

            true_or_false = False  # true positive or false positive
            lp_vertices = [[s['x'], s['y']] for s in annotation['bounding']['vertices']]
            try:
                if polygons_iou(lp_vertices, vertices_lp_gt) >= iou_threshold:
                # if IoU(pts_to_BBCor(*lp_vertices), pts_to_BBCor(*vertices_lp_gt)) >= iou_threshold:
                    true_or_false = True
            except:
                # traceback.print_exc()
                if IoU(pts_to_BBCor(*lp_vertices), pts_to_BBCor(*vertices_lp_gt)) >= iou_threshold:
                    true_or_false = True

            lp_results.append([annotation['attributes']['system']['string']['confidence'], true_or_false])'''

        f.close()

    lp_results.sort(key=lambda s: s[0], reverse=True)

    # calculate the recall and precision and save into the list recalls_precisions
    true_positive = 0.
    detections = 0.
    for lp_result in lp_results:
        detections += 1
        if lp_result[1]:
            true_positive += 1
        recall = true_positive / total_gt
        precision = true_positive / detections
        res_pres.append([recall, precision])

    # assign the bigger precision value to eliminate the zig-zag
    for i, recall_precision in enumerate(res_pres):
        recall_precision[1] = max([x[1] for x in res_pres[i:]])

    # remove the duplications and make a finer list for recall_precision
    pres_dupfree = []
    res_met = []
    for recall_precision in res_pres:
        if recall_precision[0] not in res_met:
            pres_dupfree.append(recall_precision)
            res_met.append(recall_precision[0])
    res_pres_dupfree = []
    pres_met = []
    for recall_precision in pres_dupfree[-1::-1]:
        if recall_precision[1] not in pres_met:
            res_pres_dupfree.append(recall_precision)
            pres_met.append(recall_precision[1])

    res_pres_dupfree = res_pres_dupfree[-1::-1]

    # interpolation of 101 points as the evaluation method used in COCO
    i = 0
    precision = 0
    for points in range(101):
        recall = points / 100.

        while res_pres_dupfree[i][0] < recall:
            i += 1
            if i == len(res_pres_dupfree):
                break
        if i == len(res_pres_dupfree):
            break
        precision += res_pres_dupfree[i][1]
    precision = precision / 101.

    # to calculate the classification precision
    class_precision, average_iou = 0., 0.
    if classify_cal and fr_results:
        true_classification = 0.
        total_iou = 0.
        for fr_result in fr_results:
            if fr_result[0]:
                true_classification += 1
                total_iou += fr_result[1]
        class_precision = true_classification / len(fr_results)
        try:
            average_iou = total_iou / true_classification
        except ZeroDivisionError:
            pass

    '''
    plt.plot([x[0] for x in res_pres_dupfree], [y[1] for y in res_pres_dupfree])
    plt.plot([x[0] for x in res_pres], [y[1] for y in res_pres])
    plt.show()
    '''

    return precision, class_precision, average_iou
Exemplo n.º 8
0
def test_on_benchmark(model, weight_name, weight_folder, load_weight=True):

    c = Configs()

    if splitext(weight_name)[1] not in ['.h5']:
        print(weight_name, 'skipped')
        return 0
    if isfile(join(c.info_saving_folder, splitext(weight_name)[0] + '.txt')):
        print(splitext(weight_name)[0] + '.txt', 'existed already, skipped')
        return 0

    if load_weight:
        print('loading weight:', weight_name)
        model.load_weights(join(weight_folder, weight_name))

    if not isdir(c.temp_outout_folder):
        mkdir(c.temp_outout_folder)

    imgs_paths = read_img_from_dir(c.valid_data_folder)
    time_spent = 0

    print('processing benchmark image results ...')
    for img_path in imgs_paths:

        final_labels, sec = single_img_predict(model=model,
                                               img_path=img_path,
                                               input_norm=c.val_input_norm,
                                               model_code=c.model_code)

        time_spent += sec

        img = cv2.imread(img_path)

        infos = {'lps': []}

        for i, final_label in enumerate(final_labels[:c.val_LPs_to_find]):

            prob, vertices_lp = final_label[:2]
            vertices_lp = vertices_lp.tolist()
            vertices_lp = vertices_rearange(vertices_lp)

            # save each license plate
            '''
            lp_img = planar_rectification(img, vertices_lp)
            cv2.imwrite(join(c.temp_outout_folder, splitext(basename(img_path))[0] + '_%d' % i + '.jpg'), lp_img)
            '''

            # draw visualization results
            img = draw_LP_by_vertices(img, vertices_lp)
            # if it's lpfr model, then draw front and rear
            if c.model_code in ['Hourglass+Vernex_lpfr', 'WPOD+vernex_lpfr']:
                vertices_fr = final_label[2].tolist()
                fr_class, class_prob = final_label[3]
                img = draw_FR_color_by_class(img, prob, vertices_fr, fr_class,
                                             class_prob)

                # add output results in order to save into json file
                infos['lps'].append({
                    'lp_prob': float(prob),
                    'vertices_lp': vertices_lp,
                    'vertices_fr': vertices_fr,
                    'fr_class': fr_class,
                    'class_prob': float(class_prob)
                })
        '''
        save result for mAP calculation'''
        json_path = join(c.temp_outout_folder,
                         splitext(basename(img_path))[0] + '_result.json')
        if isfile(json_path):
            remove(json_path)
        with open(json_path, 'a+') as f:
            json.dump(infos, f, indent=2)

        cv2.imwrite(join(c.temp_outout_folder, basename(img_path)), img)

    print('processing, %d images, spend:%.3f seconds' %
          (len(imgs_paths), time_spent))

    # coco mAP
    mAP = 0.
    for threshold in range(50, 100, 5):
        mAP += coco_mAP_vernex(c.temp_outout_folder,
                               threshold / 100.,
                               classify_cal=False)[0]
    mAP = mAP / 10

    # coco mAP50
    coco_map_50, class_accuracy, iou_front_rear = coco_mAP_vernex(
        c.temp_outout_folder, 0.5, classify_cal=True)
    # coco mAP75
    coco_map_75 = coco_mAP_vernex(c.temp_outout_folder,
                                  0.75,
                                  classify_cal=False)[0]

    print('\tCOCO mAP:', '%.1f' % (mAP * 100))
    print('\tCOCO mAP50:', '%.1f' % (coco_map_50 * 100))
    print('\tCOCO mAP75:', '%.1f' % (coco_map_75 * 100))
    print('\tclassification accuracy:', '%.1f' % (class_accuracy * 100))
    print('\taverage iou for front-rear', '%.1f' % (iou_front_rear * 100))

    with open(join(c.info_saving_folder,
                   splitext(weight_name)[0] + '.txt'), 'w+') as f:
        f.writelines([
            'COCO mAP:',
            '%.1f\n' % (mAP * 100), 'COCO mAP50:',
            '%.1f\n' % (coco_map_50 * 100), 'COCO mAP75:',
            '%.1f\n' % (coco_map_75 * 100), 'classification accuracy:',
            '%.1f\n' % (class_accuracy * 100), 'average iou for front-rear:',
            '%.1f\n' % (iou_front_rear * 100)
        ])
Exemplo n.º 9
0
    if c.use_nms:
        final_labels = nms(final_labels)

    return final_labels, time_spent


if __name__ == '__main__':

    c = Configs()

    model = model_and_loss(training=False)

    if not isdir(c.output_dir):
        mkdir(c.output_dir)
    imgs_paths = read_img_from_dir(c.input_dir)
    time_spent = 0
    for img_path in imgs_paths:

        print('processing', img_path)
        final_labels, sec = single_img_predict(img_path=img_path,
                                               input_norm=c.input_norm,
                                               model_code=c.model_code)

        time_spent += sec
        if len(final_labels) == 0:
            print('fail to detect')
        else:
            print('%d LPs found' % len(final_labels))

        img = cv2.imread(img_path)
Exemplo n.º 10
0
import seaborn
import numpy as np
import matplotlib.pyplot as plt
import cv2

from config import Configs
from model_define import model_and_loss
from img_utility import read_img_from_dir

c = Configs()
model = model_and_loss(training=False)

sample_dir = '/home/shaoheng/Documents/Thesis_KSH/benchmark/cd_hard_vernex'

for img_path in read_img_from_dir(sample_dir):
    img = cv2.imread(img_path)

    plt.subplot(2, 2, 1).set_xlabel('(a)')
    img_to_show = cv2.cvtColor(cv2.resize(img, c.multi_scales[0]),
                               cv2.COLOR_BGR2RGB)
    img_to_show = np.pad(img_to_show, ((0, 0), (0, 50), (0, 0)),
                         'constant',
                         constant_values=255)
    plt.imshow(img_to_show)
    plt.axis('off')

    if c.input_norm:
        div = 255.
    else:
        div = 1.
Exemplo n.º 11
0
just need to give the input image directory and output directory
"""
from os.path import basename, join, isfile

import cv2
import numpy as np

from dataset_utility import CCPD_vertices_info, openALPR_BBCor_info, CCPD_FR_vertices_info
from yolo_utility import yolo_readline, yolo_to_BBCor
from img_utility import cut_by_BBCor, read_img_from_dir, cor_sys_trans, BBCor_to_pts

if __name__ == '__main__':
    output_dir = '/home/shaoheng/Documents/Thesis_KSH/training_data/CCPD_FR_tilt'
    input_dir = '/home/shaoheng/Documents/cars_label_FRNet/CCPD_2019_first_part/rotandtilt_2000'

    img_paths = read_img_from_dir(input_dir)
    print('reading all image file paths from input dir done')

    for img_path in img_paths:
        txt_path = img_path.split('.')[0] + '.txt'
        # skip when there is no annotation txt file (in here, txt file includes front-rear in YOLO format)
        if not isfile(txt_path):
            continue

        img = cv2.imread(img_path)
        print('processing', img_path)

        # find the front-rear BB coordinate using YOLO label
        BBCor = yolo_to_BBCor(np.shape(img), *yolo_readline(txt_path))
        FR_img = cut_by_BBCor(img, *BBCor)