cv-benchkit 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,50 @@
1
+ """cv-benchkit: Computer-vision benchmark toolkit."""
2
+
3
+ from __future__ import annotations
4
+
5
+ __version__ = "0.1.0"
6
+
7
+ from cv_benchkit.converters import (
8
+ BBox,
9
+ COCOAnnotation,
10
+ COCODataset,
11
+ coco_to_voc,
12
+ coco_to_yolo,
13
+ cxcywh_to_xyxy,
14
+ normalized_to_xyxy,
15
+ voc_to_coco,
16
+ voc_to_yolo,
17
+ xywh_to_xyxy,
18
+ xyxy_to_cxcywh,
19
+ xyxy_to_normalized,
20
+ xyxy_to_xywh,
21
+ yolo_to_coco,
22
+ yolo_to_voc,
23
+ )
24
+ from cv_benchkit.metrics import (
25
+ compute_confusion_matrix,
26
+ compute_giou,
27
+ compute_iou,
28
+ )
29
+
30
+ __all__ = [
31
+ "BBox",
32
+ "COCOAnnotation",
33
+ "COCODataset",
34
+ "__version__",
35
+ "coco_to_voc",
36
+ "coco_to_yolo",
37
+ "compute_confusion_matrix",
38
+ "compute_giou",
39
+ "compute_iou",
40
+ "cxcywh_to_xyxy",
41
+ "normalized_to_xyxy",
42
+ "voc_to_coco",
43
+ "voc_to_yolo",
44
+ "xywh_to_xyxy",
45
+ "xyxy_to_cxcywh",
46
+ "xyxy_to_normalized",
47
+ "xyxy_to_xywh",
48
+ "yolo_to_coco",
49
+ "yolo_to_voc",
50
+ ]
cv_benchkit/cli.py ADDED
@@ -0,0 +1,182 @@
1
+ """CLI entry point for cv-benchkit.
2
+
3
+ Subcommands
4
+ -----------
5
+ - ``cv-benchkit convert`` — convert between dataset formats
6
+ - ``cv-benchkit validate`` — validate a COCO JSON file
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import argparse
12
+ import json
13
+ import sys
14
+ from collections.abc import Sequence
15
+ from pathlib import Path
16
+
17
+ from cv_benchkit.converters import (
18
+ coco_to_voc,
19
+ coco_to_yolo,
20
+ validate_coco_file,
21
+ voc_to_coco,
22
+ yolo_to_coco,
23
+ )
24
+
25
+
26
+ def _build_parser() -> argparse.ArgumentParser:
27
+ parser = argparse.ArgumentParser(
28
+ prog="cv-benchkit",
29
+ description="Computer-vision benchmark toolkit: convert and validate datasets.",
30
+ )
31
+ sub = parser.add_subparsers(dest="command", required=True)
32
+
33
+ # ── convert ──────────────────────────────────────────────────────────
34
+ convert_p = sub.add_parser("convert", help="Convert between dataset formats.")
35
+ convert_p.add_argument(
36
+ "--from",
37
+ dest="src_format",
38
+ required=True,
39
+ choices=["coco", "yolo", "voc"],
40
+ help="Source format.",
41
+ )
42
+ convert_p.add_argument(
43
+ "--to",
44
+ dest="dst_format",
45
+ required=True,
46
+ choices=["coco", "yolo", "voc"],
47
+ help="Target format.",
48
+ )
49
+ convert_p.add_argument(
50
+ "--input",
51
+ dest="input_path",
52
+ required=True,
53
+ help="Input file or directory.",
54
+ )
55
+ convert_p.add_argument(
56
+ "--output",
57
+ dest="output_path",
58
+ required=True,
59
+ help="Output directory.",
60
+ )
61
+ convert_p.add_argument(
62
+ "--image-width",
63
+ type=int,
64
+ default=None,
65
+ help="Image width (required for YOLO source).",
66
+ )
67
+ convert_p.add_argument(
68
+ "--image-height",
69
+ type=int,
70
+ default=None,
71
+ help="Image height (required for YOLO source).",
72
+ )
73
+ convert_p.add_argument(
74
+ "--categories",
75
+ nargs="+",
76
+ default=None,
77
+ help="Category names (required for YOLO/VOC source).",
78
+ )
79
+
80
+ # ── validate ─────────────────────────────────────────────────────────
81
+ validate_p = sub.add_parser("validate", help="Validate a COCO JSON file.")
82
+ validate_p.add_argument("file", help="Path to the COCO JSON file.")
83
+
84
+ return parser
85
+
86
+
87
+ def _cmd_convert(args: argparse.Namespace) -> int:
88
+ src = args.src_format
89
+ dst = args.dst_format
90
+ inp = Path(args.input_path)
91
+ out = Path(args.output_path)
92
+
93
+ if src == dst:
94
+ print(f"Error: source and target format are the same ({src}).", file=sys.stderr)
95
+ return 1
96
+
97
+ if src == "coco":
98
+ coco = validate_coco_file(inp)
99
+ paths = coco_to_yolo(coco, out) if dst == "yolo" else coco_to_voc(coco, out)
100
+ print(f"Wrote {len(paths)} files to {out}")
101
+ return 0
102
+
103
+ if src == "yolo":
104
+ cats = args.categories
105
+ if not cats:
106
+ print("Error: --categories required for YOLO source.", file=sys.stderr)
107
+ return 1
108
+ w = args.image_width
109
+ h = args.image_height
110
+ if not w or not h:
111
+ print(
112
+ "Error: --image-width and --image-height required for YOLO source.", file=sys.stderr
113
+ )
114
+ return 1
115
+ stems = [p.stem for p in inp.glob("*.txt")]
116
+ image_sizes = {s: (w, h) for s in stems}
117
+ coco = yolo_to_coco(inp, image_sizes, cats)
118
+ if dst == "coco":
119
+ out.mkdir(parents=True, exist_ok=True)
120
+ json_path = out / "annotations.json"
121
+ json_path.write_text(
122
+ json.dumps(coco.model_dump(), indent=2),
123
+ encoding="utf-8",
124
+ )
125
+ print(f"Wrote {json_path}")
126
+ else: # voc
127
+ paths = coco_to_voc(coco, out)
128
+ print(f"Wrote {len(paths)} files to {out}")
129
+ return 0
130
+
131
+ if src == "voc":
132
+ cats = args.categories
133
+ if not cats:
134
+ print("Error: --categories required for VOC source.", file=sys.stderr)
135
+ return 1
136
+ coco = voc_to_coco(inp, cats)
137
+ if dst == "coco":
138
+ out.mkdir(parents=True, exist_ok=True)
139
+ json_path = out / "annotations.json"
140
+ json_path.write_text(
141
+ json.dumps(coco.model_dump(), indent=2),
142
+ encoding="utf-8",
143
+ )
144
+ print(f"Wrote {json_path}")
145
+ else: # yolo
146
+ paths = coco_to_yolo(coco, out)
147
+ print(f"Wrote {len(paths)} files to {out}")
148
+ return 0
149
+
150
+ return 1 # pragma: no cover
151
+
152
+
153
+ def _cmd_validate(args: argparse.Namespace) -> int:
154
+ path = Path(args.file)
155
+ if not path.exists():
156
+ print(f"Error: file {path} not found.", file=sys.stderr)
157
+ return 1
158
+ try:
159
+ coco = validate_coco_file(path)
160
+ except Exception as exc: # noqa: BLE001
161
+ print(f"Validation FAILED: {exc}", file=sys.stderr)
162
+ return 1
163
+ msg = (
164
+ f"Valid COCO dataset: {len(coco.images)} images, "
165
+ f"{len(coco.annotations)} annotations, {len(coco.categories)} categories."
166
+ )
167
+ print(msg)
168
+ return 0
169
+
170
+
171
+ def main(argv: Sequence[str] | None = None) -> int:
172
+ """CLI entry point."""
173
+ parser = _build_parser()
174
+ args = parser.parse_args(argv)
175
+
176
+ if args.command == "convert":
177
+ return _cmd_convert(args)
178
+ if args.command == "validate":
179
+ return _cmd_validate(args)
180
+
181
+ parser.print_help()
182
+ return 1 # pragma: no cover