Fix PIL import in check_and_register (real infinite-loop cause); known_paths table for dup-path tracking

This commit is contained in:
2026-08-08 14:34:30 +10:00
parent 39b5a39334
commit a776722d53

View File

@@ -15,6 +15,7 @@ use walk_files for a streaming generator when batch_size is set.
from pathlib import Path
from prefect import flow, task
from PIL import Image
import photo_db as db
@@ -40,11 +41,15 @@ def find_unprocessed(base_dir: str, batch_size: int, source: str = None) -> list
"""Find the next batch of files NOT yet in the fingerprint DB."""
db.init_db()
conn = db.get_db()
conn.execute(
"CREATE TABLE IF NOT EXISTS known_paths (path TEXT PRIMARY KEY, sha256 TEXT NOT NULL)"
)
batch = []
for p_str in walk_files(base_dir):
# cheap check: path already known?
# cheap check: path already seen (image_hashes OR known_paths)
known = conn.execute(
"SELECT 1 FROM image_hashes WHERE path=?", (p_str,)
"SELECT 1 FROM image_hashes WHERE path=? UNION SELECT 1 FROM known_paths WHERE path=?",
(p_str, p_str),
).fetchone()
if known:
continue
@@ -54,10 +59,9 @@ def find_unprocessed(base_dir: str, batch_size: int, source: str = None) -> list
"SELECT 1 FROM image_hashes WHERE sha256=?", (sha,)
).fetchone()
if sha_known:
# record this path too, so we don't re-hash it every loop
# record this path in known_paths so we don't re-hash it every loop
conn.execute(
"INSERT OR IGNORE INTO image_hashes (sha256, phash, dhash, file_size, path, source) "
"SELECT sha256, phash, dhash, file_size, ?, source FROM image_hashes WHERE sha256=?",
"INSERT OR IGNORE INTO known_paths (path, sha256) VALUES (?,?)",
(p_str, sha))
continue
batch.append(p_str)
@@ -95,7 +99,7 @@ def check_and_register(image_paths: list[str], source: str) -> dict:
near_dups.append((p_str, near, dist))
continue
# new — register
with __import__("PIL.Image", fromlist=["Image"]).Image.open(p) as im:
with Image.open(p) as im:
w, h = im.size
conn.execute(
"INSERT INTO image_hashes (sha256, phash, dhash, file_size, width, height, path, source) "