Repository navigation
Expand file tree
/
Copy pathannotation_csv.py
More file actions
167 lines (143 loc) · 5.15 KB
/
Copy pathannotation_csv.py
File metadata and controls
167 lines (143 loc) · 5.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
"""Load and save polygon / OBB annotation CSV files for the annotation viewer."""
from __future__ import annotations
import ast
import csv
import math
from dataclasses import dataclass, field
from pathlib import Path
from typing import Dict, Iterable, List, Optional, Sequence
from oriented_det import RBox
from hbb_to_obb import OrientedAnn, polygon_string, write_oriented_csv
_IMAGE_EXTENSIONS = {".png", ".jpg", ".jpeg", ".bmp", ".tif", ".tiff"}
@dataclass
class AnnotationRecord:
"""One editable oriented annotation row."""
ann_id: str
image_id: str
class_name: str
rbox: RBox
source: str = "manual"
difficult: int = 0
score: Optional[float] = None
match_iou: Optional[float] = None
extra: Dict[str, str] = field(default_factory=dict)
def to_oriented_ann(self) -> OrientedAnn:
return OrientedAnn(
ann_id=self.ann_id,
image_id=self.image_id,
class_name=self.class_name,
rbox=self.rbox,
source=self.source,
difficult=self.difficult,
score=self.score,
match_iou=self.match_iou,
)
def _rbox_from_row(row: dict) -> RBox:
geometry = (row.get("geometry") or "").strip()
if geometry:
pts = ast.literal_eval(geometry)
if pts and pts[0] == pts[-1]:
pts = pts[:-1]
return RBox.from_points(pts)
cx = float(row["cx"])
cy = float(row["cy"])
width = float(row["width"])
height = float(row["height"])
angle_deg = row.get("angle_deg", "0")
angle = math.radians(float(angle_deg)) if angle_deg != "" else 0.0
return RBox(cx, cy, width, height, angle)
def _parse_optional_float(value: str) -> Optional[float]:
value = (value or "").strip()
if not value:
return None
return float(value)
def load_annotation_csv(path: Path) -> Dict[str, List[AnnotationRecord]]:
"""Load annotations grouped by ``image_id``."""
by_image: Dict[str, List[AnnotationRecord]] = {}
with path.open(encoding="utf-8", newline="") as f:
reader = csv.DictReader(f)
if reader.fieldnames is None:
return {}
known = {
"id",
"image_id",
"class",
"geometry",
"cx",
"cy",
"width",
"height",
"angle_deg",
"source",
"score",
"match_iou",
"difficult",
}
for row in reader:
image_id = row.get("image_id", "").strip()
if not image_id:
continue
ann_id = str(row.get("id", "")).strip() or f"{image_id}:{len(by_image.get(image_id, []))}"
extra = {k: v for k, v in row.items() if k not in known and v}
rec = AnnotationRecord(
ann_id=ann_id,
image_id=image_id,
class_name=str(row.get("class", "object")).strip() or "object",
rbox=_rbox_from_row(row),
source=str(row.get("source", "") or "manual").strip() or "manual",
difficult=int(float(row.get("difficult") or 0)),
score=_parse_optional_float(row.get("score", "")),
match_iou=_parse_optional_float(row.get("match_iou", "")),
extra=extra,
)
by_image.setdefault(image_id, []).append(rec)
return by_image
def save_annotation_csv(path: Path, by_image: Dict[str, List[AnnotationRecord]]) -> None:
"""Write all annotations to a CSV file (sorted by image_id, stable row order)."""
rows: List[OrientedAnn] = []
for image_id in sorted(by_image):
for rec in by_image[image_id]:
rows.append(rec.to_oriented_ann())
write_oriented_csv(path, rows)
def list_images_from_annotations(
by_image: Dict[str, Sequence[AnnotationRecord]],
data_root: Path,
images_dir: str = "images",
) -> List[str]:
"""Build ordered image list: annotated images first, then unannotated scans."""
seen: set[str] = set()
ordered: List[str] = []
for image_id in sorted(by_image):
if image_id not in seen:
ordered.append(image_id)
seen.add(image_id)
images_path = data_root / images_dir
if images_path.is_dir():
for path in sorted(images_path.iterdir()):
if path.is_file() and path.suffix.lower() in _IMAGE_EXTENSIONS:
if path.name not in seen:
ordered.append(path.name)
seen.add(path.name)
return ordered
def next_ann_id(by_image: Dict[str, List[AnnotationRecord]]) -> str:
"""Return a new numeric string id one above the max existing id."""
max_id = 0
for recs in by_image.values():
for rec in recs:
try:
max_id = max(max_id, int(rec.ann_id))
except ValueError:
continue
return str(max_id + 1)
def image_path_for_id(
data_root: Path,
image_id: str,
images_dir: str = "images",
) -> Path:
direct = data_root / image_id
if direct.is_file():
return direct
under_images = data_root / images_dir / image_id
if under_images.is_file():
return under_images
return under_images