Repository navigation
Expand file tree
/
Copy pathdatabase.py
More file actions
372 lines (324 loc) · 16.6 KB
/
Copy pathdatabase.py
File metadata and controls
372 lines (324 loc) · 16.6 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
"""
database.py — SQLite persistence layer for CVVision
"""
import sqlite3
import os
DB_PATH = os.path.join(os.path.dirname(__file__), "cvvision.db")
# ── Módulos / funciones metadata (fuente de verdad estática) ────────────────
MODULES_META = {
"personas": {
"label": "Detección de Personas",
"functions": {
"conteo": {"label": "Conteo de personas", "description": "Cuenta el número total de personas en escena"},
"permanencia": {"label": "Tiempo de permanencia", "description": "Mide el tiempo que cada persona permanece en zona"},
"heatmap": {"label": "Mapa de calor", "description": "Genera mapa de densidad de movimiento"},
},
},
"armas": {
"label": "Detección de Armas",
"functions": {
"deteccion_arma": {"label": "Detección de arma", "description": "Detecta presencia de armas en escena"},
"captura_rostro": {"label": "Captura automática de rostro", "description": "Captura y guarda foto del portador del arma"},
"tipo_arma": {"label": "Clasificar tipo de arma", "description": "Clasifica entre arma blanca o arma de fuego", "locked": True},
},
},
"acciones": {
"label": "Detección de Acciones",
"functions": {
"deteccion_acciones": {"label": "Detección de acciones", "description": "Dibuja esqueleto de pose COCO-17 sobre cada persona"},
"deteccion_violencia": {"label": "Alertas de violencia", "description": "Detecta golpes, patadas y agresión física"},
"deteccion_robo": {"label": "Alertas de robo / amenaza", "description": "Detecta brazo apuntando y posturas de amenaza"},
"deteccion_sospechosa":{"label": "Actividad sospechosa", "description": "Detecta agachado, rastreo y movimientos furtivos"},
},
},
"troncos": {
"label": "Conteo de Troncos",
"functions": {
"conteo": {"label": "Conteo de troncos", "description": "Cuenta troncos que cruzan la línea vertical"},
},
},
"pallets": {
"label": "Conteo de Pallets",
"functions": {
"conteo": {"label": "Conteo de pallets", "description": "Cuenta pallets dentro del área de reconocimiento"},
},
},
"cajas": {
"label": "Conteo de Cajas",
"functions": {
"conteo": {"label": "Conteo de cajas", "description": "Cuenta cajas que cruzan la línea horizontal"},
},
},
"reglamento": {
"label": "Detección de Reglamento",
"functions": {
"deteccion_botas": {"label": "Detección de botas", "description": "Detecta si las personas usan botas dentro del área"},
"conteo_tiempo": {"label": "Conteo de tiempo", "description": "Mide el tiempo de permanencia en el área"},
"alertas": {"label": "Alertas", "description": "Genera alertas visuales por incumplimiento"},
"analytics": {"label": "Analytics", "description": "Estadísticas y gráficos del módulo"},
},
},
"carga_descarga": {
"label": "Detección de Carga y Descarga",
"functions": {
"conteo": {"label": "Conteo de carga/descarga", "description": "Cuenta objetos que cruzan la línea de conteo"},
"analytics":{"label": "Analytics", "description": "Estadísticas y gráficos del módulo"},
},
},
}
def get_conn():
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
conn.execute("PRAGMA journal_mode=WAL")
return conn
def init_db():
with get_conn() as conn:
conn.executescript("""
CREATE TABLE IF NOT EXISTS settings (
key TEXT PRIMARY KEY,
value TEXT NOT NULL DEFAULT ''
);
CREATE TABLE IF NOT EXISTS modules (
module_id TEXT PRIMARY KEY,
enabled INTEGER NOT NULL DEFAULT 0
);
CREATE TABLE IF NOT EXISTS module_functions (
module_id TEXT NOT NULL,
func_id TEXT NOT NULL,
enabled INTEGER NOT NULL DEFAULT 0,
PRIMARY KEY (module_id, func_id)
);
CREATE TABLE IF NOT EXISTS sources (
id INTEGER PRIMARY KEY AUTOINCREMENT,
module_id TEXT NOT NULL,
name TEXT NOT NULL,
type TEXT NOT NULL CHECK(type IN ('video','stream')),
path TEXT NOT NULL,
created_at TEXT NOT NULL DEFAULT (datetime('now','localtime'))
);
CREATE TABLE IF NOT EXISTS reglamento_detections (
id INTEGER PRIMARY KEY AUTOINCREMENT,
source_id INTEGER NOT NULL,
track_id INTEGER NOT NULL,
boot_status TEXT NOT NULL,
time_compliance TEXT NOT NULL,
seconds_in_area REAL NOT NULL,
capture_path TEXT,
created_at TEXT NOT NULL DEFAULT (datetime('now','localtime'))
);
CREATE TABLE IF NOT EXISTS carga_descarga_detections (
id INTEGER PRIMARY KEY AUTOINCREMENT,
source_id INTEGER NOT NULL,
track_id INTEGER NOT NULL,
direction TEXT NOT NULL,
model_id TEXT,
created_at TEXT NOT NULL DEFAULT (datetime('now','localtime'))
);
""")
# Defaults de settings
for k, v in [("system_name", "Computer Vision"), ("version", "V2.0"), ("logo", ""), ("armas_model", ""), ("personas_model", ""), ("acciones_model", ""), ("troncos_model", ""), ("pallets_model", ""), ("cajas_model", ""), ("reglamento_model", ""), ("personas_conf", "0.35"), ("armas_conf", "0.20"), ("acciones_conf", "0.35"), ("troncos_conf", "0.35"), ("pallets_conf", "0.35"), ("cajas_conf", "0.35"), ("reglamento_conf", "0.45"), ("personas_half", "0"), ("armas_half", "0"), ("acciones_half", "0"), ("troncos_half", "0"), ("pallets_half", "0"), ("cajas_half", "0"), ("reglamento_half", "0"), ("personas_line_y", "85"), ("troncos_line_x", "50"), ("pallets_area_x1", "25"), ("pallets_area_y1", "25"), ("pallets_area_x2", "75"), ("pallets_area_y2", "75"), ("pallets_classes", "0,1,2,3"), ("cajas_line_y", "85"), ("reglamento_area_x1", "30"), ("reglamento_area_y1", "30"), ("reglamento_area_x2", "70"), ("reglamento_area_y2", "70"), ("reglamento_min_time", "10"), ("carga_descarga_conf", "0.35"), ("carga_descarga_half", "0"), ("carga_descarga_line_mode", "horizontal"), ("carga_descarga_line_pos", "50"), ("carga_descarga_cur_model", "0"), ("carga_descarga_models", "[]")]:
conn.execute("INSERT OR IGNORE INTO settings(key,value) VALUES(?,?)", (k, v))
# Seed módulos y funciones
for mod_id, meta in MODULES_META.items():
conn.execute("INSERT OR IGNORE INTO modules(module_id,enabled) VALUES(?,0)", (mod_id,))
for func_id, fmeta in meta["functions"].items():
# tipo_arma siempre habilitada (locked)
default_enabled = 1 if fmeta.get("locked") else 0
conn.execute(
"INSERT OR IGNORE INTO module_functions(module_id,func_id,enabled) VALUES(?,?,?)",
(mod_id, func_id, default_enabled),
)
# Forzar tipo_arma = 1 siempre (por si la BD existente tiene 0)
conn.execute(
"UPDATE module_functions SET enabled=1 WHERE module_id='armas' AND func_id='tipo_arma'"
)
conn.commit()
# ── Settings ────────────────────────────────────────────────────────────────
def get_settings():
with get_conn() as conn:
rows = conn.execute("SELECT key, value FROM settings").fetchall()
return {r["key"]: r["value"] for r in rows}
def set_setting(key, value):
with get_conn() as conn:
conn.execute("INSERT OR REPLACE INTO settings(key,value) VALUES(?,?)", (key, str(value)))
conn.commit()
# ── Módulos ─────────────────────────────────────────────────────────────────
def get_modules_state():
with get_conn() as conn:
mod_rows = conn.execute("SELECT module_id, enabled FROM modules").fetchall()
func_rows = conn.execute("SELECT module_id, func_id, enabled FROM module_functions").fetchall()
mod_enabled = {r["module_id"]: bool(r["enabled"]) for r in mod_rows}
func_enabled = {}
for r in func_rows:
func_enabled.setdefault(r["module_id"], {})[r["func_id"]] = bool(r["enabled"])
result = {}
for mod_id, meta in MODULES_META.items():
functions = {}
for func_id, fmeta in meta["functions"].items():
functions[func_id] = {
**fmeta,
"enabled": func_enabled.get(mod_id, {}).get(func_id, False),
}
result[mod_id] = {
"label": meta["label"],
"enabled": mod_enabled.get(mod_id, False),
"functions": functions,
}
return result
def db_toggle_module(module_id):
with get_conn() as conn:
row = conn.execute("SELECT enabled FROM modules WHERE module_id=?", (module_id,)).fetchone()
if row is None:
return None
new_val = 0 if row["enabled"] else 1
conn.execute("UPDATE modules SET enabled=? WHERE module_id=?", (new_val, module_id))
conn.commit()
return bool(new_val)
def db_toggle_function(module_id, func_id):
# Funciones bloqueadas (siempre activas) no se pueden togglear
fmeta = MODULES_META.get(module_id, {}).get("functions", {}).get(func_id, {})
if fmeta.get("locked"):
return None # indica "no modificable"
with get_conn() as conn:
row = conn.execute(
"SELECT enabled FROM module_functions WHERE module_id=? AND func_id=?",
(module_id, func_id),
).fetchone()
if row is None:
return None
new_val = 0 if row["enabled"] else 1
conn.execute(
"UPDATE module_functions SET enabled=? WHERE module_id=? AND func_id=?",
(new_val, module_id, func_id),
)
conn.commit()
return bool(new_val)
# ── Fuentes (sources) ────────────────────────────────────────────────────────
def get_sources(module_id=None):
with get_conn() as conn:
if module_id:
rows = conn.execute(
"SELECT * FROM sources WHERE module_id=? ORDER BY created_at DESC",
(module_id,),
).fetchall()
else:
rows = conn.execute(
"SELECT * FROM sources ORDER BY created_at DESC"
).fetchall()
return [dict(r) for r in rows]
def add_source(module_id, name, src_type, path):
with get_conn() as conn:
cur = conn.execute(
"INSERT INTO sources(module_id, name, type, path) VALUES(?,?,?,?)",
(module_id, name.strip(), src_type, path.strip()),
)
conn.commit()
row = conn.execute("SELECT * FROM sources WHERE id=?", (cur.lastrowid,)).fetchone()
return dict(row)
def update_source(source_id, name=None, path=None):
with get_conn() as conn:
if name is not None:
conn.execute("UPDATE sources SET name=? WHERE id=?", (name.strip(), source_id))
if path is not None:
conn.execute("UPDATE sources SET path=? WHERE id=?", (path.strip(), source_id))
conn.commit()
row = conn.execute("SELECT * FROM sources WHERE id=?", (source_id,)).fetchone()
return dict(row) if row else None
def delete_source(source_id):
with get_conn() as conn:
# Obtener path antes de borrar para eliminar archivo local si es un upload
row = conn.execute("SELECT path FROM sources WHERE id=?", (source_id,)).fetchone()
affected = conn.execute("DELETE FROM sources WHERE id=?", (source_id,)).rowcount
conn.commit()
return {"deleted": affected > 0, "path": dict(row)["path"] if row else None}
# ── Reglamento detections ────────────────────────────────────────────────────
def insert_reglamento_detection(source_id, track_id, boot_status,
time_compliance, seconds_in_area,
capture_path=None):
with get_conn() as conn:
conn.execute(
"""INSERT INTO reglamento_detections
(source_id, track_id, boot_status, time_compliance,
seconds_in_area, capture_path)
VALUES (?,?,?,?,?,?)""",
(source_id, track_id, boot_status, time_compliance,
seconds_in_area, capture_path),
)
conn.commit()
def get_reglamento_detections(source_id=None, days=7):
with get_conn() as conn:
if source_id:
rows = conn.execute(
"""SELECT * FROM reglamento_detections
WHERE source_id=? AND created_at >= datetime('now', ?)
ORDER BY created_at DESC""",
(source_id, f'-{days} days'),
).fetchall()
else:
rows = conn.execute(
"""SELECT * FROM reglamento_detections
WHERE created_at >= datetime('now', ?)
ORDER BY created_at DESC""",
(f'-{days} days',),
).fetchall()
return [dict(r) for r in rows]
def get_reglamento_analytics(source_id, days=7):
with get_conn() as conn:
rows = conn.execute(
"""SELECT boot_status, time_compliance,
COUNT(*) as count,
ROUND(AVG(seconds_in_area), 1) as avg_seconds
FROM reglamento_detections
WHERE source_id=? AND created_at >= datetime('now', ?)
GROUP BY boot_status, time_compliance""",
(source_id, f'-{days} days'),
).fetchall()
daily = conn.execute(
"""SELECT DATE(created_at) as day,
SUM(CASE WHEN boot_status='con_botas' THEN 1 ELSE 0 END) as con_botas,
SUM(CASE WHEN boot_status='sin_botas' THEN 1 ELSE 0 END) as sin_botas,
SUM(CASE WHEN time_compliance='cumplio' THEN 1 ELSE 0 END) as cumplimientos,
SUM(CASE WHEN time_compliance='incumplio' THEN 1 ELSE 0 END) as incumplimientos
FROM reglamento_detections
WHERE source_id=? AND created_at >= datetime('now', ?)
GROUP BY DATE(created_at)
ORDER BY day ASC""",
(source_id, f'-{days} days'),
).fetchall()
return {
"summary": [dict(r) for r in rows],
"daily": [dict(r) for r in daily],
}
# ── Carga / Descarga detections ─────────────────────────────────────────────
def insert_carga_descarga_detection(source_id, track_id, direction, model_id=None):
with get_conn() as conn:
conn.execute(
"""INSERT INTO carga_descarga_detections
(source_id, track_id, direction, model_id)
VALUES (?,?,?,?)""",
(source_id, track_id, direction, model_id),
)
conn.commit()
def get_carga_descarga_analytics(source_id, days=7):
with get_conn() as conn:
rows = conn.execute(
"""SELECT direction, COUNT(*) as count
FROM carga_descarga_detections
WHERE source_id=? AND created_at >= datetime('now', ?)
GROUP BY direction""",
(source_id, f'-{days} days'),
).fetchall()
daily = conn.execute(
"""SELECT DATE(created_at) as day,
SUM(CASE WHEN direction='in' THEN 1 ELSE 0 END) as entradas,
SUM(CASE WHEN direction='out' THEN 1 ELSE 0 END) as salidas
FROM carga_descarga_detections
WHERE source_id=? AND created_at >= datetime('now', ?)
GROUP BY DATE(created_at)
ORDER BY day ASC""",
(source_id, f'-{days} days'),
).fetchall()
return {
"summary": [dict(r) for r in rows],
"daily": [dict(r) for r in daily],
}