freealise commited on
Commit
19275bd
·
verified ·
1 Parent(s): df5410c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +4 -119
app.py CHANGED
@@ -136,105 +136,6 @@ def infer(f_in, interpolation, fps_output):
136
  return final_vid, files
137
 
138
 
139
- def remove_bg(fl, s, l, v, l_t):
140
- frame = cv2.imread(fl).astype(np.uint8)
141
-
142
- b = 5
143
- #subtract background (get scene with shadow)
144
- bg = cv2.medianBlur(frame, 255)
145
-
146
- frame_ = ((bg.astype(np.int16)-frame.astype(np.int16))+127).astype(np.uint8)
147
- frame_ = cv2.bilateralFilter(frame_, b*4, b*8, b*2)
148
- frame_ = cv2.medianBlur(frame_, b)
149
-
150
- element = cv2.getStructuringElement(cv2.MORPH_RECT, (2*b+1, 2*b+1), (b,b))
151
- frame_ = cv2.erode(cv2.dilate(frame_, element), element)
152
-
153
-
154
- #correct hue against light
155
- bg_gray = cv2.cvtColor(cv2.cvtColor(bg, cv2.COLOR_BGR2GRAY), cv2.COLOR_GRAY2BGR)
156
- bg_diff = (bg-bg_gray).astype(np.int16)
157
- frame_c = (frame.astype(np.int16)-bg_diff).astype(np.uint8)
158
-
159
-
160
- hsv_ = cv2.cvtColor(frame_c, cv2.COLOR_BGR2HSV)
161
- edges = cv2.Laplacian(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), cv2.CV_64F)
162
- blur_s = np.zeros_like(edges)
163
- for i in range(2, frame.shape[0]-2):
164
- for j in range(2, frame.shape[1]-2):
165
- d = edges[i-2:i+2, j-2:j+2].var()
166
- blur_s[i,j] = d.astype(np.uint8)
167
-
168
- print(fl)
169
- print("detail")
170
- print(np.average(blur_s))
171
- print(np.median(blur_s))
172
- print("saturation")
173
- print(np.average(hsv_[:,:,1]))
174
- print(np.median(hsv_[:,:,1]))
175
- print("lightness")
176
- print(np.average(hsv_[:,:,2]))
177
- print(np.median(hsv_[:,:,2]))
178
-
179
- #remove regions of low saturation, lightness and detail (get scene without shadow)
180
- if l_t == "slider":
181
- m = cv2.inRange(hsv_, np.array([0,0,0]), np.array([180,s,l]))
182
- mask = cv2.inRange(blur_s, 0, v)
183
- elif l_t == "average":
184
- m = cv2.inRange(hsv_, np.array([0,0,0]), np.array([180, int(np.average(hsv_[:,:,1])), int(np.average(hsv_[:,:,2]))]))
185
- mask = cv2.inRange(blur_s, 0, int(np.average(blur_s)))
186
- elif l_t == "median":
187
- m = cv2.inRange(hsv_, np.array([0,0,0]), np.array([180, int(np.median(hsv_[:,:,1])), int(np.median(hsv_[:,:,2]))]))
188
- mask = cv2.inRange(blur_s, 0, int(np.median(blur_s)))
189
-
190
- masks = np.bitwise_and(m, mask)
191
- frame_[masks==0] = (0,0,0)
192
-
193
-
194
- m_ = frame_.reshape((-1,3))
195
- # convert to np.float32
196
- m_ = np.float32(m_)
197
- # define criteria, number of clusters(K) and apply kmeans()
198
- criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 16, 1.0)
199
- K = 3
200
- ret,label,center=cv2.kmeans(m_, K, None, criteria, 16, cv2.KMEANS_PP_CENTERS)
201
- # Now convert back into uint8, and make original image
202
- center = np.uint8(center)
203
- res = center[label.flatten()]
204
- frame_ = res.reshape((frame_.shape))
205
-
206
-
207
- #remove shadows at edges
208
- m_ = cv2.inRange(frame_, np.array([128,128,128]), np.array([255,255,255]))
209
- frame_[m_>0] = (255,255,255)
210
- cv2.rectangle(frame_,(0,0),(frame_.shape[1]-1,frame_.shape[0]-1),(255,255,255),7)
211
- mask = cv2.floodFill(frame_, None, (0, 0), 255, 0, 0, (4 | cv2.FLOODFILL_FIXED_RANGE))[2] #(4 | cv2.FLOODFILL_FIXED_RANGE | cv2.FLOODFILL_MASK_ONLY | 255 << 8)
212
- # 255 << 8 tells to fill with the value 255)
213
- mask = mask[1:mask.shape[0]-1, 1:mask.shape[1]-1]
214
- frame_[mask>0] = (127,127,127)
215
- m_ = cv2.inRange(frame_, np.array([1,1,1]), np.array([127,127,127]))
216
- frame_[m_>0] = (127,127,127)
217
-
218
-
219
- #shadow is black, bg is white, fg is gray
220
- frame_ = 255 - cv2.cvtColor(frame_, cv2.COLOR_BGR2GRAY)
221
- m_ = cv2.inRange(frame_, 255, 255)
222
- frame_[m_>0] = 127
223
- m_ = cv2.inRange(frame_, 128, 128)
224
- frame_[m_>0] = 255
225
-
226
-
227
- #apply mask to output
228
- m = cv2.inRange(frame, np.array([240,240,240]), np.array([255,255,255]))
229
- frame[m>0] = (239,239,239)
230
- m = cv2.inRange(frame, np.array([0,0,0]), np.array([15,15,15]))
231
- frame[m>0] = (16,16,16)
232
- frame[frame_==0] = (frame[frame_==0] / 17).astype(np.uint8)
233
- frame[frame_==255] = (255,255,255)
234
-
235
- cv2.imwrite(fl, frame)
236
- return fl
237
-
238
  def logscale(linear):
239
  return int(math.pow(2, linear))
240
 
@@ -278,7 +179,7 @@ def sortFiles(e):
278
  e = e.split('/')
279
  return e[len(e)-1]
280
 
281
- def loadf(f, s, l, v, l_t, r_bg):
282
  if f != None and f[0] != None:
283
  f.sort(key=sortFiles)
284
  fnew = []
@@ -287,12 +188,8 @@ def loadf(f, s, l, v, l_t, r_bg):
287
  ftype = fl.split('/')
288
  if ftype[len(ftype)-1].split('.')[1] == 'mp4':
289
  fl = sharpest(fl, i)
290
-
291
- if r_bg == True:
292
- fl = remove_bg(fl, s, l, v, l_t)
293
 
294
  fnew.append(fl)
295
-
296
  return fnew, fnew
297
  else:
298
  return f, f
@@ -330,18 +227,7 @@ with gr.Blocks() as demo:
330
  files_orig = gr.File(file_count="multiple", file_types=['image', '.mp4'])
331
  files_input = gr.File(file_count="multiple", visible=False)
332
  gallery_input = gr.Gallery(label="Slideshow", preview=True, columns=8192, interactive=False)
333
- with gr.Accordion(label="Background removal settings", open=False):
334
- with gr.Tab(label="Shadow maximums"):
335
- max_s = gr.Slider(minimum=0, maximum=255, step=1, value=32, label="Saturation")
336
- max_l = gr.Slider(minimum=0, maximum=255, step=1, value=64, label="Lightness")
337
- max_v = gr.Slider(minimum=0, maximum=255, step=1, value=16, label="Detail")
338
- lt = gr.Radio(label="Maximum is", choices=["average", "median", "slider"], value="slider")
339
- rbg = gr.Checkbox(label="Remove background", value=True)
340
- files_orig.upload(fn=loadf, inputs=[files_orig, max_s, max_l, max_v, lt, rbg], outputs=[files_input, gallery_input])
341
- max_s.change(fn=loadf, inputs=[files_orig, max_s, max_l, max_v, lt, rbg], outputs=[files_input, gallery_input])
342
- max_l.change(fn=loadf, inputs=[files_orig, max_s, max_l, max_v, lt, rbg], outputs=[files_input, gallery_input])
343
- max_v.change(fn=loadf, inputs=[files_orig, max_s, max_l, max_v, lt, rbg], outputs=[files_input, gallery_input])
344
- lt.change(fn=loadf, inputs=[files_orig, max_s, max_l, max_v, lt, rbg], outputs=[files_input, gallery_input])
345
 
346
  with gr.Row():
347
  interpolation_slider = gr.Slider(minimum=1, maximum=5, step=1, value=1, label="Interpolation Steps: ")
@@ -359,11 +245,10 @@ with gr.Blocks() as demo:
359
 
360
  gr.Examples(
361
  examples=[[
362
- ["./examples/0.png", "./examples/1.png", "./examples/2.png", "./examples/3.png", "./examples/4.png"],
363
- 32, 64, 16, "slider", True
364
  ]],
365
  fn=loadf,
366
- inputs=[files_orig, max_s, max_l, max_v, lt, rbg],
367
  outputs=[files_input, gallery_input],
368
  cache_examples=True
369
  )
 
136
  return final_vid, files
137
 
138
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
139
  def logscale(linear):
140
  return int(math.pow(2, linear))
141
 
 
179
  e = e.split('/')
180
  return e[len(e)-1]
181
 
182
+ def loadf(f):
183
  if f != None and f[0] != None:
184
  f.sort(key=sortFiles)
185
  fnew = []
 
188
  ftype = fl.split('/')
189
  if ftype[len(ftype)-1].split('.')[1] == 'mp4':
190
  fl = sharpest(fl, i)
 
 
 
191
 
192
  fnew.append(fl)
 
193
  return fnew, fnew
194
  else:
195
  return f, f
 
227
  files_orig = gr.File(file_count="multiple", file_types=['image', '.mp4'])
228
  files_input = gr.File(file_count="multiple", visible=False)
229
  gallery_input = gr.Gallery(label="Slideshow", preview=True, columns=8192, interactive=False)
230
+ files_orig.upload(fn=loadf, inputs=[files_orig], outputs=[files_input, gallery_input])
 
 
 
 
 
 
 
 
 
 
 
231
 
232
  with gr.Row():
233
  interpolation_slider = gr.Slider(minimum=1, maximum=5, step=1, value=1, label="Interpolation Steps: ")
 
245
 
246
  gr.Examples(
247
  examples=[[
248
+ ["./examples/0.png", "./examples/1.png", "./examples/2.png", "./examples/3.png", "./examples/4.png"]
 
249
  ]],
250
  fn=loadf,
251
+ inputs=[files_orig],
252
  outputs=[files_input, gallery_input],
253
  cache_examples=True
254
  )