Spaces:
Paused
Paused
Download python_files/verify_yolo_v2_boxes.py from Rachel7/hvac_fable: direct link, hf CLI and curl.
- Browser
- Download file 1.69 kB
-
https://huggingface.co/spaces/Rachel7/hvac_fable/resolve/main/python_files/verify_yolo_v2_boxes.py
- Command line
-
hf download hf://spaces/Rachel7/hvac_fable/python_files/verify_yolo_v2_boxes.py
-
curl -L -o verify_yolo_v2_boxes.py https://huggingface.co/spaces/Rachel7/hvac_fable/resolve/main/python_files/verify_yolo_v2_boxes.py
1.69 kB
| """Draw YOLO v2 annotations on the cellar image to verify.""" | |
| from pathlib import Path | |
| from PIL import Image, ImageDraw, ImageFont | |
| ROOT = Path(__file__).resolve().parent | |
| V2_DIR = ROOT / "yolo_v2_data" | |
| RAW_DIR = V2_DIR / "raw" | |
| EXTRACT_DIR = V2_DIR / "extracted" | |
| image_files = list(RAW_DIR.glob("*.jpg")) + list(RAW_DIR.glob("*.png")) | |
| label_files = [f for f in EXTRACT_DIR.rglob("*.txt") if f.name != "classes.txt"] | |
| img_path = image_files[0] | |
| lbl_path = label_files[0] | |
| img = Image.open(img_path).convert("RGB") | |
| W, H = img.size | |
| print(f"Image size: {W} x {H}") | |
| draw = ImageDraw.Draw(img) | |
| font = ImageFont.load_default() | |
| colors = { | |
| "0": (0, 255, 0), # ceiling_ducted = green | |
| "1": (255, 100, 0), # wall_mounted = orange | |
| } | |
| names = { | |
| "0": "ceiling_ducted", | |
| "1": "wall_mounted", | |
| } | |
| count_per_class = {"0": 0, "1": 0} | |
| with open(lbl_path) as f: | |
| for idx, line in enumerate(f, 1): | |
| parts = line.strip().split() | |
| if len(parts) != 5: | |
| continue | |
| cls, xc, yc, w, h = parts | |
| count_per_class[cls] += 1 | |
| xc, yc, w, h = float(xc), float(yc), float(w), float(h) | |
| x1 = int((xc - w / 2) * W) | |
| y1 = int((yc - h / 2) * H) | |
| x2 = int((xc + w / 2) * W) | |
| y2 = int((yc + h / 2) * H) | |
| color = colors[cls] | |
| draw.rectangle([x1, y1, x2, y2], outline=color, width=4) | |
| label = f"{idx}.{names[cls][0:3]}" | |
| draw.text((x1, y1 - 15), label, fill=color, font=font) | |
| out_path = V2_DIR / "annotations_verified.jpg" | |
| img.save(out_path, quality=80) | |
| print(f"Saved verification: {out_path}") | |
| print(f"Ceiling ducted (green): {count_per_class['0']}") | |
| print(f"Wall mounted (orange): {count_per_class['1']}") | |