-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeMP4.py
More file actions
72 lines (60 loc) · 1.99 KB
/
makeMP4.py
File metadata and controls
72 lines (60 loc) · 1.99 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
"""
This script is used to generate mp4 from images.
The images are stored in the 'img' folder.
The mp4 is saved in the 'out_sim.mp4' file.
Created by: David Leonardo Ramírez Parada
Email: david.parada@cimat.mx
"""
import matplotlib
matplotlib.use('TkAgg')
import matplotlib.animation as animation
import matplotlib.pyplot as plt
import numpy as np
import glob
import cv2
import sys
import os
from pathlib import Path
path = Path(__file__).parent.absolute()
num = sys.argv[1] if len(sys.argv) == 2 else 1
# Read images
img_dir = f'{path}/img'
img_all = glob.glob(os.path.join(img_dir, '*.jpg'))
# img_all.sort(key=lambda x: int(x.split('.')[0]))
img_names = []
for img_name in img_all:
# print(f'Loading image {(len(img_names)+1):>4}|{len(img_all):<4}', end='\r')
if img_name.split('_')[1].split(".")[0] == str(num):
img_names.append(img_name)
img_names.sort(key=lambda x: (x.split('/')[-1]).split('_')[0])
imgs = []
for iter, img_name in enumerate(img_names):
print(f'Loading image {(iter+1):>4}|{len(img_names):<4}', end='\r')
img = cv2.imread(os.path.join(img_dir, img_name))
imgs.append(img)
# Save images into mp4
fps = 30
size = (imgs[0].shape[1], imgs[0].shape[0])
videoWriter = cv2.VideoWriter(f'{path}/out/out_sim_{num}.mp4', cv2.VideoWriter_fourcc('m', 'p', '4', 'v'), fps, size)
for iter, img in enumerate(imgs):
print(f'Saving mp4 at {(iter+1):>4}|{len(imgs):<4}', end='\r')
videoWriter.write(img)
videoWriter.release()
print(f'The video has been saved as out_sim_{num}.mp4')
def quitCV2(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
cv2.destroyAllWindows()
exit()
cap = cv2.VideoCapture(f'{path}/out/out_sim_{num}.mp4')
cv2.namedWindow('Video', cv2.WINDOW_NORMAL)
cv2.setMouseCallback("Video", quitCV2)
while(cap.isOpened()):
ret, frame = cap.read()
if ret == True:
cv2.imshow('Video', frame)
if cv2.waitKey(25) & 0xFF == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()