Ejemplo n.º 1
0
def __convert_file_annotation_rect(location):
    file = open(location, 'r')
    if file.mode == 'r':
        lines = file.readlines()
        rects = []
        for l in lines:
            tokens = l.split(' ')
            rects.append(AnnotationRect(int(tokens[0]), int(tokens[1]), int(tokens[2]), int(tokens[3])))
        return rects
Ejemplo n.º 2
0
def anchor_max_gt_overlaps(anchor_grid, gts):
    output = np.zeros(anchor_grid.shape[:-1])

    for y in range(anchor_grid.shape[0]):
        for x in range(anchor_grid.shape[1]):
            for s in range(anchor_grid.shape[2]):
                for a in range(anchor_grid.shape[3]):
                    anchor_box = AnnotationRect(anchor_grid[y, x, s, a, 0],
                                                anchor_grid[y, x, s, a, 1],
                                                anchor_grid[y, x, s, a, 2],
                                                anchor_grid[y, x, s, a, 3])
                    output[y, x, s,
                           a] = max([iou(anchor_box, gt) for gt in gts])
    return output
def create_boxes_dict(data, anchor_grid, fg_threshold=0.05):
    boxes_dict = {}
    scores = []
    calc_softmax = softmax(data, axis=-1)
    foreground = np.delete(calc_softmax, [0], axis=-1)
    # Get the scores from the data
    shape = foreground.shape
    for i in range(shape[0]):
        for j in range(shape[1]):
            for k in range(shape[2]):
                for l in range(shape[3]):
                    for m in range(shape[4]):
                        if foreground[i, j, k, l, m] > fg_threshold:
                            scores.append(foreground[i, j, k, l, m])
    # Get the boxes from the data
    indices = np.where(foreground > fg_threshold)[:4]
    max_boxes = anchor_grid[indices]
    boxes = [AnnotationRect(*max_boxes[i]) for i in range(max_boxes.shape[0])]
    for i in range(len(boxes)):
        boxes_dict[boxes[i]] = scores[i]
    return boxes_dict
for file in tqdm(os.listdir(src_folder)):
    if file.endswith(".txt"):
        src_txt = src_folder + '/' + file
        src_img = src_folder + '/' + file.split('.', 1)[0] + '.jpg'
        dest_txt = dest_folder + '/' + file
        dest_img = dest_folder + '/' + file.split('.', 1)[0] + '.jpg'
        dest_txt_remove = remove_folder + '/' + file
        dest_img_remove = remove_folder + '/' + file.split('.', 1)[0] + '.jpg'
        file = open(src_txt, 'r')
        good_ratio = True
        if file.mode == 'r':
            lines = file.readlines()
            for l in lines:
                tokens = l.split(' ')
                rect = AnnotationRect(int(tokens[0]), int(tokens[1]), int(tokens[2]), int(tokens[3]))
                if rect.width() > rect.height():
                    good_ratio = False

        if good_ratio:
            shutil.copyfile(src_txt, dest_txt)
            shutil.copyfile(src_img, dest_img)
            num_kept += 1
        else:
            shutil.copyfile(src_txt, dest_txt_remove)
            shutil.copyfile(src_img, dest_img_remove)
            num_filtered += 1

print('kept: ' + str(num_kept))
print('removed: ' + str(num_filtered))
Ejemplo n.º 5
0
def convert_to_annotation_rects_label(anchor_grid, labels):
    indices = np.where(labels == 1)[:4]
    max_boxes = anchor_grid[indices]
    annotated_boxes = [AnnotationRect(*max_boxes[i]) for i in range(len(max_boxes))]
    return annotated_boxes
Ejemplo n.º 6
0
def convert_to_annotation_rects_output(anchor_grid, output, confidence=0.7):
    calc_softmax = softmax(output, axis=-1)
    foreground = np.delete(calc_softmax, [0], axis=-1)
    indices = np.where(foreground > confidence)[:4]
    max_boxes = anchor_grid[indices]
    return [AnnotationRect(*max_boxes[i]) for i in range(len(max_boxes))]
for file in tqdm(os.listdir(src_folder)):
    if file.endswith(".txt"):
        src_txt = src_folder + '/' + file
        src_img = src_folder + '/' + file.split('.', 1)[0] + '.jpg'
        dest_txt = dest_folder + '/' + file
        dest_img = dest_folder + '/' + file.split('.', 1)[0] + '.jpg'
        dest_txt_remove = remove_folder + '/' + file
        dest_img_remove = remove_folder + '/' + file.split('.', 1)[0] + '.jpg'
        file = open(src_txt, 'r')
        good_sizes = True
        if file.mode == 'r':
            lines = file.readlines()
            for l in lines:
                tokens = l.split(' ')
                rect = AnnotationRect(int(tokens[0]), int(tokens[1]),
                                      int(tokens[2]), int(tokens[3]))
                if not min_size < rect.area():
                    good_sizes = False

        if good_sizes:
            shutil.copyfile(src_txt, dest_txt)
            shutil.copyfile(src_img, dest_img)
            num_kept += 1
        else:
            shutil.copyfile(src_txt, dest_txt_remove)
            shutil.copyfile(src_img, dest_img_remove)
            num_filtered += 1

print('kept: ' + str(num_kept))
print('removed: ' + str(num_filtered))