forked from daviddao/spatial-transformer-tensorflow
-
Notifications
You must be signed in to change notification settings - Fork 1
/
deploy_flow_.py
156 lines (141 loc) · 6.25 KB
/
deploy_flow_.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
import tensorflow as tf
import numpy as np
from config import *
from PIL import Image
import cv2
import time
import os
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('--model-dir')
parser.add_argument('--model-name')
parser.add_argument('--before-ch', type=int)
parser.add_argument('--after-ch', type=int)
parser.add_argument('--output-dir', default='data_video_local')
parser.add_argument('--infer_with_stable', action='store_true')
args = parser.parse_args()
start_with_stable = True
sess = tf.Session()
model_dir = args.model_dir#'models/vbeta-1.1.0/'
model_name = args.model_name#'model-5000'
before_ch = args.before_ch
after_ch = args.after_ch
new_saver = tf.train.import_meta_graph(model_dir + model_name + '.meta')
new_saver.restore(sess, model_dir + model_name)
graph = tf.get_default_graph()
x_tensor = graph.get_tensor_by_name('stable_net/input/x_tensor:0')
#output = graph.get_tensor_by_name('stable_net/SpatialTransformer/_transform/Reshape_7:0')
#black_pix = graph.get_tensor_by_name('stable_net/SpatialTransformer/_transform/Reshape_6:0')
output = graph.get_tensor_by_name('stable_net/inference/SpatialTransformer/_transform/Reshape_7:0')
black_pix = graph.get_tensor_by_name('stable_net/inference/SpatialTransformer/_transform/Reshape_6:0')
#black_pix = graph.get_tensor_by_name('stable_net/img_loss/StopGradient:0')
#list_f = open('data_video/test_list_deploy', 'r')
list_f = open('data_video/test_list_deploy', 'r')
temp = list_f.read()
video_list = temp.split('\n')
list_f = open('data_video/train_list_deploy', 'r')
temp = list_f.read()
video_list.extend(temp.split('\n'))
def make_dirs(path):
if not os.path.exists(path): os.makedirs(path)
def draw_imgs(net_output, stable_frame, unstable_frame):
cvt2int32 = lambda x: x.astype(np.int32)
assert(net_output.ndim == 2)
assert(stable_frame.ndim == 2)
assert(unstable_frame.ndim == 2)
net_output = cvt2int32(net_output)
stable_frame = cvt2int32(stable_frame)
unstable_frame = cvt2int32(unstable_frame)
output_minus_input = abs(net_output - unstable_frame)
output_minus_stable = abs(net_output - stable_frame)
img_top = np.concatenate([net_output, output_minus_stable], axis=1)
img_bottom = np.concatenate([output_minus_input, np.zeros_like(output_minus_input)], axis=1)
img = np.concatenate([img_top, img_bottom], axis=0).astype(np.uint8)
return cv2.cvtColor(img, cv2.COLOR_GRAY2BGR)
production_dir = os.path.join(args.output_dir, 'output')
visual_dir = os.path.join(args.output_dir, 'output-vis')
make_dirs(production_dir)
make_dirs(visual_dir)
for video_name in video_list:
if (video_name == ""):
continue
print(video_name)
unstable_cap = cv2.VideoCapture('data_video/unstable/' + video_name)
fps = unstable_cap.get(cv2.CAP_PROP_FPS)
print('data_video/unstable/' + video_name)
videoWriter = cv2.VideoWriter(os.path.join(production_dir, video_name),
cv2.VideoWriter_fourcc('M','J','P','G'), fps, (width, height))
videoWriterVis = cv2.VideoWriter(os.path.join(visual_dir, video_name),
cv2.VideoWriter_fourcc('M','J','P','G'), fps, (width * 2, height * 2))
before_frames = []
after_frames = []
print(video_name)
if (not start_with_stable):
ret, frame = unstable_cap.read()
for i in range(before_ch):
before_frames.append(cvt_img2train(frame, crop_rate))
for i in range(after_ch + 1):
ret, frame = unstable_cap.read()
after_frames.append(cvt_img2train(frame, 1))
temp = before_frames[0]
temp = ((np.reshape(temp, (height, width)) + 0.5) * 255).astype(np.uint8)
videoWriter.write(cv2.cvtColor(temp, cv2.COLOR_GRAY2BGR))
else:
stable_cap = cv2.VideoCapture('data_video/stable/' + video_name)
for i in range(before_ch):
ret, frame = unstable_cap.read()
ret, frame = stable_cap.read()
before_frames.append(cvt_img2train(frame, crop_rate))
temp = before_frames[i]
temp = ((np.reshape(temp, (height, width)) + 0.5) * 255).astype(np.uint8)
videoWriter.write(cv2.cvtColor(temp, cv2.COLOR_GRAY2BGR))
temp = np.concatenate([temp, np.zeros_like(temp)], axis=1)
temp = np.concatenate([temp, np.zeros_like(temp)], axis=0)
videoWriterVis.write(cv2.cvtColor(temp, cv2.COLOR_GRAY2BGR))
for i in range(after_ch + 1):
ret, frame = unstable_cap.read()
after_frames.append(cvt_img2train(frame, 1))
len = 0
while(True):
_, stable_cap_frame = stable_cap.read()
stable_train_frame = cvt_img2train(stable_cap_frame, crop_rate)
cvt_train2img = lambda x: ((np.reshape(x, (height, width)) + 0.5) * 255).astype(np.uint8)
stable_frame = cvt_train2img(stable_train_frame)
unstable_frame = cvt_train2img(after_frames[0])
in_x = before_frames[0]
for i in range(1, before_ch):
in_x = np.concatenate((in_x, before_frames[i]), axis = 3)
for i in range(after_ch + 1):
in_x = np.concatenate((in_x, after_frames[i]), axis = 3)
'''
in_x_t = in_x
for i in range(batch_size - 1):
in_x_t = np.concatenate((in_x_t, in_x), axis = 0)
'''
img, black = sess.run([output, black_pix], feed_dict={x_tensor:in_x})
black = black[0, :, :]
img = img[0, :, :, :].reshape(height, width)
frame = img + black * (-1)
frame = frame.reshape(1, height, width, 1)
img = ((np.reshape(img, (height, width)) + 0.5) * 255).astype(np.uint8)
net_output = img
img = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
videoWriter.write(img)
videoWriterVis.write(draw_imgs(net_output, stable_frame, unstable_frame))
ret, frame_unstable = unstable_cap.read()
if (not ret):
break
len = len + 1
if (len % 10 == 0):
print("len: " + str(len))
if args.infer_with_stable:
before_frames.append(stable_train_frame)
else:
before_frames.append(frame)
before_frames.pop(0)
after_frames.append(cvt_img2train(frame_unstable, 1))
after_frames.pop(0)
#if (len == 100):
# break
videoWriter.release()
unstable_cap.release()