Spaces:
Sleeping
Sleeping
Upload 2 files
Browse files- final_project.py +105 -0
- weapons_yolo.pt +3 -0
final_project.py
ADDED
|
@@ -0,0 +1,105 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
from ultralytics import YOLO
|
| 4 |
+
|
| 5 |
+
# ============================================================
|
| 6 |
+
# Cargar YOLO
|
| 7 |
+
# ============================================================
|
| 8 |
+
|
| 9 |
+
WEIGHTS_PATH = "yolov8n.pt" # el modelo que ya te funcionaba
|
| 10 |
+
print(f"[INFO] Cargando modelo YOLO: {WEIGHTS_PATH}")
|
| 11 |
+
|
| 12 |
+
model = YOLO(WEIGHTS_PATH)
|
| 13 |
+
print("[INFO] Modelo YOLO cargado correctamente.")
|
| 14 |
+
|
| 15 |
+
|
| 16 |
+
# ============================================================
|
| 17 |
+
# Preprocesado básico
|
| 18 |
+
# ============================================================
|
| 19 |
+
|
| 20 |
+
def preprocess_frame(frame):
|
| 21 |
+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
|
| 22 |
+
blur = cv2.GaussianBlur(gray, (5, 5), 0)
|
| 23 |
+
|
| 24 |
+
# Detecta cambios bruscos de intensidad (útil para bordes de objetos alargados)
|
| 25 |
+
sobel = cv2.Sobel(blur, cv2.CV_64F, 1, 1, ksize=3)
|
| 26 |
+
sobel = cv2.convertScaleAbs(sobel)
|
| 27 |
+
|
| 28 |
+
# Filtro de bordes más duro
|
| 29 |
+
edges = cv2.Canny(blur, 80, 150)
|
| 30 |
+
|
| 31 |
+
# Pequeña expansión de bordes
|
| 32 |
+
kernel = np.ones((3, 3), np.uint8)
|
| 33 |
+
dilated = cv2.dilate(edges, kernel, iterations=1)
|
| 34 |
+
|
| 35 |
+
# Cierra huecos pequeños en formas
|
| 36 |
+
morph = cv2.morphologyEx(dilated, cv2.MORPH_CLOSE, kernel, iterations=1)
|
| 37 |
+
|
| 38 |
+
return gray, sobel, morph
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
# ============================================================
|
| 42 |
+
# Detección usando YOLO
|
| 43 |
+
# ============================================================
|
| 44 |
+
|
| 45 |
+
def detect_knives(frame):
|
| 46 |
+
results = model(frame, verbose=False)[0]
|
| 47 |
+
|
| 48 |
+
detections = []
|
| 49 |
+
for box in results.boxes:
|
| 50 |
+
cls = int(box.cls[0])
|
| 51 |
+
label = results.names[cls].lower()
|
| 52 |
+
|
| 53 |
+
# Verifica si YOLO considera la clase como cuchillo
|
| 54 |
+
if "knife" in label or "cuchillo" in label:
|
| 55 |
+
x1, y1, x2, y2 = box.xyxy[0].cpu().numpy()
|
| 56 |
+
detections.append((int(x1), int(y1), int(x2), int(y2)))
|
| 57 |
+
|
| 58 |
+
return detections
|
| 59 |
+
|
| 60 |
+
|
| 61 |
+
# ============================================================
|
| 62 |
+
# Lógica principal
|
| 63 |
+
# ============================================================
|
| 64 |
+
|
| 65 |
+
def main():
|
| 66 |
+
print("[INFO] Iniciando cámara...")
|
| 67 |
+
cap = cv2.VideoCapture(0)
|
| 68 |
+
|
| 69 |
+
if not cap.isOpened():
|
| 70 |
+
print("[ERROR] No se pudo abrir la cámara.")
|
| 71 |
+
return
|
| 72 |
+
|
| 73 |
+
while True:
|
| 74 |
+
ret, frame = cap.read()
|
| 75 |
+
if not ret:
|
| 76 |
+
print("[ERROR] No se pudo leer el frame.")
|
| 77 |
+
break
|
| 78 |
+
|
| 79 |
+
gray, sobel, morph = preprocess_frame(frame)
|
| 80 |
+
detections = detect_knives(frame)
|
| 81 |
+
|
| 82 |
+
# Dibuja detecciones
|
| 83 |
+
for (x1, y1, x2, y2) in detections:
|
| 84 |
+
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 0, 255), 2)
|
| 85 |
+
cv2.putText(frame, "KNIFE", (x1, y1 - 10),
|
| 86 |
+
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
|
| 87 |
+
|
| 88 |
+
# Ventana principal
|
| 89 |
+
cv2.imshow("Detección de Cuchillos (YOLO)", frame)
|
| 90 |
+
|
| 91 |
+
# ------- Ventana extra con 2 filtros juntos -------
|
| 92 |
+
filters = np.hstack((gray, sobel))
|
| 93 |
+
cv2.imshow("Filtros (Gray | Sobel)", filters)
|
| 94 |
+
|
| 95 |
+
# Salir con Q
|
| 96 |
+
if cv2.waitKey(1) & 0xFF == ord('q'):
|
| 97 |
+
break
|
| 98 |
+
|
| 99 |
+
cap.release()
|
| 100 |
+
cv2.destroyAllWindows()
|
| 101 |
+
print("[INFO] Cámara finalizada.")
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
if __name__ == "__main__":
|
| 105 |
+
main()
|
weapons_yolo.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a010e4a9a18f9b1ccaffe063334f088d5b64083e4ccd7b9ef2dbc7b183fc2d80
|
| 3 |
+
size 22531960
|