-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathphilately_tool.py
More file actions
396 lines (328 loc) · 11.5 KB
/
philately_tool.py
File metadata and controls
396 lines (328 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
import sys
import platform
# --- Optional: pysqlite3 override (used on some Linux/macOS setups) ---
try:
__import__("pysqlite3")
sys.modules["sqlite3"] = sys.modules.pop("pysqlite3")
except ImportError:
pass
# --- Platform-specific sqlite backend ---
if platform.system() == "Windows":
import sqlite3
else:
try:
import sqlean as sqlite3
except ImportError:
import sqlite3
import argparse
import json
from pathlib import Path
import cv2
import numpy as np
from PIL import Image
import inspect
from tqdm import tqdm
from rich.console import Console
from rich.panel import Panel
from rich.table import Table
from ultralytics import YOLO
from rembg import remove, new_session
from sentence_transformers import SentenceTransformer
import sqlite_vec
from sqlite_vec import serialize_float32
import os
def resource_path(relative_path):
"""Get absolute path to resource, works for dev and PyInstaller"""
if hasattr(sys, "_MEIPASS"):
return os.path.join(sys._MEIPASS, relative_path)
return os.path.join(os.path.abspath("."), relative_path)
# -----------------------------
# Configuration Management
# -----------------------------
console = Console()
CONFIG_PATH = Path("philately.json")
DEFAULT_CONFIG = {
"db_path": "philately.db",
"stamps_dir": "stamps",
"model_path": "model.pt",
"clip_model_name": "clip-ViT-B-32",
"vector_dimension": 512,
"margin_percent": 0.02,
"default_top": 5,
"default_distance": 1.0,
"yolo_conf": 0.4,
"rembg_model": "u2netp"
}
def load_config():
if not CONFIG_PATH.exists():
CONFIG_PATH.write_text(json.dumps(DEFAULT_CONFIG, indent=4))
console.print(f"Created default config at {CONFIG_PATH}", style="yellow")
local_cfg = json.loads(CONFIG_PATH.read_text())
return {**DEFAULT_CONFIG, **local_cfg}
CFG = load_config()
DB_PATH = Path(CFG["db_path"])
STAMPS_DIR = Path(CFG["stamps_dir"])
session = new_session(CFG["rembg_model"])
# -----------------------------
# Helpers
# -----------------------------
def banner(text):
console.print(Panel(text, style="bold cyan"))
def get_db_connection():
conn = sqlite3.connect(DB_PATH)
conn.enable_load_extension(True)
sqlite_vec.load(conn)
return conn
# -----------------------------
# Init
# -----------------------------
def init_project(on_status=None):
if DB_PATH.exists():
DB_PATH.unlink()
msg = f"Existing database {DB_PATH} deleted"
console.print(msg, style="yellow")
if on_status:
on_status(msg)
conn = get_db_connection()
conn.execute("""
CREATE TABLE stamps (
id INTEGER PRIMARY KEY,
album TEXT,
page TEXT,
stamp TEXT
)
""")
conn.execute(f"""
CREATE VIRTUAL TABLE stamp_vec USING vec0(
embedding FLOAT[{CFG['vector_dimension']}] distance_metric=cosine
)
""")
conn.commit()
conn.close()
STAMPS_DIR.mkdir(exist_ok=True)
msg = "Project initialized with settings from JSON"
console.print(msg, style="bold green")
if on_status:
on_status(msg)
# -----------------------------
# Core Logic (GUI-safe)
# -----------------------------
def extract_stamps(
album_folder,
do_index=False,
use_rembg=False,
on_status=None,
on_progress=None,
on_error=None,
):
try:
# Normalize callbacks: GUI may pass progress/status swapped positionally.
def _positional_params(fn):
try:
sig = inspect.signature(fn)
return len(
[p for p in sig.parameters.values() if p.kind in (p.POSITIONAL_ONLY, p.POSITIONAL_OR_KEYWORD)]
)
except Exception:
return None
if callable(on_status) and callable(on_progress):
a_status = _positional_params(on_status)
a_progress = _positional_params(on_progress)
# If they were swapped (progress fn provided where status expected)
if a_status == 2 and a_progress == 1:
on_status, on_progress = on_progress, on_status
# If only one callback provided but it looks like a progress function,
# move it to on_progress so status calls won't try to call it with a single arg.
if callable(on_status) and (on_progress is None):
a = _positional_params(on_status)
if a == 2:
on_progress = on_status
on_status = None
# If only on_progress provided but it looks like a status function, move it.
if callable(on_progress) and (on_status is None):
a = _positional_params(on_progress)
if a == 1:
on_status = on_progress
on_progress = None
msg = f"Loading YOLO: {CFG['model_path']}"
banner(msg)
if on_status:
on_status(msg)
model = YOLO(resource_path(CFG["model_path"]))
embedder = None
conn = None
cur = None
if do_index:
msg = f"Loading CLIP: {CFG['clip_model_name']}"
banner(msg)
if on_status:
on_status(msg)
embedder = SentenceTransformer(CFG["clip_model_name"])
conn = get_db_connection()
cur = conn.cursor()
margin_pct = CFG["margin_percent"]
images = [
p for p in Path(album_folder).glob("*.*")
if p.name != ".DS_Store"
]
total = len(images)
for idx, img_path in enumerate(images, start=1):
msg = f"Processing File: {img_path.name} ({idx}/{total})"
banner(msg)
if on_status:
on_status(msg)
if on_progress:
on_progress(idx, total)
results = model(str(img_path), conf=CFG["yolo_conf"], verbose=False)[0]
if results.boxes is None or results.orig_img is None:
continue
img = results.orig_img
h, w = img.shape[:2]
for i, box in enumerate(results.boxes):
x1, y1, x2, y2 = map(int, box.xyxy[0])
bw, bh = x2 - x1, y2 - y1
x1 = max(0, int(x1 - bw * margin_pct))
y1 = max(0, int(y1 - bh * margin_pct))
x2 = min(w, int(x2 + bw * margin_pct))
y2 = min(h, int(y2 + bh * margin_pct))
crop = img[y1:y2, x1:x2]
if crop.size == 0:
continue
stamp = Image.fromarray(
cv2.cvtColor(crop, cv2.COLOR_BGR2RGB)
).convert("RGBA")
if use_rembg:
transparent = remove(
stamp, session=session, alpha_matting=True
)
bbox = transparent.getbbox()
final_img = transparent.crop(bbox) if bbox else transparent
else:
final_img = stamp
name = f"{Path(album_folder).name}_{img_path.stem}_seg{i}.png"
final_img.save(STAMPS_DIR / name)
if do_index:
cur.execute(
"INSERT INTO stamps(album,page,stamp) VALUES (?,?,?)",
(Path(album_folder).name, img_path.name, name),
)
stamp_id = cur.lastrowid
vec = embedder.encode(
final_img.convert("RGB")
).astype(np.float32)
cur.execute(
"INSERT INTO stamp_vec(rowid, embedding) VALUES (?,?)",
(stamp_id, serialize_float32(vec)),
)
if do_index:
conn.commit()
conn.close()
msg = "Task complete"
console.print(msg, style="bold green")
if on_status:
on_status(msg)
except Exception as e:
if on_error:
on_error(e)
else:
raise
def perform_search(
query_type,
query_val,
top,
distance,
on_status=None,
on_result=None,
on_error=None,
):
try:
msg = f"{query_type.title()} Search"
banner(msg)
if on_status:
on_status(msg)
embedder = SentenceTransformer(CFG["clip_model_name"])
query_input = (
Image.open(query_val).convert("RGB")
if query_type == "image"
else query_val
)
qvec = embedder.encode(query_input).astype(np.float32)
conn = get_db_connection()
rows = conn.execute(
"""
SELECT stamps.album, stamps.page, stamps.stamp,
vec_distance_cosine(stamp_vec.embedding, ?) AS distance
FROM stamp_vec
JOIN stamps ON stamp_vec.rowid = stamps.id
WHERE vec_distance_cosine(stamp_vec.embedding, ?) <= ?
ORDER BY distance ASC
LIMIT ?
""",
(
serialize_float32(qvec),
serialize_float32(qvec),
distance,
top,
),
).fetchall()
conn.close()
if on_result:
on_result(rows)
else:
render_results(rows)
return rows
except Exception as e:
if on_error:
on_error(e)
else:
raise
def render_results(rows):
if not rows:
console.print("No results found", style="bold red")
return
table = Table(title="Search Results")
table.add_column("Album")
table.add_column("Page")
table.add_column("Stamp")
table.add_column("Distance", justify="right")
for r in rows:
table.add_row(r[0], r[1], r[2], f"{r[3]:.4f}")
console.print(table)
# -----------------------------
# CLI Entry Point (unchanged)
# -----------------------------
def main():
parser = argparse.ArgumentParser("Philately Tool")
sub = parser.add_subparsers(dest="cmd")
sub.add_parser("init")
extract = sub.add_parser("extract")
extract.add_argument("folder")
extract.add_argument("--rembg", action="store_true")
index = sub.add_parser("index")
index.add_argument("folder")
index.add_argument("--rembg", action="store_true")
s_img = sub.add_parser("search_image")
s_img.add_argument("image")
s_img.add_argument("--top", type=int, default=CFG["default_top"])
s_img.add_argument("--distance", type=float, default=CFG["default_distance"])
s_txt = sub.add_parser("search_text")
s_txt.add_argument("text")
s_txt.add_argument("--top", type=int, default=CFG["default_top"])
s_txt.add_argument("--distance", type=float, default=CFG["default_distance"])
args = parser.parse_args()
if args.cmd == "init":
init_project()
elif args.cmd in ["extract", "index"]:
extract_stamps(
args.folder,
do_index=(args.cmd == "index"),
use_rembg=args.rembg,
)
elif args.cmd == "search_image":
perform_search("image", args.image, args.top, args.distance)
elif args.cmd == "search_text":
perform_search("text", args.text, args.top, args.distance)
else:
parser.print_help()
if __name__ == "__main__":
main()