freealise commited on
Commit
4ad0232
·
verified ·
1 Parent(s): e23cd3b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +22 -12
app.py CHANGED
@@ -145,7 +145,7 @@ def logscale(linear):
145
  def linscale(linear):
146
  return int(math.log2(linear))
147
 
148
- def remove_bg(fl, count, mh, ms, md):
149
  global fl_
150
  fr = cv2.imread(fl).astype(np.uint8)
151
 
@@ -164,6 +164,14 @@ def remove_bg(fl, count, mh, ms, md):
164
  fr_diff = cv2.cvtColor(fr_diff, cv2.COLOR_BGR2GRAY)
165
 
166
  #md = 12
 
 
 
 
 
 
 
 
167
  bg = cv2.inRange(hsv, np.array([0,0,0]), np.array([mh,ms,md]))
168
  fr_diff[bg>0] = 0
169
  fr_diff[bg==0] = 255
@@ -234,7 +242,7 @@ def sortFiles(e):
234
  e = e.split('/')
235
  return e[len(e)-1]
236
 
237
- def loadf(f, r_bg, mh, ms, md):
238
  if f != None and f[0] != None:
239
  f.sort(key=sortFiles)
240
  fnew = []
@@ -245,7 +253,7 @@ def loadf(f, r_bg, mh, ms, md):
245
  fl = sharpest(fl, i)
246
 
247
  if r_bg == True:
248
- fl = remove_bg(fl, i, mh, ms, md)
249
  if i % 2: # odd: is photo without the flash
250
  fnew.append(fl)
251
  else:
@@ -288,12 +296,14 @@ with gr.Blocks() as demo:
288
  files_orig = gr.File(file_count="multiple", file_types=['image', '.mp4'])
289
  files_input = gr.File(file_count="multiple", visible=False)
290
  gallery_input = gr.Gallery(label="Slideshow", preview=True, columns=8192, interactive=False)
291
- with gr.Accordion(label="Max differences for background", open=False):
292
- mh = gr.Slider(minimum=0, maximum=180, step=1, value=180, label="Hue")
293
- ms = gr.Slider(minimum=0, maximum=255, step=1, value=255, label="Saturation")
294
- md = gr.Slider(minimum=0, maximum=255, step=1, value=12, label="Lightness")
295
- r_bg = gr.Checkbox(label="Remove background", value=True)
296
- files_orig.upload(fn=loadf, inputs=[files_orig, r_bg, mh, ms, md], outputs=[files_input, gallery_input])
 
 
297
 
298
  with gr.Row():
299
  interpolation_slider = gr.Slider(minimum=1, maximum=5, step=1, value=1, label="Interpolation Steps: ")
@@ -311,12 +321,12 @@ with gr.Blocks() as demo:
311
 
312
  gr.Examples(
313
  examples=[[
314
- ["./examples/0.png", "./examples/1.png", "./examples/2.png", "./examples/3.png", "./examples/4.png"], False, 0, 0, 0
315
  ], [
316
- ["./examples/0_flash.jpg", "./examples/1_noflash.jpg", "./examples/2_flash.jpg", "./examples/3_noflash.jpg"], True, 180, 255, 12
317
  ]],
318
  fn=loadf,
319
- inputs=[files_orig, r_bg, mh, ms, md],
320
  outputs=[files_input, gallery_input],
321
  cache_examples=True
322
  )
 
145
  def linscale(linear):
146
  return int(math.log2(linear))
147
 
148
+ def remove_bg(fl, count, mh, ms, md, lm):
149
  global fl_
150
  fr = cv2.imread(fl).astype(np.uint8)
151
 
 
164
  fr_diff = cv2.cvtColor(fr_diff, cv2.COLOR_BGR2GRAY)
165
 
166
  #md = 12
167
+ if lm == "median":
168
+ mh = np.median(hsv[:,:,0])
169
+ ms = np.median(hsv[:,:,1])
170
+ md = np.median(hsv[:,:,2])
171
+ elif lm == "average":
172
+ mh = np.average(hsv[:,:,0])
173
+ ms = np.average(hsv[:,:,1])
174
+ md = np.average(hsv[:,:,2])
175
  bg = cv2.inRange(hsv, np.array([0,0,0]), np.array([mh,ms,md]))
176
  fr_diff[bg>0] = 0
177
  fr_diff[bg==0] = 255
 
242
  e = e.split('/')
243
  return e[len(e)-1]
244
 
245
+ def loadf(f, r_bg, mh, ms, md, lm):
246
  if f != None and f[0] != None:
247
  f.sort(key=sortFiles)
248
  fnew = []
 
253
  fl = sharpest(fl, i)
254
 
255
  if r_bg == True:
256
+ fl = remove_bg(fl, i, mh, ms, md, lm)
257
  if i % 2: # odd: is photo without the flash
258
  fnew.append(fl)
259
  else:
 
296
  files_orig = gr.File(file_count="multiple", file_types=['image', '.mp4'])
297
  files_input = gr.File(file_count="multiple", visible=False)
298
  gallery_input = gr.Gallery(label="Slideshow", preview=True, columns=8192, interactive=False)
299
+ with gr.Group(label="Background removal settings"):
300
+ r_bg = gr.Checkbox(label="Remove background", value=True)
301
+ with gr.Accordion(label="Max differences for background", open=False):
302
+ mh = gr.Slider(minimum=0, maximum=180, step=1, value=180, label="Hue")
303
+ ms = gr.Slider(minimum=0, maximum=255, step=1, value=255, label="Saturation")
304
+ md = gr.Slider(minimum=0, maximum=255, step=1, value=12, label="Lightness")
305
+ lm = gr.Radio(label="Use max diffs from", choices=["average", "median", "slider"], value="slider")
306
+ files_orig.upload(fn=loadf, inputs=[files_orig, r_bg, mh, ms, md, lm], outputs=[files_input, gallery_input])
307
 
308
  with gr.Row():
309
  interpolation_slider = gr.Slider(minimum=1, maximum=5, step=1, value=1, label="Interpolation Steps: ")
 
321
 
322
  gr.Examples(
323
  examples=[[
324
+ ["./examples/0.png", "./examples/1.png", "./examples/2.png", "./examples/3.png", "./examples/4.png"], False, 0, 0, 0, "slider"
325
  ], [
326
+ ["./examples/0_flash.jpg", "./examples/1_noflash.jpg", "./examples/2_flash.jpg", "./examples/3_noflash.jpg"], True, 180, 255, 12, "slider"
327
  ]],
328
  fn=loadf,
329
+ inputs=[files_orig, r_bg, mh, ms, md, lm],
330
  outputs=[files_input, gallery_input],
331
  cache_examples=True
332
  )