Ayushnangia commited on
Commit
461400f
·
1 Parent(s): baed345
Files changed (1) hide show
  1. app.py +252 -1
app.py CHANGED
@@ -1,3 +1,254 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import os
2
  import cv2
3
  import gradio as gr
@@ -13,7 +264,7 @@ def download_youtube_video(youtube_url, output_filename):
13
  ydl.download([youtube_url])
14
 
15
  def process_video(youtube_url, start_time, flow_frame_offset):
16
- model_path = 'raft_small_iter10_240x320.onnx'
17
  flow_estimator = Raft(model_path)
18
 
19
  output_filename = 'downloaded_video.mp4'
 
1
+ import cv2
2
+ import time
3
+ import numpy as np
4
+ import onnx
5
+ import onnxruntime
6
+
7
+ # Ref: https://github.com/liruoteng/OpticalFlowToolkit/blob/5cf87b947a0032f58c922bbc22c0afb30b90c418/lib/flowlib.py#L249
8
+
9
+ import numpy as np
10
+
11
+ UNKNOWN_FLOW_THRESH = 1e7
12
+
13
+ def make_color_wheel():
14
+ """
15
+ Generate color wheel according Middlebury color code
16
+ :return: Color wheel
17
+ """
18
+ RY = 15
19
+ YG = 6
20
+ GC = 4
21
+ CB = 11
22
+ BM = 13
23
+ MR = 6
24
+
25
+ ncols = RY + YG + GC + CB + BM + MR
26
+
27
+ colorwheel = np.zeros([ncols, 3])
28
+
29
+ col = 0
30
+
31
+ # RY
32
+ colorwheel[0:RY, 0] = 255
33
+ colorwheel[0:RY, 1] = np.transpose(np.floor(255*np.arange(0, RY) / RY))
34
+ col += RY
35
+
36
+ # YG
37
+ colorwheel[col:col+YG, 0] = 255 - np.transpose(np.floor(255*np.arange(0, YG) / YG))
38
+ colorwheel[col:col+YG, 1] = 255
39
+ col += YG
40
+
41
+ # GC
42
+ colorwheel[col:col+GC, 1] = 255
43
+ colorwheel[col:col+GC, 2] = np.transpose(np.floor(255*np.arange(0, GC) / GC))
44
+ col += GC
45
+
46
+ # CB
47
+ colorwheel[col:col+CB, 1] = 255 - np.transpose(np.floor(255*np.arange(0, CB) / CB))
48
+ colorwheel[col:col+CB, 2] = 255
49
+ col += CB
50
+
51
+ # BM
52
+ colorwheel[col:col+BM, 2] = 255
53
+ colorwheel[col:col+BM, 0] = np.transpose(np.floor(255*np.arange(0, BM) / BM))
54
+ col += + BM
55
+
56
+ # MR
57
+ colorwheel[col:col+MR, 2] = 255 - np.transpose(np.floor(255 * np.arange(0, MR) / MR))
58
+ colorwheel[col:col+MR, 0] = 255
59
+
60
+ return colorwheel
61
+
62
+ colorwheel = make_color_wheel()
63
+
64
+ def compute_color(u, v):
65
+ """
66
+ compute optical flow color map
67
+ :param u: optical flow horizontal map
68
+ :param v: optical flow vertical map
69
+ :return: optical flow in color code
70
+ """
71
+ [h, w] = u.shape
72
+ img = np.zeros([h, w, 3])
73
+ nanIdx = np.isnan(u) | np.isnan(v)
74
+ u[nanIdx] = 0
75
+ v[nanIdx] = 0
76
+
77
+ ncols = np.size(colorwheel, 0)
78
+
79
+ rad = np.sqrt(u**2+v**2)
80
+
81
+ a = np.arctan2(-v, -u) / np.pi
82
+
83
+ fk = (a+1) / 2 * (ncols - 1) + 1
84
+
85
+ k0 = np.floor(fk).astype(int)
86
+
87
+ k1 = k0 + 1
88
+ k1[k1 == ncols+1] = 1
89
+ f = fk - k0
90
+
91
+ for i in range(0, np.size(colorwheel,1)):
92
+ tmp = colorwheel[:, i]
93
+ col0 = tmp[k0-1] / 255
94
+ col1 = tmp[k1-1] / 255
95
+ col = (1-f) * col0 + f * col1
96
+
97
+ idx = rad <= 1
98
+ col[idx] = 1-rad[idx]*(1-col[idx])
99
+ notidx = np.logical_not(idx)
100
+
101
+ col[notidx] *= 0.75
102
+ img[:, :, i] = np.uint8(np.floor(255 * col*(1-nanIdx)))
103
+
104
+ return img
105
+
106
+ def flow_to_image(flow):
107
+ """
108
+ Convert flow into middlebury color code image
109
+ :param flow: optical flow map
110
+ :return: optical flow image in middlebury color
111
+ """
112
+ u = flow[:, :, 0]
113
+ v = flow[:, :, 1]
114
+
115
+ maxu = -999.
116
+ maxv = -999.
117
+ minu = 999.
118
+ minv = 999.
119
+
120
+ idxUnknow = (abs(u) > UNKNOWN_FLOW_THRESH) | (abs(v) > UNKNOWN_FLOW_THRESH)
121
+ u[idxUnknow] = 0
122
+ v[idxUnknow] = 0
123
+
124
+ maxu = max(maxu, np.max(u))
125
+ minu = min(minu, np.min(u))
126
+
127
+ maxv = max(maxv, np.max(v))
128
+ minv = min(minv, np.min(v))
129
+
130
+ rad = np.sqrt(u ** 2 + v ** 2)
131
+ maxrad = max(-1, np.max(rad))
132
+
133
+ u = u/(maxrad + np.finfo(float).eps)
134
+ v = v/(maxrad + np.finfo(float).eps)
135
+
136
+ img = compute_color(u, v)
137
+
138
+ idx = np.repeat(idxUnknow[:, :, np.newaxis], 3, axis=2)
139
+ img[idx] = 0
140
+
141
+ return np.uint8(img)
142
+ class Raft():
143
+
144
+ def __init__(self, model_path):
145
+
146
+ # Initialize model
147
+ self.initialize_model(model_path)
148
+
149
+ def __call__(self, img1, img2):
150
+
151
+ return self.estimate_flow(img1, img2)
152
+
153
+ def initialize_model(self, model_path):
154
+
155
+ self.session = onnxruntime.InferenceSession(model_path, providers=['CUDAExecutionProvider', 'CPUExecutionProvider'])
156
+
157
+ # Get model info
158
+ self.get_input_details()
159
+ self.get_output_details()
160
+
161
+ def estimate_flow(self, img1, img2):
162
+
163
+ input_tensor1 = self.prepare_input(img1)
164
+ input_tensor2 = self.prepare_input(img2)
165
+
166
+ outputs = self.inference(input_tensor1, input_tensor2)
167
+
168
+ self.flow_map = self.process_output(outputs)
169
+
170
+ return self.flow_map
171
+
172
+ def prepare_input(self, img):
173
+
174
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
175
+
176
+ self.img_height, self.img_width = img.shape[:2]
177
+
178
+ img_input = cv2.resize(img, (self.input_width,self.input_height))
179
+
180
+ # img_input = img_input/255
181
+ img_input = img_input.transpose(2, 0, 1)
182
+ img_input = img_input[np.newaxis,:,:,:]
183
+
184
+ return img_input.astype(np.float32)
185
+
186
+ def inference(self, input_tensor1, input_tensor2):
187
+
188
+ # start = time.time()
189
+ outputs = self.session.run(self.output_names, {self.input_names[0]: input_tensor1,
190
+ self.input_names[1]: input_tensor2})
191
+
192
+ # print(time.time() - start)
193
+ return outputs
194
+
195
+ def process_output(self, output):
196
+
197
+ flow_map = output[1][0].transpose(1, 2, 0)
198
+
199
+ return flow_map
200
+
201
+ def draw_flow(self):
202
+
203
+ # Convert flow to image
204
+ flow_img = flow_to_image(self.flow_map)
205
+
206
+ # Convert to BGR
207
+ flow_img = cv2.cvtColor(flow_img, cv2.COLOR_RGB2BGR)
208
+
209
+ # Resize the depth map to match the input image shape
210
+ return cv2.resize(flow_img, (self.img_width,self.img_height))
211
+
212
+ def get_input_details(self):
213
+
214
+ model_inputs = self.session.get_inputs()
215
+ self.input_names = [model_inputs[i].name for i in range(len(model_inputs))]
216
+
217
+ self.input_shape = model_inputs[0].shape
218
+ self.input_height = self.input_shape[2]
219
+ self.input_width = self.input_shape[3]
220
+
221
+ def get_output_details(self):
222
+
223
+ model_outputs = self.session.get_outputs()
224
+ self.output_names = [model_outputs[i].name for i in range(len(model_outputs))]
225
+
226
+ self.output_shape = model_outputs[0].shape
227
+ self.output_height = self.output_shape[2]
228
+ self.output_width = self.output_shape[3]
229
+
230
+ if __name__ == '__main__':
231
+
232
+ from imread_from_url import imread_from_url
233
+
234
+ # Initialize model
235
+ model_path='raft_small_iter10_240x320.onnx'
236
+ flow_estimator = Raft(model_path)
237
+
238
+ # Read inference image
239
+ img1 = imread_from_url("https://github.com/princeton-vl/RAFT/blob/master/demo-frames/frame_0016.png?raw=true")
240
+ img2 = imread_from_url("https://github.com/princeton-vl/RAFT/blob/master/demo-frames/frame_0025.png?raw=true")
241
+
242
+ # Estimate flow and colorize it
243
+ flow_map = flow_estimator(img1, img2)
244
+ flow_img = flow_estimator.draw_flow()
245
+
246
+ combined_img = np.hstack((img1, img2, flow_img))
247
+
248
+ cv2.namedWindow("Estimated flow", cv2.WINDOW_NORMAL)
249
+ cv2.imshow("Estimated flow", combined_img)
250
+ cv2.waitKey(0)
251
+
252
  import os
253
  import cv2
254
  import gradio as gr
 
264
  ydl.download([youtube_url])
265
 
266
  def process_video(youtube_url, start_time, flow_frame_offset):
267
+ model_path = 'models/raft_small_iter10_240x320.onnx'
268
  flow_estimator = Raft(model_path)
269
 
270
  output_filename = 'downloaded_video.mp4'