示例#1
0
def crop_rect(mat, cx_norm, cy_norm, w_norm, h_norm, crop_size):
    img_h0, img_w0 = image_helper.image_h_w(mat)
    cx0 = cx_norm * img_w0
    cy0 = cy_norm * img_h0
    w0 = w_norm * img_w0
    h0 = h_norm * img_h0

    r0_x1 = int(min(max(0, cx0 - w0 * 0.5), img_w0))
    r0_y1 = int(min(max(0, cy0 - h0 * 0.5), img_h0))
    # r0_x2 = int(min(max(0, cx0 + w0 * 0.5), img_w0))
    # r0_y2 = int(min(max(0, cy0 + h0 * 0.5), img_h0))

    crop_w = crop_size[0] * 0.5
    crop_h = crop_size[1] * 0.5
    crop_x1 = int(min(max(0, cx0 - crop_w), img_w0))
    crop_y1 = int(min(max(0, cy0 - crop_h), img_h0))
    crop_x2 = int(min(max(0, cx0 + crop_w), img_w0))
    crop_y2 = int(min(max(0, cy0 + crop_h), img_h0))

    img = image_helper.crop(mat, [crop_y1, crop_x1, crop_y2, crop_x2])
    img_h1, img_w1 = image_helper.image_h_w(img)

    r1_x1 = min(max(r0_x1 - crop_x1, 0), img_w1)
    r1_y1 = min(max(r0_y1 - crop_y1, 0), img_h1)
    r1_x2 = min(r1_x1 + w0, img_w1)
    r1_y2 = min(r1_y1 + h0, img_h1)

    w = (r1_x2 - r1_x1)
    h = (r1_y2 - r1_y1)
    cx = r1_x1 + w * 0.5
    cy = r1_y1 + h * 0.5

    return img, cx / img_w1, cy / img_h1, w / img_w1, h / img_h1
示例#2
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)))
示例#3
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)
示例#4
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)))
示例#5
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)))
示例#6
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")
示例#7
0
                   ("_truck.samp", classes.index("truck")),
                   ("_truck_trail.samp", classes.index("lorry")),
                   ("_van_trailer.samp", classes.index("van")),
                   ("_cam.samp", classes.index("airplane")),
                   ("_pkw_trail.samp", classes.index("boat"))]


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)