labeltify 0.4.2__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.2
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
@@ -314,8 +314,8 @@ class LabelTifyClient:
314
314
  Every file is a ``file`` part; a file whose name ends in a label extension (.json, .jsonl, .xml,
315
315
  .txt, .yaml, .yml, .names, .csv, .zip) is a label file, anything else an image. With any label
316
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`` and ``corrupt_reason``
318
- for each image. ``skip_corrupt`` leaves truncated and blank frames out of the upload and reports
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
319
  them as skipped. Without Pillow both options do nothing.
320
320
  """
321
321
  if not files:
@@ -345,7 +345,7 @@ class LabelTifyClient:
345
345
  score_by_name: dict[int, tuple[float, str] | None] = {}
346
346
  if detect_corrupt or skip_corrupt:
347
347
  try:
348
- from .corrupt import corrupt_score
348
+ from .corrupt import CORRUPT_DETECTOR_VERSION, corrupt_score
349
349
  except ImportError:
350
350
  detect_corrupt = False
351
351
  else:
@@ -419,12 +419,15 @@ class LabelTifyClient:
419
419
  for f in files:
420
420
  file(f["filename"], f["content_type"], f["data"])
421
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))
422
425
  for n, _ in enumerate(images):
423
426
  h = next(hashes, None)
424
427
  if h:
425
428
  field("dhash", h.lower())
426
429
  scored = score_by_name.get(n) if detect_corrupt else None
427
- if detect_corrupt and any(score_by_name.get(i) is not None for i in range(len(images))):
430
+ if sending_scores:
428
431
  if scored is None:
429
432
  field("corrupt_score", "")
430
433
  field("corrupt_reason", "")
@@ -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.2
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
@@ -14,4 +14,5 @@ labeltify.egg-info/requires.txt
14
14
  labeltify.egg-info/top_level.txt
15
15
  tests/test_client.py
16
16
  tests/test_corrupt.py
17
+ tests/test_corrupt_eval.py
17
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.2"
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 not in {"dhash", "corrupt_score", "corrupt_reason"}}
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
 
@@ -464,6 +464,8 @@ def test_corrupt_fields_and_skip(api):
464
464
  assert reasons and all(r != "blank" for r in reasons)
465
465
  scores = [n for n, _, _ in sent if n == "corrupt_score"]
466
466
  assert scores
467
+ versions = [v.decode() for n, _, v in sent if n == "corrupt_detector_version"]
468
+ assert versions == ["2"]
467
469
 
468
470
 
469
471
  def test_no_dhash_without_asking(api, 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)
@@ -1,189 +0,0 @@
1
- """Cheap corrupt-frame heuristic. Same thresholds as `.corrupt-ref/detect.py`.
2
-
3
- Pillow only (``pip install labeltify[images]``). No numpy. A 240 px wide thumbnail.
4
- ``corrupt_score(data) -> (score, reason)``.
5
- """
6
-
7
- from __future__ import annotations
8
-
9
- import io
10
-
11
- TARGET_W = 240
12
- BLANK_STD_SOFT, BLANK_STD_HARD = 8.0, 4.0
13
- SMEAR_ROW_DIFF = 0.6
14
- SMEAR_REL = 0.08
15
- SMEAR_SOFT, SMEAR_HARD = 0.06, 0.15
16
- GREY_TOL = 3
17
- FLAT_GRAD = 0.75
18
- CODEC_SOFT, CODEC_HARD = 0.02, 0.05
19
- TEAR_REL = 2.5
20
- TEAR_GAIN = 0.5
21
- TEAR_MAXSHIFT = 96
22
- THRESHOLD = 0.5
23
-
24
-
25
- def _ramp(x: float, soft: float, hard: float) -> float:
26
- t = (x - soft) / (hard - soft)
27
- return float(min(1.0, max(0.0, t)))
28
-
29
-
30
- def _median(values: list[float]) -> float:
31
- if not values:
32
- return 0.0
33
- ordered = sorted(values)
34
- n = len(ordered)
35
- mid = n // 2
36
- if n % 2:
37
- return float(ordered[mid])
38
- return (ordered[mid - 1] + ordered[mid]) / 2.0
39
-
40
-
41
- def _thumb(im):
42
- from PIL import Image
43
-
44
- if im.width >= 2 * TARGET_W:
45
- im = im.reduce(max(1, im.width // TARGET_W))
46
- if im.width != TARGET_W:
47
- im = im.resize((TARGET_W, max(8, round(im.height * TARGET_W / im.width))), Image.BILINEAR)
48
- return im.convert("RGB")
49
-
50
-
51
- def _decode(data: bytes, allow_truncated: bool):
52
- from PIL import Image, ImageFile
53
-
54
- previous = ImageFile.LOAD_TRUNCATED_IMAGES
55
- if allow_truncated:
56
- ImageFile.LOAD_TRUNCATED_IMAGES = True
57
- try:
58
- im = Image.open(io.BytesIO(data))
59
- if im.format == "JPEG":
60
- im.draft("RGB", (TARGET_W, 1))
61
- im.load()
62
- return im
63
- finally:
64
- if allow_truncated:
65
- ImageFile.LOAD_TRUNCATED_IMAGES = previous
66
-
67
-
68
- def _load(data: bytes) -> tuple[list[tuple[int, int, int]], int, int, bool]:
69
- truncated = data[:2] == b"\xff\xd8" and not data[-32:].rstrip(b"\x00\r\n ").endswith(b"\xff\xd9")
70
- try:
71
- im = _decode(data, False)
72
- except OSError:
73
- truncated = True
74
- im = _decode(data, True)
75
- thumb = _thumb(im)
76
- w, h = thumb.size
77
- pixels = list(thumb.getdata())
78
- return pixels, w, h, truncated
79
-
80
-
81
- def _luma(rgb: tuple[int, int, int]) -> float:
82
- return 0.299 * rgb[0] + 0.587 * rgb[1] + 0.114 * rgb[2]
83
-
84
-
85
- def _metrics(pixels: list[tuple[int, int, int]], w: int, h: int) -> dict:
86
- g = [_luma(p) for p in pixels]
87
- n = w * h
88
- mean = sum(g) / n
89
- std = (sum((v - mean) ** 2 for v in g) / n) ** 0.5
90
- rd: list[float] = []
91
- for y in range(h - 1):
92
- row = y * w
93
- nxt = row + w
94
- rd.append(sum(abs(g[nxt + x] - g[row + x]) for x in range(w)) / w)
95
- med = _median(rd) + 1e-6
96
- rep = [v < SMEAR_ROW_DIFF or v < SMEAR_REL * med for v in rd]
97
- run = len(rep)
98
- for i in range(len(rep) - 1, -1, -1):
99
- if not rep[i]:
100
- run = len(rep) - 1 - i
101
- break
102
- start = h // 3
103
- mid = 0
104
- in_run = False
105
- run_start = 0
106
- slice_rep = rep[start:]
107
- for i, bit in enumerate(slice_rep + [False]):
108
- if bit and not in_run:
109
- in_run = True
110
- run_start = i
111
- elif not bit and in_run:
112
- mid = max(mid, i - run_start)
113
- in_run = False
114
- grey = 0
115
- if h >= 2 and w >= 2:
116
- for y in range(h - 1):
117
- for x in range(w - 1):
118
- i = y * w + x
119
- if abs(g[i + 1] - g[i]) + abs(g[i + w] - g[i]) >= FLAT_GRAD:
120
- continue
121
- r, gg, b = pixels[i]
122
- if abs(r - 128) < GREY_TOL and abs(gg - 128) < GREY_TOL and abs(b - 128) < GREY_TOL:
123
- grey += 1
124
- green = sum(1 for r, gg, b in pixels if gg > 90 and r < 50 and b < 50)
125
- tear = 0.0
126
- s = min(TEAR_MAXSHIFT, w * 2 // 5)
127
- if s >= 3 and len(rd) > 6:
128
- inner = list(range(3, len(rd) - 3))
129
- inner.sort(key=lambda i: (rd[i], i))
130
- for k in inner[-3:]:
131
- if rd[k] < TEAR_REL * med:
132
- continue
133
- d0 = _shift_mean(g, w, k, 0)
134
- best = float("inf")
135
- for shift in range(-s, s + 1):
136
- if -2 <= shift <= 2:
137
- continue
138
- val = _shift_mean(g, w, k, shift)
139
- if val < best:
140
- best = val
141
- gain = best / (d0 + 1e-6)
142
- t = _ramp(gain, TEAR_GAIN, TEAR_GAIN / 2)
143
- if t > tear:
144
- tear = t
145
- return {
146
- "std": std,
147
- "smear_frac": max(run, mid) / h if h else 0.0,
148
- "grey_frac": grey / n,
149
- "green_frac": green / n,
150
- "tear": tear,
151
- }
152
-
153
-
154
- def _shift_mean(g: list[float], w: int, k: int, shift: int) -> float:
155
- a0 = k * w
156
- b0 = (k + 1) * w
157
- total = 0.0
158
- count = 0
159
- for x in range(w):
160
- ax = x + shift
161
- if ax < 0 or ax >= w:
162
- continue
163
- total += abs(g[a0 + ax] - g[b0 + x])
164
- count += 1
165
- return total / count if count else float("inf")
166
-
167
-
168
- def _score(m: dict, truncated: bool) -> tuple[float, str]:
169
- if truncated:
170
- return 1.0, "truncated"
171
- scores = {
172
- "blank": _ramp(m["std"], BLANK_STD_SOFT, BLANK_STD_HARD),
173
- "smear": _ramp(m["smear_frac"], SMEAR_SOFT, SMEAR_HARD),
174
- "grey_blocks": _ramp(m["grey_frac"], CODEC_SOFT, CODEC_HARD),
175
- "green_blocks": _ramp(m["green_frac"], CODEC_SOFT, CODEC_HARD),
176
- "tear": m["tear"],
177
- }
178
- reason = max(scores, key=scores.get)
179
- best = scores[reason]
180
- return round(best, 3), (reason if best > 0 else "ok")
181
-
182
-
183
- def corrupt_score(data: bytes) -> tuple[float, str]:
184
- """Score encoded image bytes. ``(score, reason)`` with score in 0..1."""
185
- try:
186
- pixels, w, h, truncated = _load(data)
187
- except Exception:
188
- return 1.0, "unreadable"
189
- return _score(_metrics(pixels, w, h), truncated)
@@ -1,88 +0,0 @@
1
- """Synthetic frames only. 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
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 test_clean_scene_is_ok():
19
- data = png(240, 48, lambda x, y: ((v := (x * 3 + y * 5) % 256), (v + 40) % 256, (v + 90) % 256))
20
- score, reason = corrupt_score(data)
21
- assert reason == "ok"
22
- assert score < THRESHOLD
23
-
24
-
25
- def test_black_frame_is_blank():
26
- score, reason = corrupt_score(png(240, 32, lambda x, y: (0, 0, 0)))
27
- assert reason == "blank"
28
- assert score >= THRESHOLD
29
-
30
-
31
- def test_bottom_row_repeat_is_smear():
32
- def pixel(x, y):
33
- row = 27 if y >= 28 else y
34
- v = 40 + row * 6
35
- return (v, 30, 180)
36
-
37
- score, reason = corrupt_score(png(240, 40, pixel))
38
- assert reason == "smear"
39
- assert score >= THRESHOLD
40
-
41
-
42
- def test_grey_block():
43
- def pixel(x, y):
44
- if 20 <= x < 200 and 8 <= y < 40:
45
- return (128, 128, 128)
46
- v = (x * 7 + y * 3) % 256
47
- return (v, (v + 20) % 256, (v + 70) % 256)
48
-
49
- score, reason = corrupt_score(png(240, 60, pixel))
50
- assert reason == "grey_blocks"
51
- assert score >= THRESHOLD
52
-
53
-
54
- def test_green_block():
55
- def pixel(x, y):
56
- if 10 <= x < 200 and 8 <= y < 40:
57
- return (0, 140, 0)
58
- v = (x * 7 + y * 3) % 256
59
- return (v, (v + 20) % 256, (v + 70) % 256)
60
-
61
- score, reason = corrupt_score(png(240, 60, pixel))
62
- assert reason == "green_blocks"
63
- assert score >= THRESHOLD
64
-
65
-
66
- def test_shift_seam_is_tear():
67
- seam = 20
68
-
69
- def pixel(x, y):
70
- xx = x + 8 if y == seam + 1 else x
71
- yy = seam if y == seam + 1 else y
72
- v = (xx * 3 + yy) % 256
73
- return (v, v, v)
74
-
75
- score, reason = corrupt_score(png(240, 48, pixel))
76
- assert reason == "tear"
77
- assert score >= THRESHOLD
78
-
79
-
80
- def test_jpeg_missing_end_marker_is_truncated():
81
- im = Image.new("RGB", (32, 32), (20, 40, 60))
82
- buf = io.BytesIO()
83
- im.save(buf, format="JPEG")
84
- raw = buf.getvalue()
85
- assert raw.endswith(b"\xff\xd9")
86
- score, reason = corrupt_score(raw[:-2] + b"\x00\x11")
87
- assert reason == "truncated"
88
- assert score == 1.0
File without changes
File without changes
File without changes