freealise commited on
Commit
15db140
·
verified ·
1 Parent(s): 7288a9d

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -4
app.py CHANGED
@@ -23,6 +23,9 @@ interpolator = interpolator.Interpolator(model, None)
23
  ffmpeg_path = util.get_ffmpeg_path()
24
  mediapy.set_ffmpeg(ffmpeg_path)
25
 
 
 
 
26
 
27
  def do_interpolation(frame1, frame2, interpolation, n):
28
  print("tween frames: " + str(interpolation))
@@ -142,6 +145,43 @@ def logscale(linear):
142
  def linscale(linear):
143
  return int(math.log2(linear))
144
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
145
  def sharpest(fl, i):
146
  break_vid = get_frames(fl, "vid_input_frame", "origin", i)
147
 
@@ -179,7 +219,7 @@ def sortFiles(e):
179
  e = e.split('/')
180
  return e[len(e)-1]
181
 
182
- def loadf(f):
183
  if f != None and f[0] != None:
184
  f.sort(key=sortFiles)
185
  fnew = []
@@ -188,6 +228,9 @@ def loadf(f):
188
  ftype = fl.split('/')
189
  if ftype[len(ftype)-1].split('.')[1] == 'mp4':
190
  fl = sharpest(fl, i)
 
 
 
191
 
192
  fnew.append(fl)
193
  return fnew, fnew
@@ -227,7 +270,8 @@ with gr.Blocks() as demo:
227
  files_orig = gr.File(file_count="multiple", file_types=['image', '.mp4'])
228
  files_input = gr.File(file_count="multiple", visible=False)
229
  gallery_input = gr.Gallery(label="Slideshow", preview=True, columns=8192, interactive=False)
230
- files_orig.upload(fn=loadf, inputs=[files_orig], outputs=[files_input, gallery_input])
 
231
 
232
  with gr.Row():
233
  interpolation_slider = gr.Slider(minimum=1, maximum=5, step=1, value=1, label="Interpolation Steps: ")
@@ -245,10 +289,10 @@ with gr.Blocks() as demo:
245
 
246
  gr.Examples(
247
  examples=[[
248
- ["./examples/0.png", "./examples/1.png", "./examples/2.png", "./examples/3.png", "./examples/4.png"]
249
  ]],
250
  fn=loadf,
251
- inputs=[files_orig],
252
  outputs=[files_input, gallery_input],
253
  cache_examples=True
254
  )
 
23
  ffmpeg_path = util.get_ffmpeg_path()
24
  mediapy.set_ffmpeg(ffmpeg_path)
25
 
26
+ fl_ = ""
27
+ fl_mask = ""
28
+
29
 
30
  def do_interpolation(frame1, frame2, interpolation, n):
31
  print("tween frames: " + str(interpolation))
 
145
  def linscale(linear):
146
  return int(math.log2(linear))
147
 
148
+ def remove_bg(fl, i):
149
+ global fl_
150
+ fr = cv2.imread(fl)
151
+
152
+ b = 1
153
+ element = cv2.getStructuringElement(cv2.MORPH_RECT, (2 * b + 1, 2 * b + 1), (b, b))
154
+
155
+ n = int((fr.shape[0]*fr.shape[1]) / (256*256))
156
+ fr_bg = cv2.medianBlur(fr, 255)
157
+
158
+ for i in range(0, n):
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
+ fr_diff[fr_diff>=md] = 255
166
+ fr_diff[fr_diff<md] = 0
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)
170
+ # 255 << 8 tells to fill with the value 255)
171
+ mask = mask[1:mask.shape[0]-1, 1:mask.shape[1]-1]
172
+ fr_diff[mask>0] = 0
173
+
174
+ fr_diff = cv2.dilate(cv2.erode(fr_diff, element), element)
175
+
176
+ if int(i/2) == i/2: # is photo with the flash
177
+ fl_ = fl.split(".")[0] + "_.png"
178
+ cv2.imwrite(fl_, fr_diff)
179
+ else: # is without
180
+ fr = cv2.imread(fl_)
181
+ fr = fr_diff - fr
182
+ cv2.imwrite(fl_, fr)
183
+ return fl_
184
+
185
  def sharpest(fl, i):
186
  break_vid = get_frames(fl, "vid_input_frame", "origin", i)
187
 
 
219
  e = e.split('/')
220
  return e[len(e)-1]
221
 
222
+ def loadf(f, r_bg):
223
  if f != None and f[0] != None:
224
  f.sort(key=sortFiles)
225
  fnew = []
 
228
  ftype = fl.split('/')
229
  if ftype[len(ftype)-1].split('.')[1] == 'mp4':
230
  fl = sharpest(fl, i)
231
+
232
+ if r_bg == True:
233
+ fl = remove_bg(fl, i)
234
 
235
  fnew.append(fl)
236
  return fnew, fnew
 
270
  files_orig = gr.File(file_count="multiple", file_types=['image', '.mp4'])
271
  files_input = gr.File(file_count="multiple", visible=False)
272
  gallery_input = gr.Gallery(label="Slideshow", preview=True, columns=8192, interactive=False)
273
+ r_bg = gr.Checkbox(label="Remove background", value=True)
274
+ files_orig.upload(fn=loadf, inputs=[files_orig, r_bg], outputs=[files_input, gallery_input])
275
 
276
  with gr.Row():
277
  interpolation_slider = gr.Slider(minimum=1, maximum=5, step=1, value=1, label="Interpolation Steps: ")
 
289
 
290
  gr.Examples(
291
  examples=[[
292
+ ["./examples/0.png", "./examples/1.png", "./examples/2.png", "./examples/3.png", "./examples/4.png"], True
293
  ]],
294
  fn=loadf,
295
+ inputs=[files_orig, r_bg],
296
  outputs=[files_input, gallery_input],
297
  cache_examples=True
298
  )