import cv2
import numpy as np
from glob import glob
import os.path as osp

out_base = "./coco"

def image_resize(image, width = None, height = None, inter = cv2.INTER_LINEAR):
    # initialize the dimensions of the image to be resized and
    # grab the image size
    dim = None
    (h, w) = image.shape[:2]

    # if both the width and height are None, then return the
    # original image
    if width is None and height is None:
        return image

    # check to see if the width is None
    if width is None:
        # calculate the ratio of the height and construct the
        # dimensions
        r = height / float(h)
        dim = (int(w * r), height)

    # otherwise, the height is None
    else:
        # calculate the ratio of the width and construct the
        # dimensions
        r = width / float(w)
        dim = (width, int(h * r))

    # resize the image
    resized = cv2.resize(image, dim, interpolation = inter)

    # return the resized image
    return resized

def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleFill=False, scaleup=True, stride=32):
    # Resize and pad image while meeting stride-multiple constraints
    shape = im.shape[:2]  # current shape [height, width]
    if isinstance(new_shape, int):
        new_shape = (new_shape, new_shape)

    # Scale ratio (new / old)
    r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])
    if not scaleup:  # only scale down, do not scale up (for better val mAP)
        r = min(r, 1.0)

    # Compute padding
    ratio = r, r  # width, height ratios
    new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))
    dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1]  # wh padding
    if auto:  # minimum rectangle
        dw, dh = np.mod(dw, stride), np.mod(dh, stride)  # wh padding
    elif scaleFill:  # stretch
        dw, dh = 0.0, 0.0
        new_unpad = (new_shape[1], new_shape[0])
        ratio = new_shape[1] / shape[1], new_shape[0] / shape[0]  # width, height ratios

    dw /= 2  # divide padding into 2 sides
    dh /= 2

    if shape[::-1] != new_unpad:  # resize
        im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR)
    top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))
    left, right = int(round(dw - 0.1)), int(round(dw + 0.1))
    im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)  # add border
    return im, ratio, (dw, dh)

img_path_list = glob(osp.join(r"D:\Deep_Learning\yolov5\whitelines\train\images", "*.*"))
label_path_list = glob(osp.join(r"D:\Deep_Learning\yolov5\whitelines\train\labels", "*.*"))

for img_path, label_path in zip(img_path_list, label_path_list):
    if img_path == 'D:\\Deep_Learning\\yolov5\\whitelines\\train\\images\\501.png':
        print(1)
    original = cv2.imread(img_path)
    original_h, original_w, _ = original.shape
    if original_h < original_w:
        original = image_resize(original, width=640)
    else:
        original = image_resize(original, height=640)
    original_h, original_w, _ = original.shape
    img = letterbox(original, stride=32, auto=False)[0]
    out_h, out_w, _ = img.shape
    encoded = []
    with open(label_path, "r") as f:
        src_labels = f.readlines()
        for src_label in src_labels:
            class_id, x_c, y_c, x_w, y_w = src_label.replace("\n", "").split(" ")
            class_id = int(class_id)
            x_c, y_c, x_w, y_w = float(x_c), float(y_c), float(x_w), float(y_w)
            # x_sc, y_sc, x_sw, y_sw = int(x_c * original_w), int(y_c * original_h), int(x_w * original_w), int(y_w * original_h)
            # cv2.rectangle(original, (x_sc - (x_sw // 2), y_sc - (y_sw // 2)), (x_sc + (x_sw // 2), y_sc + (y_sw // 2)), (100, 200, 100), 3)
            x_offset, y_offset = (out_w - original_w) // 2, (out_h - original_h) // 2
            new_x_c, new_y_c = (x_c * original_w + x_offset) / 640, (y_c * original_h + y_offset) / 640
            new_x_w, new_y_w = (x_w * original_w) / 640, (y_w * original_h) / 640
            encoded.append(f"{class_id} {new_x_c} {new_y_c} {new_x_w} {new_y_w}\n")
    #         x_sc, y_sc, x_sw, y_sw = int(new_x_c * 640), int(new_y_c * 640), int(new_x_w * 640), int(new_y_w * 640)
    #         cv2.rectangle(img, (x_sc - (x_sw // 2), y_sc - (y_sw // 2)), (x_sc + (x_sw // 2), y_sc + (y_sw // 2)),
    #                       (100, 200, 100), 3)
    # cv2.imshow("test", img)
    # cv2.waitKey(10)
    img_base, label_base = osp.basename(img_path), osp.basename(label_path)
    cv2.imwrite(f"./coco/img/{img_base.split('.')[0]}.jpg", img)
    with open(f"./coco/label/{label_base}", "w") as f:
        for item in encoded:
            f.write(item)
