-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtimelapse.py
64 lines (54 loc) · 2.28 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
55
56
57
58
59
60
61
62
63
64
#!/usr/local/bin/python3
import cv2 as cv
import argparse
import os
import time
# Construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-dir", "--directory", required=False, default='.', help="image directory. default is current directory")
ap.add_argument("-ext", "--extension", required=False, default='png', help="extension name. default is 'png'.")
ap.add_argument("-o", "--output", required=False, default='output.mp4', help="output video file")
ap.add_argument("-t", "--time", required=True, help="")
ap.add_argument("-et", "--ending_time", required=False, default="-1", help="")
args = vars(ap.parse_args())
# Arguments
dir_path = args['directory']
ext = args['extension']
output = args['output']
images = []
for f in os.listdir(dir_path):
if f.endswith(ext):
images.append(f)
images.sort(key=lambda x: os.path.getctime(os.path.join(dir_path, x)))
end_time = os.path.getctime(os.path.join(dir_path, images[-1]))
start_time = os.path.getctime(os.path.join(dir_path, images[0]))
ending_time = int(args['ending_time'])
if end_time < 0:
end_time = (end_time - start_time)/size(images)
time_diff = end_time - start_time + ending_time
# Determine the width and height from the first image
image_path = os.path.join(dir_path, images[0])
frame = cv.imread(image_path)
cv.imshow('video',frame)
height, width, channels = frame.shape
# Define the codec and create VideoWriter object
fourcc = cv.VideoWriter_fourcc(*'mp4v') # Be sure to use lower case
out = cv.VideoWriter(output, fourcc, 20.0, (width, height))
cur_image = 0
for timestep in range(int(float(args['time'])*60)-ending_time):
print(timestep)
percent = timestep/(float(args['time'])*60-ending_time)
date = start_time + time_diff*percent
if date >= os.path.getctime(os.path.join(dir_path, images[cur_image+1])):
cur_image += 1
image_path = os.path.join(dir_path, images[cur_image])
frame = cv.imread(image_path)
out.write(frame) # Write out frame to video
cv.imshow('video',frame)
if (cv.waitKey(1) & 0xFF) == ord('q'): # Hit `q` to exit
break
time.sleep(1)
# Release everything if job is finished
out.release()
cv.destroyAllWindows()
print("The output video is {}".format(output))