|
|
|
|
|
import cv2 |
|
import os |
|
|
|
def record_video(duration=2, output_dir='videos', filename='sample'): |
|
""" |
|
Records a short video from the webcam. |
|
|
|
Args: |
|
duration (int): Duration of the video in seconds. |
|
output_dir (str): Directory to save the videos. |
|
filename (str): Name of the output video file. |
|
""" |
|
if not os.path.exists(output_dir): |
|
os.makedirs(output_dir) |
|
|
|
cap = cv2.VideoCapture(0) |
|
|
|
if not cap.isOpened(): |
|
print("Error: Could not open webcam.") |
|
return |
|
|
|
|
|
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) |
|
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) |
|
fps = 20 |
|
|
|
|
|
fourcc = cv2.VideoWriter_fourcc(*'XVID') |
|
out = cv2.VideoWriter(os.path.join(output_dir, f"{filename}.avi"), fourcc, fps, (frame_width, frame_height)) |
|
|
|
print("Recording started. Press 'q' to stop early.") |
|
|
|
frame_count = 0 |
|
total_frames = duration * fps |
|
|
|
while frame_count < total_frames: |
|
ret, frame = cap.read() |
|
if ret: |
|
out.write(frame) |
|
cv2.imshow('Recording', frame) |
|
frame_count += 1 |
|
|
|
|
|
if cv2.waitKey(1) & 0xFF == ord('q'): |
|
break |
|
else: |
|
print("Failed to grab frame.") |
|
break |
|
|
|
|
|
cap.release() |
|
out.release() |
|
cv2.destroyAllWindows() |
|
print(f"Recording finished. Video saved as {filename}.avi") |
|
|
|
if __name__ == "__main__": |
|
|
|
label = input("Enter movement label (e.g., 'upward_eyebrow'): ") |
|
filename = input("Enter filename (e.g., 'movement1'): ") |
|
record_video(duration=2, filename=filename) |