Update square_crop.py
Browse files- square_crop.py +10 -38
square_crop.py
CHANGED
@@ -4,38 +4,15 @@ square_crop.py
|
|
4 |
|
5 |
Crop each COCO bounding box to the smallest square that contains it.
|
6 |
|
7 |
-
Usage
|
8 |
-
-----
|
9 |
-
python square_crop.py --images_dir <IMG_DIR> \
|
10 |
-
--coco_json <COCO_JSON> \
|
11 |
-
--output_dir <OUT_DIR>
|
12 |
-
|
13 |
-
Dependencies
|
14 |
-
------------
|
15 |
-
pip install pillow pycocotools tqdm
|
16 |
"""
|
17 |
import argparse
|
18 |
import json
|
19 |
from pathlib import Path
|
|
|
20 |
|
21 |
from PIL import Image, ImageOps
|
22 |
from tqdm import tqdm
|
23 |
|
24 |
-
|
25 |
-
def parse_args():
|
26 |
-
parser = argparse.ArgumentParser(
|
27 |
-
description="Crop COCO bounding boxes to squares.")
|
28 |
-
parser.add_argument("--images_dir", required=True, type=Path,
|
29 |
-
help="Directory containing the source images.")
|
30 |
-
parser.add_argument("--coco_json", required=True, type=Path,
|
31 |
-
help="Path to the COCO annotation file (JSON).")
|
32 |
-
parser.add_argument("--output_dir", required=True, type=Path,
|
33 |
-
help="Directory to write the cropped images.")
|
34 |
-
parser.add_argument("--pad_color", default=0, type=int,
|
35 |
-
help="Gray-scale padding color 0–255 (default 0 = black).")
|
36 |
-
return parser.parse_args()
|
37 |
-
|
38 |
-
|
39 |
def load_coco(json_path):
|
40 |
with open(json_path, "r") as f:
|
41 |
coco = json.load(f)
|
@@ -84,11 +61,8 @@ def crop_annotation(img_path, ann, out_dir, pad_color=0):
|
|
84 |
crop.save(out_dir / out_name)
|
85 |
|
86 |
|
87 |
-
def
|
88 |
-
|
89 |
-
args.output_dir.mkdir(parents=True, exist_ok=True)
|
90 |
-
|
91 |
-
id2fname, annotations = load_coco(args.coco_json)
|
92 |
|
93 |
# Group annotations by image for efficient loading
|
94 |
im2anns = {}
|
@@ -96,14 +70,12 @@ def main():
|
|
96 |
im2anns.setdefault(ann["image_id"], []).append(ann)
|
97 |
|
98 |
for img_id, anns in tqdm(im2anns.items(), desc="Processing images"):
|
99 |
-
img_path =
|
100 |
-
if not img_path.is_file():
|
101 |
-
print(f"Warning: image {img_path} not found — skipping.")
|
102 |
-
continue
|
103 |
-
for ann in anns:
|
104 |
-
crop_annotation(img_path, ann, args.output_dir,
|
105 |
-
pad_color=args.pad_color)
|
106 |
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
-
|
109 |
-
main()
|
|
|
4 |
|
5 |
Crop each COCO bounding box to the smallest square that contains it.
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
"""
|
8 |
import argparse
|
9 |
import json
|
10 |
from pathlib import Path
|
11 |
+
import os
|
12 |
|
13 |
from PIL import Image, ImageOps
|
14 |
from tqdm import tqdm
|
15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
def load_coco(json_path):
|
17 |
with open(json_path, "r") as f:
|
18 |
coco = json.load(f)
|
|
|
61 |
crop.save(out_dir / out_name)
|
62 |
|
63 |
|
64 |
+
def run_square_crop(input_dir, coco_json_path, cropped_dir):
|
65 |
+
id2fname, annotations = load_coco(coco_json_path)
|
|
|
|
|
|
|
66 |
|
67 |
# Group annotations by image for efficient loading
|
68 |
im2anns = {}
|
|
|
70 |
im2anns.setdefault(ann["image_id"], []).append(ann)
|
71 |
|
72 |
for img_id, anns in tqdm(im2anns.items(), desc="Processing images"):
|
73 |
+
img_path = os.path.join(input_dir, id2fname[img_id])
|
|
|
|
|
|
|
|
|
|
|
|
|
74 |
|
75 |
+
for ann in anns:
|
76 |
+
try:
|
77 |
+
crop_annotation(img_path, ann, cropped_dir)
|
78 |
+
except error:
|
79 |
+
print(f"Error on {img_path}.")
|
80 |
|
81 |
+
return os.listdir(cropped_dir)
|
|