-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbatch_annotate.py
More file actions
241 lines (200 loc) · 8.77 KB
/
Copy pathbatch_annotate.py
File metadata and controls
241 lines (200 loc) · 8.77 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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
"""
batch_annotate.py — Annotate a folder of images using the Gemma API.
Reads every image in --input, calls /v1/chat/completions concurrently,
and appends one JSON record per image to a .jsonl output file.
Supports resuming: images already present in the output file are skipped.
Usage examples
--------------
# Basic — annotate all images in ./images, save to annotations.jsonl
python batch_annotate.py --input ./images
# Custom output file and concurrency
python batch_annotate.py --input ./images --output results.jsonl --concurrency 8
# Custom annotation schema via system prompt
python batch_annotate.py --input ./images --prompt "Return JSON with keys: label, defects (list), severity (low/medium/high)."
# Call a remote server instead of localhost
python batch_annotate.py --input ./images --api-url http://your-server:8000/v1/chat/completions
"""
import argparse
import asyncio
import base64
import json
import re
import sys
from pathlib import Path
import httpx
from tqdm.asyncio import tqdm as atqdm
# ── Constants ──────────────────────────────────────────────────────────────
IMAGE_EXTENSIONS = {".jpg", ".jpeg", ".png", ".gif", ".webp", ".bmp"}
MIME_MAP = {
".jpg": "image/jpeg", ".jpeg": "image/jpeg",
".png": "image/png", ".gif": "image/gif",
".webp": "image/webp", ".bmp": "image/bmp",
}
DEFAULT_SYSTEM_PROMPT = """\
You are an image annotation assistant.
For each image, return ONLY a JSON object with these fields — no extra text:
{
"label": "<short category, e.g. 'cat', 'street scene', 'product'>",
"description": "<1–2 sentence description of the image>",
"objects": ["<main objects or elements visible>"],
"colors": ["<dominant colors>"],
"confidence": "<high | medium | low>"
}"""
DEFAULT_API_URL = "http://localhost:8000/v1/chat/completions"
DEFAULT_MODEL = "gemma-4-e4b"
DEFAULT_CONCURRENCY = 5
DEFAULT_MAX_RETRIES = 3
DEFAULT_TEMPERATURE = 0.2 # low for consistent/deterministic annotations
DEFAULT_MAX_TOKENS = 512
# ── Helpers ────────────────────────────────────────────────────────────────
def to_data_url(path: Path) -> str:
mime = MIME_MAP.get(path.suffix.lower(), "image/jpeg")
b64 = base64.b64encode(path.read_bytes()).decode()
return f"data:{mime};base64,{b64}"
def extract_json(text: str) -> dict:
"""Parse JSON from model output, handling extra prose around it."""
text = text.strip()
try:
return json.loads(text)
except json.JSONDecodeError:
pass
match = re.search(r"\{[\s\S]*\}", text)
if match:
try:
return json.loads(match.group())
except json.JSONDecodeError:
pass
return {"raw_response": text}
# ── Core annotation logic ──────────────────────────────────────────────────
async def annotate_image(
client: httpx.AsyncClient,
image_path: Path,
system_prompt: str,
api_url: str,
model: str,
semaphore: asyncio.Semaphore,
max_retries: int,
) -> dict:
payload = {
"model": model,
"messages": [
{"role": "system", "content": system_prompt},
{
"role": "user",
"content": [
{"type": "image_url", "image_url": {"url": to_data_url(image_path)}},
],
},
],
"temperature": DEFAULT_TEMPERATURE,
"max_tokens": DEFAULT_MAX_TOKENS,
"stream": False,
}
async with semaphore:
for attempt in range(max_retries):
try:
resp = await client.post(api_url, json=payload)
resp.raise_for_status()
content = resp.json()["choices"][0]["message"]["content"]
return {
"filename": image_path.name,
"path": str(image_path),
"status": "ok",
"annotation": extract_json(content),
}
except Exception as exc:
if attempt == max_retries - 1:
return {
"filename": image_path.name,
"path": str(image_path),
"status": "error",
"error": str(exc),
}
await asyncio.sleep(2 ** attempt) # exponential backoff
# ── Main ───────────────────────────────────────────────────────────────────
async def run(args: argparse.Namespace) -> None:
input_dir = Path(args.input)
output_path = Path(args.output)
if not input_dir.is_dir():
print(f"Error: '{input_dir}' is not a directory.")
sys.exit(1)
# Collect images
all_images = sorted(
p for p in input_dir.rglob("*")
if p.suffix.lower() in IMAGE_EXTENSIONS
)
if not all_images:
print(f"No images found in '{input_dir}'.")
sys.exit(1)
# Resume: skip images already recorded in the output file
already_done: set[str] = set()
if output_path.exists():
with open(output_path, encoding="utf-8") as f:
for line in f:
try:
already_done.add(json.loads(line)["filename"])
except (json.JSONDecodeError, KeyError):
pass
remaining = [img for img in all_images if img.name not in already_done]
print("\n" + "=" * 50)
print(" Gemma Batch Annotator")
print("=" * 50)
print(f" Input folder : {input_dir}")
print(f" Output file : {output_path}")
print(f" Total images : {len(all_images)}")
print(f" Already done : {len(already_done)}")
print(f" To process : {len(remaining)}")
print(f" Concurrency : {args.concurrency}")
print(f" API URL : {args.api_url}")
print("=" * 50 + "\n")
if not remaining:
print("All images are already annotated. Nothing to do.")
return
semaphore = asyncio.Semaphore(args.concurrency)
timeout = httpx.Timeout(connect=30.0, read=120.0, write=30.0, pool=30.0)
ok = err = 0
with open(output_path, "a", encoding="utf-8") as out_f:
async with httpx.AsyncClient(timeout=timeout) as client:
tasks = [
annotate_image(
client, img, args.prompt, args.api_url,
args.model, semaphore, args.retries,
)
for img in remaining
]
async for result in atqdm.as_completed(
tasks, total=len(remaining), desc="Annotating", unit="img"
):
out_f.write(json.dumps(result) + "\n")
out_f.flush() # write each result immediately
if result["status"] == "ok":
ok += 1
else:
err += 1
print(f"\n ✗ {result['filename']}: {result.get('error', '?')}")
print(f"\nFinished — {ok} succeeded, {err} failed.")
print(f"Results saved to: {output_path}\n")
def main() -> None:
parser = argparse.ArgumentParser(
description="Batch-annotate images using the Gemma OpenAI-compatible API.",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=__doc__,
)
parser.add_argument("--input", required=True,
help="Folder of images to annotate (searched recursively)")
parser.add_argument("--output", default="annotations.jsonl",
help="Output file (JSONL, one record per image). Default: annotations.jsonl")
parser.add_argument("--prompt", default=DEFAULT_SYSTEM_PROMPT,
help="System prompt defining the annotation schema")
parser.add_argument("--api-url", default=DEFAULT_API_URL,
help=f"Chat completions endpoint. Default: {DEFAULT_API_URL}")
parser.add_argument("--model", default=DEFAULT_MODEL,
help=f"Model name. Default: {DEFAULT_MODEL}")
parser.add_argument("--concurrency", type=int, default=DEFAULT_CONCURRENCY,
help=f"Max parallel requests (Modal spins up one GPU container per request). Default: {DEFAULT_CONCURRENCY}")
parser.add_argument("--retries", type=int, default=DEFAULT_MAX_RETRIES,
help=f"Max retries per image on failure. Default: {DEFAULT_MAX_RETRIES}")
args = parser.parse_args()
asyncio.run(run(args))
if __name__ == "__main__":
main()