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
|
import argparse
import os
import time
from datetime import datetime
from pytz import timezone
import pytz
import cv2
dir_current = os.path.dirname(os.path.realpath(__file__))
def save_video(save_folder=os.path.join(dir_current, 'video'),
target_fps=10,
save_interval_mins=30):
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1280)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH) # float
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) # float
print(f'Current W: {width}, H: {height}')
fps = cap.get(cv2.CAP_PROP_FPS)
#cap.set(cv2.CAP_PROP_FPS, 10)
date_format='%m_%d_%Y_%H_%M_%S'
os.makedirs(save_folder, exist_ok=True)
time_started = time.time()
date = datetime.now(tz=pytz.utc).astimezone(timezone('US/Pacific'))
save_path = os.path.join(save_folder, f'{date.strftime(date_format)}.mp4')
print(save_path)
vid_writer = cv2.VideoWriter(
save_path, cv2.VideoWriter_fourcc(*"mp4v"), fps, (int(width), int(height)))
prev = 0
try:
while True:
time_current = time.time()
if time_current - time_started >= save_interval_mins*60: # save new file
time_started = time_current
date = datetime.now(tz=pytz.utc).astimezone(timezone('US/Pacific'))
save_path = os.path.join(save_folder, f'{date.strftime(date_format)}.mp4')
vid_writer = cv2.VideoWriter(
save_path, cv2.VideoWriter_fourcc(*"mp4v"), fps, (int(width), int(height)))
print(save_path)
time_elapsed = time_current - prev
ret_val, frame = cap.read()
if target_fps > 0:
if time_elapsed < 1. / target_fps:
continue
prev = time_current
if ret_val:
#frame = cv2.rotate(frame, cv2.ROTATE_180)
date = datetime.now(tz=pytz.utc).astimezone(timezone('US/Pacific'))
frame = cv2.putText(frame,f'{date.strftime(date_format)}',
(10, 50), cv2.FONT_HERSHEY_SIMPLEX,
1, (255, 0, 0), 1, cv2.LINE_AA)
vid_writer.write(frame)
key_pressed = cv2.waitKey(1)
if key_pressed == 27 or key_pressed == ord("q") or key_pressed == ord("Q"):
break
print('exiting')
vid_writer.release()
cap.release()
except:
print('terminating')
vid_writer.release()
cap.release()
if __name__ == "__main__":
save_video(target_fps=10, save_interval_mins=30)
|