Update app.py
Browse files
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):
|
140 |
frame = cv2.imread(fl).astype(np.uint8)
|
141 |
|
142 |
b = 5
|
@@ -157,19 +157,27 @@ def remove_bg(fl, s, l, v):
|
|
157 |
frame_c = (frame.astype(np.int16)-bg_diff).astype(np.uint8)
|
158 |
|
159 |
|
|
|
|
|
160 |
edges = cv2.Laplacian(cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY), cv2.CV_64F)
|
161 |
blur_s = np.zeros_like(edges)
|
162 |
for i in range(2, frame.shape[0]-2):
|
163 |
for j in range(2, frame.shape[1]-2):
|
164 |
-
d = edges[i-2:i+2,j-2:j+2].var()
|
165 |
blur_s[i,j] = (d/512).astype(np.uint8)
|
|
|
|
|
|
|
166 |
|
|
|
|
|
167 |
#remove regions of low saturation and lightness (get scene without shadow)
|
168 |
-
m = cv2.inRange(
|
|
|
169 |
mask = cv2.inRange(blur_s, 0, v)
|
170 |
masks = np.bitwise_and(m, mask)
|
|
|
171 |
frame_[masks==0] = (0,0,0)
|
172 |
-
#frame = cv2.medianBlur(frame, b)
|
173 |
|
174 |
|
175 |
m_ = frame_.reshape((-1,3))
|
@@ -259,7 +267,7 @@ def sortFiles(e):
|
|
259 |
e = e.split('/')
|
260 |
return e[len(e)-1]
|
261 |
|
262 |
-
def loadf(f, s, l, v, r_bg):
|
263 |
if f != None and f[0] != None:
|
264 |
f.sort(key=sortFiles)
|
265 |
fnew = []
|
@@ -270,7 +278,7 @@ def loadf(f, s, l, v, r_bg):
|
|
270 |
fl = sharpest(fl, i)
|
271 |
|
272 |
if r_bg == True:
|
273 |
-
fl = remove_bg(fl, s, l, v)
|
274 |
|
275 |
fnew.append(fl)
|
276 |
|
@@ -314,9 +322,10 @@ with gr.Blocks() as demo:
|
|
314 |
with gr.Tab(label="Shadow maximums"):
|
315 |
max_s = gr.Slider(minimum=0, maximum=255, step=1, value=32, label="Saturation")
|
316 |
max_l = gr.Slider(minimum=0, maximum=255, step=1, value=64, label="Lightness")
|
317 |
-
max_v = gr.Slider(minimum=0, maximum=255, step=1, value=16, label="
|
|
|
318 |
rbg = gr.Checkbox(label="Remove background", value=True)
|
319 |
-
files_input.upload(fn=loadf, inputs=[files_input, max_s, max_l, max_v, rbg], outputs=[files_input, gallery_input])
|
320 |
with gr.Row():
|
321 |
interpolation_slider = gr.Slider(minimum=1, maximum=5, step=1, value=1, label="Interpolation Steps: ")
|
322 |
interpolation = gr.Number(value=1, show_label=False, interactive=False)
|
@@ -334,10 +343,10 @@ with gr.Blocks() as demo:
|
|
334 |
gr.Examples(
|
335 |
examples=[[
|
336 |
["./examples/0.png", "./examples/1.png", "./examples/2.png", "./examples/3.png", "./examples/4.png"],
|
337 |
-
32, 64, 16, True
|
338 |
]],
|
339 |
fn=loadf,
|
340 |
-
inputs=[files_input, max_s, max_l, max_v, rbg],
|
341 |
outputs=[files_input, gallery_input],
|
342 |
cache_examples=True
|
343 |
)
|
|
|
136 |
return final_vid, files
|
137 |
|
138 |
|
139 |
+
def remove_bg(fl, s, l, v, h):
|
140 |
frame = cv2.imread(fl).astype(np.uint8)
|
141 |
|
142 |
b = 5
|
|
|
157 |
frame_c = (frame.astype(np.int16)-bg_diff).astype(np.uint8)
|
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/512).astype(np.uint8)
|
168 |
+
|
169 |
+
d = hsv[i-2:i+2, j-2:j+2, 0].var()
|
170 |
+
hue_s[i,j] = (d/512).astype(np.uint8)
|
171 |
|
172 |
+
print(np.max(hue_s))
|
173 |
+
print(np.min(hue_s))
|
174 |
#remove regions of low saturation and lightness (get scene without shadow)
|
175 |
+
m = cv2.inRange(hsv, np.array([0,0,0]), np.array([180,s,l]))
|
176 |
+
m_ = cv2.inRange(hue_s, 0, h)
|
177 |
mask = cv2.inRange(blur_s, 0, v)
|
178 |
masks = np.bitwise_and(m, mask)
|
179 |
+
masks = np.bitwise_and(m_, masks)
|
180 |
frame_[masks==0] = (0,0,0)
|
|
|
181 |
|
182 |
|
183 |
m_ = frame_.reshape((-1,3))
|
|
|
267 |
e = e.split('/')
|
268 |
return e[len(e)-1]
|
269 |
|
270 |
+
def loadf(f, s, l, v, h, r_bg):
|
271 |
if f != None and f[0] != None:
|
272 |
f.sort(key=sortFiles)
|
273 |
fnew = []
|
|
|
278 |
fl = sharpest(fl, i)
|
279 |
|
280 |
if r_bg == True:
|
281 |
+
fl = remove_bg(fl, s, l, v, h)
|
282 |
|
283 |
fnew.append(fl)
|
284 |
|
|
|
322 |
with gr.Tab(label="Shadow maximums"):
|
323 |
max_s = gr.Slider(minimum=0, maximum=255, step=1, value=32, label="Saturation")
|
324 |
max_l = gr.Slider(minimum=0, maximum=255, step=1, value=64, label="Lightness")
|
325 |
+
max_v = gr.Slider(minimum=0, maximum=255, step=1, value=16, label="Sharpness")
|
326 |
+
max_h = gr.Slider(minimum=0, maximum=255, step=1, value=127, label="Hue variance")
|
327 |
rbg = gr.Checkbox(label="Remove background", value=True)
|
328 |
+
files_input.upload(fn=loadf, inputs=[files_input, max_s, max_l, max_v, max_h, rbg], outputs=[files_input, gallery_input])
|
329 |
with gr.Row():
|
330 |
interpolation_slider = gr.Slider(minimum=1, maximum=5, step=1, value=1, label="Interpolation Steps: ")
|
331 |
interpolation = gr.Number(value=1, show_label=False, interactive=False)
|
|
|
343 |
gr.Examples(
|
344 |
examples=[[
|
345 |
["./examples/0.png", "./examples/1.png", "./examples/2.png", "./examples/3.png", "./examples/4.png"],
|
346 |
+
32, 64, 16, 127, True
|
347 |
]],
|
348 |
fn=loadf,
|
349 |
+
inputs=[files_input, max_s, max_l, max_v, max_h, rbg],
|
350 |
outputs=[files_input, gallery_input],
|
351 |
cache_examples=True
|
352 |
)
|