Update app.py
Browse files
app.py
CHANGED
@@ -140,21 +140,24 @@ def remove_bg(fl):
|
|
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 |
bg_gray = cv2.cvtColor(cv2.cvtColor(bg, cv2.COLOR_BGR2GRAY), cv2.COLOR_GRAY2BGR)
|
146 |
bg_diff = (bg-bg_gray).astype(np.int16)
|
147 |
-
frame_ = ((bg.astype(np.int16)-frame.astype(np.int16))+127).astype(np.uint8)
|
148 |
frame = (frame.astype(np.int16)-bg_diff).astype(np.uint8)
|
149 |
-
frame_ = cv2.bilateralFilter(frame_, b*4, b*8, b*2)
|
150 |
|
151 |
#remove regions of low saturation (get scene without shadow)
|
152 |
-
|
153 |
-
m = cv2.inRange(mask, np.array([0,0,0]), np.array([180,32,256]))
|
154 |
frame[m>0] = (127,127,127)
|
155 |
-
frame_ = cv2.medianBlur(frame_, b)
|
156 |
-
element = cv2.getStructuringElement(cv2.MORPH_RECT, (2*b+1, 2*b+1), (b,b))
|
157 |
-
frame_ = cv2.erode(cv2.dilate(frame_, element), element)
|
158 |
frame = cv2.medianBlur(frame, b)
|
159 |
|
160 |
|
@@ -164,7 +167,7 @@ def remove_bg(fl):
|
|
164 |
# define criteria, number of clusters(K) and apply kmeans()
|
165 |
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 16, 1.0)
|
166 |
K = 3
|
167 |
-
ret,label,center=cv2.kmeans(m_,K,None,criteria,16,cv2.KMEANS_PP_CENTERS)
|
168 |
# Now convert back into uint8, and make original image
|
169 |
center = np.uint8(center)
|
170 |
res = center[label.flatten()]
|
@@ -172,15 +175,18 @@ def remove_bg(fl):
|
|
172 |
|
173 |
|
174 |
#remove shadows at edges
|
175 |
-
cv2.
|
|
|
|
|
176 |
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)
|
177 |
# 255 << 8 tells to fill with the value 255)
|
178 |
mask = mask[1:mask.shape[0]-1, 1:mask.shape[1]-1]
|
179 |
-
frame_[mask>0] = (
|
180 |
-
frame_[
|
|
|
|
|
181 |
|
182 |
cv2.imwrite(fl, frame) #frame_
|
183 |
-
|
184 |
return fl
|
185 |
|
186 |
def logscale(linear):
|
@@ -292,7 +298,7 @@ with gr.Blocks() as demo:
|
|
292 |
|
293 |
gr.Examples(
|
294 |
examples=[[["./examples/0.png", "./examples/1.png", "./examples/2.png", "./examples/3.png", "./examples/4.png"], 1, 0]],
|
295 |
-
fn=
|
296 |
inputs=[files_input, interpolation_slider, fps_output_slider],
|
297 |
outputs=[video_output, file_output],
|
298 |
cache_examples=True
|
|
|
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 |
+
#correct hue against light
|
154 |
bg_gray = cv2.cvtColor(cv2.cvtColor(bg, cv2.COLOR_BGR2GRAY), cv2.COLOR_GRAY2BGR)
|
155 |
bg_diff = (bg-bg_gray).astype(np.int16)
|
|
|
156 |
frame = (frame.astype(np.int16)-bg_diff).astype(np.uint8)
|
|
|
157 |
|
158 |
#remove regions of low saturation (get scene without shadow)
|
159 |
+
m = cv2.inRange(cv2.cvtColor(frame, cv2.COLOR_RGB2HSV), np.array([0,0,0]), np.array([180,32,256]))
|
|
|
160 |
frame[m>0] = (127,127,127)
|
|
|
|
|
|
|
161 |
frame = cv2.medianBlur(frame, b)
|
162 |
|
163 |
|
|
|
167 |
# define criteria, number of clusters(K) and apply kmeans()
|
168 |
criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 16, 1.0)
|
169 |
K = 3
|
170 |
+
ret,label,center=cv2.kmeans(m_, K, None, criteria, 16, cv2.KMEANS_PP_CENTERS)
|
171 |
# Now convert back into uint8, and make original image
|
172 |
center = np.uint8(center)
|
173 |
res = center[label.flatten()]
|
|
|
175 |
|
176 |
|
177 |
#remove shadows at edges
|
178 |
+
m_ = cv2.inRange(frame_, np.array([128,128,128]), np.array([255,255,255]))
|
179 |
+
frame_[m_>0] = (255,255,255)
|
180 |
+
cv2.rectangle(frame_,(0,0),(frame_.shape[1]-1,frame_.shape[0]-1),(255,255,255),7)
|
181 |
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)
|
182 |
# 255 << 8 tells to fill with the value 255)
|
183 |
mask = mask[1:mask.shape[0]-1, 1:mask.shape[1]-1]
|
184 |
+
frame_[mask>0] = (127,127,127)
|
185 |
+
m_ = cv2.inRange(frame_, np.array([0,0,0]), np.array([127,127,127]))
|
186 |
+
frame_[m_>0] = (127,127,127)
|
187 |
+
|
188 |
|
189 |
cv2.imwrite(fl, frame) #frame_
|
|
|
190 |
return fl
|
191 |
|
192 |
def logscale(linear):
|
|
|
298 |
|
299 |
gr.Examples(
|
300 |
examples=[[["./examples/0.png", "./examples/1.png", "./examples/2.png", "./examples/3.png", "./examples/4.png"], 1, 0]],
|
301 |
+
fn=loadf,
|
302 |
inputs=[files_input, interpolation_slider, fps_output_slider],
|
303 |
outputs=[video_output, file_output],
|
304 |
cache_examples=True
|