Exemplo n.º 1
0
def coco_separate_classes(input_dir, output_dir):
    classes_fn = file_helper.path_join(input_dir, "_classes.txt")
    classes = []
    for line in file_helper.read_lines(classes_fn):
        classes.append(line)

    i = 0
    ann_fn = file_helper.path_join(input_dir, "_annotations.txt")
    img_fn = None
    for line in file_helper.read_lines(ann_fn):
        try:
            items = line.split(" ")
            img_fn = file_helper.path_join(input_dir, items[0])
            dir_name, name, extension = file_helper.get_file_name_extension(
                img_fn)
            i += 1
            print("{} - {}".format(i, img_fn), end=end_txt)

            mat = cv2.imread(img_fn)
            h, w = image_helper.image_h_w(mat)

            for j in range(1, len(items)):
                item = items[j]
                values_str = item.split(",")
                values = []
                for v in values_str:
                    values.append(int(v))
                class_name = classes[values[4]]
                write_dir = file_helper.path_join(output_dir, class_name)
                if not os.path.isdir(write_dir):
                    file_helper.create_dir(write_dir)

                write_img_fn = file_helper.path_join(write_dir,
                                                     name + extension)
                if not os.path.isfile(write_img_fn):
                    file_helper.copy_file(img_fn, write_img_fn)

                x1, y1, x2, y2 = list(values[:4])
                w_ = (x2 - x1)
                h_ = (y2 - y1)
                cx = (x1 + w_ * 0.5) / w
                cy = (y1 + h_ * 0.5) / h
                line = "0 {} {} {} {}".format(cx, cy, w_ / w, h_ / h)

                write_lbl_fn = file_helper.path_join(write_dir, name + ".txt")
                if os.path.isfile(write_lbl_fn):
                    file_helper.append_line(write_lbl_fn, line)
                else:
                    file_helper.write_lines(write_lbl_fn, [line])

        except Exception as e:
            print('Error - file:{} msg:{}'.format(img_fn, str(e)))
Exemplo n.º 2
0
def check_labels(labels_dir):
    if not os.path.isdir(labels_dir):
        raise Exception("bad dir: " + labels_dir)
    i = 0
    for fn in file_helper.enumerate_files(labels_dir):
        try:
            i += 1
            print("{} - {}".format(i, fn), end=end_txt)
            dir_name, name, extension = file_helper.get_file_name_extension(fn)
            if string_helper.equals_case_insensitive(extension, ".txt"):
                if name == "classes" and extension == ".txt":
                    continue

                lines = []
                changed = False
                for line in file_helper.read_lines(fn):
                    lst = line.split(" ")
                    for j in range(1, len(lst)):
                        val = float(lst[j])
                        if val < 0:
                            changed = True
                            lst[j] = "0"
                        elif val > 1:
                            changed = True
                            lst[j] = "1"
                    new_line = string_helper.join(lst, " ")
                    lines.append(new_line)
                if changed:
                    file_helper.delete_file(fn)
                    file_helper.write_lines(fn, lines)
        except Exception as e:
            print("error - mirror_images: {}".format(fn))

    print("change_class_id finished")
Exemplo n.º 3
0
def change_class_id_list(labels_dir, class_id_from_list, class_id_to):
    if not os.path.isdir(labels_dir):
        raise Exception("bad dir: " + labels_dir)
    i = 0
    for fn in file_helper.enumerate_files(labels_dir):
        try:
            i += 1
            print("{} - {}".format(i, fn), end=end_txt)
            dir_name, name, extension = file_helper.get_file_name_extension(fn)
            if string_helper.equals_case_insensitive(extension, ".txt"):
                if name == "classes" and extension == ".txt":
                    continue

                lines = []
                changed = False
                for line in file_helper.read_lines(fn):
                    lst = line.split(" ")
                    if int(lst[0]) in class_id_from_list:
                        changed = True
                        lst[0] = str(class_id_to)
                    new_line = string_helper.join(lst, " ")
                    lines.append(new_line)
                if changed:
                    file_helper.delete_file(fn)
                    file_helper.write_lines(fn, lines)
        except Exception as e:
            print("error - change_class_id: {}".format(fn))

    print("change_class_id finished")
Exemplo n.º 4
0
def merge_single_classes(input_dirs, merge_dir):
    i = 0
    for input_dir in input_dirs:
        for fn in file_helper.enumerate_files(input_dir):
            try:
                i += 1
                print("{} - {}".format(i, fn), end=end_txt)
                dir_name, name, extension = file_helper.get_file_name_extension(
                    fn)
                if extension != ".txt":
                    fn_input_txt = file_helper.path_join(
                        input_dir, name + ".txt")
                    if not os.path.isfile(fn_input_txt):
                        raise Exception("no label file!")
                    fn_output_txt = file_helper.path_join(
                        merge_dir, name + ".txt")

                    fn_input_img = fn
                    fn_output_img = file_helper.path_join(
                        merge_dir, name + ".jpg")
                    if not os.path.isfile(fn_output_img):
                        mat = cv2.imread(fn_input_img)
                        cv2.imwrite(fn_output_img, mat)

                    if not os.path.isfile(fn_output_txt):
                        file_helper.copy_file(fn_input_txt, fn_output_txt)
                    else:
                        file_helper.append_lines(
                            fn_output_txt,
                            file_helper.read_lines(fn_input_txt))
                        print('Merged: {}'.format(fn_output_txt), end=end_txt)
            except Exception as e:
                print('Error - merge_single_classes - file: {} msg: {}'.format(
                    fn, str(e)))
Exemplo n.º 5
0
def class_info_yolo(labels_dir, classes_txt_fn):
    class_names = file_helper.read_lines(classes_txt_fn)
    class_counts = {}
    for name in class_names:
        class_counts[name] = 0
    for fn in file_helper.enumerate_files(labels_dir,
                                          wildcard_pattern="*.txt"):
        line = None
        try:
            for line in file_helper.read_lines(fn):
                class_id = int(line.split(" ")[0])
                name = class_names[class_id]
                class_counts[name] += 1
            print(class_counts, end=end_txt)
        except Exception as e:
            print("error - class_info_yolo: {} - line: {}".format(fn, line))

    print(class_counts)
Exemplo n.º 6
0
def _oidv6_to_yolo(class_id, class_name, images_dir, labels_dir, yolo_dir):
    for label_fn in file_helper.enumerate_files(labels_dir, recursive=False):
        try:
            print(label_fn, end="\r")
            name = os.path.basename(label_fn)
            image_fn = file_helper.path_join(images_dir,
                                             name.replace(".txt", ".jpg"))
            if os.path.isfile(image_fn):
                write_image_fn = file_helper.path_join(
                    yolo_dir, name.replace(".txt", ".jpg"))
                write_fn_txt = file_helper.path_join(yolo_dir, name)
                if os.path.isfile(write_image_fn) and os.path.isfile(
                        write_fn_txt):
                    continue

                mat = cv2.imread(image_fn)
                h, w = image_helper.image_h_w(mat)
                lines = []
                for line in file_helper.read_lines(label_fn):
                    line = line.replace("\t", " ").replace("  ", " ").replace(
                        "  ", " ").replace("  ", " ").replace("  ", " ")
                    arr0 = line.split(" ")
                    arr = [class_id]
                    x1 = float(arr0[1])
                    y1 = float(arr0[2])
                    x2 = float(arr0[3])
                    y2 = float(arr0[4])
                    arr.append((x1 + x2) * 0.5 / w)
                    arr.append((y1 + y2) * 0.5 / h)
                    arr.append((x2 - x1) / w)
                    arr.append((y2 - y1) / h)
                    line = ' '.join(str(e) for e in arr)
                    lines.append(line)

                if len(lines) > 0:
                    # write_image_fn = file_helper.path_join(yolo_dir, name.replace(".txt", ".jpg"))
                    # cv2.imwrite(write_image_fn, mat)
                    file_helper.copy_file(image_fn, write_image_fn)

                    with contextlib.suppress(FileNotFoundError):
                        os.remove(write_fn_txt)
                    file_helper.write_lines(write_fn_txt, lines)
                else:
                    print("nok: " + label_fn)
            else:
                print("no image: " + image_fn)
        except Exception as e:
            print('Error - file:{} msg:{}'.format(label_fn, str(e)))

    print("finished: " + class_name)
Exemplo n.º 7
0
def add_to_examples(head_path, examples, cl):
    lines = read_lines(head_path)

    last_path = ''
    for line in lines:
        infos = line.split()
        if last_path != infos[0]:
            examples.append({
                'img_path': infos[0],
                'boxes': list(),
                'class': cl,
            })
        examples[-1]['boxes'].append(infos[1:5])
    return examples
Exemplo n.º 8
0
def oidv6_to_yolo2(input_images_dir, input_labels_dir,
                   output_labels_and_images_dir, class_id):
    i = 0
    if not os.path.isdir(output_labels_and_images_dir):
        file_helper.create_dir(output_labels_and_images_dir)
    for label_fn in file_helper.enumerate_files(input_labels_dir,
                                                recursive=False,
                                                wildcard_pattern="*.txt"):
        try:
            i += 1
            print("{} - {}".format(i, label_fn), end=end_txt)
            dir_name, name, extension = file_helper.get_file_name_extension(
                label_fn)
            image_fn = file_helper.path_join(input_images_dir, name + ".jpg")
            if os.path.isfile(image_fn):
                mat = cv2.imread(image_fn)
                h, w = image_helper.image_h_w(mat)
                lines = []
                for line in file_helper.read_lines(label_fn):
                    line = line.replace("\t", " ").replace("  ", " ").replace(
                        "  ", " ").replace("  ", " ").replace("  ", " ")
                    arr0 = line.split(" ")
                    arr = [class_id]
                    x1 = float(arr0[1])
                    y1 = float(arr0[2])
                    x2 = float(arr0[3])
                    y2 = float(arr0[4])
                    arr.append((x1 + x2) * 0.5 / w)
                    arr.append((y1 + y2) * 0.5 / h)
                    arr.append((x2 - x1) / w)
                    arr.append((y2 - y1) / h)
                    line = ' '.join(str(e) for e in arr)
                    lines.append(line)

                out_img_fn = file_helper.path_join(
                    output_labels_and_images_dir, name + ".jpg")
                out_lbl_fn = file_helper.path_join(
                    output_labels_and_images_dir, name + ".txt")
                if os.path.isfile(out_img_fn):
                    os.remove(out_img_fn)
                if os.path.isfile(out_lbl_fn):
                    os.remove(out_lbl_fn)

                file_helper.write_lines(out_lbl_fn, lines)
                file_helper.copy_file(image_fn, out_img_fn)
            else:
                print("no image: " + image_fn)
        except Exception as e:
            print('Error - file:{} msg:{}'.format(label_fn, str(e)))
Exemplo n.º 9
0
def label_boxes(input_path, output_box_path):
    img_pathes = read_lines(input_path)
    global cur_box_list
    for img_name in img_pathes:
        cur_box_list = []
        global img, cur_img_path
        cur_img_path = img_name.strip()
        img = cv2.imread(cur_img_path)
        img = imutils.resize(img, width=400)
        cv2.namedWindow('image')
        cv2.setMouseCallback('image', on_mouse)
        cv2.imshow('image', img)
        cv2.waitKey(0)
        for i in range(len(cur_box_list)):
            cur_line = '%s\t%4f\t%4f\t%4f\t%4f\n' % (
                cur_img_path, float(cur_box_list[i][0][0]) / img.shape[1],
                float(cur_box_list[i][0][1]) / img.shape[0],
                float(cur_box_list[i][1][0]) / img.shape[1],
                float(cur_box_list[i][1][1]) / img.shape[0])
            write(output_box_path, cur_line)
Exemplo n.º 10
0
def label_cattle_boxes():
    img_pathes = read_lines('data/cattle.txt')
    global cur_box_list
    for img_name in img_pathes:
        cur_box_list = []
        global img, cur_img_path
        cur_img_path = 'data/hard_cattle/' + img_name.strip()
        img = cv2.imread(cur_img_path)
        img = imutils.resize(img, width=400)
        cv2.namedWindow('image')
        cv2.setMouseCallback('image', on_mouse)
        cv2.imshow('image', img)
        cv2.waitKey(0)
        for i in range(len(cur_box_list)):
            cur_line = '%s\t%4f\t%4f\t%4f\t%4f\n' % (
                cur_img_path, float(cur_box_list[i][0][0]) / img.shape[1],
                float(cur_box_list[i][0][1]) / img.shape[0],
                float(cur_box_list[i][1][0]) / img.shape[1],
                float(cur_box_list[i][1][1]) / img.shape[0])
            write('body_box.txt', cur_line)
Exemplo n.º 11
0
def delete_classes(images_dir, classes_old, classes_new):
    class_id_map = {}
    for old_name in classes_old:
        old_id = str(classes_old.index(old_name))
        if old_name in classes_new:
            class_id_map[old_id] = str(classes_new.index(old_name))
        else:
            class_id_map[old_id] = None

    i = 0
    for fn in file_helper.enumerate_files(images_dir,
                                          wildcard_pattern="*.txt"):
        try:
            i += 1
            lines = []
            changed = False
            for line in file_helper.read_lines(fn):
                items = line.split(" ")
                old_id = items[0]
                new_id = class_id_map[old_id]
                if new_id is not None:
                    if new_id != old_id:
                        changed = True
                        items[0] = new_id
                    lines.append(string_helper.join(items, " "))
                else:
                    changed = True
            if changed:
                file_helper.delete_file(fn)
                if len(lines) > 0:
                    # print("changed - {}".format(fn))
                    file_helper.write_lines(fn, lines)
                    print("{} - {} EDITED".format(i, fn), end=end_txt)
                else:
                    print("{} - {} DELETED".format(i, fn), end=end_txt)
            else:
                print("{} - {}".format(i, fn), end=end_txt)
        except Exception as e:
            print('Error - merge_single_classes - file: {} msg: {}'.format(
                fn, str(e)))
    check_single_files(images_dir)
Exemplo n.º 12
0
def oidv6_to_yolo(images_and_labels_dir, class_id):
    for label_fn in file_helper.enumerate_files(images_and_labels_dir,
                                                recursive=False,
                                                wildcard_pattern="*.txt"):
        try:
            print(label_fn, end=end_txt)
            name = os.path.basename(label_fn)
            image_fn = file_helper.path_join(images_and_labels_dir,
                                             name.replace(".txt", ".jpg"))
            if os.path.isfile(image_fn):
                mat = cv2.imread(image_fn)
                h, w = image_helper.image_h_w(mat)
                lines = []
                for line in file_helper.read_lines(label_fn):
                    line = line.replace("\t", " ").replace("  ", " ").replace(
                        "  ", " ").replace("  ", " ").replace("  ", " ")
                    arr0 = line.split(" ")
                    arr = [class_id]
                    x1 = float(arr0[1])
                    y1 = float(arr0[2])
                    x2 = float(arr0[3])
                    y2 = float(arr0[4])
                    arr.append((x1 + x2) * 0.5 / w)
                    arr.append((y1 + y2) * 0.5 / h)
                    arr.append((x2 - x1) / w)
                    arr.append((y2 - y1) / h)
                    line = ' '.join(str(e) for e in arr)
                    lines.append(line)

                with contextlib.suppress(FileNotFoundError):
                    os.remove(label_fn)
                file_helper.write_lines(label_fn, lines)
            else:
                print("no image: " + image_fn)
        except Exception as e:
            print('Error - file:{} msg:{}'.format(label_fn, str(e)))
Exemplo n.º 13
0
def mirror_images(source_dir_name, target_dir_name):
    if not os.path.isdir(target_dir_name):
        file_helper.create_dir(target_dir_name)
    i = 0
    for fn in file_helper.enumerate_files(source_dir_name):
        try:
            i += 1
            print("{} - {}".format(i, fn), end=end_txt)
            dir_name, name, extension = file_helper.get_file_name_extension(fn)
            if string_helper.equals_case_insensitive(extension, ".txt"):
                if name == "classes" and extension == ".txt":
                    new_fn = file_helper.path_join(target_dir_name,
                                                   name + extension)
                    if not os.path.isfile(new_fn):
                        file_helper.copy_file(fn, new_fn)
                    continue

                new_fn = file_helper.path_join(target_dir_name,
                                               name + "_mirror" + extension)
                if os.path.isfile(new_fn):
                    raise Exception()
                for line in file_helper.read_lines(fn):
                    lst = line.split(" ")
                    lst[1] = str(1 - float(lst[1]))
                    new_line = string_helper.join(lst, " ")
                    file_helper.append_line(new_fn, new_line)
            else:
                mat = cv2.imread(fn)
                new_fn = file_helper.path_join(target_dir_name,
                                               name + "_mirror" + ".jpg")
                mat = cv2.flip(mat, 1)
                cv2.imwrite(new_fn, mat)
        except Exception as e:
            print("error - mirror_images: {}".format(fn))

    print("mirror_images finished")
Exemplo n.º 14
0
def read_caltech():
    train_features = list()
    train_labels = list()
    test_features = list()
    test_labels = list()
    i = 0
    max_read = 15000
    for files in os.listdir(caltech_train_pos_path):
        if i > max_read:
            break
        i += 1
        path = os.path.join(caltech_train_pos_path, files)
        features = read_lines(path)
        # print(path)
        train_features.append(features)
        train_labels.append(1)
        train_features.append(features)
        train_labels.append(1)
        train_features.append(features)
        train_labels.append(1)
    i = 0
    for files in os.listdir(caltech_train_neg_path):
        if i > max_read:
            break
        i += 1
        path = os.path.join(caltech_train_neg_path, files)
        # print(path)
        features = read_lines(path)
        train_features.append(features)
        train_labels.append(0)
    i = 0
    for files in os.listdir(caltech_test_pos_path):
        if i > max_read:
            break
        i += 1
        path = os.path.join(caltech_test_pos_path, files)
        # print(path)
        features = read_lines(path)
        test_features.append(features)
        test_labels.append(1)
    i = 0
    for files in os.listdir(caltech_test_neg_path):
        if i > max_read:
            break
        i += 1
        path = os.path.join(caltech_test_neg_path, files)
        # print(path)
        features = read_lines(path)
        test_features.append(features)
        test_labels.append(0)
    train_tuples = zip(train_features, train_labels)
    random.shuffle(train_tuples)
    train_features = [train_tuple[0] for train_tuple in train_tuples]
    train_labels = [train_tuple[1] for train_tuple in train_tuples]
    test_tuples = zip(test_features, test_labels)

    random.shuffle(test_tuples)
    test_features = [test_tuple[0] for test_tuple in test_tuples]
    test_labels = [test_tuple[1] for test_tuple in test_tuples]

    return train_features, train_labels, test_features, test_labels
Exemplo n.º 15
0
def oidv6_to_yolo_multi(input_multi_oidv6_dir, output_yolo_dir):
    if not os.path.isdir(output_yolo_dir):
        file_helper.create_dir(output_yolo_dir)

    output_images_dir = file_helper.path_join(output_yolo_dir, "images")
    if not os.path.isdir(output_images_dir):
        file_helper.create_dir(output_images_dir)

    output_labels_dir = file_helper.path_join(output_yolo_dir, "labels")
    if not os.path.isdir(output_labels_dir):
        file_helper.create_dir(output_labels_dir)

    classes = []

    print("started.... oidv6_to_yolo_multi - {}".format(input_multi_oidv6_dir))
    i = 0
    for sub_dir in ["test", "train", "validation"]:
        images_dir = file_helper.path_join(input_multi_oidv6_dir, sub_dir)
        labels_dir = file_helper.path_join(images_dir, "labels")
        for image_fn in file_helper.enumerate_files(images_dir,
                                                    recursive=False):
            try:
                dir_name, name, extension = file_helper.get_file_name_extension(
                    image_fn)
                label_fn = file_helper.path_join(labels_dir, name + ".txt")
                if not os.path.isfile(label_fn):
                    print("!!! File has no label: {}".format(image_fn))
                else:
                    i += 1
                    print("processing {} - {}".format(str(i), image_fn),
                          end="                                    \r")

                    key_name = name[name.rfind("_") + 1:]

                    out_image_fn = file_helper.path_join(
                        output_yolo_dir, "images", key_name + extension)
                    out_label_fn_1 = file_helper.path_join(
                        output_yolo_dir, "images", key_name + ".txt")
                    out_label_fn_2 = file_helper.path_join(
                        output_yolo_dir, "labels", key_name + ".txt")

                    class_name = name[:name.rfind("_")]
                    if class_name not in classes:
                        classes.append(class_name)
                        print(class_name)

                    class_id = classes.index(class_name)

                    if os.path.isfile(out_image_fn) and os.path.isfile(
                            out_label_fn_2):
                        exists = False
                        for line in file_helper.read_lines(out_label_fn_2):
                            line = line.replace("\t", " ").replace(
                                "  ", " ").replace("  ", " ").replace(
                                    "  ", " ").replace("  ", " ")
                            if class_id == int(line.split(" ")[0]):
                                exists = True
                                break
                        if exists:
                            continue

                    mat = cv2.imread(image_fn)
                    h, w = image_helper.image_h_w(mat)
                    lines = []
                    for line in file_helper.read_lines(label_fn):
                        line = line.replace("\t", " ").replace(
                            "  ",
                            " ").replace("  ",
                                         " ").replace("  ",
                                                      " ").replace("  ", " ")
                        arr0 = line.split(" ")
                        arr = [class_id]
                        x1 = float(arr0[1])
                        y1 = float(arr0[2])
                        x2 = float(arr0[3])
                        y2 = float(arr0[4])
                        arr.append((x1 + x2) * 0.5 / w)
                        arr.append((y1 + y2) * 0.5 / h)
                        arr.append((x2 - x1) / w)
                        arr.append((y2 - y1) / h)
                        line = ' '.join(str(e) for e in arr)
                        lines.append(line)

                    if len(lines) > 0:
                        cv2.imwrite(out_image_fn, mat)
                        # file_helper.copy_file(image_fn, out_image_fn)

                        # if os.path.isfile(out_label_fn_2):
                        #     print("merged: " + out_label_fn_2)

                        for line in lines:
                            file_helper.append_line(out_label_fn_1, line)
                            file_helper.append_line(out_label_fn_2, line)
                    else:
                        print("nok: " + label_fn)
            except Exception as e:
                print('Error - file:{} msg:{}'.format(image_fn, str(e)))

    classes_txt_fn = file_helper.path_join(output_yolo_dir, "classes.txt")
    file_helper.write_lines(classes_txt_fn, classes)
    print("finished: oidv6_to_yolo_multi")
Exemplo n.º 16
0
def combine_classes(class_items,
                    out_images_dir,
                    out_labels_dir,
                    out_classes_txt_fn,
                    out_style="yolo"):
    # a typical class_item
    # {
    #     "class_name": "vehicle license plate",
    #     "dirs":
    #         [
    #             {
    #                 "images": "C:/_koray/train_datasets/yolo_oidv6_class0/vehicle_registration_plate",
    #                 "labels": "C:/_koray/train_datasets/yolo_oidv6_class0/vehicle_registration_plate"
    #             },
    #             {
    #                 "images": "C:/_koray/train_datasets/yolo_misc/vehicle_registration_plate/class0",
    #                 "labels": "C:/_koray/train_datasets/yolo_misc/vehicle_registration_plate/class0"
    #             }
    #         ],
    #     "resize_style": None, "if_larger" or "crop" - if "crop" -> make the image unique, don't combine, other combine with other classes
    #     "image_size": 640, - if None no resize, combine all
    #     "style": "yolo"
    # }

    class_names = []
    if out_style == "yolo":
        if not os.path.isdir(out_images_dir):
            file_helper.create_dir(out_images_dir)

        if not os.path.isdir(out_labels_dir):
            file_helper.create_dir(out_labels_dir)

        if os.path.isfile(out_classes_txt_fn):
            for class_name in file_helper.read_lines(out_classes_txt_fn):
                class_names.append(class_name)
    else:
        raise Exception("Bad out_style: " + out_style)

    for class_item in class_items:
        class_name = class_item["class_name"]
        resize_style = class_item["resize_style"]
        image_size = class_item["image_size"]
        if image_size is not None:
            image_size_txt = "{}_{}".format(image_size[0], image_size[1])
        else:
            image_size_txt = None
        input_style = class_item["input_style"]

        if class_name in class_names:
            class_index = class_names.index(class_name)
        else:
            class_index = len(class_names)
            class_names.append(class_name)
            file_helper.append_line(out_classes_txt_fn, class_name)

        i = 0
        for dir_item in class_item["dirs"]:
            images_dir = dir_item["images"]
            labels_dir = dir_item["labels"]

            if input_style == "yolo":
                for label_fn in file_helper.enumerate_files(
                        labels_dir, wildcard_pattern="*.txt"):
                    try:
                        _dir_name, name, _extension = file_helper.get_file_name_extension(
                            label_fn)

                        i += 1
                        print("{} - {}".format(i, label_fn), end=end_txt)

                        for line in file_helper.read_lines(label_fn):
                            line_items = line.split(" ")
                            cx_norm = float(line_items[1])
                            cy_norm = float(line_items[2])
                            w_norm = float(line_items[3])
                            h_norm = float(line_items[4])
                            line_items[0] = str(class_index)

                            out_lbl_fn = None
                            out_img_fn = None
                            mat = None
                            # for image_fn in file_helper.enumerate_files(images_dir, wildcard_pattern=name + ".*"):
                            image_fn = find_image_file(images_dir, name)
                            if image_fn is not None:
                                _dir_name, _name, extension = file_helper.get_file_name_extension(
                                    image_fn)
                                if extension != ".txt":
                                    try:
                                        mat = cv2.imread(image_fn)
                                    except Exception as e:
                                        print(
                                            'Error reading image file: {} msg:{}'
                                            .format(image_fn, str(e)))
                                    if mat is not None:
                                        if image_size is None or resize_style is None:
                                            out_img_fn = file_helper.path_join(
                                                out_images_dir, name + ".jpg")
                                            out_lbl_fn = file_helper.path_join(
                                                out_labels_dir, name + ".txt")
                                        elif resize_style == "if_larger":
                                            out_img_fn = file_helper.path_join(
                                                out_images_dir, name + "_" +
                                                image_size_txt + ".jpg")
                                            out_lbl_fn = file_helper.path_join(
                                                out_labels_dir, name + "_" +
                                                image_size_txt + ".txt")
                                            mat = image_helper.resize_if_larger(
                                                mat,
                                                max(image_size[0],
                                                    image_size[1]))
                                        elif resize_style == "crop":
                                            new_name = file_helper.get_unique_file_name(
                                            )
                                            out_img_fn = file_helper.path_join(
                                                out_images_dir, name +
                                                "_crop_" + new_name + ".jpg")
                                            out_lbl_fn = file_helper.path_join(
                                                out_labels_dir, name +
                                                "_crop_" + new_name + ".txt")
                                            mat, cx, cy, w, h = crop_rect(
                                                mat, cx_norm, cy_norm, w_norm,
                                                h_norm, image_size)
                                            line_items[1] = str(cx)
                                            line_items[2] = str(cy)
                                            line_items[3] = str(w)
                                            line_items[4] = str(h)
                                        else:
                                            raise Exception(
                                                "Bad resize_style: " +
                                                resize_style)
                            else:
                                raise Exception("Cannot find image file")

                            if out_lbl_fn is not None:
                                line = string_helper.join(line_items, " ")
                                if os.path.isfile(out_lbl_fn):
                                    file_helper.append_line(out_lbl_fn, line)
                                else:
                                    file_helper.write_lines(out_lbl_fn, [line])
                            if out_img_fn is not None and mat is not None:
                                if not os.path.isfile(out_img_fn):
                                    cv2.imwrite(out_img_fn, mat)
                    except Exception as e:
                        print('Error - file:{} msg:{}'.format(
                            label_fn, str(e)))
            else:
                raise Exception("Bad input_style: " + out_style)
Exemplo n.º 17
0
def class_info_names(classes_txt_fn):
    class_names = file_helper.read_lines(classes_txt_fn)
    print(class_names)
    return class_names
Exemplo n.º 18
0
def mirror_images_of_classes(input_images_dir,
                             input_labels_dir,
                             output_dir,
                             class_ids,
                             copy_other_classes=True):
    out_images_dir = file_helper.path_join(output_dir, "images")
    out_labels_dir = file_helper.path_join(output_dir, "labels")
    for dir_name in [out_images_dir, out_labels_dir]:
        if not os.path.isdir(dir_name):
            file_helper.create_dir(dir_name)

    i = 0
    for fn_label in file_helper.enumerate_files(input_labels_dir,
                                                recursive=False,
                                                wildcard_pattern="*.txt"):
        try:
            i += 1
            print("{} - {}".format(i, fn_label), end=end_txt)
            dir_name, name, extension = file_helper.get_file_name_extension(
                fn_label)
            mirror = False
            for line in file_helper.read_lines(fn_label):
                class_id = int(line.split(" ")[0])
                if class_id in class_ids:
                    mirror = True
                    break

            if mirror:
                fn_image = file_helper.path_join(input_images_dir,
                                                 name + ".jpg")
                if not os.path.isfile(fn_image):
                    print("No image: {}".format(fn_image))
                else:
                    new_image_fn = file_helper.path_join(
                        out_images_dir, name + "_mirror" + ".jpg")
                    cv2.imwrite(new_image_fn,
                                image_helper.mirror(cv2.imread(fn_image)))

                    new_label_fn1 = file_helper.path_join(
                        out_labels_dir, name + "_mirror" + ".txt")
                    new_label_fn2 = file_helper.path_join(
                        out_images_dir, name + "_mirror" + ".txt")
                    new_label_file_names = [new_label_fn1, new_label_fn2]
                    for new_label_fn in new_label_file_names:
                        if os.path.isfile(new_label_fn):
                            raise Exception()
                        for line in file_helper.read_lines(fn_label):
                            lst = line.split(" ")
                            lst[1] = str(1 - float(lst[1]))
                            new_line = string_helper.join(lst, " ")
                            file_helper.append_line(new_label_fn, new_line)
            if mirror or copy_other_classes:
                fn_image = file_helper.path_join(input_images_dir,
                                                 name + ".jpg")
                if not os.path.isfile(fn_image):
                    print("No image: {}".format(fn_image))
                else:
                    new_image_fn = file_helper.path_join(
                        out_images_dir, name + ".jpg")
                    cv2.imwrite(new_image_fn, cv2.imread(fn_image))

                    new_label_fn1 = file_helper.path_join(
                        out_labels_dir, name + ".txt")
                    new_label_fn2 = file_helper.path_join(
                        out_images_dir, name + ".txt")
                    new_label_file_names = [new_label_fn1, new_label_fn2]
                    for new_label_fn in new_label_file_names:
                        if os.path.isfile(new_label_fn):
                            raise Exception()
                        file_helper.copy_file(fn_label, new_label_fn)

        except Exception as e:
            print("error - mirror_images_of_classes: {}".format(fn_label))

    print("mirror_images_of_classes {} finished".format(class_ids))
Exemplo n.º 19
0
os.system('mkdir detect4video')
os.system('rm detect4video/*')
import numpy as np
import PIL.ImageDraw as ImageDraw
import PIL.ImageFont as ImageFont
import tensorflow as tf
from PIL import Image
from file_helper import read_lines
from object_detection.utils import visualization_utils as vis_util


def load_image_into_numpy_array(image):
    (im_width, im_height) = image.size
    return np.array(image.getdata()).reshape(
        (im_height, im_width, 3)).astype(np.uint8)


lines = read_lines('oid_cattle.txt')
examples = list()
last_path = ''
for line in lines:
    infos = line.split()
    img_path = infos[0]
    box = list()
    for bi in infos[1:5]:
        box.append(float(bi))
    image = Image.open(img_path)
    draw = ImageDraw.Draw(image)
    vis_util.draw_bounding_box_on_image(image, box[1], box[0], box[3], box[2])
    image.show()
    raw_input(img_path)
Exemplo n.º 20
0
def label2xml(list_path, category_id, ANN_DIR='data/Annotations/', IMG_DIR='data/images/', SET_DIR='data/ImageSets/',
              train=True, start_img_cnt=0):
    set_file_list = [SET_DIR + 'train.txt', SET_DIR + 'minival.txt', SET_DIR + 'testdev.txt', SET_DIR + 'test.txt']
    TYPEPREFIX = 'train' if train else 'val'
    lines = read_lines(list_path)
    line_i = 0
    last_name = ''
    E = None
    img_annotation = None
    image_file = None
    while line_i < len(lines):
        line = lines[line_i]
        infos = line.split()
        image_path = infos[0]
        box = infos[1:]
        imagename = int(image_path.split('.')[0].split('/')[-1])
        dir_num = int(image_path.split('.')[0].split('/')[-2][-5:])
        target_name = '%05d%04d' % (dir_num, imagename)

        print('last_path: %s, image_path: %s' % (last_name, image_path))
        if last_name != target_name:
            if train:
                write_line(set_file_list[0], "%s.jpg %s.xml" % (target_name, target_name))
            else:
                for i in range(1, 4):
                    write_line(set_file_list[i], "%s.jpg %s.xml" % (target_name, target_name))
            if last_name != '':
                xml_pretty = etree.tostring(img_annotation, pretty_print=True)
                with open(ANN_DIR + last_name + ".xml", 'wb') as ann_file:
                    ann_file.write(xml_pretty)
            image_file = imread(image_path)
            copyfile(image_path, IMG_DIR + '%s.jpg' % target_name)
            if path.exists(ANN_DIR + "%s.xml" % target_name):
                E = objectify.ElementMaker(annotate=False)
                img_annotation = objectify.fromstring(read_content(ANN_DIR + target_name + ".xml"))
            else:
                E = objectify.ElementMaker(annotate=False)
                img_annotation = E.annotation(
                    E.folder(TYPEPREFIX),
                    E.filename(target_name),
                    E.source(
                        E.database('coco_cattle'),
                    ),
                    E.size(
                        E.width(image_file.shape[1]),
                        E.height(image_file.shape[0]),
                        E.depth(3),
                    ),
                    E.segmented(0)
                )

            last_name = target_name
        objectNode = E.object(
            E.name(str(category_id)),
            E.pose("Unspecified"),
            E.truncated("0"),
            E.difficult("0"),
            E.bndbox(
                E.xmin(str(int(float(box[0]) * image_file.shape[1]))),
                E.ymin(str(int(float(box[1]) * image_file.shape[0]))),
                E.xmax(str(int(float(box[2]) * image_file.shape[1]))),
                E.ymax(str(int(float(box[3]) * image_file.shape[0]))),
            ),
        )
        img_annotation.append(objectNode)
        line_i += 1
Exemplo n.º 21
0
def rotate(cx, cy, w, h, angle_in_degrees):
    alpha = math.radians(angle_in_degrees)
    w1 = w * math.cos(alpha) - h * math.sin(alpha)
    h1 = w * math.sin(alpha) + h * math.cos(alpha)
    return w1, h1


for fn_jpg in file_helper.enumerate_files(read_dir, wildcard_pattern="*.jpg"):
    h0, w0 = image_helper.image_h_w(cv2.imread(fn_jpg))
    lines = []

    for suffix, class_id in suffix_class_id:
        fn0 = fn_jpg.replace(".JPG", suffix)
        if os.path.isfile(fn0):
            for line in file_helper.read_lines(fn0):
                line = line.replace("\t", " ").replace("  ", " ").replace(
                    "  ", " ").replace("  ", " ").replace("  ", " ")
                arr0 = line.split(" ")
                arr = []
                if len(arr0) > 0:
                    first_char = arr0[0][0]
                    if first_char not in ["#", "@"]:
                        arr.append(class_id)

                        angle_in_degrees = float(arr0[6])

                        # mult_h = math.sin(math.radians(angle))
                        # if abs(mult_h) < 0.001:
                        #     mult_h = 1.0
                        # else: