mirror of
https://github.com/MrSleeps/jellyfin-scene-search.git
synced 2026-11-19 00:03:36 +00:00
247 lines
9.0 KiB
Python
247 lines
9.0 KiB
Python
from __future__ import annotations
|
|
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from PIL import Image
|
|
|
|
from scene_search.caption_windows import (
|
|
build_contact_sheet,
|
|
caption_window_policy,
|
|
group_caption_windows,
|
|
load_media_caption_windows,
|
|
restore_media_caption_windows,
|
|
save_caption_window,
|
|
)
|
|
from scene_search.database import connect, init_db
|
|
|
|
|
|
class FakeFrameSource:
|
|
def image(self, row):
|
|
value = int(row["frame_number"]) * 20
|
|
return Image.new("RGB", (20, 10), (value, value, value))
|
|
|
|
|
|
def routed_frame(index: int, *, route: str = "general"):
|
|
row = {
|
|
"id": f"frame-{index}",
|
|
"media_id": "media-1",
|
|
"frame_number": index,
|
|
"timestamp_ms": index * 5000,
|
|
"interval_ms": 5000,
|
|
"title": "Movie",
|
|
}
|
|
score = 0.9 if route == "specialist" else 0.1
|
|
return (row, route, score, "score")
|
|
|
|
|
|
class CaptionWindowTests(unittest.TestCase):
|
|
def test_groups_thirty_seconds_and_promotes_any_specialist_member(self) -> None:
|
|
routed = [routed_frame(index) for index in range(6)]
|
|
routed[3] = routed_frame(3, route="specialist")
|
|
|
|
windows = group_caption_windows(
|
|
routed,
|
|
window_seconds=30,
|
|
max_frames=3,
|
|
route_model="router-model",
|
|
route_policy="policy-v1",
|
|
)
|
|
|
|
self.assertEqual(len(windows), 1)
|
|
window = windows[0]
|
|
self.assertEqual(window.start_ms, 0)
|
|
self.assertEqual(window.end_ms, 30000)
|
|
self.assertEqual(window.route, "specialist")
|
|
self.assertEqual(len(window.members), 6)
|
|
self.assertEqual(len(window.sample_rows), 3)
|
|
self.assertEqual(window.sample_rows[0]["frame_number"], 0)
|
|
self.assertEqual(window.sample_rows[-1]["frame_number"], 5)
|
|
self.assertEqual(
|
|
window.window_policy,
|
|
caption_window_policy(30, 3),
|
|
)
|
|
|
|
def test_contact_sheet_is_chronological_horizontal_sequence(self) -> None:
|
|
window = group_caption_windows(
|
|
[routed_frame(index) for index in range(3)],
|
|
window_seconds=30,
|
|
max_frames=3,
|
|
route_model="router-model",
|
|
route_policy="policy-v1",
|
|
)[0]
|
|
|
|
sheet = build_contact_sheet(FakeFrameSource(), window, gap_px=4)
|
|
try:
|
|
self.assertEqual(sheet.size, (68, 10))
|
|
self.assertEqual(sheet.getpixel((0, 0)), (0, 0, 0))
|
|
self.assertEqual(sheet.getpixel((24, 0)), (20, 20, 20))
|
|
self.assertEqual(sheet.getpixel((48, 0)), (40, 40, 40))
|
|
finally:
|
|
sheet.close()
|
|
|
|
def test_save_is_window_canonical_and_clears_legacy_frame_captions(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
db_path = Path(tmp) / "scenes.sqlite3"
|
|
init_db(db_path)
|
|
with connect(db_path) as con:
|
|
con.execute(
|
|
"""
|
|
INSERT INTO media (
|
|
id, source_path, title, content_sha256, size_bytes,
|
|
mtime_ns, interval_ms, model_id, embedding_dim
|
|
)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)
|
|
""",
|
|
(
|
|
"media-1", "/movie.mkv", "Movie", "hash", 0, 0,
|
|
5000, "embed-model", 1,
|
|
),
|
|
)
|
|
for index in range(3):
|
|
con.execute(
|
|
"""
|
|
INSERT INTO frames (
|
|
id, media_id, frame_number, timestamp_ms, thumb_path,
|
|
embedding_f16, caption, caption_model, captioned_at,
|
|
caption_route, route_policy
|
|
)
|
|
VALUES (?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP, ?, ?)
|
|
""",
|
|
(
|
|
f"frame-{index}", "media-1", index, index * 5000,
|
|
f"/frame-{index}.jpg", b"\x00\x00",
|
|
f"legacy {index}", "old-model", "general", "policy-v1",
|
|
),
|
|
)
|
|
|
|
window = group_caption_windows(
|
|
[routed_frame(index) for index in range(3)],
|
|
window_seconds=30,
|
|
max_frames=3,
|
|
route_model="router-model",
|
|
route_policy="policy-v1",
|
|
)[0]
|
|
ids = save_caption_window(
|
|
db_path,
|
|
window,
|
|
caption="A person walks across the room.",
|
|
model="general-model",
|
|
)
|
|
|
|
self.assertEqual(ids, ["frame-0", "frame-1", "frame-2"])
|
|
with connect(db_path) as con:
|
|
stored = con.execute(
|
|
"SELECT * FROM caption_windows WHERE id = ?",
|
|
(window.id,),
|
|
).fetchone()
|
|
members = con.execute(
|
|
"""
|
|
SELECT frame_id
|
|
FROM caption_window_frames
|
|
WHERE window_id = ?
|
|
ORDER BY position
|
|
""",
|
|
(window.id,),
|
|
).fetchall()
|
|
frames = con.execute(
|
|
"SELECT caption, meili_synced_at FROM frames ORDER BY frame_number"
|
|
).fetchall()
|
|
|
|
self.assertEqual(stored["caption"], "A person walks across the room.")
|
|
self.assertEqual(
|
|
[row["frame_id"] for row in members],
|
|
["frame-0", "frame-1", "frame-2"],
|
|
)
|
|
self.assertTrue(all(row["caption"] is None for row in frames))
|
|
self.assertTrue(all(row["meili_synced_at"] is None for row in frames))
|
|
|
|
def test_snapshot_restore_preserves_window_across_media_rebuild(self) -> None:
|
|
with tempfile.TemporaryDirectory() as tmp:
|
|
db_path = Path(tmp) / "scenes.sqlite3"
|
|
init_db(db_path)
|
|
with connect(db_path) as con:
|
|
con.execute(
|
|
"""
|
|
INSERT INTO media (
|
|
id, source_path, title, content_sha256, size_bytes,
|
|
mtime_ns, interval_ms, model_id, embedding_dim
|
|
)
|
|
VALUES ('media-1', '/movie.mkv', 'Movie', 'hash', 0, 0, 5000,
|
|
'embed-model', 1)
|
|
"""
|
|
)
|
|
for index in range(2):
|
|
con.execute(
|
|
"""
|
|
INSERT INTO frames (
|
|
id, media_id, frame_number, timestamp_ms,
|
|
thumb_path, embedding_f16
|
|
)
|
|
VALUES (?, 'media-1', ?, ?, ?, ?)
|
|
""",
|
|
(
|
|
f"frame-{index}", index, index * 5000,
|
|
f"/frame-{index}.jpg", b"\x00\x00",
|
|
),
|
|
)
|
|
|
|
window = group_caption_windows(
|
|
[routed_frame(0), routed_frame(1)],
|
|
window_seconds=30,
|
|
max_frames=3,
|
|
route_model="router-model",
|
|
route_policy="policy-v1",
|
|
)[0]
|
|
save_caption_window(
|
|
db_path,
|
|
window,
|
|
caption="Two moments from one scene.",
|
|
model="general-model",
|
|
)
|
|
snapshot = load_media_caption_windows(db_path, "media-1")
|
|
|
|
with connect(db_path) as con:
|
|
con.execute("DELETE FROM media WHERE id = 'media-1'")
|
|
con.execute(
|
|
"""
|
|
INSERT INTO media (
|
|
id, source_path, title, content_sha256, size_bytes,
|
|
mtime_ns, interval_ms, model_id, embedding_dim
|
|
)
|
|
VALUES ('media-1', '/movie.mkv', 'Movie', 'hash', 0, 0, 5000,
|
|
'embed-model', 1)
|
|
"""
|
|
)
|
|
for index in range(2):
|
|
con.execute(
|
|
"""
|
|
INSERT INTO frames (
|
|
id, media_id, frame_number, timestamp_ms,
|
|
thumb_path, embedding_f16
|
|
)
|
|
VALUES (?, 'media-1', ?, ?, ?, ?)
|
|
""",
|
|
(
|
|
f"frame-{index}", index, index * 5000,
|
|
f"/frame-{index}.jpg", b"\x00\x00",
|
|
),
|
|
)
|
|
restored = restore_media_caption_windows(
|
|
con,
|
|
snapshot,
|
|
{"frame-0", "frame-1"},
|
|
)
|
|
|
|
self.assertEqual(restored, 1)
|
|
with connect(db_path) as con:
|
|
row = con.execute(
|
|
"SELECT caption FROM caption_windows"
|
|
).fetchone()
|
|
self.assertEqual(row["caption"], "Two moments from one scene.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
unittest.main()
|