freealise commited on
Commit
78416ce
·
verified ·
1 Parent(s): 3f3f85d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +9 -21
app.py CHANGED
@@ -136,7 +136,7 @@ def infer(f_in, interpolation, fps_output):
136
  return final_vid, files
137
 
138
 
139
- def remove_bg(fl, s, l, v, h, l_t):
140
  frame = cv2.imread(fl).astype(np.uint8)
141
 
142
  b = 5
@@ -158,25 +158,17 @@ def remove_bg(fl, s, l, v, h, l_t):
158
 
159
 
160
  hsv_ = cv2.cvtColor(frame_c, cv2.COLOR_BGR2HSV)
161
- hue_s = np.zeros_like(hsv_)
162
  edges = cv2.Laplacian(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), cv2.CV_64F)
163
  blur_s = np.zeros_like(edges)
164
  for i in range(2, frame.shape[0]-2):
165
  for j in range(2, frame.shape[1]-2):
166
  d = edges[i-2:i+2, j-2:j+2].var()
167
  blur_s[i,j] = d.astype(np.uint8)
168
-
169
- d = hsv_[i-1:i+1, j-1:j+1, 0].var()
170
- hue_s[i,j] = (d*16).astype(np.uint8)
171
 
172
  print(fl)
173
- print("hue variance")
174
- print(np.average(hue_s))
175
- print(np.median(hue_s))
176
- print("texture")
177
  print(np.average(blur_s))
178
  print(np.median(blur_s))
179
- #remove regions of low saturation and lightness (get scene without shadow)
180
  print("saturation")
181
  print(np.average(hsv_[:,:,1]))
182
  print(np.median(hsv_[:,:,1]))
@@ -184,21 +176,18 @@ def remove_bg(fl, s, l, v, h, l_t):
184
  print(np.average(hsv_[:,:,2]))
185
  print(np.median(hsv_[:,:,2]))
186
 
 
187
  if l_t == "slider":
188
  m = cv2.inRange(hsv_, np.array([0,0,0]), np.array([180,s,l]))
189
- m_ = cv2.inRange(hue_s, 0, h)
190
  mask = cv2.inRange(blur_s, 0, v)
191
  elif l_t == "average":
192
  m = cv2.inRange(hsv_, np.array([0,0,0]), np.array([180, int(np.average(hsv_[:,:,1])), int(np.average(hsv_[:,:,2]))]))
193
- m_ = cv2.inRange(hue_s, 0, int(np.average(hue_s)))
194
  mask = cv2.inRange(blur_s, 0, int(np.average(blur_s)))
195
  elif l_t == "median":
196
  m = cv2.inRange(hsv_, np.array([0,0,0]), np.array([180, int(np.median(hsv_[:,:,1])), int(np.median(hsv_[:,:,2]))]))
197
- m_ = cv2.inRange(hue_s, 0, int(np.median(hue_s)))
198
  mask = cv2.inRange(blur_s, 0, int(np.median(blur_s)))
199
 
200
  masks = np.bitwise_and(m, mask)
201
- masks = np.bitwise_and(m_, masks)
202
  frame_[masks==0] = (0,0,0)
203
 
204
 
@@ -289,7 +278,7 @@ def sortFiles(e):
289
  e = e.split('/')
290
  return e[len(e)-1]
291
 
292
- def loadf(f, s, l, v, h, l_t, r_bg):
293
  if f != None and f[0] != None:
294
  f.sort(key=sortFiles)
295
  fnew = []
@@ -300,7 +289,7 @@ def loadf(f, s, l, v, h, l_t, r_bg):
300
  fl = sharpest(fl, i)
301
 
302
  if r_bg == True:
303
- fl = remove_bg(fl, s, l, v, h, l_t)
304
 
305
  fnew.append(fl)
306
 
@@ -344,11 +333,10 @@ with gr.Blocks() as demo:
344
  with gr.Tab(label="Shadow maximums"):
345
  max_s = gr.Slider(minimum=0, maximum=255, step=1, value=32, label="Saturation")
346
  max_l = gr.Slider(minimum=0, maximum=255, step=1, value=64, label="Lightness")
347
- max_v = gr.Slider(minimum=0, maximum=255, step=1, value=16, label="Texture")
348
- max_h = gr.Slider(minimum=0, maximum=255, step=1, value=127, label="Hue variance")
349
  lt = gr.Radio(label="Maximum is", choices=["average", "median", "slider"], value="slider")
350
  rbg = gr.Checkbox(label="Remove background", value=True)
351
- files_input.upload(fn=loadf, inputs=[files_input, max_s, max_l, max_v, max_h, lt, rbg], outputs=[files_input, gallery_input])
352
  with gr.Row():
353
  interpolation_slider = gr.Slider(minimum=1, maximum=5, step=1, value=1, label="Interpolation Steps: ")
354
  interpolation = gr.Number(value=1, show_label=False, interactive=False)
@@ -366,10 +354,10 @@ with gr.Blocks() as demo:
366
  gr.Examples(
367
  examples=[[
368
  ["./examples/0.png", "./examples/1.png", "./examples/2.png", "./examples/3.png", "./examples/4.png"],
369
- 32, 64, 16, 127, "slider", True
370
  ]],
371
  fn=loadf,
372
- inputs=[files_input, max_s, max_l, max_v, max_h, lt, rbg],
373
  outputs=[files_input, gallery_input],
374
  cache_examples=True
375
  )
 
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
 
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]))
 
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
 
 
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 = []
 
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
 
 
333
  with gr.Tab(label="Shadow maximums"):
334
  max_s = gr.Slider(minimum=0, maximum=255, step=1, value=32, label="Saturation")
335
  max_l = gr.Slider(minimum=0, maximum=255, step=1, value=64, label="Lightness")
336
+ max_v = gr.Slider(minimum=0, maximum=255, step=1, value=16, label="Detail")
 
337
  lt = gr.Radio(label="Maximum is", choices=["average", "median", "slider"], value="slider")
338
  rbg = gr.Checkbox(label="Remove background", value=True)
339
+ files_input.upload(fn=loadf, inputs=[files_input, max_s, max_l, max_v, lt, rbg], outputs=[files_input, gallery_input])
340
  with gr.Row():
341
  interpolation_slider = gr.Slider(minimum=1, maximum=5, step=1, value=1, label="Interpolation Steps: ")
342
  interpolation = gr.Number(value=1, show_label=False, interactive=False)
 
354
  gr.Examples(
355
  examples=[[
356
  ["./examples/0.png", "./examples/1.png", "./examples/2.png", "./examples/3.png", "./examples/4.png"],
357
+ 32, 64, 16, "slider", True
358
  ]],
359
  fn=loadf,
360
+ inputs=[files_input, max_s, max_l, max_v, lt, rbg],
361
  outputs=[files_input, gallery_input],
362
  cache_examples=True
363
  )