HumanSignal / labelImg

LabelImg is now part of the Label Studio community. The popular image annotation tool created by Tzutalin is no longer actively being developed, but you can check out Label Studio, the open source data labeling tool for images, text, hypertext, audio, video and time-series data.
https://youtu.be/p0nR2YsCY_U
MIT License
22.19k stars 6.23k forks source link

Is there any way to crop the image using the labelled data #955

Open JY132 opened 1 year ago

JY132 commented 1 year ago

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.

JY132 commented 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

Opens a image in RGB mode

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))