-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreorder.py
164 lines (121 loc) · 4.85 KB
/
reorder.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
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
160
161
162
163
164
import argparse
import numpy as np
import cv2
from scipy.spatial import distance
def write_video_full_resolution(video_file_in, video_file_out, frames, l_coresp):
"""Write a reordered video in its original resolution
Args:
video_file_in: original file
video_file_out: reordered file
frames: frames order
l_coresp: links between the initial video and the video without the outliers
"""
cap = cv2.VideoCapture(video_file_in)
info = {}
info['fps'] = cap.get(cv2.CAP_PROP_FPS)
info['codec'] = cap.get(cv2.CAP_PROP_FOURCC)
info['frame'] = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
print(info)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter(video_file_out,fourcc, info['fps'], info['frame'])
for el in frames:
idx = 0
while 1:
ret, frame_f = cap.read()
if not ret:
break
if idx == l_coresp[el]:
out.write(frame_f)
idx+=1
cap.set(cv2.CAP_PROP_POS_FRAMES, 0)
def import_video_for_analysis(video_file):
"""Read the original video file, outputs two lists
Args:
video_file: the file to read
Returns:
frame_list: the frames in low resolution (64x64)
histo_list: one histogram per frames
"""
cap = cv2.VideoCapture(video_file)
frame_list_dim = []
while 1:
ret, frame_f = cap.read()
if not ret:
break
frame_f = cv2.resize(frame_f, (64,64))
frame_list_dim.append(frame_f)
frame_list = [x.flatten() for x in frame_list_dim]
histo_list = [cv2.calcHist([x], [0, 1, 2], None, [8, 8, 8],
[0, 256, 0, 256, 0, 256]).flatten()
for x in frame_list_dim]
return frame_list, histo_list
def remove_outliers(histo_list, frame_list):
"""remove outliers from frame_list
Args:
histo_list: the histograms computed for each frame
frame_list: the frames, *the function changes this argument*
Returns
l_coresp: the connection between the initial video and the video without outliers
"""
histo_med = np.median(histo_list, axis=0)
dist_to_med = [cv2.compareHist(histo_med,x,
cv2.HISTCMP_CORREL) for x in histo_list]
excl_list = [idx for idx, _ in enumerate(histo_list) if dist_to_med[idx] < 0.85]
#0.85 is an empirical value
#remove outliers from the list
l_coresp = [x for x in range(len(frame_list))]
for i in sorted(excl_list, reverse=True):
del frame_list[i]
del l_coresp[i]
return l_coresp
def reord_frames(frame_list):
"""returns a list of frame in correct order"""
Y = distance.cdist(frame_list, frame_list, 'euclidean')
start_img = 0 #arbitrary start at 0
#algorithms
#1. search a frames order based on distance between frames. We assume if the distance between two frames is small, they should be consecutive.
#2. search the largest distance between two consecutive frames (to find the beginning of the video. We iterate 10 times to find this value.
for _ in range(10):
cur_im = start_img
seen_im = [cur_im]
last_img = len(frame_list)-len(seen_im)
for _ in range(last_img):
arg_l = np.argsort(Y[cur_im])
idx = 0
while(cur_im in seen_im):
cur_im = arg_l[idx]
idx += 1
seen_im.append(cur_im)
#search the largest distance between two consecutive frames
im1 = seen_im[-1]
maxi = 0
im_max = 0
for im2 in seen_im:
if Y[im1][im2] > maxi:
im_max = im1
maxi = Y[im1][im2]
im1 = im2
start_img = im_max
return seen_im
def main():
parser = argparse.ArgumentParser(description='Reorder video')
parser.add_argument("-vi", "--video_in", required=True,
help="Input video file")
parser.add_argument("-vo1", "--video_out_order_1", required=True,
help="Output video file 1")
parser.add_argument("-vo2", "--video_out_order_2", required=True,
help="Output video file 2")
args = parser.parse_args()
frame_list, histo_list = import_video_for_analysis(args.video_in)
l_coresp = remove_outliers(histo_list, frame_list)
frame_ordo = reord_frames(frame_list)
write_video_full_resolution(args.video_in,
args.video_out_order_1,
frame_ordo,
l_coresp)
write_video_full_resolution(args.video_in,
args.video_out_order_2,
frame_ordo[::-1],
l_coresp)
if __name__ == "__main__":
main()