Mateo commited on
Commit
be27f23
·
verified ·
1 Parent(s): ee6a661

Sync from temporal-model/demo

Browse files
Files changed (3) hide show
  1. README.md +3 -2
  2. app.py +15 -9
  3. test_app.py +6 -6
README.md CHANGED
@@ -32,11 +32,12 @@ Frames of a sequence are ordered **by filename**, the production convention
32
  (`<prefix>_<YYYY-MM-DDTHH-MM-SS>.jpg`). A video is sampled into evenly spaced
33
  frames instead.
34
 
35
- The sequence tab answers with an animated GIF of the scored frames plus a
36
  frame-by-frame carousel under it. The model scores only the first `max_frames`
37
  (20) of a sequence, so the extra frames are reported as dropped and are shown in
38
  neither view — displaying frames the model never looked at would misrepresent
39
- the result.
 
40
 
41
  The trigger frame (earliest frame the model would have fired on) is off by
42
  default: it re-scores growing prefixes of each tube, which is slow on CPU.
 
32
  (`<prefix>_<YYYY-MM-DDTHH-MM-SS>.jpg`). A video is sampled into evenly spaced
33
  frames instead.
34
 
35
+ The sequence tab answers with an animated WebP of the scored frames plus a
36
  frame-by-frame carousel under it. The model scores only the first `max_frames`
37
  (20) of a sequence, so the extra frames are reported as dropped and are shown in
38
  neither view — displaying frames the model never looked at would misrepresent
39
+ the result. (WebP rather than GIF: a GIF's 256-colour palette is picked from the
40
+ photo and quantizes the magenta boxes away to grey.)
41
 
42
  The trigger frame (earliest frame the model would have fired on) is off by
43
  default: it re-scores growing prefixes of each tube, which is slow on CPU.
app.py CHANGED
@@ -36,8 +36,9 @@ IMAGE_SUFFIXES = {".jpg", ".jpeg", ".png", ".webp"}
36
  # palette disappears on a hazy frame. Tube 0 (usually the only one) is magenta.
37
  TUBE_PALETTE = ["#ff00ff", "#00e5ff", "#ffea00", "#00ff5f", "#ff6d00"]
38
  BOX_COLOR = TUBE_PALETTE[0]
39
- GIF_MAX_WIDTH = 900
40
- GIF_FRAME_MS = 600
 
41
 
42
  try:
43
  FONT = ImageFont.load_default(size=18)
@@ -132,22 +133,27 @@ def draw_boxes(image_path: Path | str, boxes: list[tuple]) -> Image.Image:
132
  return image
133
 
134
 
135
- def build_gif(frames: list[Image.Image]) -> str | None:
136
- """Write the annotated frames as a looping GIF; return its path."""
 
 
 
 
137
  if not frames:
138
  return None
139
- scale = min(1.0, GIF_MAX_WIDTH / frames[0].width)
140
  size = (round(frames[0].width * scale), round(frames[0].height * scale))
141
  resized = [f.resize(size) for f in frames]
142
- # ponytail: one GIF per run left in the OS temp dir (Gradio copies it into
143
  # its own cache); add explicit cleanup if the Space ever runs out of disk.
144
- fd, path = tempfile.mkstemp(suffix=".gif")
145
  os.close(fd)
146
  resized[0].save(
147
  path,
148
  save_all=True,
149
  append_images=resized[1:],
150
- duration=GIF_FRAME_MS,
 
151
  loop=0,
152
  )
153
  return path
@@ -231,7 +237,7 @@ def classify_sequence(
231
  if trigger == i:
232
  caption += " ⚡ trigger"
233
  gallery.append((draw_boxes(path, boxes_per_frame.get(i, [])), caption))
234
- animation = build_gif([image for image, _ in gallery])
235
 
236
  probabilities = [t["probability"] for t in kept if t["probability"] is not None]
237
  verdict = "🔥 **Smoke**" if output.is_positive else "✅ **No smoke**"
 
36
  # palette disappears on a hazy frame. Tube 0 (usually the only one) is magenta.
37
  TUBE_PALETTE = ["#ff00ff", "#00e5ff", "#ffea00", "#00ff5f", "#ff6d00"]
38
  BOX_COLOR = TUBE_PALETTE[0]
39
+ ANIM_MAX_WIDTH = 900
40
+ ANIM_FRAME_MS = 600
41
+ ANIM_QUALITY = 85
42
 
43
  try:
44
  FONT = ImageFont.load_default(size=18)
 
133
  return image
134
 
135
 
136
+ def build_animation(frames: list[Image.Image]) -> str | None:
137
+ """Write the annotated frames as a looping animation; return its path.
138
+
139
+ WebP, not GIF: a GIF's 256-colour palette is chosen from the photo, and the
140
+ few hundred magenta box pixels get quantized away to grey.
141
+ """
142
  if not frames:
143
  return None
144
+ scale = min(1.0, ANIM_MAX_WIDTH / frames[0].width)
145
  size = (round(frames[0].width * scale), round(frames[0].height * scale))
146
  resized = [f.resize(size) for f in frames]
147
+ # ponytail: one file per run left in the OS temp dir (Gradio copies it into
148
  # its own cache); add explicit cleanup if the Space ever runs out of disk.
149
+ fd, path = tempfile.mkstemp(suffix=".webp")
150
  os.close(fd)
151
  resized[0].save(
152
  path,
153
  save_all=True,
154
  append_images=resized[1:],
155
+ duration=ANIM_FRAME_MS,
156
+ quality=ANIM_QUALITY,
157
  loop=0,
158
  )
159
  return path
 
237
  if trigger == i:
238
  caption += " ⚡ trigger"
239
  gallery.append((draw_boxes(path, boxes_per_frame.get(i, [])), caption))
240
+ animation = build_animation([image for image, _ in gallery])
241
 
242
  probabilities = [t["probability"] for t in kept if t["probability"] is not None]
243
  verdict = "🔥 **Smoke**" if output.is_positive else "✅ **No smoke**"
test_app.py CHANGED
@@ -7,7 +7,7 @@ silently wrong (frame ordering, padded-index mapping, box geometry).
7
  import tempfile
8
  from pathlib import Path
9
 
10
- from app import build_gif, collect_frames, draw_boxes, input_index, tube_color
11
  from PIL import Image
12
 
13
 
@@ -55,12 +55,12 @@ def test_draw_boxes_marks_the_box_pixels():
55
  assert out.getpixel((100, 50)) == (0, 0, 0), "box interior stays untouched"
56
 
57
 
58
- def test_build_gif_is_animated_and_downscaled():
59
- assert build_gif([]) is None
60
  frames = [Image.new("RGB", (1800, 900), c) for c in ("red", "green", "blue")]
61
- gif = Image.open(build_gif(frames))
62
- assert gif.n_frames == 3
63
- assert gif.size == (900, 450), "wider frames are scaled down to GIF_MAX_WIDTH"
64
 
65
 
66
  def test_tube_color_cycles():
 
7
  import tempfile
8
  from pathlib import Path
9
 
10
+ from app import build_animation, collect_frames, draw_boxes, input_index, tube_color
11
  from PIL import Image
12
 
13
 
 
55
  assert out.getpixel((100, 50)) == (0, 0, 0), "box interior stays untouched"
56
 
57
 
58
+ def test_build_animation_is_animated_and_downscaled():
59
+ assert build_animation([]) is None
60
  frames = [Image.new("RGB", (1800, 900), c) for c in ("red", "green", "blue")]
61
+ anim = Image.open(build_animation(frames))
62
+ assert anim.n_frames == 3
63
+ assert anim.size == (900, 450), "wider frames scale down to ANIM_MAX_WIDTH"
64
 
65
 
66
  def test_tube_color_cycles():