forked from ronvidev/modelo_lstm_lsp
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcapture_samples.py
84 lines (69 loc) · 3.45 KB
/
capture_samples.py
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
import os
import cv2
import numpy as np
from mediapipe.python.solutions.holistic import Holistic
from helpers import create_folder, draw_keypoints, mediapipe_detection, save_frames, there_hand
from constants import FONT, FONT_POS, FONT_SIZE, FRAME_ACTIONS_PATH, ROOT_PATH
from datetime import datetime
def capture_samples(path, margin_frame=2, min_cant_frames=5, delay_frames=2):
'''
### CAPTURA DE MUESTRAS PARA UNA PALABRA
Recibe como parámetro la ubicación de guardado y guarda los frames
`path` ruta de la carpeta de la palabra \n
`margin_frame` cantidad de frames que se ignoran al comienzo y al final \n
`min_cant_frames` cantidad de frames minimos para cada muestra \n
`delay_frames` cantidad de frames que espera antes de detener la captura después de no detectar manos
'''
create_folder(path)
count_frame = 0
frames = []
fix_frames = 0
recording = False
samples_count = len([name for name in os.listdir(path) if os.path.isdir(os.path.join(path, name))])
with Holistic() as holistic_model:
video = cv2.VideoCapture(0)
while video.isOpened():
ret, frame = video.read()
if not ret:
break
image = frame.copy()
results = mediapipe_detection(frame, holistic_model)
if there_hand(results) or recording:
recording = False
count_frame += 1
if count_frame > margin_frame:
cv2.putText(image, 'Capturando...', FONT_POS, FONT, FONT_SIZE, (255, 50, 0))
frames.append(np.asarray(frame))
else:
if len(frames) >= min_cant_frames + margin_frame:
fix_frames += 1
if fix_frames < delay_frames:
recording = True
continue
frames = frames[: - (margin_frame + delay_frames)]
today = datetime.now().strftime('%y%m%d%H%M%S%f')
output_folder = os.path.join(path, f"sample_{today}")
create_folder(output_folder)
save_frames(frames, output_folder)
samples_count += 1
recording, fix_frames = False, 0
frames, count_frame = [], 0
cv2.putText(image, 'Listo para capturar...', FONT_POS, FONT, FONT_SIZE, (0,220, 100))
draw_keypoints(image, results)
# Agregar recuadro con el conteo de muestras
height, width, _ = image.shape # Obtener las dimensiones de la imagen
# Definir las coordenadas para el rectángulo en el costado derecho
top_left = (width - 200, 10) # Punto superior izquierdo
bottom_right = (width - 10, 60) # Punto inferior derecho
text_position = (top_left[0] + 10, top_left[1] + 40)
cv2.rectangle(image, top_left, bottom_right, (0, 0, 0), -1)
cv2.putText(image, f'Muestras: {samples_count}', text_position, FONT, FONT_SIZE, (255, 255, 255), 2)
cv2.imshow(f'Toma de muestras para "{os.path.basename(path)}"', image)
if cv2.waitKey(10) & 0xFF == ord('q'):
break
video.release()
cv2.destroyAllWindows()
if __name__ == "__main__":
word_name = "azul"
word_path = os.path.join(ROOT_PATH, FRAME_ACTIONS_PATH, word_name)
capture_samples(word_path)