Kamlesh21 commited on
Commit
9df1f37
·
verified ·
1 Parent(s): 34771dc

Deploy Preprocessing Lab

Browse files
server.py CHANGED
@@ -1,436 +1,436 @@
1
- """Web server for the Image Preprocessing Lab.
2
-
3
- All image logic lives in image_lab.ipynb. This file runs that notebook (skipping cells tagged "demo") and
4
- serves its functions to the browser. It only adds web plumbing: a gallery, a cache, and image encoding.
5
-
6
- Run: uvicorn server:app --port 8000 (from the backend folder)
7
-
8
- Environment:
9
- ALLOWED_ORIGINS comma-separated origins allowed to call the API (default "*"), e.g. https://your-app.vercel.app
10
- """
11
-
12
- import ast
13
- import hashlib
14
- import json
15
- import os
16
- import re
17
- import threading
18
- import time
19
- from collections import OrderedDict
20
- from contextlib import asynccontextmanager
21
- from pathlib import Path
22
-
23
- import cv2
24
- import nbformat
25
- import numpy as np
26
- from fastapi import Body, FastAPI, File, HTTPException, UploadFile
27
- from fastapi.middleware.cors import CORSMiddleware
28
- from fastapi.responses import JSONResponse, Response
29
-
30
- HERE = Path(__file__).resolve().parent
31
- DATA = HERE / "data"
32
- UPLOADS = HERE / "uploads"
33
- MAX_UPLOAD_BYTES = 10 * 1024 * 1024
34
- UPLOAD_MAX_SIDE = 1024
35
-
36
-
37
- # ---------------------------------------------------------------- 1. load the notebook
38
-
39
- def load_notebook(path):
40
- """Run every non-demo code cell into one namespace; also keep each function's source text."""
41
- nb = nbformat.read(path, as_version=4)
42
- lab = {"__file__": str(path), "__name__": "image_lab"}
43
- sources = {}
44
- for i, cell in enumerate(nb.cells):
45
- if cell.cell_type != "code" or "demo" in cell.metadata.get("tags", []):
46
- continue
47
- exec(compile(cell.source, f"{path.name} (cell {i})", "exec"), lab)
48
- for node in ast.parse(cell.source).body:
49
- if isinstance(node, ast.FunctionDef):
50
- sources[node.name] = ast.get_source_segment(cell.source, node)
51
- return lab, sources
52
-
53
-
54
- LAB, SOURCES = load_notebook(HERE / "image_lab.ipynb")
55
- STEPS = LAB["STEPS"]
56
-
57
-
58
- def step_source(step_id):
59
- """The step function's source, preceded by any private helper it calls (e.g. _gray)."""
60
- main = SOURCES[step_id]
61
- helpers = [SOURCES[n] for n in dict.fromkeys(re.findall(r"\b(_[a-z]\w*)\(", main)) if n in SOURCES]
62
- return "\n\n\n".join([*helpers, main])
63
-
64
-
65
- def call_line(step_id, params):
66
- """The exact call the pipeline makes, e.g. img = rotate(img, angle=45)."""
67
- args = ["img", *(f"{k}={v!r}" for k, v in params.items())]
68
- return f"img = {step_id}({', '.join(args)})"
69
-
70
-
71
- # ---------------------------------------------------------------- 2. images
72
-
73
- def gallery():
74
- path = DATA / "manifest.json"
75
- return json.loads(path.read_text())["images"] if path.exists() else []
76
-
77
-
78
- def image_meta(image_id):
79
- if image_id.startswith("upload-") and (UPLOADS / f"{image_id[7:]}.png").exists():
80
- # Uploads are never listed in the gallery: only the person who uploaded one knows its id.
81
- return {"id": image_id, "use_case": "upload", "file": str(UPLOADS / f"{image_id[7:]}.png"),
82
- "title": "Your image", "license": "Your image", "source": "Uploaded"}
83
- for item in gallery():
84
- if item["id"] == image_id:
85
- return item
86
- raise HTTPException(404, f"No image called {image_id!r}")
87
-
88
-
89
- def image_path(image_id):
90
- file = Path(image_meta(image_id)["file"])
91
- return file if file.is_absolute() else DATA / file
92
-
93
-
94
- RAW = OrderedDict() # image id -> the BGR array from load_image
95
-
96
-
97
- def load(image_id):
98
- if image_id not in RAW:
99
- if not re.fullmatch(r"[\w-]+", image_id):
100
- raise HTTPException(404, "Bad image id")
101
- img = LAB["load_image"](image_path(image_id))
102
- img.setflags(write=False) # shared by every run: steps must return new arrays
103
- RAW[image_id] = img
104
- while len(RAW) > 64:
105
- RAW.popitem(last=False)
106
- return RAW[image_id]
107
-
108
-
109
- # ---------------------------------------------------------------- 3. turning arrays into pictures and numbers
110
-
111
- def is_chw(a):
112
- return a.ndim == 3 and a.shape[0] in (1, 3) and a.shape[2] not in (1, 3, 4)
113
-
114
-
115
- def to_display(a):
116
- """RGB or gray uint8 for the browser, plus a note when values had to be rescaled to be visible."""
117
- if is_chw(a):
118
- a = a.transpose(1, 2, 0)
119
- if a.dtype == np.uint8:
120
- return a, None
121
- lo, hi = float(a.min()), float(a.max())
122
- scaled = cv2.normalize(a.astype(np.float32), None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8)
123
- return scaled, f"{a.dtype} values {lo:.2f} to {hi:.2f}, stretched to 0..255 so you can see them"
124
-
125
-
126
- def stats(a):
127
- return {"shape": list(a.shape), "dtype": str(a.dtype), "min": round(float(a.min()), 3),
128
- "max": round(float(a.max()), 3),
129
- "layout": "CHW" if is_chw(a) else ("HW" if a.ndim == 2 else "HWC")}
130
-
131
-
132
- def encode(img, fmt="png", width=None):
133
- if width and img.shape[1] > width:
134
- img = cv2.resize(img, (width, round(img.shape[0] * width / img.shape[1])), interpolation=cv2.INTER_AREA)
135
- bgr = img[:, :, ::-1] if img.ndim == 3 else img
136
- ok, buf = cv2.imencode("." + fmt, np.ascontiguousarray(bgr), [cv2.IMWRITE_JPEG_QUALITY, 90] if fmt == "jpg" else [])
137
- return buf.tobytes()
138
-
139
-
140
- # ---------------------------------------------------------------- 4. running a line, with a cache
141
-
142
- # Each step result is cached under a key built from the image and every step up to it, so moving one
143
- # slider only recomputes that step and the ones after it.
144
- CACHE = OrderedDict() # key -> dict(array, error, ms)
145
- CACHE_LOCK = threading.Lock()
146
- MODELS = {}
147
- MODEL_LOCKS = {"resnet50": threading.Lock(), "yolo": threading.Lock()}
148
-
149
-
150
- def remember(key, entry):
151
- with CACHE_LOCK:
152
- CACHE[key] = entry
153
- while len(CACHE) > 600:
154
- CACHE.popitem(last=False) # drop the oldest
155
- return entry
156
-
157
-
158
- def recall(key):
159
- with CACHE_LOCK:
160
- if key in CACHE:
161
- CACHE.move_to_end(key)
162
- return CACHE[key]
163
- return None
164
-
165
-
166
- KEY_LOCKS = {} # cache key -> lock, while that result is being computed
167
-
168
-
169
- def once(key, compute):
170
- """Recall key, or compute it exactly once even when several requests ask at the same time.
171
-
172
- A slider drag sends many requests and an aborted fetch does not stop the server's work, so without this the
173
- same slow step (denoise takes about a second) could run several times in parallel."""
174
- entry = recall(key)
175
- if entry is not None:
176
- return entry
177
- with CACHE_LOCK:
178
- lock = KEY_LOCKS.setdefault(key, threading.Lock())
179
- with lock:
180
- try:
181
- entry = recall(key)
182
- return entry if entry is not None else remember(key, compute())
183
- finally:
184
- with CACHE_LOCK:
185
- KEY_LOCKS.pop(key, None)
186
-
187
-
188
- def with_model(name, fn):
189
- """Run fn(model) while holding that model's lock: one inference at a time per model."""
190
- with MODEL_LOCKS[name]:
191
- if name not in MODELS:
192
- MODELS[name] = LAB["load_resnet50"]() if name == "resnet50" else LAB["load_yolo"]()
193
- return fn(MODELS[name])
194
-
195
-
196
- def make_key(*parts):
197
- return hashlib.sha1("|".join(parts).encode()).hexdigest()[:16]
198
-
199
-
200
- def histogram(display, bins=64):
201
- """Brightness histogram of the displayed picture: `bins` counts over 0..255."""
202
- gray = cv2.cvtColor(display, cv2.COLOR_RGB2GRAY) if display.ndim == 3 and display.shape[2] == 3 else display
203
- if gray.ndim == 3:
204
- gray = gray[:, :, 0]
205
- counts = cv2.calcHist([np.ascontiguousarray(gray)], [0], None, [bins], [0, 256])
206
- return [int(c) for c in counts.ravel()]
207
-
208
-
209
- def describe(key, entry, **extra):
210
- if "hist" not in entry: # computed once per cached result
211
- display, note = to_display(entry["array"])
212
- entry["hist"], entry["note"] = histogram(display), note
213
- return {**extra, "key": key, "error": entry.get("error"), "ms": entry.get("ms", 0),
214
- "stats": stats(entry["array"]), "display_note": entry["note"], "hist": entry["hist"]}
215
-
216
-
217
- def run_steps(image_id, steps):
218
- """Run a line's steps through the cache. Returns (final key, final entry, one description per step)."""
219
- key = make_key("load", image_id)
220
- current = recall(key) or remember(key, {"array": load(image_id)})
221
- out = [describe(key, current, uid="load", op="load", name="Load image", kind="load", enabled=True, params={},
222
- call="img = load_image(path)")]
223
- for step in steps:
224
- step_id = step.get("op")
225
- if step_id not in STEPS:
226
- continue
227
- enabled = bool(step.get("enabled", True))
228
- params = LAB["clean_params"](step_id, step.get("params"))
229
- key = make_key(key, json.dumps([step_id, params, enabled], sort_keys=True))
230
- entry = once(key, lambda: compute_step(current, step_id, params, enabled))
231
- # uid and rev are echoed untouched so the app can tell which step, and which edit, a result belongs to.
232
- out.append(describe(key, entry, uid=step.get("uid"), rev=step.get("rev"), op=step_id,
233
- name=STEPS[step_id]["name"], kind=STEPS[step_id]["kind"], enabled=enabled, params=params,
234
- call=call_line(step_id, params)))
235
- current = entry
236
- return key, current, out
237
-
238
-
239
- def compute_step(current, step_id, params, enabled):
240
- if not enabled:
241
- return {"array": current["array"]}
242
- t0 = time.perf_counter()
243
- try:
244
- entry = {"array": np.asarray(LAB["run_step"](current["array"], step_id, params))}
245
- except Exception as exc: # show the error on the step; the image passes through unchanged
246
- msg = str(exc).strip().splitlines()[-1] if str(exc).strip() else ""
247
- entry = {"array": current["array"], "error": f"{type(exc).__name__}: {msg}"}
248
- entry["ms"] = round((time.perf_counter() - t0) * 1000, 1)
249
- return entry
250
-
251
-
252
- def clean_version(steps):
253
- """The same line with every preparation step on at its defaults and every break-it and fix-it step off."""
254
- return [{"op": s.get("op"), "enabled": STEPS[s["op"]]["kind"] == "prep"} for s in steps if s.get("op") in STEPS]
255
-
256
-
257
- def run_line(image_id, steps, task):
258
- key, final, out = run_steps(image_id, steps)
259
- result = run_task(task, key, final)
260
- if result is not None:
261
- bkey, bfinal, _ = run_steps(image_id, clean_version(steps))
262
- result["baseline"] = run_task(task, bkey, bfinal)
263
- if result["type"] == "classify":
264
- # How sure the model is NOW of what it answered on the clean image: this is what drops.
265
- base, pk = result["baseline"], result["probs_key"]
266
- result["baseline_label_now"] = None
267
- if base["top5"] and pk in CACHE_PROBS:
268
- label = base["top5"][0]["label"]
269
- result["baseline_label_now"] = round(float(CACHE_PROBS[pk][LAB["LABELS"].index(label)]), 4)
270
- return {"image": image_meta(image_id), "steps": out, "result": result}
271
-
272
-
273
- CACHE_PROBS = {} # probs key -> the 1000 class probabilities (small, kept apart from the image cache)
274
-
275
-
276
- def run_task(task, key, final):
277
- kind = (task or {}).get("type", "none")
278
- if kind == "classify":
279
- rkey = make_key(key, "classify")
280
- cached = recall(rkey)
281
- if cached is None:
282
- t0 = time.perf_counter()
283
- a = final["array"]
284
- result = {"type": "classify", "model": "ResNet50", "input_shape": [1, *a.shape], "input_dtype": str(a.dtype),
285
- "input_range": [round(float(a.min()), 2), round(float(a.max()), 2)], "probs_key": rkey,
286
- "error": None, "top5": []}
287
- try:
288
- probs = with_model("resnet50", lambda m: LAB["classify_resnet50"](a, m))
289
- CACHE_PROBS[rkey] = probs
290
- while len(CACHE_PROBS) > 2000:
291
- CACHE_PROBS.pop(next(iter(CACHE_PROBS)))
292
- result["top5"] = [{"label": label, "prob": p} for label, p in LAB["top_k"](probs, 5)]
293
- except Exception as exc: # e.g. channels last: PyTorch refuses, and that is the lesson
294
- msg = str(exc).strip().splitlines()[0] if str(exc).strip() else type(exc).__name__
295
- result["error"] = msg
296
- result["probs_key"] = None
297
- result["ms"] = round((time.perf_counter() - t0) * 1000, 1)
298
- cached = remember(rkey, {"array": a, "result": result})
299
- pk = cached["result"]["probs_key"]
300
- if pk is not None and pk not in CACHE_PROBS: # evicted: recompute on the next run
301
- with CACHE_LOCK:
302
- CACHE.pop(rkey, None)
303
- return run_task(task, key, final)
304
- return dict(cached["result"])
305
- if kind == "segment":
306
- conf = min(0.95, max(0.05, float(task.get("conf", 0.25))))
307
- rkey = make_key(key, "segment", f"{conf:.3f}")
308
- cached = recall(rkey)
309
- if cached is None:
310
- t0 = time.perf_counter()
311
- img = LAB["as_uint8_rgb"](final["array"])
312
- overlay, instances = with_model("yolo", lambda m: LAB["segment_yolo"](img, m, conf))
313
- counts = {}
314
- for name, _ in instances:
315
- counts[name] = counts.get(name, 0) + 1
316
- # class name -> the mask color segment_yolo drew, so the app's legend matches the picture
317
- colors = {n: "#%02x%02x%02x" % LAB["class_color"](i) for i, n in MODELS["yolo"].names.items()}
318
- cached = remember(rkey, {"array": overlay, "result": {
319
- "type": "segment", "model": "YOLO11s-seg", "conf": conf, "total": len(instances),
320
- "counts": dict(sorted(counts.items(), key=lambda kv: -kv[1])), "overlay_key": rkey,
321
- "colors": colors,
322
- "instances": [{"label": n, "conf": c} for n, c in instances], "input_shape": list(img.shape),
323
- "notes": [] if final["array"].dtype == np.uint8 else ["The pipeline output was converted to uint8 RGB first."],
324
- "ms": round((time.perf_counter() - t0) * 1000, 1)}})
325
- return dict(cached["result"])
326
- return None
327
-
328
-
329
- # ---------------------------------------------------------------- 5. HTTP routes
330
-
331
- def warm_up():
332
- """Load both models, then run every gallery image through its clean line, so first clicks are instant."""
333
- with_model("resnet50", lambda m: LAB["classify_resnet50"](np.zeros((3, 224, 224), np.float32), m))
334
- with_model("yolo", lambda m: LAB["segment_yolo"](np.zeros((640, 640, 3), np.uint8), m))
335
- lines = {line["id"]: line for line in LAB["LINES"]}
336
- for item in gallery():
337
- line = lines[item["use_case"]]
338
- try:
339
- run_line(item["id"], line["steps"], line["task"])
340
- except Exception as exc: # a missing file should not stop the server
341
- print("warm-up skipped", item["id"], exc)
342
- # The default photo is what the presenter breaks and fixes first: run every break, alone and with each fix
343
- # meant for it (denoise takes about a second), so those first clicks are instant too.
344
- for line in LAB["LINES"]:
345
- for s in line["steps"]:
346
- if STEPS.get(s["op"], {}).get("kind") != "break":
347
- continue
348
- fixes = [f for f, d in STEPS.items() if d["kind"] == "fix" and s["op"] in d["helps"]]
349
- for on in [{s["op"]}, *({s["op"], f} for f in fixes)]:
350
- steps = [{**x, "enabled": True} if x["op"] in on else x for x in line["steps"]]
351
- try:
352
- run_line(line["default_image"], steps, line["task"])
353
- except Exception as exc:
354
- print("warm-up skipped", line["id"], sorted(on), exc)
355
-
356
-
357
- @asynccontextmanager
358
- async def lifespan(_):
359
- threading.Thread(target=warm_up, daemon=True).start()
360
- yield
361
-
362
-
363
- app = FastAPI(title="Image Preprocessing Lab", lifespan=lifespan)
364
- app.add_middleware(CORSMiddleware, allow_origins=[o.strip() for o in os.environ.get("ALLOWED_ORIGINS", "*").split(",")],
365
- allow_methods=["GET", "POST"], allow_headers=["*"])
366
- FOREVER = {"Cache-Control": "public, max-age=31536000, immutable"}
367
-
368
-
369
- @app.get("/api/health")
370
- def health():
371
- return {"ok": True, "models_ready": len(MODELS) == 2}
372
-
373
-
374
- @app.get("/api/steps")
375
- def steps():
376
- return {
377
- "kinds": LAB["KINDS"],
378
- "steps": [{k: v for k, v in s.items() if k != "fn"} | {"source": step_source(s["id"])} for s in STEPS.values()],
379
- "load_source": SOURCES["load_image"],
380
- "model_sources": {"classify": "\n\n\n".join(SOURCES[n] for n in ("classify_resnet50", "top_k")),
381
- "segment": "\n\n\n".join(SOURCES[n] for n in ("draw_label", "segment_yolo"))},
382
- }
383
-
384
-
385
- @app.get("/api/lines")
386
- def lines():
387
- return {"lines": LAB["LINES"]}
388
-
389
-
390
- @app.get("/api/images")
391
- def images():
392
- return {"images": gallery()}
393
-
394
-
395
- @app.get("/api/images/{image_id}/thumb")
396
- def image_thumb(image_id: str):
397
- return Response(encode(load(image_id)[:, :, ::-1], "jpg", width=320), media_type="image/jpeg", headers=FOREVER)
398
-
399
-
400
- @app.post("/api/upload")
401
- async def upload(file: UploadFile = File(...)):
402
- data = await file.read(MAX_UPLOAD_BYTES + 1)
403
- if len(data) > MAX_UPLOAD_BYTES:
404
- raise HTTPException(413, "Images up to 10 MB, please.")
405
- arr = cv2.imdecode(np.frombuffer(data, np.uint8), cv2.IMREAD_COLOR)
406
- if arr is None:
407
- raise HTTPException(400, "That file is not an image OpenCV can read.")
408
- h, w = arr.shape[:2]
409
- s = UPLOAD_MAX_SIDE / max(h, w)
410
- if s < 1:
411
- arr = cv2.resize(arr, (round(w * s), round(h * s)), interpolation=cv2.INTER_AREA)
412
- UPLOADS.mkdir(exist_ok=True)
413
- digest = hashlib.sha1(data).hexdigest()[:12]
414
- cv2.imwrite(str(UPLOADS / f"{digest}.png"), arr)
415
- return {"id": f"upload-{digest}"}
416
-
417
-
418
- @app.post("/api/run")
419
- def run(body: dict = Body(...)):
420
- return run_line(str(body["image"]), body.get("steps", []), body.get("task", {}))
421
-
422
-
423
- @app.get("/api/img/{key}")
424
- def step_image(key: str, w: int | None = None):
425
- entry = recall(key)
426
- if entry is None:
427
- raise HTTPException(404, "Image expired from the cache; run the line again")
428
- display, _ = to_display(entry["array"])
429
- if w:
430
- return Response(encode(display, "jpg", width=min(w, 2048)), media_type="image/jpeg", headers=FOREVER)
431
- return Response(encode(display, "png"), media_type="image/png", headers=FOREVER)
432
-
433
-
434
- @app.exception_handler(Exception)
435
- def crash(_, exc):
436
- return JSONResponse({"detail": f"{type(exc).__name__}: {exc}"}, status_code=500)
 
1
+ """Web server for the Image Preprocessing Lab.
2
+
3
+ All image logic lives in image_lab.ipynb. This file runs that notebook (skipping cells tagged "demo") and
4
+ serves its functions to the browser. It only adds web plumbing: a gallery, a cache, and image encoding.
5
+
6
+ Run: uvicorn server:app --port 8000 (from the backend folder)
7
+
8
+ Environment:
9
+ ALLOWED_ORIGINS comma-separated origins allowed to call the API (default "*"), e.g. https://your-app.vercel.app
10
+ """
11
+
12
+ import ast
13
+ import hashlib
14
+ import json
15
+ import os
16
+ import re
17
+ import threading
18
+ import time
19
+ from collections import OrderedDict
20
+ from contextlib import asynccontextmanager
21
+ from pathlib import Path
22
+
23
+ import cv2
24
+ import nbformat
25
+ import numpy as np
26
+ from fastapi import Body, FastAPI, File, HTTPException, UploadFile
27
+ from fastapi.middleware.cors import CORSMiddleware
28
+ from fastapi.responses import JSONResponse, Response
29
+
30
+ HERE = Path(__file__).resolve().parent
31
+ DATA = HERE / "data"
32
+ UPLOADS = HERE / "uploads"
33
+ MAX_UPLOAD_BYTES = 10 * 1024 * 1024
34
+ UPLOAD_MAX_SIDE = 1024
35
+
36
+
37
+ # ---------------------------------------------------------------- 1. load the notebook
38
+
39
+ def load_notebook(path):
40
+ """Run every non-demo code cell into one namespace; also keep each function's source text."""
41
+ nb = nbformat.read(path, as_version=4)
42
+ lab = {"__file__": str(path), "__name__": "image_lab"}
43
+ sources = {}
44
+ for i, cell in enumerate(nb.cells):
45
+ if cell.cell_type != "code" or "demo" in cell.metadata.get("tags", []):
46
+ continue
47
+ exec(compile(cell.source, f"{path.name} (cell {i})", "exec"), lab)
48
+ for node in ast.parse(cell.source).body:
49
+ if isinstance(node, ast.FunctionDef):
50
+ sources[node.name] = ast.get_source_segment(cell.source, node)
51
+ return lab, sources
52
+
53
+
54
+ LAB, SOURCES = load_notebook(HERE / "image_lab.ipynb")
55
+ STEPS = LAB["STEPS"]
56
+
57
+
58
+ def step_source(step_id):
59
+ """The step function's source, preceded by any private helper it calls (e.g. _gray)."""
60
+ main = SOURCES[step_id]
61
+ helpers = [SOURCES[n] for n in dict.fromkeys(re.findall(r"\b(_[a-z]\w*)\(", main)) if n in SOURCES]
62
+ return "\n\n\n".join([*helpers, main])
63
+
64
+
65
+ def call_line(step_id, params):
66
+ """The exact call the pipeline makes, e.g. img = rotate(img, angle=45)."""
67
+ args = ["img", *(f"{k}={v!r}" for k, v in params.items())]
68
+ return f"img = {step_id}({', '.join(args)})"
69
+
70
+
71
+ # ---------------------------------------------------------------- 2. images
72
+
73
+ def gallery():
74
+ path = DATA / "manifest.json"
75
+ return json.loads(path.read_text())["images"] if path.exists() else []
76
+
77
+
78
+ def image_meta(image_id):
79
+ if image_id.startswith("upload-") and (UPLOADS / f"{image_id[7:]}.png").exists():
80
+ # Uploads are never listed in the gallery: only the person who uploaded one knows its id.
81
+ return {"id": image_id, "use_case": "upload", "file": str(UPLOADS / f"{image_id[7:]}.png"),
82
+ "title": "Your image", "license": "Your image", "source": "Uploaded"}
83
+ for item in gallery():
84
+ if item["id"] == image_id:
85
+ return item
86
+ raise HTTPException(404, f"No image called {image_id!r}")
87
+
88
+
89
+ def image_path(image_id):
90
+ file = Path(image_meta(image_id)["file"])
91
+ return file if file.is_absolute() else DATA / file
92
+
93
+
94
+ RAW = OrderedDict() # image id -> the BGR array from load_image
95
+
96
+
97
+ def load(image_id):
98
+ if image_id not in RAW:
99
+ if not re.fullmatch(r"[\w-]+", image_id):
100
+ raise HTTPException(404, "Bad image id")
101
+ img = LAB["load_image"](image_path(image_id))
102
+ img.setflags(write=False) # shared by every run: steps must return new arrays
103
+ RAW[image_id] = img
104
+ while len(RAW) > 64:
105
+ RAW.popitem(last=False)
106
+ return RAW[image_id]
107
+
108
+
109
+ # ---------------------------------------------------------------- 3. turning arrays into pictures and numbers
110
+
111
+ def is_chw(a):
112
+ return a.ndim == 3 and a.shape[0] in (1, 3) and a.shape[2] not in (1, 3, 4)
113
+
114
+
115
+ def to_display(a):
116
+ """RGB or gray uint8 for the browser, plus a note when values had to be rescaled to be visible."""
117
+ if is_chw(a):
118
+ a = a.transpose(1, 2, 0)
119
+ if a.dtype == np.uint8:
120
+ return a, None
121
+ lo, hi = float(a.min()), float(a.max())
122
+ scaled = cv2.normalize(a.astype(np.float32), None, 0, 255, cv2.NORM_MINMAX).astype(np.uint8)
123
+ return scaled, f"{a.dtype} values {lo:.2f} to {hi:.2f}, stretched to 0..255 so you can see them"
124
+
125
+
126
+ def stats(a):
127
+ return {"shape": list(a.shape), "dtype": str(a.dtype), "min": round(float(a.min()), 3),
128
+ "max": round(float(a.max()), 3),
129
+ "layout": "CHW" if is_chw(a) else ("HW" if a.ndim == 2 else "HWC")}
130
+
131
+
132
+ def encode(img, fmt="png", width=None):
133
+ if width and img.shape[1] > width:
134
+ img = cv2.resize(img, (width, round(img.shape[0] * width / img.shape[1])), interpolation=cv2.INTER_AREA)
135
+ bgr = img[:, :, ::-1] if img.ndim == 3 else img
136
+ ok, buf = cv2.imencode("." + fmt, np.ascontiguousarray(bgr), [cv2.IMWRITE_JPEG_QUALITY, 80] if fmt == "jpg" else [])
137
+ return buf.tobytes()
138
+
139
+
140
+ # ---------------------------------------------------------------- 4. running a line, with a cache
141
+
142
+ # Each step result is cached under a key built from the image and every step up to it, so moving one
143
+ # slider only recomputes that step and the ones after it.
144
+ CACHE = OrderedDict() # key -> dict(array, error, ms)
145
+ CACHE_LOCK = threading.Lock()
146
+ MODELS = {}
147
+ MODEL_LOCKS = {"resnet50": threading.Lock(), "yolo": threading.Lock()}
148
+
149
+
150
+ def remember(key, entry):
151
+ with CACHE_LOCK:
152
+ CACHE[key] = entry
153
+ while len(CACHE) > 600:
154
+ CACHE.popitem(last=False) # drop the oldest
155
+ return entry
156
+
157
+
158
+ def recall(key):
159
+ with CACHE_LOCK:
160
+ if key in CACHE:
161
+ CACHE.move_to_end(key)
162
+ return CACHE[key]
163
+ return None
164
+
165
+
166
+ KEY_LOCKS = {} # cache key -> lock, while that result is being computed
167
+
168
+
169
+ def once(key, compute):
170
+ """Recall key, or compute it exactly once even when several requests ask at the same time.
171
+
172
+ A slider drag sends many requests and an aborted fetch does not stop the server's work, so without this the
173
+ same slow step (denoise takes about a second) could run several times in parallel."""
174
+ entry = recall(key)
175
+ if entry is not None:
176
+ return entry
177
+ with CACHE_LOCK:
178
+ lock = KEY_LOCKS.setdefault(key, threading.Lock())
179
+ with lock:
180
+ try:
181
+ entry = recall(key)
182
+ return entry if entry is not None else remember(key, compute())
183
+ finally:
184
+ with CACHE_LOCK:
185
+ KEY_LOCKS.pop(key, None)
186
+
187
+
188
+ def with_model(name, fn):
189
+ """Run fn(model) while holding that model's lock: one inference at a time per model."""
190
+ with MODEL_LOCKS[name]:
191
+ if name not in MODELS:
192
+ MODELS[name] = LAB["load_resnet50"]() if name == "resnet50" else LAB["load_yolo"]()
193
+ return fn(MODELS[name])
194
+
195
+
196
+ def make_key(*parts):
197
+ return hashlib.sha1("|".join(parts).encode()).hexdigest()[:16]
198
+
199
+
200
+ def histogram(display, bins=64):
201
+ """Brightness histogram of the displayed picture: `bins` counts over 0..255."""
202
+ gray = cv2.cvtColor(display, cv2.COLOR_RGB2GRAY) if display.ndim == 3 and display.shape[2] == 3 else display
203
+ if gray.ndim == 3:
204
+ gray = gray[:, :, 0]
205
+ counts = cv2.calcHist([np.ascontiguousarray(gray)], [0], None, [bins], [0, 256])
206
+ return [int(c) for c in counts.ravel()]
207
+
208
+
209
+ def describe(key, entry, **extra):
210
+ if "hist" not in entry: # computed once per cached result
211
+ display, note = to_display(entry["array"])
212
+ entry["hist"], entry["note"] = histogram(display), note
213
+ return {**extra, "key": key, "error": entry.get("error"), "ms": entry.get("ms", 0),
214
+ "stats": stats(entry["array"]), "display_note": entry["note"], "hist": entry["hist"]}
215
+
216
+
217
+ def run_steps(image_id, steps):
218
+ """Run a line's steps through the cache. Returns (final key, final entry, one description per step)."""
219
+ key = make_key("load", image_id)
220
+ current = recall(key) or remember(key, {"array": load(image_id)})
221
+ out = [describe(key, current, uid="load", op="load", name="Load image", kind="load", enabled=True, params={},
222
+ call="img = load_image(path)")]
223
+ for step in steps:
224
+ step_id = step.get("op")
225
+ if step_id not in STEPS:
226
+ continue
227
+ enabled = bool(step.get("enabled", True))
228
+ params = LAB["clean_params"](step_id, step.get("params"))
229
+ key = make_key(key, json.dumps([step_id, params, enabled], sort_keys=True))
230
+ entry = once(key, lambda: compute_step(current, step_id, params, enabled))
231
+ # uid and rev are echoed untouched so the app can tell which step, and which edit, a result belongs to.
232
+ out.append(describe(key, entry, uid=step.get("uid"), rev=step.get("rev"), op=step_id,
233
+ name=STEPS[step_id]["name"], kind=STEPS[step_id]["kind"], enabled=enabled, params=params,
234
+ call=call_line(step_id, params)))
235
+ current = entry
236
+ return key, current, out
237
+
238
+
239
+ def compute_step(current, step_id, params, enabled):
240
+ if not enabled:
241
+ return {"array": current["array"]}
242
+ t0 = time.perf_counter()
243
+ try:
244
+ entry = {"array": np.asarray(LAB["run_step"](current["array"], step_id, params))}
245
+ except Exception as exc: # show the error on the step; the image passes through unchanged
246
+ msg = str(exc).strip().splitlines()[-1] if str(exc).strip() else ""
247
+ entry = {"array": current["array"], "error": f"{type(exc).__name__}: {msg}"}
248
+ entry["ms"] = round((time.perf_counter() - t0) * 1000, 1)
249
+ return entry
250
+
251
+
252
+ def clean_version(steps):
253
+ """The same line with every preparation step on at its defaults and every break-it and fix-it step off."""
254
+ return [{"op": s.get("op"), "enabled": STEPS[s["op"]]["kind"] == "prep"} for s in steps if s.get("op") in STEPS]
255
+
256
+
257
+ def run_line(image_id, steps, task):
258
+ key, final, out = run_steps(image_id, steps)
259
+ result = run_task(task, key, final)
260
+ if result is not None:
261
+ bkey, bfinal, _ = run_steps(image_id, clean_version(steps))
262
+ result["baseline"] = run_task(task, bkey, bfinal)
263
+ if result["type"] == "classify":
264
+ # How sure the model is NOW of what it answered on the clean image: this is what drops.
265
+ base, pk = result["baseline"], result["probs_key"]
266
+ result["baseline_label_now"] = None
267
+ if base["top5"] and pk in CACHE_PROBS:
268
+ label = base["top5"][0]["label"]
269
+ result["baseline_label_now"] = round(float(CACHE_PROBS[pk][LAB["LABELS"].index(label)]), 4)
270
+ return {"image": image_meta(image_id), "steps": out, "result": result}
271
+
272
+
273
+ CACHE_PROBS = {} # probs key -> the 1000 class probabilities (small, kept apart from the image cache)
274
+
275
+
276
+ def run_task(task, key, final):
277
+ kind = (task or {}).get("type", "none")
278
+ if kind == "classify":
279
+ rkey = make_key(key, "classify")
280
+ cached = recall(rkey)
281
+ if cached is None:
282
+ t0 = time.perf_counter()
283
+ a = final["array"]
284
+ result = {"type": "classify", "model": "ResNet50", "input_shape": [1, *a.shape], "input_dtype": str(a.dtype),
285
+ "input_range": [round(float(a.min()), 2), round(float(a.max()), 2)], "probs_key": rkey,
286
+ "error": None, "top5": []}
287
+ try:
288
+ probs = with_model("resnet50", lambda m: LAB["classify_resnet50"](a, m))
289
+ CACHE_PROBS[rkey] = probs
290
+ while len(CACHE_PROBS) > 2000:
291
+ CACHE_PROBS.pop(next(iter(CACHE_PROBS)))
292
+ result["top5"] = [{"label": label, "prob": p} for label, p in LAB["top_k"](probs, 5)]
293
+ except Exception as exc: # e.g. channels last: PyTorch refuses, and that is the lesson
294
+ msg = str(exc).strip().splitlines()[0] if str(exc).strip() else type(exc).__name__
295
+ result["error"] = msg
296
+ result["probs_key"] = None
297
+ result["ms"] = round((time.perf_counter() - t0) * 1000, 1)
298
+ cached = remember(rkey, {"array": a, "result": result})
299
+ pk = cached["result"]["probs_key"]
300
+ if pk is not None and pk not in CACHE_PROBS: # evicted: recompute on the next run
301
+ with CACHE_LOCK:
302
+ CACHE.pop(rkey, None)
303
+ return run_task(task, key, final)
304
+ return dict(cached["result"])
305
+ if kind == "segment":
306
+ conf = min(0.95, max(0.05, float(task.get("conf", 0.25))))
307
+ rkey = make_key(key, "segment", f"{conf:.3f}")
308
+ cached = recall(rkey)
309
+ if cached is None:
310
+ t0 = time.perf_counter()
311
+ img = LAB["as_uint8_rgb"](final["array"])
312
+ overlay, instances = with_model("yolo", lambda m: LAB["segment_yolo"](img, m, conf))
313
+ counts = {}
314
+ for name, _ in instances:
315
+ counts[name] = counts.get(name, 0) + 1
316
+ # class name -> the mask color segment_yolo drew, so the app's legend matches the picture
317
+ colors = {n: "#%02x%02x%02x" % LAB["class_color"](i) for i, n in MODELS["yolo"].names.items()}
318
+ cached = remember(rkey, {"array": overlay, "result": {
319
+ "type": "segment", "model": "YOLO11s-seg", "conf": conf, "total": len(instances),
320
+ "counts": dict(sorted(counts.items(), key=lambda kv: -kv[1])), "overlay_key": rkey,
321
+ "colors": colors,
322
+ "instances": [{"label": n, "conf": c} for n, c in instances], "input_shape": list(img.shape),
323
+ "notes": [] if final["array"].dtype == np.uint8 else ["The pipeline output was converted to uint8 RGB first."],
324
+ "ms": round((time.perf_counter() - t0) * 1000, 1)}})
325
+ return dict(cached["result"])
326
+ return None
327
+
328
+
329
+ # ---------------------------------------------------------------- 5. HTTP routes
330
+
331
+ def warm_up():
332
+ """Load both models, then run every gallery image through its clean line, so first clicks are instant."""
333
+ with_model("resnet50", lambda m: LAB["classify_resnet50"](np.zeros((3, 224, 224), np.float32), m))
334
+ with_model("yolo", lambda m: LAB["segment_yolo"](np.zeros((640, 640, 3), np.uint8), m))
335
+ lines = {line["id"]: line for line in LAB["LINES"]}
336
+ for item in gallery():
337
+ line = lines[item["use_case"]]
338
+ try:
339
+ run_line(item["id"], line["steps"], line["task"])
340
+ except Exception as exc: # a missing file should not stop the server
341
+ print("warm-up skipped", item["id"], exc)
342
+ # The default photo is what the presenter breaks and fixes first: run every break, alone and with each fix
343
+ # meant for it (denoise takes about a second), so those first clicks are instant too.
344
+ for line in LAB["LINES"]:
345
+ for s in line["steps"]:
346
+ if STEPS.get(s["op"], {}).get("kind") != "break":
347
+ continue
348
+ fixes = [f for f, d in STEPS.items() if d["kind"] == "fix" and s["op"] in d["helps"]]
349
+ for on in [{s["op"]}, *({s["op"], f} for f in fixes)]:
350
+ steps = [{**x, "enabled": True} if x["op"] in on else x for x in line["steps"]]
351
+ try:
352
+ run_line(line["default_image"], steps, line["task"])
353
+ except Exception as exc:
354
+ print("warm-up skipped", line["id"], sorted(on), exc)
355
+
356
+
357
+ @asynccontextmanager
358
+ async def lifespan(_):
359
+ threading.Thread(target=warm_up, daemon=True).start()
360
+ yield
361
+
362
+
363
+ app = FastAPI(title="Image Preprocessing Lab", lifespan=lifespan)
364
+ app.add_middleware(CORSMiddleware, allow_origins=[o.strip() for o in os.environ.get("ALLOWED_ORIGINS", "*").split(",")],
365
+ allow_methods=["GET", "POST"], allow_headers=["*"])
366
+ FOREVER = {"Cache-Control": "public, max-age=31536000, immutable"}
367
+
368
+
369
+ @app.get("/api/health")
370
+ def health():
371
+ return {"ok": True, "models_ready": len(MODELS) == 2}
372
+
373
+
374
+ @app.get("/api/steps")
375
+ def steps():
376
+ return {
377
+ "kinds": LAB["KINDS"],
378
+ "steps": [{k: v for k, v in s.items() if k != "fn"} | {"source": step_source(s["id"])} for s in STEPS.values()],
379
+ "load_source": SOURCES["load_image"],
380
+ "model_sources": {"classify": "\n\n\n".join(SOURCES[n] for n in ("classify_resnet50", "top_k")),
381
+ "segment": "\n\n\n".join(SOURCES[n] for n in ("draw_label", "segment_yolo"))},
382
+ }
383
+
384
+
385
+ @app.get("/api/lines")
386
+ def lines():
387
+ return {"lines": LAB["LINES"]}
388
+
389
+
390
+ @app.get("/api/images")
391
+ def images():
392
+ return {"images": gallery()}
393
+
394
+
395
+ @app.get("/api/images/{image_id}/thumb")
396
+ def image_thumb(image_id: str):
397
+ return Response(encode(load(image_id)[:, :, ::-1], "jpg", width=200), media_type="image/jpeg", headers=FOREVER)
398
+
399
+
400
+ @app.post("/api/upload")
401
+ async def upload(file: UploadFile = File(...)):
402
+ data = await file.read(MAX_UPLOAD_BYTES + 1)
403
+ if len(data) > MAX_UPLOAD_BYTES:
404
+ raise HTTPException(413, "Images up to 10 MB, please.")
405
+ arr = cv2.imdecode(np.frombuffer(data, np.uint8), cv2.IMREAD_COLOR)
406
+ if arr is None:
407
+ raise HTTPException(400, "That file is not an image OpenCV can read.")
408
+ h, w = arr.shape[:2]
409
+ s = UPLOAD_MAX_SIDE / max(h, w)
410
+ if s < 1:
411
+ arr = cv2.resize(arr, (round(w * s), round(h * s)), interpolation=cv2.INTER_AREA)
412
+ UPLOADS.mkdir(exist_ok=True)
413
+ digest = hashlib.sha1(data).hexdigest()[:12]
414
+ cv2.imwrite(str(UPLOADS / f"{digest}.png"), arr)
415
+ return {"id": f"upload-{digest}"}
416
+
417
+
418
+ @app.post("/api/run")
419
+ def run(body: dict = Body(...)):
420
+ return run_line(str(body["image"]), body.get("steps", []), body.get("task", {}))
421
+
422
+
423
+ @app.get("/api/img/{key}")
424
+ def step_image(key: str, w: int | None = None):
425
+ entry = recall(key)
426
+ if entry is None:
427
+ raise HTTPException(404, "Image expired from the cache; run the line again")
428
+ display, _ = to_display(entry["array"])
429
+ if w:
430
+ return Response(encode(display, "jpg", width=min(w, 2048)), media_type="image/jpeg", headers=FOREVER)
431
+ return Response(encode(display, "png"), media_type="image/png", headers=FOREVER)
432
+
433
+
434
+ @app.exception_handler(Exception)
435
+ def crash(_, exc):
436
+ return JSONResponse({"detail": f"{type(exc).__name__}: {exc}"}, status_code=500)
static/assets/{index-CUsGOyLW.js → index-DbtPZ3mQ.js} RENAMED
The diff for this file is too large to render. See raw diff
 
static/index.html CHANGED
@@ -5,7 +5,7 @@
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
  <title>Image Preprocessing Lab</title>
7
  <link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Ccircle cx='16' cy='16' r='15' fill='%23da291c'/%3E%3Ccircle cx='16' cy='16' r='7' fill='%23fff'/%3E%3C/svg%3E" />
8
- <script type="module" crossorigin src="/assets/index-CUsGOyLW.js"></script>
9
  <link rel="stylesheet" crossorigin href="/assets/index-D1h952PA.css">
10
  </head>
11
  <body>
 
5
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
  <title>Image Preprocessing Lab</title>
7
  <link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 32 32'%3E%3Ccircle cx='16' cy='16' r='15' fill='%23da291c'/%3E%3Ccircle cx='16' cy='16' r='7' fill='%23fff'/%3E%3C/svg%3E" />
8
+ <script type="module" crossorigin src="/assets/index-DbtPZ3mQ.js"></script>
9
  <link rel="stylesheet" crossorigin href="/assets/index-D1h952PA.css">
10
  </head>
11
  <body>