Repository navigation
Expand file tree
/
Copy pathdatabase.py
More file actions
81 lines (64 loc) · 1.95 KB
/
Copy pathdatabase.py
File metadata and controls
81 lines (64 loc) · 1.95 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
import sqlite3
DATABASE_NAME = "detections.db"
def get_connection():
"""One place to open the DB. WAL + timeout stops 'database is locked'
errors when app.py is writing while the dashboard is reading."""
conn = sqlite3.connect(DATABASE_NAME, timeout=10)
conn.execute("PRAGMA journal_mode=WAL")
return conn
def create_database():
conn = get_connection()
cursor = conn.cursor()
cursor.execute("""
CREATE TABLE IF NOT EXISTS detections(
id INTEGER PRIMARY KEY AUTOINCREMENT,
track_id INTEGER,
person_id TEXT,
name TEXT,
confidence REAL,
image_path TEXT,
detected_time TEXT
)
""")
# --- migration for databases created before person_id existed ---
columns = [row[1] for row in cursor.execute("PRAGMA table_info(detections)")]
if "person_id" not in columns:
cursor.execute("ALTER TABLE detections ADD COLUMN person_id TEXT")
# Old rows: known people keep their name as person_id.
# Old unknown rows can't be merged (no face data), so each stays separate.
cursor.execute("""
UPDATE detections
SET person_id = CASE
WHEN name = 'Unknown' THEN 'Legacy_Unknown_' || id
ELSE name
END
WHERE person_id IS NULL
""")
cursor.execute(
"CREATE INDEX IF NOT EXISTS idx_detections_person ON detections(person_id)"
)
conn.commit()
conn.close()
def save_detection(
track_id,
name,
confidence,
image_path,
detected_time,
person_id=None,
):
if person_id is None:
person_id = name
conn = get_connection()
cursor = conn.cursor()
cursor.execute(
"""
INSERT INTO detections
(track_id, person_id, name, confidence, image_path, detected_time)
VALUES (?, ?, ?, ?, ?, ?)
""",
(track_id, person_id, name, confidence, image_path, detected_time),
)
conn.commit()
conn.close()
create_database()