cuibinge commited on
Commit
cd57a6b
·
verified ·
1 Parent(s): a94f255

Remove bbox detection route artifacts

Browse files
Files changed (1) hide show
  1. scripts/visualize_yolo_errors.py +0 -92
scripts/visualize_yolo_errors.py DELETED
@@ -1,92 +0,0 @@
1
- """Create a contact sheet for YOLO error cases with GT and prediction boxes."""
2
-
3
- from __future__ import annotations
4
-
5
- import argparse
6
- import json
7
- from pathlib import Path
8
-
9
- from PIL import Image, ImageDraw, ImageFont
10
-
11
- from evaluate_yolo_detection import label_for_image, read_label
12
-
13
-
14
- def parse_args() -> argparse.Namespace:
15
- parser = argparse.ArgumentParser(description=__doc__)
16
- parser.add_argument("--weights", required=True)
17
- parser.add_argument("--metrics-json", required=True)
18
- parser.add_argument("--output", required=True)
19
- parser.add_argument("--imgsz", type=int, default=640)
20
- parser.add_argument("--conf", type=float, default=0.25)
21
- parser.add_argument("--device", default="0")
22
- parser.add_argument("--limit", type=int, default=12)
23
- parser.add_argument("--thumb", type=int, default=360)
24
- return parser.parse_args()
25
-
26
-
27
- def draw_scaled_box(draw: ImageDraw.ImageDraw, box: list[float], scale: float, color: str, width: int = 3) -> None:
28
- coords = [box[0] * scale, box[1] * scale, box[2] * scale, box[3] * scale]
29
- draw.rectangle(coords, outline=color, width=width)
30
-
31
-
32
- def main() -> None:
33
- args = parse_args()
34
- from ultralytics import YOLO
35
-
36
- metrics = json.loads(Path(args.metrics_json).read_text(encoding="utf-8"))
37
- error_images = sorted(
38
- metrics["error_images"],
39
- key=lambda item: int(item["false_negatives"]) + int(item["false_positives"]),
40
- reverse=True,
41
- )[: args.limit]
42
- model = YOLO(args.weights)
43
-
44
- tiles: list[Image.Image] = []
45
- for item in error_images:
46
- image_path = Path(str(item["image"]))
47
- image = Image.open(image_path).convert("RGB")
48
- width, height = image.size
49
- scale = args.thumb / max(width, height)
50
- tile_size = (int(width * scale), int(height * scale))
51
- tile = image.resize(tile_size)
52
- draw = ImageDraw.Draw(tile)
53
-
54
- gt_boxes = read_label(label_for_image(image_path), width, height)
55
- for box in gt_boxes:
56
- draw_scaled_box(draw, box, scale, "lime")
57
-
58
- result = model.predict(
59
- source=str(image_path),
60
- imgsz=args.imgsz,
61
- conf=args.conf,
62
- device=args.device,
63
- verbose=False,
64
- )[0]
65
- pred_boxes = result.boxes.xyxy.cpu().tolist() if result.boxes is not None else []
66
- for box in pred_boxes:
67
- draw_scaled_box(draw, box, scale, "red")
68
-
69
- caption = f"FN {item['false_negatives']} FP {item['false_positives']} {image_path.name}"
70
- caption_h = 34
71
- panel = Image.new("RGB", (args.thumb, args.thumb + caption_h), "white")
72
- panel.paste(tile, ((args.thumb - tile.width) // 2, 0))
73
- panel_draw = ImageDraw.Draw(panel)
74
- panel_draw.text((6, args.thumb + 8), caption[:70], fill="black", font=ImageFont.load_default())
75
- tiles.append(panel)
76
-
77
- cols = 3
78
- rows = (len(tiles) + cols - 1) // cols
79
- sheet = Image.new("RGB", (cols * args.thumb, rows * (args.thumb + 34)), "white")
80
- for idx, tile in enumerate(tiles):
81
- x = (idx % cols) * args.thumb
82
- y = (idx // cols) * (args.thumb + 34)
83
- sheet.paste(tile, (x, y))
84
-
85
- output = Path(args.output)
86
- output.parent.mkdir(parents=True, exist_ok=True)
87
- sheet.save(output)
88
- print(output)
89
-
90
-
91
- if __name__ == "__main__":
92
- main()