IdentificacionIA/seguimiento2.py

595 lines
30 KiB
Python
Raw Normal View History

2026-03-18 17:45:30 +00:00
import cv2
import numpy as np
import time
import threading
from scipy.optimize import linear_sum_assignment
from scipy.spatial.distance import cosine
from ultralytics import YOLO
import onnxruntime as ort
import os
# ──────────────────────────────────────────────────────────────────────────────
# CONFIGURACIÓN DEL SISTEMA
# ──────────────────────────────────────────────────────────────────────────────
USUARIO, PASSWORD, IP_DVR = "admin", "TCA200503", "192.168.1.244"
2026-03-18 17:45:30 +00:00
SECUENCIA = [1, 7, 5, 8, 3, 6]
# 🛡️ RED ESTABILIZADA (Timeout de 3s para evitar congelamientos de FFmpeg)
os.environ["OPENCV_FFMPEG_CAPTURE_OPTIONS"] = "rtsp_transport;tcp|stimeout;3000000"
2026-03-18 17:45:30 +00:00
URLS = [f"rtsp://{USUARIO}:{PASSWORD}@{IP_DVR}:554/Streaming/Channels/{i}02" for i in SECUENCIA]
2026-03-18 18:27:26 +00:00
ONNX_MODEL_PATH = "osnet_x0_25_msmt17.onnx"
2026-03-18 17:45:30 +00:00
VECINOS = {
"1": ["7"], "7": ["1", "5"], "5": ["7", "8"],
"8": ["5", "3"], "3": ["8", "6"], "6": ["3"]
}
2026-03-18 18:27:26 +00:00
ASPECT_RATIO_MIN = 0.5
ASPECT_RATIO_MAX = 4.0
AREA_MIN_CALIDAD = 1200
FRAMES_CALIDAD = 2
TIEMPO_MAX_AUSENCIA = 800.0
UMBRAL_REID_MISMA_CAM = 0.53 # Antes 0.65
UMBRAL_REID_VECINO = 0.48 # Antes 0.53
UMBRAL_REID_NO_VECINO = 0.67 # Antes 0.72
MAX_FIRMAS_MEMORIA = 10
2026-03-18 18:27:26 +00:00
C_CANDIDATO = (150, 150, 150)
C_LOCAL = (0, 255, 0)
C_GLOBAL = (0, 165, 255)
C_GRUPO = (0, 0, 255)
C_APRENDIZAJE = (255, 255, 0)
2026-03-18 17:45:30 +00:00
FUENTE = cv2.FONT_HERSHEY_SIMPLEX
# ──────────────────────────────────────────────────────────────────────────────
# INICIALIZACIÓN OSNET
2026-03-18 17:45:30 +00:00
# ──────────────────────────────────────────────────────────────────────────────
2026-03-18 18:27:26 +00:00
print("Cargando cerebro de Re-Identificación (OSNet)...")
2026-03-18 17:45:30 +00:00
try:
ort_session = ort.InferenceSession(ONNX_MODEL_PATH, providers=['CPUExecutionProvider'])
2026-03-18 18:27:26 +00:00
input_name = ort_session.get_inputs()[0].name
print("Modelo OSNet cargado exitosamente.")
2026-03-18 17:45:30 +00:00
except Exception as e:
2026-03-18 18:27:26 +00:00
print(f"ERROR FATAL: No se pudo cargar {ONNX_MODEL_PATH}.")
2026-03-18 17:45:30 +00:00
exit()
MEAN = np.array([0.485, 0.456, 0.406], dtype=np.float32).reshape(1, 3, 1, 1)
STD = np.array([0.229, 0.224, 0.225], dtype=np.float32).reshape(1, 3, 1, 1)
# ──────────────────────────────────────────────────────────────────────────────
# 1. EXTRACCIÓN DE FIRMAS (Deep + Color + Textura)
2026-03-18 17:45:30 +00:00
# ──────────────────────────────────────────────────────────────────────────────
def analizar_calidad(box):
x1, y1, x2, y2 = box
w, h = x2 - x1, y2 - y1
2026-03-18 18:27:26 +00:00
if w <= 0 or h <= 0: return False
2026-03-18 17:45:30 +00:00
return (ASPECT_RATIO_MIN < (h / w) < ASPECT_RATIO_MAX) and ((w * h) > AREA_MIN_CALIDAD)
def preprocess_onnx(roi):
img = cv2.resize(roi, (128, 256))
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
2026-03-18 18:27:26 +00:00
img = img.transpose(2, 0, 1).astype(np.float32) / 255.0
img = np.expand_dims(img, axis=0)
img = (img - MEAN) / STD
2026-03-18 17:45:30 +00:00
return img
2026-03-18 18:27:26 +00:00
def extraer_color_zonas(img):
h_roi = img.shape[0]
t1, t2 = int(h_roi * 0.15), int(h_roi * 0.55)
2026-03-18 18:27:26 +00:00
zonas = [img[:t1, :], img[t1:t2, :], img[t2:, :]]
2026-03-18 17:45:30 +00:00
def hist_zona(z):
2026-03-18 18:27:26 +00:00
if z.size == 0: return np.zeros(16 * 8)
hsv = cv2.cvtColor(z, cv2.COLOR_BGR2HSV)
2026-03-18 17:45:30 +00:00
hist = cv2.calcHist([hsv], [0, 1], None, [16, 8], [0, 180, 0, 256])
cv2.normalize(hist, hist)
return hist.flatten()
return np.concatenate([hist_zona(z) for z in zonas])
def extraer_textura_rapida(roi):
if roi.size == 0: return np.zeros(16)
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
gray_eq = cv2.equalizeHist(gray)
gx = cv2.Sobel(gray_eq, cv2.CV_32F, 1, 0, ksize=3)
gy = cv2.Sobel(gray_eq, cv2.CV_32F, 0, 1, ksize=3)
mag, _ = cv2.cartToPolar(gx, gy)
hist = cv2.calcHist([mag], [0], None, [16], [0, 256])
cv2.normalize(hist, hist)
return hist.flatten()
def extraer_firma_hibrida(frame_hd, box_480):
2026-03-18 17:45:30 +00:00
try:
h_hd, w_hd = frame_hd.shape[:2]
escala_x = w_hd / 480.0
escala_y = h_hd / 270.0
2026-03-18 18:27:26 +00:00
x1, y1, x2, y2 = box_480
x1_hd, y1_hd = int(x1 * escala_x), int(y1 * escala_y)
x2_hd, y2_hd = int(x2 * escala_x), int(y2 * escala_y)
x1_c, y1_c = max(0, x1_hd), max(0, y1_hd)
x2_c, y2_c = min(w_hd, x2_hd), min(h_hd, y2_hd)
roi = frame_hd[y1_c:y2_c, x1_c:x2_c]
if roi.size == 0 or roi.shape[0] < 40 or roi.shape[1] < 20: return None
2026-03-18 18:27:26 +00:00
2026-03-18 17:45:30 +00:00
calidad_area = (x2_c - x1_c) * (y2_c - y1_c)
blob = preprocess_onnx(roi)
blob_16 = np.zeros((16, 3, 256, 128), dtype=np.float32)
blob_16[0] = blob[0]
deep_feat = ort_session.run(None, {input_name: blob_16})[0][0].flatten()
norma = np.linalg.norm(deep_feat)
2026-03-18 18:27:26 +00:00
if norma > 0: deep_feat = deep_feat / norma
2026-03-18 17:45:30 +00:00
2026-03-18 18:27:26 +00:00
color_feat = extraer_color_zonas(roi)
textura_feat = extraer_textura_rapida(roi)
2026-03-18 17:45:30 +00:00
return {'deep': deep_feat, 'color': color_feat, 'textura': textura_feat, 'calidad': calidad_area}
except Exception:
2026-03-18 17:45:30 +00:00
return None
# ⚡ EL SECRETO: 100% IA entre cámaras. Textura solo en la misma cámara.
# ⚡ EL SECRETO: 100% IA entre cámaras. Textura solo en la misma cámara.
def similitud_hibrida(f1, f2, cross_cam=False):
2026-03-18 18:27:26 +00:00
if f1 is None or f2 is None: return 0.0
2026-03-18 17:45:30 +00:00
sim_deep = max(0.0, 1.0 - cosine(f1['deep'], f2['deep']))
2026-03-18 18:27:26 +00:00
# 1. Calculamos SIEMPRE los histogramas de color por zonas
2026-03-18 17:45:30 +00:00
if f1['color'].shape == f2['color'].shape and f1['color'].size > 1:
L = len(f1['color']) // 3
2026-03-18 18:27:26 +00:00
sim_head = max(0.0, float(cv2.compareHist(f1['color'][:L].astype(np.float32), f2['color'][:L].astype(np.float32), cv2.HISTCMP_CORREL)))
sim_torso = max(0.0, float(cv2.compareHist(f1['color'][L:2*L].astype(np.float32), f2['color'][L:2*L].astype(np.float32), cv2.HISTCMP_CORREL)))
sim_legs = max(0.0, float(cv2.compareHist(f1['color'][2*L:].astype(np.float32), f2['color'][2*L:].astype(np.float32), cv2.HISTCMP_CORREL)))
else:
sim_head, sim_torso, sim_legs = 0.0, 0.0, 0.0
if cross_cam:
# Si OSNet se confunde por la chamarra, revisamos las piernas.
# Una similitud < 0.25 en HISTCMP_CORREL significa colores radicalmente distintos (Ej. Negro vs Gris/Blanco).
if sim_legs < 0.25:
sim_deep -= 0.15 # Castigo letal: Le tumbamos 15 puntos porcentuales a OSNet
return max(0.0, sim_deep)
2026-03-18 18:27:26 +00:00
# 2. Si está en la misma cámara, usamos la fórmula híbrida completa
sim_color = (0.10 * sim_head) + (0.60 * sim_torso) + (0.30 * sim_legs)
if 'textura' in f1 and 'textura' in f2 and f1['textura'].size > 1:
sim_textura = max(0.0, float(cv2.compareHist(f1['textura'].astype(np.float32), f2['textura'].astype(np.float32), cv2.HISTCMP_CORREL)))
else: sim_textura = 0.0
return (sim_deep * 0.80) + (sim_color * 0.10) + (sim_textura * 0.10)
2026-03-18 17:45:30 +00:00
# ──────────────────────────────────────────────────────────────────────────────
# 2. KALMAN TRACKER
# ──────────────────────────────────────────────────────────────────────────────
class KalmanTrack:
_count = 0
def __init__(self, box, now):
self.kf = cv2.KalmanFilter(7, 4)
self.kf.measurementMatrix = np.array([[1,0,0,0,0,0,0], [0,1,0,0,0,0,0], [0,0,1,0,0,0,0], [0,0,0,1,0,0,0]], np.float32)
2026-03-18 17:45:30 +00:00
self.kf.transitionMatrix = np.eye(7, dtype=np.float32)
2026-03-18 18:27:26 +00:00
self.kf.transitionMatrix[0,4] = 1; self.kf.transitionMatrix[1,5] = 1; self.kf.transitionMatrix[2,6] = 1
2026-03-18 17:45:30 +00:00
self.kf.processNoiseCov *= 0.03
self.kf.statePost = np.zeros((7, 1), np.float32)
self.kf.statePost[:4] = self._convert_bbox_to_z(box)
2026-03-18 18:27:26 +00:00
self.local_id = KalmanTrack._count
2026-03-18 17:45:30 +00:00
KalmanTrack._count += 1
2026-03-18 18:27:26 +00:00
self.gid = None
self.origen_global = False
self.aprendiendo = False
self.box = list(box)
self.ts_creacion = now
self.ts_ultima_deteccion = now
self.time_since_update = 0
self.en_grupo = False
self.frames_buena_calidad = 0
self.listo_para_id = False
self.area_referencia = 0.0
2026-03-18 17:45:30 +00:00
def _convert_bbox_to_z(self, bbox):
2026-03-18 18:27:26 +00:00
w = bbox[2] - bbox[0]; h = bbox[3] - bbox[1]; x = bbox[0] + w/2.; y = bbox[1] + h/2.
2026-03-18 17:45:30 +00:00
return np.array([[x],[y],[w*h],[w/float(h+1e-6)]]).astype(np.float32)
def _convert_x_to_bbox(self, x):
2026-03-18 18:27:26 +00:00
cx, cy, s, r = float(x[0].item()), float(x[1].item()), float(x[2].item()), float(x[3].item())
w = np.sqrt(s * r); h = s / (w + 1e-6)
2026-03-18 17:45:30 +00:00
return [cx-w/2., cy-h/2., cx+w/2., cy+h/2.]
def predict(self, turno_activo=True):
2026-03-18 18:27:26 +00:00
if (self.kf.statePost[6] + self.kf.statePost[2]) <= 0: self.kf.statePost[6] *= 0.0
2026-03-18 17:45:30 +00:00
self.kf.predict()
2026-03-18 18:27:26 +00:00
if turno_activo: self.time_since_update += 1
self.aprendiendo = False
2026-03-18 17:45:30 +00:00
self.box = self._convert_x_to_bbox(self.kf.statePre)
return self.box
def update(self, box, en_grupo, now):
self.ts_ultima_deteccion = now
2026-03-18 18:27:26 +00:00
self.time_since_update = 0
self.box = list(box)
2026-03-18 17:45:30 +00:00
self.en_grupo = en_grupo
self.kf.correct(self._convert_bbox_to_z(box))
if analizar_calidad(box) and not en_grupo:
self.frames_buena_calidad += 1
if self.frames_buena_calidad >= FRAMES_CALIDAD:
self.listo_para_id = True
elif self.gid is None:
self.frames_buena_calidad = max(0, self.frames_buena_calidad - 1)
# ──────────────────────────────────────────────────────────────────────────────
# 3. MEMORIA GLOBAL (Anti-Robos y Físicas de Tiempo)
2026-03-18 17:45:30 +00:00
# ──────────────────────────────────────────────────────────────────────────────
class GlobalMemory:
def __init__(self):
2026-03-18 18:27:26 +00:00
self.db = {}
2026-03-18 17:45:30 +00:00
self.next_gid = 100
2026-03-18 18:27:26 +00:00
self.lock = threading.Lock()
2026-03-18 17:45:30 +00:00
def _es_transito_posible(self, data, cam_destino, now):
2026-03-18 18:27:26 +00:00
ultima_cam = str(data['last_cam'])
2026-03-18 17:45:30 +00:00
cam_destino = str(cam_destino)
2026-03-18 18:27:26 +00:00
dt = now - data['ts']
if ultima_cam == cam_destino: return True
2026-03-18 17:45:30 +00:00
vecinos = VECINOS.get(ultima_cam, [])
# Permite teletransportación mínima (-0.5s) para que no te fragmente en los pasillos conectados
2026-03-18 18:27:26 +00:00
if cam_destino in vecinos: return dt >= -0.5
return dt >= 4.0
2026-03-18 17:45:30 +00:00
def _sim_robusta(self, firma_nueva, firmas_guardadas, cross_cam=False):
2026-03-18 18:27:26 +00:00
if not firmas_guardadas: return 0.0
sims = sorted([similitud_hibrida(firma_nueva, f, cross_cam) for f in firmas_guardadas], reverse=True)
if len(sims) == 1: return sims[0]
elif len(sims) <= 4: return (sims[0] * 0.6) + (sims[1] * 0.4)
else: return (sims[0] * 0.50) + (sims[1] * 0.30) + (sims[2] * 0.20)
# ⚡ SE AGREGÓ 'en_borde' A LOS PARÁMETROS
def identificar_candidato(self, firma_hibrida, cam_id, now, active_gids, en_borde=True):
# 1. LIMPIEZA DE MEMORIA (El Recolector de Basura)
# Esto asume que ya agregaste la función limpiar_fantasmas(self) a tu clase
self.limpiar_fantasmas()
2026-03-18 18:27:26 +00:00
2026-03-18 17:45:30 +00:00
with self.lock:
candidatos = []
2026-03-18 18:27:26 +00:00
vecinos = VECINOS.get(str(cam_id), [])
2026-03-18 17:45:30 +00:00
for gid, data in self.db.items():
2026-03-18 18:27:26 +00:00
if gid in active_gids: continue
2026-03-18 17:45:30 +00:00
dt = now - data['ts']
2026-03-18 18:27:26 +00:00
if dt > TIEMPO_MAX_AUSENCIA or not self._es_transito_posible(data, cam_id, now): continue
if not data['firmas']: continue
2026-03-18 17:45:30 +00:00
misma_cam = (str(data['last_cam']) == str(cam_id))
es_cross_cam = not misma_cam
2026-03-18 17:45:30 +00:00
es_vecino = str(data['last_cam']) in vecinos
# ⚡ FÍSICA DE PUERTAS
if es_vecino and not en_borde:
es_vecino = False
sim = self._sim_robusta(firma_hibrida, data['firmas'], cross_cam=es_cross_cam)
2026-03-18 18:27:26 +00:00
if misma_cam: umbral = UMBRAL_REID_MISMA_CAM
elif es_vecino: umbral = UMBRAL_REID_VECINO
else: umbral = UMBRAL_REID_NO_VECINO
2026-03-18 17:45:30 +00:00
# PROTECCIÓN VIP
if data.get('nombre') is not None:
if misma_cam:
umbral += 0.04
else:
umbral += 0.03
2026-03-18 17:45:30 +00:00
if sim > umbral:
# ⚡ MODIFICACIÓN: Guardamos también si es de la misma cam o vecino para el desempate final
candidatos.append((sim, gid, misma_cam, es_vecino))
if not candidatos:
2026-03-18 18:27:26 +00:00
nid = self.next_gid; self.next_gid += 1
2026-03-18 17:45:30 +00:00
self._actualizar_sin_lock(nid, firma_hibrida, cam_id, now)
return nid, False
# Ordena por la similitud (el primer elemento de la tupla) de mayor a menor
candidatos.sort(reverse=True)
best_sim, best_gid, best_misma, best_vecino = candidatos[0]
if len(candidatos) >= 2:
segunda_sim, segundo_gid, seg_misma, seg_vecino = candidatos[1]
margen = best_sim - segunda_sim
# Si las chamarras son casi idénticas...
if margen <= 0.02 and best_sim < 0.75:
# ⚖️ DESEMPATE GEOGRÁFICO INTELLIGENTE
# Le damos un "peso" matemático extra a la lógica espacial
peso_1 = best_sim + (0.10 if (best_misma or best_vecino) else 0.0)
peso_2 = segunda_sim + (0.10 if (seg_misma or seg_vecino) else 0.0)
if peso_1 > peso_2:
best_gid = best_gid # Gana el primero por lógica espacial
elif peso_2 > peso_1:
best_gid = segundo_gid # Gana el segundo por lógica espacial
else:
# Si empatan en TODO (ropa y espacio), entonces sí entramos en pánico seguro
print(f"\n[ ALERTA] Empate extremo entre ID {best_gid} ({best_sim:.2f}) y ID {segundo_gid} ({segunda_sim:.2f}). Se asigna temporal.")
nid = self.next_gid; self.next_gid += 1
self._actualizar_sin_lock(nid, firma_hibrida, cam_id, now)
return nid, False
self._actualizar_sin_lock(best_gid, firma_hibrida, cam_id, now)
return best_gid, True
2026-03-18 17:45:30 +00:00
def _actualizar_sin_lock(self, gid, firma_dict, cam_id, now):
2026-03-18 18:27:26 +00:00
if gid not in self.db: self.db[gid] = {'firmas': [], 'last_cam': cam_id, 'ts': now}
2026-03-18 17:45:30 +00:00
if firma_dict is not None:
firmas_list = self.db[gid]['firmas']
if not firmas_list:
firmas_list.append(firma_dict)
else:
if firma_dict['calidad'] > (firmas_list[0]['calidad'] * 1.50):
vieja_ancla = firmas_list[0]; firmas_list[0] = firma_dict; firma_dict = vieja_ancla
2026-03-18 17:45:30 +00:00
if len(firmas_list) >= MAX_FIRMAS_MEMORIA:
max_sim_interna = -1.0; idx_redundante = 1
2026-03-18 17:45:30 +00:00
for i in range(1, len(firmas_list)):
sims_con_otras = [similitud_hibrida(firmas_list[i], firmas_list[j]) for j in range(1, len(firmas_list)) if j != i]
2026-03-18 18:27:26 +00:00
sim_promedio = np.mean(sims_con_otras) if sims_con_otras else 0.0
if sim_promedio > max_sim_interna: max_sim_interna = sim_promedio; idx_redundante = i
2026-03-18 17:45:30 +00:00
firmas_list[idx_redundante] = firma_dict
else:
firmas_list.append(firma_dict)
self.db[gid]['last_cam'] = cam_id
2026-03-18 18:27:26 +00:00
self.db[gid]['ts'] = now
2026-03-18 17:45:30 +00:00
def actualizar(self, gid, firma, cam_id, now):
2026-03-18 18:27:26 +00:00
with self.lock: self._actualizar_sin_lock(gid, firma, cam_id, now)
2026-03-18 17:45:30 +00:00
def limpiar_fantasmas(self):
""" Aniquila los IDs temporales que llevan más de 15 segundos sin verse """
with self.lock:
ahora = time.time()
ids_a_borrar = []
for gid, data in self.db.items():
tiempo_inactivo = ahora - data.get('ts', ahora)
# Si es un ID anónimo y lleva 15 segundos desaparecido, es basura
if data.get('nombre') is None and tiempo_inactivo > 600.0:
ids_a_borrar.append(gid)
# Si es un VIP (con nombre), le damos 5 minutos de memoria antes de borrarlo
elif data.get('nombre') is not None and tiempo_inactivo > 900.0:
ids_a_borrar.append(gid)
for gid in ids_a_borrar:
del self.db[gid]
def confirmar_firma_vip(self, gid, ts):
"""
ArcFace confirma la identidad con alta certeza (>0.55).
Aniquila las firmas viejas y deja solo la más reciente como la firma VIP definitiva.
"""
with self.lock:
if gid in self.db and self.db[gid]['firmas']:
# Tomamos la última firma que el hilo principal guardó (la ropa actual)
firma_actual_pura = self.db[gid]['firmas'][-1]
# Sobrescribimos el historial dejando solo esta firma confirmada
self.db[gid]['firmas'] = [firma_actual_pura]
self.db[gid]['ts'] = ts
2026-03-18 17:45:30 +00:00
# ──────────────────────────────────────────────────────────────────────────────
# 4. GESTOR LOCAL (Kalman Elasticity & Ghost Killer)
2026-03-18 17:45:30 +00:00
# ──────────────────────────────────────────────────────────────────────────────
def iou_overlap(boxA, boxB):
2026-03-18 18:27:26 +00:00
xA, yA, xB, yB = max(boxA[0], boxB[0]), max(boxA[1], boxB[1]), min(boxA[2], boxB[2]), min(boxA[3], boxB[3])
inter = max(0, xB-xA) * max(0, yB-yA)
areaA = (boxA[2]-boxA[0]) * (boxA[3]-boxA[1]); areaB = (boxB[2]-boxB[0]) * (boxB[3]-boxB[1])
2026-03-18 17:45:30 +00:00
return inter / (areaA + areaB - inter + 1e-6)
class CamManager:
def __init__(self, cam_id, global_mem):
2026-03-18 18:27:26 +00:00
self.cam_id, self.global_mem, self.trackers = cam_id, global_mem, []
2026-03-18 17:45:30 +00:00
def update(self, boxes, frame_show, frame_hd, now, turno_activo):
2026-03-18 18:27:26 +00:00
for trk in self.trackers: trk.predict(turno_activo=turno_activo)
if not turno_activo: return self.trackers
2026-03-18 17:45:30 +00:00
2026-03-18 18:27:26 +00:00
matched, unmatched_dets, unmatched_trks = self._asignar(boxes, now)
2026-03-18 17:45:30 +00:00
for t_idx, d_idx in matched:
2026-03-18 18:27:26 +00:00
trk = self.trackers[t_idx]; box = boxes[d_idx]
en_grupo = any(other is not trk and iou_overlap(box, other.box) > 0.10 for other in self.trackers)
trk.update(box, en_grupo, now)
active_gids = {t.gid for t in self.trackers if t.gid is not None}
2026-03-18 17:45:30 +00:00
area_actual = (box[2] - box[0]) * (box[3] - box[1])
# IGNORAMOS VECTORES MUTANTES DE GRUPOS
if trk.gid is None and trk.listo_para_id and not trk.en_grupo:
# ⚡ Usamos el frame_hd
firma = extraer_firma_hibrida(frame_hd, box)
2026-03-18 17:45:30 +00:00
if firma is not None:
# ⚡ DETECCIÓN DE ZONA DE NACIMIENTO
fh, fw = frame_hd.shape[:2]
bx1, by1, bx2, by2 = map(int, box)
# Si nace a menos de 40 píxeles del margen, entró por el pasillo
nace_en_borde = (bx1 < 25 or by1 < 25 or bx2 > fw - 25 or by2 > fh - 25)
# Mandamos esa información al identificador
gid, es_reid = self.global_mem.identificar_candidato(firma, self.cam_id, now, active_gids, en_borde=nace_en_borde)
2026-03-18 18:27:26 +00:00
trk.gid, trk.origen_global, trk.area_referencia = gid, es_reid, area_actual
2026-03-18 17:45:30 +00:00
elif trk.gid is not None and not trk.en_grupo:
2026-03-18 18:27:26 +00:00
tiempo_ultima_firma = getattr(trk, 'ultimo_aprendizaje', 0)
# ⚡ APRENDIZAJE RÁPIDO: Bajamos de 1.5s a 0.5s para que llene la memoria volando
if (now - tiempo_ultima_firma) > 0.5 and analizar_calidad(box):
fh, fw = frame_hd.shape[:2]
2026-03-18 18:27:26 +00:00
x1, y1, x2, y2 = map(int, box)
en_borde = (x1 < 15 or y1 < 15 or x2 > fw - 15 or y2 > fh - 15)
2026-03-18 17:45:30 +00:00
if not en_borde:
firma_nueva = extraer_firma_hibrida(frame_hd, box)
2026-03-18 17:45:30 +00:00
if firma_nueva is not None:
with self.global_mem.lock:
2026-03-18 18:27:26 +00:00
if trk.gid in self.global_mem.db and self.global_mem.db[trk.gid]['firmas']:
# ⚡ APRENDIZAJE EN CADENA: Comparamos contra la ÚLTIMA foto (-1), no contra la primera.
# Esto permite que el sistema "entienda" cuando te estás dando la vuelta o mostrando la mochila.
firma_reciente = self.global_mem.db[trk.gid]['firmas'][-1]
sim_coherencia = similitud_hibrida(firma_nueva, firma_reciente)
# Tolerancia relajada a 0.50 para permitir la transición de la espalda
if sim_coherencia > 0.50:
2026-03-18 17:45:30 +00:00
es_coherente = True
for otro_gid, otro_data in self.global_mem.db.items():
2026-03-18 18:27:26 +00:00
if otro_gid == trk.gid or not otro_data['firmas']: continue
sim_intruso = similitud_hibrida(firma_nueva, otro_data['firmas'][0])
if sim_intruso > sim_coherencia:
2026-03-18 17:45:30 +00:00
es_coherente = False
break
if es_coherente:
2026-03-18 18:27:26 +00:00
self.global_mem._actualizar_sin_lock(trk.gid, firma_nueva, self.cam_id, now)
2026-03-18 17:45:30 +00:00
trk.ultimo_aprendizaje = now
trk.aprendiendo = True
2026-03-18 18:27:26 +00:00
for d_idx in unmatched_dets: self.trackers.append(KalmanTrack(boxes[d_idx], now))
2026-03-18 17:45:30 +00:00
vivos = []
fh, fw = frame_show.shape[:2] # Usamos frame_show para evaluar los bordes de la caja 480
2026-03-18 17:45:30 +00:00
for t in self.trackers:
x1, y1, x2, y2 = t.box
toca_borde = (x1 < 20 or y1 < 20 or x2 > fw - 20 or y2 > fh - 20)
2026-03-18 17:45:30 +00:00
tiempo_oculto = now - t.ts_ultima_deteccion
2026-03-18 18:27:26 +00:00
# ⚡ MUERTE JUSTA: Si es anónimo en el borde, muere rápido.
# Si ya tiene un ID asignado, le damos al menos 3 segundos de gracia.
if t.gid is None and toca_borde:
limite_vida = 1.0
elif t.gid is not None and toca_borde:
limite_vida = 3.0
else:
limite_vida = 10.0
2026-03-18 18:27:26 +00:00
if tiempo_oculto < limite_vida:
2026-03-18 17:45:30 +00:00
vivos.append(t)
2026-03-18 18:27:26 +00:00
2026-03-18 17:45:30 +00:00
self.trackers = vivos
return self.trackers
2026-03-18 18:27:26 +00:00
def _asignar(self, boxes, now):
n_trk = len(self.trackers); n_det = len(boxes)
2026-03-18 17:45:30 +00:00
if n_trk == 0: return [], list(range(n_det)), []
if n_det == 0: return [], [], list(range(n_trk))
2026-03-18 18:27:26 +00:00
2026-03-18 17:45:30 +00:00
cost_mat = np.zeros((n_trk, n_det), dtype=np.float32)
TIEMPO_TURNO_ROTATIVO = len(SECUENCIA) * 0.035
2026-03-18 18:27:26 +00:00
2026-03-18 17:45:30 +00:00
for t, trk in enumerate(self.trackers):
for d, det in enumerate(boxes):
iou = iou_overlap(trk.box, det)
2026-03-18 18:27:26 +00:00
cx_t, cy_t = (trk.box[0]+trk.box[2])/2, (trk.box[1]+trk.box[3])/2
cx_d, cy_d = (det[0]+det[2])/2, (det[1]+det[3])/2
dist_norm = np.sqrt((cx_t-cx_d)**2 + (cy_t-cy_d)**2) / 550.0
area_trk = (trk.box[2] - trk.box[0]) * (trk.box[3] - trk.box[1])
area_det = (det[2] - det[0]) * (det[3] - det[1])
ratio_area = max(area_trk, area_det) / (min(area_trk, area_det) + 1e-6)
castigo_tam = (ratio_area - 1.0) * 0.7
2026-03-18 18:27:26 +00:00
2026-03-18 17:45:30 +00:00
tiempo_oculto = now - trk.ts_ultima_deteccion
if tiempo_oculto > (TIEMPO_TURNO_ROTATIVO * 2) and iou < 0.10:
fantasma_penalty = 5.0
else: fantasma_penalty = 0.0
2026-03-18 18:27:26 +00:00
if iou >= 0.05 or dist_norm < 0.80:
cost_mat[t, d] = (1.0 - iou) + (dist_norm * 2.0) + fantasma_penalty + castigo_tam
else: cost_mat[t, d] = 100.0
2026-03-18 18:27:26 +00:00
2026-03-18 17:45:30 +00:00
row_ind, col_ind = linear_sum_assignment(cost_mat)
matched, unmatched_dets, unmatched_trks = [], [], []
2026-03-18 18:27:26 +00:00
2026-03-18 17:45:30 +00:00
for r, c in zip(row_ind, col_ind):
# ⚡ CAJAS PEGAJOSAS: 6.0 evita que suelte el ID si te mueves rápido
if cost_mat[r, c] > 7.0:
2026-03-18 18:27:26 +00:00
unmatched_trks.append(r); unmatched_dets.append(c)
else: matched.append((r, c))
2026-03-18 17:45:30 +00:00
for t in range(n_trk):
2026-03-18 18:27:26 +00:00
if t not in [m[0] for m in matched]: unmatched_trks.append(t)
2026-03-18 17:45:30 +00:00
for d in range(n_det):
2026-03-18 18:27:26 +00:00
if d not in [m[1] for m in matched]: unmatched_dets.append(d)
2026-03-18 17:45:30 +00:00
return matched, unmatched_dets, unmatched_trks
# ──────────────────────────────────────────────────────────────────────────────
2026-03-18 18:27:26 +00:00
# 5. STREAM Y MAIN LOOP (Standalone)
2026-03-18 17:45:30 +00:00
# ──────────────────────────────────────────────────────────────────────────────
class CamStream:
def __init__(self, url):
2026-03-18 18:27:26 +00:00
self.url, self.cap = url, cv2.VideoCapture(url)
self.cap.set(cv2.CAP_PROP_BUFFERSIZE, 1); self.frame = None
2026-03-18 17:45:30 +00:00
threading.Thread(target=self._run, daemon=True).start()
2026-03-18 18:27:26 +00:00
2026-03-18 17:45:30 +00:00
def _run(self):
while True:
ret, f = self.cap.read()
2026-03-18 18:27:26 +00:00
if ret:
self.frame = f; time.sleep(0.01)
2026-03-18 18:27:26 +00:00
else:
time.sleep(2); self.cap.open(self.url)
2026-03-18 17:45:30 +00:00
def dibujar_track(frame_show, trk):
2026-03-18 18:27:26 +00:00
try: x1, y1, x2, y2 = map(int, trk.box)
except Exception: return
if trk.gid is None: color, label = C_CANDIDATO, f"?{trk.local_id}"
elif trk.en_grupo: color, label = C_GRUPO, f"ID:{trk.gid} [grp]"
elif trk.aprendiendo: color, label = C_APRENDIZAJE, f"ID:{trk.gid} [++]"
elif trk.origen_global: color, label = C_GLOBAL, f"ID:{trk.gid} [re-id]"
else: color, label = C_LOCAL, f"ID:{trk.gid}"
2026-03-18 17:45:30 +00:00
cv2.rectangle(frame_show, (x1, y1), (x2, y2), color, 2)
(tw, th), _ = cv2.getTextSize(label, FUENTE, 0.55, 1)
cv2.rectangle(frame_show, (x1, y1-th-6), (x1+tw+2, y1), color, -1)
2026-03-18 18:27:26 +00:00
cv2.putText(frame_show, label, (x1+1, y1-4), FUENTE, 0.55, (0,0,0), 1)
2026-03-18 17:45:30 +00:00
def main():
print("Iniciando Sistema V-PRO — Tracker Resiliente (Código Unificado Maestro)")
2026-03-18 18:27:26 +00:00
model = YOLO("yolov8n.pt")
2026-03-18 17:45:30 +00:00
global_mem = GlobalMemory()
2026-03-18 18:27:26 +00:00
managers = {str(c): CamManager(c, global_mem) for c in SECUENCIA}
cams = [CamStream(u) for u in URLS]
2026-03-18 17:45:30 +00:00
cv2.namedWindow("SmartSoft", cv2.WINDOW_AUTOSIZE)
2026-03-18 18:27:26 +00:00
2026-03-18 17:45:30 +00:00
idx = 0
while True:
2026-03-18 18:27:26 +00:00
now = time.time()
tiles = []
2026-03-18 17:45:30 +00:00
cam_ia = idx % len(cams)
for i, cam_obj in enumerate(cams):
2026-03-18 18:27:26 +00:00
frame = cam_obj.frame; cid = str(SECUENCIA[i])
if frame is None: tiles.append(np.zeros((270, 480, 3), np.uint8)); continue
frame_show = cv2.resize(frame.copy(), (480, 270)); boxes = []; turno_activo = (i == cam_ia)
2026-03-18 17:45:30 +00:00
if turno_activo:
res = model.predict(frame_show, conf=0.50, iou=0.40, classes=[0], verbose=False, imgsz=480, device='cpu')
2026-03-18 18:27:26 +00:00
if res[0].boxes: boxes = res[0].boxes.xyxy.cpu().numpy().tolist()
tracks = managers[cid].update(boxes, frame_show, frame, now, turno_activo)
2026-03-18 17:45:30 +00:00
for trk in tracks:
2026-03-18 18:27:26 +00:00
if trk.time_since_update <= 1: dibujar_track(frame_show, trk)
if turno_activo: cv2.circle(frame_show, (460, 20), 6, (0, 0, 255), -1)
con_id = sum(1 for t in tracks if t.gid and t.time_since_update==0)
cv2.putText(frame_show, f"CAM {cid} [{con_id} ID]", (10, 28), FUENTE, 0.7, (255, 255, 255), 2)
2026-03-18 17:45:30 +00:00
tiles.append(frame_show)
2026-03-18 18:27:26 +00:00
if len(tiles) == 6: cv2.imshow("SmartSoft", np.vstack([np.hstack(tiles[0:3]), np.hstack(tiles[3:6])]))
2026-03-18 17:45:30 +00:00
idx += 1
2026-03-18 18:27:26 +00:00
if cv2.waitKey(1) == ord('q'): break
2026-03-18 17:45:30 +00:00
cv2.destroyAllWindows()
if __name__ == "__main__":
main()