fffiloni commited on
Commit
58c6724
·
1 Parent(s): 2fa185e

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +16 -40
app.py CHANGED
@@ -63,54 +63,30 @@ def write_flo(flow, filename):
63
  flow.tofile(f)
64
  f.close()
65
 
66
- def warpImage(im, vx, vy, cast_uint8=True):
67
- '''
68
- function to warp images with different dimensions
69
- '''
70
- print(f"WARP IM INPUT SHAPE: {im.shape}")
71
-
72
- height2, width2, nChannels = im.shape
73
- print(f"WARP vx INPUT SHAPE: {vx.shape}")
74
- height1, width1 = vx.shape
75
-
76
- x = np.linspace(1, width2, width2)
77
- y = np.linspace(1, height2, height2)
78
- X = np.linspace(1, width1, width1)
79
- Y = np.linspace(1, height1, height1)
80
- xx, yy = np.meshgrid(x, y)
81
- XX, YY = np.meshgrid(X, Y)
82
- #XX = XX + vx
83
- XX = np.concatenate([XX, vx])
84
- #YY = YY + vy
85
- YY = np.concatenate([YY, vy])
86
- mask = (XX < 1) | (XX > width2) | (YY < 1) | (YY > height2)
87
- XX = np.clip(XX, 1, width2)
88
- YY = np.clip(XX, 1, height2)
89
-
90
- warpI2 = np.zeros((height1, width1, nChannels))
91
- for i in range(nChannels):
92
- f = LinearNDInterpolator(x, y, im[:, :, i], 'linear')
93
- foo = f(X, Y)
94
- foo[mask] = 0.6
95
- warpI2[:, :, i] = foo
96
-
97
- mask = 1 - mask
98
-
99
- if cast_uint8:
100
- warpI2 = warpI2.astype(np.uint8)
101
-
102
- return warpI2, mask
103
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
 
105
  def get_warp_res(fname_image, fname_flow, fname_output='warped.png'):
106
  print(f"FNAME IMAGE: {fname_image}")
107
  im2 = imread(fname_image)
108
- #im2 = fname_image
109
  print(f"FNAME IMAGE READED: {im2.shape}")
110
  flow = fname_flow.cpu().detach().numpy()
111
  print(f"FNAME FLOW READED: {flow.shape}")
112
- im_warped, _ = warpImage(im2, flow[:, :, 0], flow[:, :, 1])
113
- #imwrite(fname_output, im_warped)
114
 
115
  def infer():
116
  video_url = "https://download.pytorch.org/tutorial/pexelscom_pavel_danilyuk_basketball_hd.mp4"
 
63
  flow.tofile(f)
64
  f.close()
65
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
66
 
67
+ def warp_flow(img, flow, mul=1.):
68
+ img = np.array(img.convert('RGB'))
69
+ #flow = np.load(flow)
70
+ h, w = flow.shape[:2]
71
+ flow = flow.copy()
72
+ flow[:, :, 0] += np.arange(w)
73
+ flow[:, :, 1] += np.arange(h)[:, np.newaxis]
74
+ # print('flow stats', flow.max(), flow.min(), flow.mean())
75
+ # print(flow)
76
+ flow*=mul
77
+ # print('flow stats mul', flow.max(), flow.min(), flow.mean())
78
+ # res = cv2.remap(img, flow, None, cv2.INTER_LINEAR)
79
+ res = cv2.remap(img, flow, None, cv2.INTER_LANCZOS4)
80
+
81
+ return res
82
 
83
  def get_warp_res(fname_image, fname_flow, fname_output='warped.png'):
84
  print(f"FNAME IMAGE: {fname_image}")
85
  im2 = imread(fname_image)
 
86
  print(f"FNAME IMAGE READED: {im2.shape}")
87
  flow = fname_flow.cpu().detach().numpy()
88
  print(f"FNAME FLOW READED: {flow.shape}")
89
+ res = warp_flow(im2, flow, 1.)
 
90
 
91
  def infer():
92
  video_url = "https://download.pytorch.org/tutorial/pexelscom_pavel_danilyuk_basketball_hd.mp4"