コード例 #1
0
ファイル: tools_YOLO.py プロジェクト: JustWinDRunneR/tools
def get_true_boxes(foldername,
                   filename,
                   smart_resized_target=None,
                   delim=' ',
                   limit=1000000):

    with open(filename) as f:
        lines = f.readlines()[1:limit]
    filenames_dict = sorted(set([line.split(delim)[0] for line in lines]))

    true_boxes = []

    print('\nGet %d true boxes\n' % len(filenames_dict))
    bar = progressbar.ProgressBar(max_value=len(filenames_dict))

    for b, filename in enumerate(filenames_dict):
        bar.update(b)
        if not os.path.isfile(foldername + filename): continue
        image = Image.open(foldername + filename)
        if image is None: continue
        width, height = image.size

        local_boxes = []
        for line in lines:
            split = line.split(delim)
            if split[0] == filename:
                class_ID = int(split[5])
                x_min, y_min, x_max, y_max = numpy.array(split[1:5]).astype(
                    numpy.float)

                if smart_resized_target is not None:
                    x_min, y_min = tools_image.smart_resize_point(
                        x_min, y_min, width, height, smart_resized_target[1],
                        smart_resized_target[0])
                    x_max, y_max = tools_image.smart_resize_point(
                        x_max, y_max, width, height, smart_resized_target[1],
                        smart_resized_target[0])

                local_boxes.append([x_min, y_min, x_max, y_max, class_ID])

        true_boxes.append(local_boxes)

    return true_boxes
コード例 #2
0
def get_true_boxes(foldername,
                   filename,
                   delim='\t',
                   smart_resized_target=None,
                   limit=100):

    with open(filename) as f:
        lines = f.readlines()[1:limit]
    list_filenames = [line.split(' ')[0] for line in lines]
    filenames_dict = sorted(set(list_filenames))

    true_boxes = []

    for filename in filenames_dict:
        local_boxes = []
        for line in lines:
            split = line.split(delim)
            if split[0] == filename:
                class_ID = int(split[5])
                x_min, y_min, x_max, y_max = numpy.array(split[1:5]).astype(
                    numpy.float)

                if smart_resized_target is not None:
                    image = cv2.imread(foldername + filename)
                    x_min, y_min = tools_image.smart_resize_point(
                        x_min, y_min, image.shape[1], image.shape[0],
                        smart_resized_target[1], smart_resized_target[0])
                    x_max, y_max = tools_image.smart_resize_point(
                        x_max, y_max, image.shape[1], image.shape[0],
                        smart_resized_target[1], smart_resized_target[0])

                local_boxes.append([x_min, y_min, x_max, y_max, class_ID])

        true_boxes.append(local_boxes)

    return true_boxes
コード例 #3
0
ファイル: tools_mAP.py プロジェクト: dryabokon/tools
def calc_hits_stats_iou(lines_true,
                        lines_pred,
                        class_ID,
                        delim,
                        folder_annotation,
                        iuo_th=0.5,
                        ovp_th=0.5,
                        ovd_th=0.5):
    file_true, file_pred = [], []
    coord_true, coord_pred, = [], []
    conf_true, conf_pred = [], []
    hit_true, hit_pred = [], []

    for line in lines_true:
        split = line.split(delim)
        if int(split[5]) == class_ID:
            x_min, y_min, x_max, y_max = int(split[1]), int(split[2]), int(
                split[3]), int(split[4])

            if not os.path.isfile(folder_annotation + split[0]): continue
            image = Image.open(folder_annotation + split[0])
            if image is None: continue
            width, height = image.size
            x_min, y_min = tools_image.smart_resize_point(
                x_min, y_min, width, height, 416, 416)
            x_max, y_max = tools_image.smart_resize_point(
                x_max, y_max, width, height, 416, 416)

            file_true.append(split[0])
            coord_true.append([x_min, y_min, x_max, y_max])
            conf_true.append(float(-1))
            hit_true.append(0)

    for line in lines_pred:
        split = line.split(delim)
        if int(split[5]) == class_ID:
            x_min, y_min, x_max, y_max = int(split[1]), int(split[2]), int(
                split[3]), int(split[4])

            if not os.path.isfile(folder_annotation + split[0]): continue
            image = Image.open(folder_annotation + split[0])
            if image is None: continue
            width, height = image.size
            x_min, y_min = tools_image.smart_resize_point(
                x_min, y_min, width, height, 416, 416)
            x_max, y_max = tools_image.smart_resize_point(
                x_max, y_max, width, height, 416, 416)
            file_pred.append(split[0])
            coord_pred.append([x_min, y_min, x_max, y_max])
            conf_pred.append(float(split[6]))
            hit_pred.append(0)

    if iuo_th is not None:
        for j, filename_true in enumerate(file_true):
            best_i, best_iou, best_conf = None, -1, None
            for i, filename_pred in enumerate(file_pred):
                if filename_true == filename_pred:
                    iuo_value = iou(coord_true[j], coord_pred[i])
                    if iuo_value >= iuo_th and iuo_value > best_iou:
                        best_i, best_iou, best_conf = i, iuo_value, conf_pred[
                            i]
            if best_i is not None:
                hit_true[j], conf_true[j] = 1, best_conf
                hit_pred[best_i], conf_pred[best_i] = 1, best_conf
            else:
                hit_true[j], conf_true[j] = 0, float(-1)
    else:
        for j, filename_true in enumerate(file_true):
            for i, filename_pred in enumerate(file_pred):
                if filename_true == filename_pred:

                    ovp_value, ovd_value = ovelraps(coord_true[j],
                                                    coord_pred[i])
                    if ovp_value >= ovp_th and ovd_value <= ovd_th:
                        hit_pred[i] = 1
                        hit_true[j] = 1
                        conf_true[j] = max(conf_true[j], conf_pred[i])

    conf_true, conf_pred = numpy.array(conf_true), numpy.array(conf_pred)
    hit_true, hit_pred = numpy.array(hit_true), numpy.array(hit_pred)
    file_true, file_pred = numpy.array(file_true), numpy.array(file_pred)
    coord_true, coord_pred = numpy.array(coord_true), numpy.array(coord_pred)

    return file_true, file_pred, coord_true, coord_pred, conf_true, conf_pred, hit_true, hit_pred
コード例 #4
0
ファイル: tools_YOLO.py プロジェクト: JustWinDRunneR/tools
def draw_annotation_boxes(file_annotations,
                          file_classes,
                          file_metadata,
                          path_out,
                          delim=' '):

    tools_IO.remove_files(path_out, create=True)

    input_image_size, class_names, anchors, anchor_mask, obj_threshold, nms_threshold = load_metadata(
        file_metadata)
    mat = tools_IO.load_mat(file_classes, numpy.str)
    if len(mat) <= len(class_names):
        class_names[:len(mat)] = mat

    foldername = '/'.join(file_annotations.split('/')[:-1]) + '/'
    with open(file_annotations) as f:
        lines = f.readlines()[1:]
    boxes_xyxy = numpy.array([line.split(delim)[1:5] for line in lines],
                             dtype=numpy.int)
    filenames = numpy.array([line.split(delim)[0] for line in lines])
    class_IDs = numpy.array([line.split(delim)[5] for line in lines],
                            dtype=numpy.int)
    colors = generate_colors(numpy.max(class_IDs) + 1)

    true_boxes = get_true_boxes(foldername,
                                file_annotations, (416, 416),
                                delim=' ')
    if len(true_boxes) > 6:
        anchors = annotation_boxes_to_ancors(true_boxes, 6)

    descript_ion = []
    for filename in set(filenames):

        image = cv2.imread(foldername + filename)
        image = tools_image.desaturate(image, 0.9)
        idx = numpy.where(filenames == filename)

        boxes_resized = []
        for box in boxes_xyxy[idx]:
            x_min, y_min = tools_image.smart_resize_point(
                box[0], box[1], image.shape[1], image.shape[0], 416, 416)
            x_max, y_max = tools_image.smart_resize_point(
                box[2], box[3], image.shape[1], image.shape[0], 416, 416)
            boxes_resized.append([x_min, y_min, x_max, y_max, 0])

        statuses = are_boxes_preprocessed_well(boxes_resized, anchors,
                                               anchor_mask, len(class_names))
        descript_ion.append([filename, 1 * (statuses.sum() == len(statuses))])

        for box, class_ID, status in zip(boxes_xyxy[idx], class_IDs[idx],
                                         statuses):
            w = 2 if status > 0 else -1
            cv2.rectangle(image, (box[0], box[1]), (box[2], box[3]),
                          colors[class_ID],
                          thickness=w)
            cv2.putText(image, '{0:d} {1:s}'.format(class_ID,
                                                    class_names[class_ID]),
                        (box[0], box[1] - 4), cv2.FONT_HERSHEY_SIMPLEX, 0.6,
                        colors[class_ID], 1, cv2.LINE_AA)

        cv2.imwrite(path_out + filename, image)

    tools_IO.save_mat(descript_ion, path_out + 'descript.ion', delim=' ')
    return