labeltify 0.4.1__tar.gz → 0.4.3__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: labeltify
3
- Version: 0.4.1
3
+ Version: 0.4.3
4
4
  Summary: Upload frames and optional annotations into LabelTify with a pipeline token.
5
5
  Author: CleverTify
6
6
  Project-URL: Homepage, https://labeltify.com
@@ -304,6 +304,8 @@ class LabelTifyClient:
304
304
  camera: str | None = None,
305
305
  pipeline: str | None = None,
306
306
  trigger: str | None = None,
307
+ detect_corrupt: bool = True,
308
+ skip_corrupt: bool = False,
307
309
  max_request_bytes: int = MAX_REQUEST_BYTES,
308
310
  max_request_images: int = MAX_REQUEST_IMAGES,
309
311
  ) -> UploadReport:
@@ -312,6 +314,9 @@ class LabelTifyClient:
312
314
  Every file is a ``file`` part; a file whose name ends in a label extension (.json, .jsonl, .xml,
313
315
  .txt, .yaml, .yml, .names, .csv, .zip) is a label file, anything else an image. With any label
314
316
  file, ``work_state`` is required. ``dhashes`` (16 hex characters each) go one per image, in order.
317
+ With Pillow installed, ``detect_corrupt`` (default) sends ``corrupt_score``, ``corrupt_reason``
318
+ and ``corrupt_detector_version`` for each image. ``skip_corrupt`` leaves truncated and blank frames out of the upload and reports
319
+ them as skipped. Without Pillow both options do nothing.
315
320
  """
316
321
  if not files:
317
322
  raise LabelTifyError("nothing to upload")
@@ -336,6 +341,54 @@ class LabelTifyClient:
336
341
  if len(dhashes) != len(images) or any(parse_dhash(h) is None for h in dhashes):
337
342
  raise LabelTifyError(f"dhashes needs one 16-hex value per image: {len(images)} images, {len(dhashes)} hashes")
338
343
 
344
+ skipped_corrupt: list[dict[str, Any]] = []
345
+ score_by_name: dict[int, tuple[float, str] | None] = {}
346
+ if detect_corrupt or skip_corrupt:
347
+ try:
348
+ from .corrupt import CORRUPT_DETECTOR_VERSION, corrupt_score
349
+ except ImportError:
350
+ detect_corrupt = False
351
+ else:
352
+ image_positions = [i for i, f in enumerate(files) if not _is_label(f["filename"])]
353
+ drop: set[int] = set()
354
+ for n, pos in enumerate(image_positions):
355
+ try:
356
+ scored = corrupt_score(files[pos]["data"])
357
+ except Exception:
358
+ score_by_name[n] = None
359
+ continue
360
+ if skip_corrupt and scored[1] in ("truncated", "blank"):
361
+ drop.add(pos)
362
+ skipped_corrupt.append(
363
+ {"filename": files[pos]["filename"], "status": "skipped", "message": scored[1]}
364
+ )
365
+ continue
366
+ score_by_name[n] = scored
367
+ if drop:
368
+ kept_scores: list[tuple[float, str] | None] = []
369
+ kept_hashes: list[str] = []
370
+ img_n = 0
371
+ new_files: list[UploadFile] = []
372
+ for pos, f in enumerate(files):
373
+ if _is_label(f["filename"]):
374
+ new_files.append(f)
375
+ continue
376
+ if pos in drop:
377
+ img_n += 1
378
+ continue
379
+ new_files.append(f)
380
+ kept_scores.append(score_by_name.get(img_n))
381
+ if dhashes is not None:
382
+ kept_hashes.append(dhashes[img_n])
383
+ img_n += 1
384
+ files = new_files
385
+ images = [f for f in files if not _is_label(f["filename"])]
386
+ score_by_name = {n: kept_scores[n] for n in range(len(kept_scores))}
387
+ if dhashes is not None:
388
+ dhashes = kept_hashes
389
+ if not files:
390
+ return {"items": [], "files": skipped_corrupt, "imported": None, "requests": 0}
391
+
339
392
  boundary = f"----labeltify{uuid.uuid4().hex}"
340
393
  parts: list[bytes] = []
341
394
 
@@ -365,11 +418,22 @@ class LabelTifyClient:
365
418
  hashes = iter(dhashes or [])
366
419
  for f in files:
367
420
  file(f["filename"], f["content_type"], f["data"])
368
- # The server pairs dhash fields with image parts in order; fields after the files are fine.
369
- for _ in images:
421
+ # The server pairs dhash and corrupt fields with image parts in order; fields after the files are fine.
422
+ sending_scores = detect_corrupt and any(score_by_name.get(i) is not None for i in range(len(images)))
423
+ if sending_scores:
424
+ field("corrupt_detector_version", str(CORRUPT_DETECTOR_VERSION))
425
+ for n, _ in enumerate(images):
370
426
  h = next(hashes, None)
371
427
  if h:
372
428
  field("dhash", h.lower())
429
+ scored = score_by_name.get(n) if detect_corrupt else None
430
+ if sending_scores:
431
+ if scored is None:
432
+ field("corrupt_score", "")
433
+ field("corrupt_reason", "")
434
+ else:
435
+ field("corrupt_score", str(scored[0]))
436
+ field("corrupt_reason", scored[1])
373
437
  parts.append(f"--{boundary}--\r\n".encode())
374
438
  result = self._request(
375
439
  "POST",
@@ -378,12 +442,13 @@ class LabelTifyClient:
378
442
  f"multipart/form-data; boundary={boundary}",
379
443
  )
380
444
  body = result if isinstance(result, dict) else {}
381
- return {
445
+ report: UploadReport = {
382
446
  "items": list(body.get("items") or []),
383
- "files": list(body.get("files") or []),
447
+ "files": skipped_corrupt + list(body.get("files") or []),
384
448
  "imported": body.get("import"),
385
449
  "requests": 1,
386
450
  }
451
+ return report
387
452
 
388
453
  def upload_directory(
389
454
  self,
@@ -397,6 +462,8 @@ class LabelTifyClient:
397
462
  pipeline: str | None = None,
398
463
  trigger: str | None = None,
399
464
  compute_dhash: bool = False,
465
+ detect_corrupt: bool = True,
466
+ skip_corrupt: bool = False,
400
467
  max_request_bytes: int | None = None,
401
468
  max_request_images: int | None = None,
402
469
  workers: int = DEFAULT_WORKERS,
@@ -554,6 +621,8 @@ class LabelTifyClient:
554
621
  camera=camera,
555
622
  pipeline=pipeline,
556
623
  trigger=trigger,
624
+ detect_corrupt=detect_corrupt,
625
+ skip_corrupt=skip_corrupt,
557
626
  max_request_bytes=max_request_bytes,
558
627
  max_request_images=max_request_images,
559
628
  )
@@ -0,0 +1,184 @@
1
+ """Corrupt-frame detector v2. The same math as ``packages/shared/src/corrupt-detect.ts``.
2
+
3
+ Pillow only (``pip install labeltify[images]``). No numpy. A 240 px wide thumbnail.
4
+ ``corrupt_score(data) -> (score, reason)``.
5
+
6
+ Constants (why they are what they are is in the TypeScript file header):
7
+
8
+ - ``REPEAT_VD_ABS`` 1.2: a copied row differs by about 0, and by under 1 after JPEG.
9
+ - ``REPEAT_VD_REL`` 0.25: the vertical step must also be small beside the row's streaks.
10
+ - ``REPEAT_HD_MIN`` 2.5: flat sky, letterbox and black stay under this.
11
+ - ``SMEAR_SOFT`` / ``SMEAR_HARD`` 0.035 / 0.07: the score crosses 0.5 near 5% of the
12
+ height and saturates at 7%. One noisy comparison may sit inside the bottom run.
13
+ """
14
+
15
+ from __future__ import annotations
16
+
17
+ import io
18
+
19
+ CORRUPT_DETECTOR_VERSION = 2
20
+ TARGET_W = 240
21
+ THRESHOLD = 0.5
22
+
23
+ BLANK_STD_SOFT, BLANK_STD_HARD = 8.0, 4.0
24
+ GREY_TOL = 3
25
+ FLAT_GRAD = 0.75
26
+ CODEC_SOFT, CODEC_HARD = 0.02, 0.05
27
+ REPEAT_VD_ABS = 1.2
28
+ REPEAT_VD_REL = 0.25
29
+ REPEAT_HD_MIN = 2.5
30
+ SMEAR_SOFT, SMEAR_HARD = 0.035, 0.07
31
+
32
+
33
+ def _ramp(x: float, soft: float, hard: float) -> float:
34
+ t = (x - soft) / (hard - soft)
35
+ return float(min(1.0, max(0.0, t)))
36
+
37
+
38
+ def _thumb(im):
39
+ from PIL import Image
40
+
41
+ if im.width >= 2 * TARGET_W:
42
+ im = im.reduce(max(1, im.width // TARGET_W))
43
+ if im.width != TARGET_W:
44
+ im = im.resize((TARGET_W, max(8, round(im.height * TARGET_W / im.width))), Image.BILINEAR)
45
+ return im.convert("RGB")
46
+
47
+
48
+ def _decode(data: bytes, allow_truncated: bool):
49
+ from PIL import Image, ImageFile
50
+
51
+ previous = ImageFile.LOAD_TRUNCATED_IMAGES
52
+ if allow_truncated:
53
+ ImageFile.LOAD_TRUNCATED_IMAGES = True
54
+ try:
55
+ im = Image.open(io.BytesIO(data))
56
+ if im.format == "JPEG":
57
+ im.draft("RGB", (TARGET_W, 1))
58
+ im.load()
59
+ return im
60
+ finally:
61
+ if allow_truncated:
62
+ ImageFile.LOAD_TRUNCATED_IMAGES = previous
63
+
64
+
65
+ def _load(data: bytes) -> tuple[list[tuple[int, int, int]], int, int, bool]:
66
+ truncated = data[:2] == b"\xff\xd8" and not data[-32:].rstrip(b"\x00\r\n ").endswith(b"\xff\xd9")
67
+ try:
68
+ im = _decode(data, False)
69
+ except OSError:
70
+ truncated = True
71
+ im = _decode(data, True)
72
+ thumb = _thumb(im)
73
+ w, h = thumb.size
74
+ pixels = list(thumb.getdata())
75
+ return pixels, w, h, truncated
76
+
77
+
78
+ def _luma(rgb: tuple[int, int, int]) -> float:
79
+ return 0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]
80
+
81
+
82
+ def _bottom_span(rep: list[bool]) -> int:
83
+ n = len(rep)
84
+ if n == 0:
85
+ return 0
86
+ i = n - 1
87
+ tail = 0
88
+ if not rep[i]:
89
+ if i == 0 or not rep[i - 1]:
90
+ return 0
91
+ tail = 1
92
+ i -= 1
93
+ span = 1 + tail
94
+ gap = 0
95
+ i -= 1
96
+ while i >= 0:
97
+ if rep[i]:
98
+ span += 1 + gap
99
+ gap = 0
100
+ i -= 1
101
+ continue
102
+ if gap >= 1 or i - 1 < 0 or not rep[i - 1]:
103
+ break
104
+ gap = 1
105
+ i -= 1
106
+ return span
107
+
108
+
109
+ def _score_luma(
110
+ pixels: list[tuple[int, int, int]], w: int, h: int
111
+ ) -> tuple[float, str, dict[str, float]]:
112
+ """Score pixels that are already the thumbnail. Same numbers as the TypeScript function."""
113
+ metrics = {"std": 0.0, "smear_frac": 0.0, "smear_rows": 0.0, "grey_frac": 0.0, "green_frac": 0.0}
114
+ if w < 2 or h < 2 or len(pixels) < w * h:
115
+ return 0.0, "ok", metrics
116
+ n = w * h
117
+ g = [_luma(pixels[i]) for i in range(n)]
118
+ mean = sum(g) / n
119
+ std = (sum((v - mean) ** 2 for v in g) / n) ** 0.5
120
+ green = sum(1 for r, gg, b in pixels[:n] if gg > 90 and r < 50 and b < 50) / n
121
+ hd = []
122
+ denom = w - 1
123
+ for y in range(h):
124
+ row = y * w
125
+ hd.append(sum(abs(g[row + x + 1] - g[row + x]) for x in range(denom)) / denom)
126
+ rep: list[bool] = []
127
+ for y in range(h - 1):
128
+ row = y * w
129
+ nxt = row + w
130
+ vd = sum(abs(g[nxt + x] - g[row + x]) for x in range(w)) / w
131
+ detail = max(hd[y], hd[y + 1])
132
+ rep.append(vd <= REPEAT_VD_ABS and vd <= REPEAT_VD_REL * detail and detail >= REPEAT_HD_MIN)
133
+ smear_rows = _bottom_span(rep)
134
+ smear_frac = smear_rows / h
135
+ grey = 0
136
+ for y in range(h - 1):
137
+ for x in range(w - 1):
138
+ i = y * w + x
139
+ if abs(g[i + 1] - g[i]) + abs(g[i + w] - g[i]) >= FLAT_GRAD:
140
+ continue
141
+ r, gg, b = pixels[i]
142
+ if abs(r - 128) < GREY_TOL and abs(gg - 128) < GREY_TOL and abs(b - 128) < GREY_TOL:
143
+ grey += 1
144
+ grey_frac = grey / n
145
+ metrics = {
146
+ "std": std,
147
+ "smear_frac": smear_frac,
148
+ "smear_rows": float(smear_rows),
149
+ "grey_frac": grey_frac,
150
+ "green_frac": green,
151
+ }
152
+ scores = {
153
+ "blank": _ramp(std, BLANK_STD_SOFT, BLANK_STD_HARD),
154
+ "smear": _ramp(smear_frac, SMEAR_SOFT, SMEAR_HARD),
155
+ "grey_blocks": _ramp(grey_frac, CODEC_SOFT, CODEC_HARD),
156
+ "green_blocks": _ramp(green, CODEC_SOFT, CODEC_HARD),
157
+ }
158
+ reason = max(scores, key=scores.get)
159
+ best = scores[reason]
160
+ return round(best, 3), (reason if best > 0 else "ok"), metrics
161
+
162
+
163
+ def corrupt_score_rgba(pixels: list[tuple[int, int, int]], w: int, h: int) -> tuple[float, str]:
164
+ """Score a raw RGB grid. ``(score, reason)``. Used to match the TypeScript function on the same pixels."""
165
+ score, reason, _metrics = _score_luma(pixels, w, h)
166
+ return score, reason
167
+
168
+
169
+ def corrupt_analyse(data: bytes) -> dict[str, float | str]:
170
+ """Score encoded bytes and return the score, the reason and the metrics."""
171
+ try:
172
+ pixels, w, h, truncated = _load(data)
173
+ except Exception:
174
+ return {"score": 1.0, "reason": "unreadable", "smear_frac": 0.0, "std": 0.0, "grey_frac": 0.0, "green_frac": 0.0}
175
+ if truncated:
176
+ return {"score": 1.0, "reason": "truncated", "smear_frac": 0.0, "std": 0.0, "grey_frac": 0.0, "green_frac": 0.0}
177
+ score, reason, metrics = _score_luma(pixels, w, h)
178
+ return {"score": score, "reason": reason, **metrics}
179
+
180
+
181
+ def corrupt_score(data: bytes) -> tuple[float, str]:
182
+ """Score encoded image bytes. ``(score, reason)`` with score in 0..1."""
183
+ report = corrupt_analyse(data)
184
+ return float(report["score"]), str(report["reason"])
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: labeltify
3
- Version: 0.4.1
3
+ Version: 0.4.3
4
4
  Summary: Upload frames and optional annotations into LabelTify with a pipeline token.
5
5
  Author: CleverTify
6
6
  Project-URL: Homepage, https://labeltify.com
@@ -3,6 +3,7 @@ pyproject.toml
3
3
  labeltify/__init__.py
4
4
  labeltify/__main__.py
5
5
  labeltify/client.py
6
+ labeltify/corrupt.py
6
7
  labeltify/dhash.py
7
8
  labeltify/download.py
8
9
  labeltify.egg-info/PKG-INFO
@@ -12,4 +13,6 @@ labeltify.egg-info/entry_points.txt
12
13
  labeltify.egg-info/requires.txt
13
14
  labeltify.egg-info/top_level.txt
14
15
  tests/test_client.py
16
+ tests/test_corrupt.py
17
+ tests/test_corrupt_eval.py
15
18
  tests/test_download.py
@@ -5,7 +5,7 @@ build-backend = "setuptools.build_meta"
5
5
  [project]
6
6
  name = "labeltify"
7
7
  # Bump this to publish. .github/workflows/publish-python.yml releases each new version pushed to main.
8
- version = "0.4.1"
8
+ version = "0.4.3"
9
9
  description = "Upload frames and optional annotations into LabelTify with a pipeline token."
10
10
  readme = "README.md"
11
11
  requires-python = ">=3.10"
@@ -220,7 +220,7 @@ def test_bundle_sends_like_the_upload_page(api):
220
220
  assert report["requests"] == 1 and report["items"] == [{"id": "img-1"}] and report["imported"] is None
221
221
  (sent,) = uploads(api)
222
222
  assert [f for n, f, _ in sent if n == "file"] == ["a.jpg", "b.jpg", "annotations.json"]
223
- fields = {n: v for n, f, v in sent if f is None and n != "dhash"}
223
+ fields = {n: v for n, f, v in sent if f is None and n not in {"dhash", "corrupt_score", "corrupt_reason", "corrupt_detector_version"}}
224
224
  assert fields == {"workState": b"done", "skipNearDuplicates": b"1"}
225
225
  assert [v for n, _, v in sent if n == "dhash"] == [b"0123456789abcdef", b"fedcba9876543210"]
226
226
 
@@ -428,6 +428,46 @@ def test_compute_dhash_with_pillow(api, tmp_path):
428
428
  assert len(hashes) == 2 and all(parse_dhash(h) is not None for h in hashes)
429
429
 
430
430
 
431
+ def test_corrupt_fields_and_skip(api):
432
+ from PIL import Image
433
+ import io
434
+
435
+ def png(color):
436
+ im = Image.new("RGB", (32, 32), color)
437
+ buf = io.BytesIO()
438
+ im.save(buf, format="PNG")
439
+ return buf.getvalue()
440
+
441
+ black = png((0, 0, 0))
442
+ varied = png((10, 180, 40))
443
+ # A flat colour may be blank; the varied one is not a full black frame.
444
+ # Paint a gradient so it is not blank.
445
+ im = Image.new("RGB", (48, 32))
446
+ im.putdata([((x * 5) % 256, (y * 3 + 40) % 256, (x + y) % 256) for y in range(32) for x in range(48)])
447
+ buf = io.BytesIO()
448
+ im.save(buf, format="PNG")
449
+ varied = buf.getvalue()
450
+ client = client_for(api)
451
+ client.upload_bundle(
452
+ "ds-1",
453
+ [
454
+ {"filename": "black.png", "data": black, "content_type": "image/png"},
455
+ {"filename": "scene.png", "data": varied, "content_type": "image/png"},
456
+ ],
457
+ skip_near_duplicates=False,
458
+ skip_corrupt=True,
459
+ )
460
+ (sent,) = uploads(api)
461
+ names = [f for n, f, _ in sent if n == "file"]
462
+ assert names == ["scene.png"]
463
+ reasons = [v.decode() for n, _, v in sent if n == "corrupt_reason"]
464
+ assert reasons and all(r != "blank" for r in reasons)
465
+ scores = [n for n, _, _ in sent if n == "corrupt_score"]
466
+ assert scores
467
+ versions = [v.decode() for n, _, v in sent if n == "corrupt_detector_version"]
468
+ assert versions == ["2"]
469
+
470
+
431
471
  def test_no_dhash_without_asking(api, tmp_path):
432
472
  (tmp_path / "a.jpg").write_bytes(b"\xff\xd8a")
433
473
  client_for(api).upload_directory("ds-1", tmp_path)
@@ -0,0 +1,176 @@
1
+ """Synthetic frames. The same outcomes as packages/shared corruptScoreFromRgba."""
2
+
3
+ import io
4
+
5
+ from PIL import Image
6
+
7
+ from labeltify.corrupt import THRESHOLD, corrupt_score, corrupt_score_rgba
8
+
9
+
10
+ def png(width, height, pixel) -> bytes:
11
+ im = Image.new("RGB", (width, height))
12
+ im.putdata([pixel(x, y) for y in range(height) for x in range(width)])
13
+ buf = io.BytesIO()
14
+ im.save(buf, format="PNG")
15
+ return buf.getvalue()
16
+
17
+
18
+ def streak(x, src):
19
+ v = (x * 17 + src * 13) % 256
20
+ return (v, (v * 3) % 256, 80)
21
+
22
+
23
+ def bottom_band(width, height, comparisons):
24
+ source = height - 1 - comparisons
25
+
26
+ def pixel(x, y):
27
+ return streak(x, source if y >= source + 1 else y)
28
+
29
+ return [[pixel(x, y) for x in range(width)] for y in range(height)]
30
+
31
+
32
+ def flat(grid):
33
+ return [pixel for row in grid for pixel in row]
34
+
35
+
36
+ def test_clean_scene_is_ok():
37
+ data = png(80, 48, lambda x, y: ((v := (x * 3 + y * 5) % 256), (v + 40) % 256, (v + 90) % 256))
38
+ score, reason = corrupt_score(data)
39
+ assert reason == "ok"
40
+ assert score < THRESHOLD
41
+
42
+
43
+ def test_black_frame_is_blank():
44
+ score, reason = corrupt_score(png(80, 32, lambda x, y: (0, 0, 0)))
45
+ assert reason == "blank"
46
+ assert score >= THRESHOLD
47
+
48
+
49
+ def test_bottom_bands():
50
+ for comparisons, floor in ((6, THRESHOLD), (20, 0.8), (90, 0.8)):
51
+ grid = bottom_band(80, 100, comparisons)
52
+ score, reason = corrupt_score_rgba(flat(grid), 80, 100)
53
+ assert reason == "smear", comparisons
54
+ assert score >= floor, (comparisons, score)
55
+
56
+
57
+ def test_partial_width_band():
58
+ def pixel(x, y):
59
+ src = 55 if y >= 56 else y
60
+ if x < 30:
61
+ return streak(x, src)
62
+ return (40, 40, 40)
63
+
64
+ score, reason = corrupt_score_rgba(flat([[pixel(x, y) for x in range(90)] for y in range(80)]), 90, 80)
65
+ assert reason == "smear"
66
+ assert score >= THRESHOLD
67
+
68
+
69
+ def test_noisy_jpeg_band():
70
+ def pixel(x, y):
71
+ src = 69 if y >= 70 else y
72
+ r, g, b = streak(x, src)
73
+ if y == 85:
74
+ return (min(255, r + 8), g, b)
75
+ return (r, g, b)
76
+
77
+ score, reason = corrupt_score_rgba(flat([[pixel(x, y) for x in range(80)] for y in range(100)]), 80, 100)
78
+ assert reason == "smear"
79
+ assert score >= 0.8
80
+
81
+
82
+ def _rows(pixel, w, h):
83
+ return [[pixel(x, y) for x in range(w)] for y in range(h)]
84
+
85
+
86
+ def test_negatives_stay_under_the_margin():
87
+ def sky(x, y):
88
+ v = min(255, round(140 + y * 1.1 + x * 0.35))
89
+ return (v, min(255, v + 20), 230)
90
+
91
+ def haze(x, y):
92
+ v = 60 + (y % 18) * 3 + (x % 14) + ((x * 3 + y) % 5)
93
+ return (v, v + 6, v + 10)
94
+
95
+ def night(x, y):
96
+ v = (x * 9 + y * 7) % 36
97
+ return (v, v, min(255, v + 4))
98
+
99
+ def busy(x, y):
100
+ v = (x * 7 + y * 11) % 256
101
+ return (v, (v + 30) % 256, (v + 90) % 256)
102
+
103
+ cases = {
104
+ "sky": _rows(sky, 80, 60),
105
+ "haze": _rows(haze, 80, 60),
106
+ "night": _rows(night, 80, 60),
107
+ "letterbox": _rows(lambda x, y: (0, 0, 0) if y >= 48 else streak(x, y), 80, 60),
108
+ "stripes": _rows(lambda x, y: (240, 240, 240) if y % 4 < 2 else (10, 10, 10), 80, 60),
109
+ "busy": _rows(busy, 80, 60),
110
+ }
111
+ for name, grid in cases.items():
112
+ score, reason = corrupt_score_rgba(flat(grid), 80, 60)
113
+ assert score <= 0.2, (name, score, reason)
114
+ assert reason != "smear", name
115
+
116
+
117
+ def test_grey_block():
118
+ def pixel(x, y):
119
+ if 8 <= x < 70 and 8 <= y < 40:
120
+ return (128, 128, 128)
121
+ v = (x * 7 + y * 3) % 256
122
+ return (v, (v + 20) % 256, (v + 70) % 256)
123
+
124
+ score, reason = corrupt_score(png(80, 60, pixel))
125
+ assert reason == "grey_blocks"
126
+ assert score >= THRESHOLD
127
+
128
+
129
+ def test_green_block():
130
+ def pixel(x, y):
131
+ if 4 <= x < 70 and 8 <= y < 40:
132
+ return (0, 140, 0)
133
+ v = (x * 7 + y * 3) % 256
134
+ return (v, (v + 20) % 256, (v + 70) % 256)
135
+
136
+ score, reason = corrupt_score(png(80, 60, pixel))
137
+ assert reason == "green_blocks"
138
+ assert score >= THRESHOLD
139
+
140
+
141
+ def test_shift_seam_is_not_a_tear():
142
+ seam = 20
143
+
144
+ def pixel(x, y):
145
+ xx = x + 8 if y == seam + 1 else x
146
+ yy = seam if y == seam + 1 else y
147
+ v = (xx * 9 + yy * 29) % 256
148
+ return (v, v, v)
149
+
150
+ grid = [[pixel(x, y) for x in range(80)] for y in range(48)]
151
+ score, reason = corrupt_score_rgba(flat(grid), 80, 48)
152
+ assert reason != "tear"
153
+ assert score < THRESHOLD
154
+
155
+
156
+ def test_jpeg_missing_end_marker_is_truncated():
157
+ im = Image.new("RGB", (32, 32), (20, 40, 60))
158
+ buf = io.BytesIO()
159
+ im.save(buf, format="JPEG")
160
+ raw = buf.getvalue()
161
+ assert raw.endswith(b"\xff\xd9")
162
+ score, reason = corrupt_score(raw[:-2] + b"\x00\x11")
163
+ assert reason == "truncated"
164
+ assert score == 1.0
165
+
166
+
167
+ def test_rgba_parity_with_typescript():
168
+ """Identical pixels, identical rounded scores. See corrupt-detect.test.ts."""
169
+ band6 = flat(bottom_band(80, 100, 6))
170
+ band20 = flat(bottom_band(80, 100, 20))
171
+ black = [(0, 0, 0)] * (16 * 16)
172
+ busy = [((x * 3 + y * 5) % 256, 40, 90) for y in range(16) for x in range(16)]
173
+ assert corrupt_score_rgba(band6, 80, 100) == (0.714, "smear")
174
+ assert corrupt_score_rgba(band20, 80, 100) == (1.0, "smear")
175
+ assert corrupt_score_rgba(black, 16, 16) == (1.0, "blank")
176
+ assert corrupt_score_rgba(busy, 16, 16) == (0.0, "ok")
@@ -0,0 +1,72 @@
1
+ """Labelled frames and the local OFAN set. Skipped unless the directories are set.
2
+
3
+ CORRUPT_EVAL_DIR is the 12 PNG screenshots. A name starting with Screenshot is corrupt.
4
+ CORRUPT_EVAL_OFAN_DIR, when set, is scored too. Flagged frames are printed and drawn
5
+ onto apps/web/e2e/artifacts/corrupt-eval-ofan-flagged.png.
6
+ """
7
+
8
+ from __future__ import annotations
9
+
10
+ import os
11
+ from pathlib import Path
12
+
13
+ import pytest
14
+
15
+ pytest.importorskip("PIL")
16
+
17
+ from labeltify.corrupt import THRESHOLD, corrupt_analyse # noqa: E402
18
+
19
+ EVAL = os.environ.get("CORRUPT_EVAL_DIR")
20
+ OFAN = os.environ.get("CORRUPT_EVAL_OFAN_DIR")
21
+ SHEET = Path(__file__).resolve().parents[3] / "apps" / "web" / "e2e" / "artifacts" / "corrupt-eval-ofan-flagged.png"
22
+
23
+
24
+ def _images(folder: str) -> list[Path]:
25
+ root = Path(folder)
26
+ return sorted(p for p in root.rglob("*") if p.suffix.lower() in {".png", ".jpg", ".jpeg"} and p.is_file())
27
+
28
+
29
+ @pytest.mark.skipif(not EVAL, reason="CORRUPT_EVAL_DIR is not set")
30
+ def test_labelled_frames_clear_the_margin():
31
+ files = _images(EVAL or "")
32
+ assert len(files) == 12
33
+ print("file\tscore\treason\tsmear_frac\tstd")
34
+ for path in files:
35
+ report = corrupt_analyse(path.read_bytes())
36
+ corrupt = path.name.startswith("Screenshot")
37
+ print(f"{path.name}\t{report['score']}\t{report['reason']}\t{float(report['smear_frac']):.3f}\t{float(report['std']):.1f}")
38
+ if corrupt:
39
+ assert float(report["score"]) >= 0.8, path.name
40
+ else:
41
+ assert float(report["score"]) <= 0.2, path.name
42
+ assert float(report["score"]) < THRESHOLD
43
+
44
+
45
+ @pytest.mark.skipif(not OFAN, reason="CORRUPT_EVAL_OFAN_DIR is not set")
46
+ def test_ofan_flags_are_listed():
47
+ from PIL import Image
48
+
49
+ files = _images(OFAN or "")
50
+ flagged: list[tuple[float, str, Path]] = []
51
+ for path in files:
52
+ report = corrupt_analyse(path.read_bytes())
53
+ if float(report["score"]) >= THRESHOLD:
54
+ flagged.append((float(report["score"]), str(report["reason"]), path))
55
+ print(f"OFAN flagged {len(flagged)} of {len(files)}")
56
+ for score, reason, path in flagged:
57
+ print(f" {score:.3f} {reason} {path}")
58
+ thumbs = []
59
+ for _score, _reason, path in flagged:
60
+ im = Image.open(path).convert("RGB")
61
+ im.thumbnail((240, 160))
62
+ thumbs.append(im)
63
+ if not thumbs:
64
+ sheet = Image.new("RGB", (240, 40), (20, 20, 20))
65
+ else:
66
+ sheet = Image.new("RGB", (240 * len(thumbs), 160), (0, 0, 0))
67
+ x = 0
68
+ for im in thumbs:
69
+ sheet.paste(im, (x, 0))
70
+ x += 240
71
+ SHEET.parent.mkdir(parents=True, exist_ok=True)
72
+ sheet.save(SHEET)
File without changes
File without changes
File without changes