-
Notifications
You must be signed in to change notification settings - Fork 0
/
timelapse.py
54 lines (44 loc) · 1.4 KB
/
timelapse.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
import numpy as np
import os
import cv2
import datetime
import time
import glob
from utils import CFEVideoConf, image_resize
cap=cv2.VideoCapture(0)
save_path='saved-media/timelapse.mp4'
frames_per_seconds=24.0
config= CFEVideoConf(cap, filepath=save_path, res='720p')
out= cv2.VideoWriter(save_path, config.video_type, frames_per_seconds, config.dims)
seconds_duration=10
seconds_between_shots= .25
timelapse_img_dir='images/timelapse/'
if not os.path.exists(timelapse_img_dir):
os.mkdir(timelapse_img_dir)
now= datetime.datetime.now()
finish_time= now+ datetime.timedelta(seconds=seconds_duration)
i=0
while datetime.datetime.now() < finish_time:
ret, frame = cap.read()
filename= f"{timelapse_img_dir}/{i}.jpg"
i+=1
cv2.imwrite(filename, frame)
out.write(frame)
cv2.imshow('frame',frame)
time.sleep(seconds_between_shots)
if cv2.waitKey(20) & 0xFF == ord('q'):
break
clear_images=True
def images_to_video(out, img_dir, clear_images=True):
image_list = glob.glob(f"{img_dir}/*.jpg")
sorted_images= sorted(image_list, key=os.path.getmtime)
for file in sorted_images:
image_frame= cv2.imread(file)
out.write(image_frame)
if clear_images:
for file in image_list:
os.remove(file)
images_to_video(out, timelapse_img_dir)
cap.release()
out.release()
cv2.destroyAllWindows()