freealise commited on
Commit
8ab7931
·
verified ·
1 Parent(s): e235e21

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +12 -8
app.py CHANGED
@@ -87,10 +87,18 @@ def get_frames(video_in, step, name, n):
87
 
88
 
89
  def create_video(frames, fps, type):
 
 
 
 
90
  print("building video result")
91
  imgs = []
92
  for j, img in enumerate(frames):
93
- imgs.append(cv2.cvtColor(cv2.imread(img).astype(np.uint8), cv2.COLOR_BGR2RGB))
 
 
 
 
94
 
95
  mediapy.write_video(type + "_result.mp4", imgs, fps=fps)
96
  return type + "_result.mp4"
@@ -145,7 +153,7 @@ def logscale(linear):
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
 
@@ -200,8 +208,7 @@ def remove_bg(fl, count, mh, ms, md, lm, backSub):
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
- fgMask = backSub.apply(fr, learningRate=1)
204
- fr[fgMask==0] = (255,255,255)
205
 
206
  cv2.imwrite(fl, fr)
207
  return fl
@@ -258,9 +265,6 @@ def loadf(f, r_bg, mh, ms, md, lm):
258
  f.sort(key=sortFiles)
259
  fnew = []
260
 
261
- backSub = cv2.createBackgroundSubtractorMOG2()
262
- #backSub = cv2.createBackgroundSubtractorKNN()
263
-
264
  for i, fl in enumerate(f):
265
  ftype = fl.split('/')
266
  if ftype[len(ftype)-1].split('.')[1] == 'mp4':
@@ -269,7 +273,7 @@ def loadf(f, r_bg, mh, ms, md, lm):
269
  fl = denoise(fl)
270
 
271
  if r_bg == True:
272
- fl = remove_bg(fl, i, mh, ms, md, lm, backSub)
273
  if i % 2: # odd: is photo without the flash
274
  fnew.append(fl)
275
  else:
 
87
 
88
 
89
  def create_video(frames, fps, type):
90
+
91
+ backSub = cv2.createBackgroundSubtractorMOG2()
92
+ #backSub = cv2.createBackgroundSubtractorKNN()
93
+
94
  print("building video result")
95
  imgs = []
96
  for j, img in enumerate(frames):
97
+ frame = cv2.cvtColor(cv2.imread(img).astype(np.uint8), cv2.COLOR_BGR2RGB)
98
+ fgMask = backSub.apply(frame)
99
+ frame[fgMask==255] = (255,255,255)
100
+ frame[fgMask==0] = (0,0,0)
101
+ imgs.append(frame)
102
 
103
  mediapy.write_video(type + "_result.mp4", imgs, fps=fps)
104
  return type + "_result.mp4"
 
153
  def linscale(linear):
154
  return int(math.log2(linear))
155
 
156
+ def remove_bg(fl, count, mh, ms, md, lm):
157
  global fl_
158
  fr = cv2.imread(fl).astype(np.uint8)
159
 
 
208
  fr_fg[fr_fg>0] = 3 #probable fg
209
  mask, bgdModel, fgdModel = cv2.grabCut(fr, fr_fg, None,None,None,31, cv2.GC_INIT_WITH_MASK)
210
  mask = np.where((mask==2)|(mask==0),0,1).astype('uint8')
211
+ #fr[mask==0] = (255,255,255)
 
212
 
213
  cv2.imwrite(fl, fr)
214
  return fl
 
265
  f.sort(key=sortFiles)
266
  fnew = []
267
 
 
 
 
268
  for i, fl in enumerate(f):
269
  ftype = fl.split('/')
270
  if ftype[len(ftype)-1].split('.')[1] == 'mp4':
 
273
  fl = denoise(fl)
274
 
275
  if r_bg == True:
276
+ fl = remove_bg(fl, i, mh, ms, md, lm)
277
  if i % 2: # odd: is photo without the flash
278
  fnew.append(fl)
279
  else: