Spaces:
Sleeping
Sleeping
d142111015
commited on
Commit
•
4b90019
1
Parent(s):
9393d45
Create crop_breast_region.py
Browse files- crop_breast_region.py +28 -0
crop_breast_region.py
ADDED
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from PIL import Image
|
3 |
+
path = "/home/user/app/image_folder"
|
4 |
+
crop_dst = '/home/user/app/cropped_images'
|
5 |
+
for img_name in os.listdir(path):
|
6 |
+
image = Image.open(os.path.join(path,img_name))
|
7 |
+
# Get the original image dimensions
|
8 |
+
image_width, image_height = image.size
|
9 |
+
with open('runs/detect/exp/labels/'+img_name[:-4]+'.txt') as f:
|
10 |
+
for line in f:
|
11 |
+
b,x,y,width,height = line.split(' ')
|
12 |
+
x,y,width,height = float(x),float(y),float(width),float(height)
|
13 |
+
# Convert YOLO coordinates to absolute coordinates
|
14 |
+
abs_x = x * image_width
|
15 |
+
abs_y = y * image_height
|
16 |
+
abs_width = width * image_width
|
17 |
+
abs_height = height * image_height
|
18 |
+
|
19 |
+
# Calculate the region coordinates
|
20 |
+
xmin_crop = int(abs_x - abs_width/2)
|
21 |
+
ymin_crop = int(abs_y - abs_height/2)
|
22 |
+
xmax_crop = int(abs_x + abs_width/2)
|
23 |
+
ymax_crop = int(abs_y + abs_height/2)
|
24 |
+
|
25 |
+
# Extract the region using absolute coordinates
|
26 |
+
region = image.crop((xmin_crop, ymin_crop, xmax_crop, ymax_crop))
|
27 |
+
width_crop,height_crop = region.size
|
28 |
+
region.save(os.path.join(crop_dst,img_name))
|