Open JY132 opened 1 year ago
I have solved the problem:
def yolo_line_to_shape(img_size, x_center, y_center, w, h): x_min = max(float(x_center) - float(w) / 2, 0) x_max = min(float(x_center) + float(w) / 2, 1) y_min = max(float(y_center) - float(h) / 2, 0) y_max = min(float(y_center) + float(h) / 2, 1) x_min = round(img_size[1] x_min) x_max = round(img_size[1] x_max) y_min = round(img_size[0] y_min) y_max = round(img_size[0] y_max) return x_min, y_min, x_max, y_max
image = Image.open(input_directory+img_name)
img_size = [image.height, image.width]
x_min, y_min, x_max, y_max = yolo_line_to_shape(img_size, x_center, y_center, w, h) points = [(x_min, y_min), (x_max, y_min), (x_max, y_max), (x_min, y_max)]
x1 = points[0][0] x2 = points[0][1] y1 = points[2][0] y2 = points[2][1]
cropped_img = image.crop((x1, x2, y1, y2))
I want to crop the labelling images and save it to other directory. However, I have no clue how the points is coordinates in the text file. Example: 0 0.440234 0.415278 0.255469 0.650000 I know the first one is label and others are the scaled coordinates. However, I don't know the calculation of scaling, then I could not get the original coordinates and crop it using PIL Image.
Thanks a lot if anyone gives some advice.