freealise commited on
Commit
70f23b0
·
verified ·
1 Parent(s): 2bf43a3

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -3
app.py CHANGED
@@ -145,9 +145,11 @@ def logscale(linear):
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
 
152
  #b = 3
153
  #element = cv2.getStructuringElement(cv2.MORPH_RECT, (2 * b + 1, 2 * b + 1), (b, b))
@@ -200,7 +202,7 @@ def remove_bg(fl, count, mh, ms, md, lm):
200
  fr_fg[fr_fg>0] = 3 #probable fg
201
  mask, bgdModel, fgdModel = cv2.grabCut(fr, fr_fg, None,None,None,31, cv2.GC_INIT_WITH_MASK)
202
  mask = np.where((mask==2)|(mask==0),0,1).astype('uint8')
203
- fr[mask==0] = (255,255,255)
204
 
205
  cv2.imwrite(fl, fr)
206
  return fl
@@ -257,6 +259,9 @@ def loadf(f, r_bg, mh, ms, md, lm):
257
  f.sort(key=sortFiles)
258
  fnew = []
259
 
 
 
 
260
  for i, fl in enumerate(f):
261
  ftype = fl.split('/')
262
  if ftype[len(ftype)-1].split('.')[1] == 'mp4':
@@ -265,7 +270,7 @@ def loadf(f, r_bg, mh, ms, md, lm):
265
  fl = denoise(fl)
266
 
267
  if r_bg == True:
268
- fl = remove_bg(fl, i, mh, ms, md, lm)
269
  if i % 2: # odd: is photo without the flash
270
  fnew.append(fl)
271
  else:
 
145
  def linscale(linear):
146
  return int(math.log2(linear))
147
 
148
+ def remove_bg(fl, count, mh, ms, md, lm, backSub):
149
  global fl_
150
  fr = cv2.imread(fl).astype(np.uint8)
151
+
152
+ fgMask = backSub.apply(fr)
153
 
154
  #b = 3
155
  #element = cv2.getStructuringElement(cv2.MORPH_RECT, (2 * b + 1, 2 * b + 1), (b, b))
 
202
  fr_fg[fr_fg>0] = 3 #probable fg
203
  mask, bgdModel, fgdModel = cv2.grabCut(fr, fr_fg, None,None,None,31, cv2.GC_INIT_WITH_MASK)
204
  mask = np.where((mask==2)|(mask==0),0,1).astype('uint8')
205
+ fr[fgMask==0] = (255,255,255)
206
 
207
  cv2.imwrite(fl, fr)
208
  return fl
 
259
  f.sort(key=sortFiles)
260
  fnew = []
261
 
262
+ backSub = cv2.createBackgroundSubtractorMOG2()
263
+ #backSub = cv2.createBackgroundSubtractorKNN()
264
+
265
  for i, fl in enumerate(f):
266
  ftype = fl.split('/')
267
  if ftype[len(ftype)-1].split('.')[1] == 'mp4':
 
270
  fl = denoise(fl)
271
 
272
  if r_bg == True:
273
+ fl = remove_bg(fl, i, mh, ms, md, lm, backSub)
274
  if i % 2: # odd: is photo without the flash
275
  fnew.append(fl)
276
  else: