Update square_crop.py
Browse files- square_crop.py +5 -5
square_crop.py
CHANGED
@@ -20,15 +20,15 @@ def load_coco(json_path):
|
|
20 |
return id2fname, coco["annotations"]
|
21 |
|
22 |
|
23 |
-
def square_from_bbox(
|
24 |
"""
|
25 |
Compute (left, top, side) of the smallest square fully containing the bbox.
|
26 |
The square is centred on the bbox; if it overflows the image, it is shifted
|
27 |
(but not resized) so it lies inside the image. Returns the final crop box
|
28 |
(left, top, side).
|
29 |
"""
|
30 |
-
side = max(
|
31 |
-
cx, cy =
|
32 |
left = int(round(cx - side / 2.0))
|
33 |
top = int(round(cy - side / 2.0))
|
34 |
|
@@ -41,8 +41,8 @@ def square_from_bbox(x, y, w, h, img_w, img_h):
|
|
41 |
def crop_annotation(img_path, ann, out_dir, pad_color=0):
|
42 |
with Image.open(img_path) as img:
|
43 |
img_w, img_h = img.size
|
44 |
-
|
45 |
-
left, top, side = square_from_bbox(
|
46 |
|
47 |
# Perform crop (may be smaller than 'side' at edges)
|
48 |
crop = img.crop((left, top, left + side, top + side))
|
|
|
20 |
return id2fname, coco["annotations"]
|
21 |
|
22 |
|
23 |
+
def square_from_bbox(x1, y1, x2, y2, img_w, img_h):
|
24 |
"""
|
25 |
Compute (left, top, side) of the smallest square fully containing the bbox.
|
26 |
The square is centred on the bbox; if it overflows the image, it is shifted
|
27 |
(but not resized) so it lies inside the image. Returns the final crop box
|
28 |
(left, top, side).
|
29 |
"""
|
30 |
+
side = max(x2-x1, y2-y1)
|
31 |
+
cx, cy = (x1+x2) / 2.0, (y1+y2) / 2.0
|
32 |
left = int(round(cx - side / 2.0))
|
33 |
top = int(round(cy - side / 2.0))
|
34 |
|
|
|
41 |
def crop_annotation(img_path, ann, out_dir, pad_color=0):
|
42 |
with Image.open(img_path) as img:
|
43 |
img_w, img_h = img.size
|
44 |
+
x1, y1, x2, y2 = ann["bbox"]
|
45 |
+
left, top, side = square_from_bbox(x1, y1, x2, y2, img_w, img_h)
|
46 |
|
47 |
# Perform crop (may be smaller than 'side' at edges)
|
48 |
crop = img.crop((left, top, left + side, top + side))
|