-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
executable file
·159 lines (115 loc) · 4.21 KB
/
utils.py
File metadata and controls
executable file
·159 lines (115 loc) · 4.21 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import os
import cv2
import numpy as np
from math import floor
from numpy.linalg import svd, inv
def blendImages(sourceTransform, referenceTransform):
'''
Naive blending for frame stitching
Input:
- sourceTransform: source frame projected onto reference frame plane
- referenceTransform: reference frame projected onto same space
Output:
- blendedOutput: naive blending result from frame stitching
'''
blendedOutput = referenceTransform
indices = referenceTransform == 0
blendedOutput[indices] = sourceTransform[indices]
return (blendedOutput / blendedOutput.max() * 255).astype(np.uint8)
def get_img_corners(img):
'''
Returns the position of the corners of an color image
Input:
img: color image.
Output:
corners: image corners (np.array)
'''
corners = np.zeros((4, 1, 2), dtype=np.float32)
height, width, channels = img.shape
corners[0] = (0, 0) #top left
corners[1] = (width, 0) #top right
corners[2] = (width, height) #bottom right
corners[3] = (0, height) #bottom left
return corners
def video2imageFolder(input_file, output_path):
'''
Extracts the frames from an input video file
and saves them as separate frames in an output directory.
Input:
input_file: Input video file.
output_path: Output directorys.
Output:
None
'''
cap = cv2.VideoCapture()
cap.open(input_file)
if not cap.isOpened():
print("Failed to open input video")
frame_count = cap.get(cv2.CAP_PROP_FRAME_COUNT)
frame_idx = 0
while frame_idx < frame_count:
ret, frame = cap.read()
if not ret:
print ("Failed to get the frame {}".format(frameId))
continue
out_name = os.path.join(output_path, 'f{:04d}.jpg'.format(frame_idx+1))
ret = cv2.imwrite(out_name, frame)
if not ret:
print ("Failed to write the frame {}".format(frame_idx))
continue
frame_idx += 1
cap.set(cv2.CAP_PROP_POS_FRAMES, frame_idx)
def imageFolder2mpeg(input_path, output_path='./output_video.mpeg', fps=30.0):
'''
Extracts the frames from an input video file
and saves them as separate frames in an output directory.
Input:
input_path: Input video file.
output_path: Output directorys.
fps: frames per second (default: 30).
Output:
None
'''
dir_frames = input_path
files_info = os.scandir(dir_frames)
file_names = [f.path for f in files_info if f.name.endswith(".jpg")]
file_names.sort(key=lambda f: int(''.join(filter(str.isdigit, f))))
frame_Height, frame_Width = cv2.imread(file_names[0]).shape[:2]
resolution = (frame_Width, frame_Height)
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'MPG1')
video_writer = cv2.VideoWriter(output_path, fourcc, fps, resolution)
frame_count = len(file_names)
frame_idx = 0
while frame_idx < frame_count:
frame_i = cv2.imread(file_names[frame_idx])
video_writer.write(frame_i)
frame_idx += 1
video_writer.release()
def imageFolder2mp4(input_path, output_path='./output_video.mpeg', fps=30.0):
'''
Extracts the frames from an input video file
and saves them as separate frames in an output directory.
Input:
input_path: Input video file.
output_path: Output directorys.
fps: frames per second (default: 30).
Output:
None
'''
dir_frames = input_path
files_info = os.scandir(dir_frames)
file_names = [f.path for f in files_info if f.name.endswith(".jpg")]
file_names.sort(key=lambda f: int(''.join(filter(str.isdigit, f))))
frame_Height, frame_Width = cv2.imread(file_names[0]).shape[:2]
resolution = (frame_Width, frame_Height)
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
video_writer = cv2.VideoWriter(output_path, fourcc, fps, resolution)
frame_count = len(file_names)
frame_idx = 0
while frame_idx < frame_count:
frame_i = cv2.imread(file_names[frame_idx])
video_writer.write(frame_i)
frame_idx += 1
video_writer.release()