-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinalCode.py
More file actions
147 lines (121 loc) · 5.79 KB
/
Copy pathFinalCode.py
File metadata and controls
147 lines (121 loc) · 5.79 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
############################### Detectar webcam 2 view + Modelo + String ###################################################
import cv2
import mediapipe as mp
import torch
import torchvision.transforms as transforms
from PIL import Image
import numpy as np
import torch.nn as nn
import torch.nn.functional as F
import time
from torchvision import datasets
# Inicializar MediaPipe Hands
mp_hands = mp.solutions.hands
hands = mp_hands.Hands(static_image_mode=False, max_num_hands=1, min_detection_confidence=0.7)
mp_drawing = mp.solutions.drawing_utils
# Definir las transformaciones necesarias (ajústalas según lo que usaste en el entrenamiento)
preprocess = transforms.Compose([
transforms.Resize((200, 200)), # Cambia esto al tamaño esperado por tu modelo
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) # Cambia esto según tu dataset
])
# Cargar el modelo guardado
model = torch.load('C:/Users/Ignacio/source/repos/DLIP/ProyectoFinalInternet/mejorModelo.pth')
model.eval()
# Obtener nombres de clases
data_dir = 'C:/Users/Ignacio/source/repos/DLIP/ProyectoFinalInternet/asl_alphabet_validation_detected'
dataset = datasets.ImageFolder(root=data_dir, transform=preprocess)
class_names = dataset.classes
# Verificar si hay una GPU disponible y usarla si es posible
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model.to(device)
# Abrir la webcam
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Error al abrir la cámara")
exit()
# Variables para la detección continua de la misma letra
tiempo_inicio = time.time()
letra_actual = None
letra_guardada = None
string_final = ''
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
# Crear una copia del frame original para la detección de la mano
detection_frame = frame.copy()
# Convertir el fotograma a RGB (MediaPipe Hands requiere imágenes en formato RGB)
image_rgb = cv2.cvtColor(detection_frame, cv2.COLOR_BGR2RGB)
results = hands.process(image_rgb)
# Reiniciar el contador si no hay manos detectadas
if not results.multi_hand_landmarks:
tiempo_inicio = time.time()
letra_actual = None
letra_guardada = None
else:
# Si se detectan manos, dibujar los landmarks en el fotograma original y clasificar la postura
for hand_landmarks in results.multi_hand_landmarks:
# Dibujar los landmarks
mp_drawing.draw_landmarks(detection_frame, hand_landmarks, mp_hands.HAND_CONNECTIONS)
# Extraer la región de la mano
img_h, img_w, _ = frame.shape
bbox = []
for landmark in hand_landmarks.landmark:
bbox.append((int(landmark.x * img_w), int(landmark.y * img_h)))
bbox = np.array(bbox)
# Obtener los límites de la caja
x_min = np.min(bbox[:, 0])
y_min = np.min(bbox[:, 1])
x_max = np.max(bbox[:, 0])
y_max = np.max(bbox[:, 1])
# Expandir ligeramente la caja para capturar mejor la mano
padding = 20
x_min = max(x_min - padding, 0)
y_min = max(y_min - padding, 0)
x_max = min(x_max + padding, img_w)
y_max = min(y_max + padding, img_h)
# Recortar la región de la mano
hand_image = frame[y_min:y_max, x_min:x_max]
hand_image_pil = Image.fromarray(cv2.cvtColor(hand_image, cv2.COLOR_BGR2RGB))
# Preprocesar la imagen de la mano
input_tensor = preprocess(hand_image_pil)
input_batch = input_tensor.unsqueeze(0).to(device)
# Realizar la predicción
with torch.no_grad():
output = model(input_batch)
_, predicted = torch.max(output, 1)
predicted_class = predicted.item()
predicted_label = class_names[predicted_class]
# Mostrar la predicción en el fotograma
cv2.putText(detection_frame, predicted_label, (x_min, y_min - 10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2, cv2.LINE_AA)
# Lógica para la detección continua de la misma letra
if letra_actual != predicted_label:
letra_actual = predicted_label
tiempo_inicio = time.time()
else:
if time.time() - tiempo_inicio >= 1.5:
if letra_actual == 'space':
letra_guardada = letra_actual
string_final += ' '
tiempo_inicio = time.time()
elif letra_actual == 'del' and len(string_final) > 0:
letra_guardada = letra_actual
string_final = string_final[:-1]
tiempo_inicio = time.time()
else:
letra_guardada = letra_actual
string_final += letra_guardada
tiempo_inicio = time.time()
# Mostrar la letra guardada en pantalla
if letra_guardada:
cv2.putText(detection_frame, f'Letra detectada: {letra_guardada}', (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
cv2.putText(detection_frame, f'String Final: {string_final}', (50, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
# Combinar el frame original y el frame con detección de mano
combined_frame = np.hstack((frame, detection_frame))
# Mostrar el fotograma combinado
cv2.imshow('Original (Left) and Hand Detection (Right)', combined_frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()