forked from ITA-TECNOLOGIA/BodyFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateVideo.py
More file actions
81 lines (65 loc) · 2.03 KB
/
Copy pathcreateVideo.py
File metadata and controls
81 lines (65 loc) · 2.03 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
import os
import cv2
import time
import threading
import numpy as np
from os.path import join
import signal
import sys
stop_video = False
def signal_handler(sig, frame):
print('You pressed Ctrl+C!')
global stop_video
stop_video = True
# sys.exit(0)
signal.signal(signal.SIGINT, signal_handler)
def capture_video(output_dir='testing'):
global stop_video
cap = cv2.VideoCapture(0)
if not cap.isOpened():
print("Error: Cannot open camera.")
return
# Dynamically obtain frame size
frame_width = int(cap.get(3))
frame_height = int(cap.get(4))
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(join(output_dir, 'video.mp4'), fourcc, 24.0, (frame_width, frame_height))
timestamps = []
new_time = time.time()
while True:
timestamps.append(time.perf_counter())
ret, frame = cap.read()
if not ret:
print("Failed to grab a frame.")
break
out.write(frame)
old_time = new_time
new_time = time.time()
print(f"FPS: {1/(new_time-old_time)}")
print(f"time: {new_time-old_time}")
# cv2.imshow('frame', frame)
if stop_video:
break
cap.release()
out.release()
timestamps = np.array([timing - timestamps[0] for timing in timestamps])
np.save(join(output_dir, 'timestamps.npy'), timestamps)
cv2.destroyAllWindows()
datadir = 'data/input/megan'
os.makedirs(datadir, exist_ok=True)
video_thread = threading.Thread(target=capture_video, args=(datadir,))
video_thread.start()
# wait for thread to finish:
video_thread.join()
timestamps = np.load(join(datadir, 'timestamps.npy'))
# Open the video file
cap = cv2.VideoCapture(join(datadir, 'video.mp4'))
# get the frame rate
fps = cap.get(cv2.CAP_PROP_FPS)
print(f"Frame Rate: {fps}")
# get length of the video
length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
print(f"Length: {length}")
print(f'timestamps: {timestamps}')
print(f'len(timestamps): {len(timestamps)}')