Update app.py
Browse files
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):
|
| 149 |
global fl_
|
| 150 |
fr = cv2.imread(fl).astype(np.uint8)
|
| 151 |
|
|
@@ -159,11 +159,14 @@ def remove_bg(fl, count):
|
|
| 159 |
fr_bg = cv2.medianBlur(fr_bg, 255)
|
| 160 |
|
| 161 |
fr_diff = cv2.convertScaleAbs(fr.astype(np.int16)-fr_bg.astype(np.int16)).astype(np.uint8)
|
|
|
|
|
|
|
| 162 |
fr_diff = cv2.cvtColor(fr_diff, cv2.COLOR_BGR2GRAY)
|
| 163 |
|
| 164 |
-
md = 12
|
| 165 |
-
|
| 166 |
-
fr_diff[
|
|
|
|
| 167 |
|
| 168 |
cv2.rectangle(fr_diff,(0,0),(fr_diff.shape[1]-1,fr_diff.shape[0]-1),(255,255,255),1)
|
| 169 |
mask = cv2.floodFill(fr_diff, None, (0, 0), 255, 0, 0, (4 | cv2.FLOODFILL_FIXED_RANGE))[2] #(4 | cv.FLOODFILL_FIXED_RANGE | cv.FLOODFILL_MASK_ONLY | 255 << 8)
|
|
@@ -231,7 +234,7 @@ def sortFiles(e):
|
|
| 231 |
e = e.split('/')
|
| 232 |
return e[len(e)-1]
|
| 233 |
|
| 234 |
-
def loadf(f, r_bg):
|
| 235 |
if f != None and f[0] != None:
|
| 236 |
f.sort(key=sortFiles)
|
| 237 |
fnew = []
|
|
@@ -242,7 +245,7 @@ def loadf(f, r_bg):
|
|
| 242 |
fl = sharpest(fl, i)
|
| 243 |
|
| 244 |
if r_bg == True:
|
| 245 |
-
fl = remove_bg(fl, i)
|
| 246 |
if i % 2: # odd: is photo without the flash
|
| 247 |
fnew.append(fl)
|
| 248 |
else:
|
|
@@ -286,7 +289,8 @@ with gr.Blocks() as demo:
|
|
| 286 |
files_input = gr.File(file_count="multiple", visible=False)
|
| 287 |
gallery_input = gr.Gallery(label="Slideshow", preview=True, columns=8192, interactive=False)
|
| 288 |
r_bg = gr.Checkbox(label="Remove background", value=True)
|
| 289 |
-
|
|
|
|
| 290 |
|
| 291 |
with gr.Row():
|
| 292 |
interpolation_slider = gr.Slider(minimum=1, maximum=5, step=1, value=1, label="Interpolation Steps: ")
|
|
@@ -304,12 +308,12 @@ with gr.Blocks() as demo:
|
|
| 304 |
|
| 305 |
gr.Examples(
|
| 306 |
examples=[[
|
| 307 |
-
["./examples/0.png", "./examples/1.png", "./examples/2.png", "./examples/3.png", "./examples/4.png"], False
|
| 308 |
], [
|
| 309 |
-
["./examples/0_flash.jpg", "./examples/1_noflash.jpg", "./examples/2_flash.jpg", "./examples/3_noflash.jpg"], True
|
| 310 |
]],
|
| 311 |
fn=loadf,
|
| 312 |
-
inputs=[files_orig, r_bg],
|
| 313 |
outputs=[files_input, gallery_input],
|
| 314 |
cache_examples=True
|
| 315 |
)
|
|
|
|
| 145 |
def linscale(linear):
|
| 146 |
return int(math.log2(linear))
|
| 147 |
|
| 148 |
+
def remove_bg(fl, count, md):
|
| 149 |
global fl_
|
| 150 |
fr = cv2.imread(fl).astype(np.uint8)
|
| 151 |
|
|
|
|
| 159 |
fr_bg = cv2.medianBlur(fr_bg, 255)
|
| 160 |
|
| 161 |
fr_diff = cv2.convertScaleAbs(fr.astype(np.int16)-fr_bg.astype(np.int16)).astype(np.uint8)
|
| 162 |
+
hsv = cv2.cvtColor(fr_diff, cv2.COLOR_BGR2HSV) # range: 180, 255, 255
|
| 163 |
+
|
| 164 |
fr_diff = cv2.cvtColor(fr_diff, cv2.COLOR_BGR2GRAY)
|
| 165 |
|
| 166 |
+
#md = 12
|
| 167 |
+
fg = np.bitwise_and(hsv[:,:,2]>=md, hsv[:,:,1]>=md, hsv[:,:,0]>=md)
|
| 168 |
+
fr_diff[fg>0] = 255
|
| 169 |
+
fr_diff[fg==0] = 0
|
| 170 |
|
| 171 |
cv2.rectangle(fr_diff,(0,0),(fr_diff.shape[1]-1,fr_diff.shape[0]-1),(255,255,255),1)
|
| 172 |
mask = cv2.floodFill(fr_diff, None, (0, 0), 255, 0, 0, (4 | cv2.FLOODFILL_FIXED_RANGE))[2] #(4 | cv.FLOODFILL_FIXED_RANGE | cv.FLOODFILL_MASK_ONLY | 255 << 8)
|
|
|
|
| 234 |
e = e.split('/')
|
| 235 |
return e[len(e)-1]
|
| 236 |
|
| 237 |
+
def loadf(f, r_bg, md):
|
| 238 |
if f != None and f[0] != None:
|
| 239 |
f.sort(key=sortFiles)
|
| 240 |
fnew = []
|
|
|
|
| 245 |
fl = sharpest(fl, i)
|
| 246 |
|
| 247 |
if r_bg == True:
|
| 248 |
+
fl = remove_bg(fl, i, md)
|
| 249 |
if i % 2: # odd: is photo without the flash
|
| 250 |
fnew.append(fl)
|
| 251 |
else:
|
|
|
|
| 289 |
files_input = gr.File(file_count="multiple", visible=False)
|
| 290 |
gallery_input = gr.Gallery(label="Slideshow", preview=True, columns=8192, interactive=False)
|
| 291 |
r_bg = gr.Checkbox(label="Remove background", value=True)
|
| 292 |
+
md = gr.Slider(minimum=0, maximum=255, step=1, value=12, label="Min color diff for foreground")
|
| 293 |
+
files_orig.upload(fn=loadf, inputs=[files_orig, r_bg, md], outputs=[files_input, gallery_input])
|
| 294 |
|
| 295 |
with gr.Row():
|
| 296 |
interpolation_slider = gr.Slider(minimum=1, maximum=5, step=1, value=1, label="Interpolation Steps: ")
|
|
|
|
| 308 |
|
| 309 |
gr.Examples(
|
| 310 |
examples=[[
|
| 311 |
+
["./examples/0.png", "./examples/1.png", "./examples/2.png", "./examples/3.png", "./examples/4.png"], False, 0
|
| 312 |
], [
|
| 313 |
+
["./examples/0_flash.jpg", "./examples/1_noflash.jpg", "./examples/2_flash.jpg", "./examples/3_noflash.jpg"], True, 12
|
| 314 |
]],
|
| 315 |
fn=loadf,
|
| 316 |
+
inputs=[files_orig, r_bg, md],
|
| 317 |
outputs=[files_input, gallery_input],
|
| 318 |
cache_examples=True
|
| 319 |
)
|