kymlcode commited on
Commit
bf0fcad
·
verified ·
1 Parent(s): bc8a1e8

Upload 4 files

Browse files
Files changed (4) hide show
  1. README.md +7 -7
  2. app.py +63 -0
  3. input.mp4 +0 -0
  4. requirements.txt +8 -0
README.md CHANGED
@@ -1,12 +1,12 @@
1
  ---
2
- title: Greenback
3
- emoji: 🐢
4
- colorFrom: green
5
- colorTo: green
6
  sdk: gradio
7
- sdk_version: 4.28.3
8
  app_file: app.py
9
  pinned: false
10
- ---
11
 
12
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: Remove Video Background
3
+ emoji: 🎞️
4
+ colorFrom: purple
5
+ colorTo: gray
6
  sdk: gradio
7
+ sdk_version: 4.24.0
8
  app_file: app.py
9
  pinned: false
10
+ license: apache-2.0
11
 
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
app.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import spaces
2
+ import gradio as gr
3
+ import cv2
4
+ import numpy as np
5
+ import time
6
+ import random
7
+
8
+ from PIL import Image
9
+ from transparent_background import Remover
10
+
11
+ @spaces.GPU # 修改装饰器,去掉括号
12
+ def doo(video, mode):
13
+ if(mode == 'Fast'):
14
+ remover = Remover(mode='fast')
15
+ else:
16
+ remover = Remover()
17
+ cap = cv2.VideoCapture(video)
18
+ fps = cap.get(cv2.CAP_PROP_FPS)
19
+
20
+ writer = None
21
+ tmpname = random.randint(111111111, 999999999)
22
+ processed_frames = 0
23
+ start_time = time.time()
24
+
25
+ while cap.isOpened():
26
+ ret, frame = cap.read()
27
+
28
+ if ret is False:
29
+ break
30
+
31
+ if time.time() - start_time >= 55:
32
+ print("GPU Timeout is coming")
33
+ cap.release()
34
+ writer.release()
35
+ return str(tmpname) + '.mp4'
36
+ frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
37
+ img = Image.fromarray(frame).convert('RGB')
38
+
39
+ if writer is None:
40
+ writer = cv2.VideoWriter(str(tmpname) + '.mp4', cv2.VideoWriter_fourcc(*'mp4v'), fps, img.size)
41
+
42
+ processed_frames += 1
43
+ print(f"Processing: {processed_frames}")
44
+ out = remover.process(img, type='green')
45
+ writer.write(cv2.cvtColor(np.array(out), cv2.COLOR_BGR2RGB))
46
+
47
+ cap.release()
48
+ writer.release()
49
+ return str(tmpname) + '.mp4'
50
+
51
+ title = "🎞️Video Background Removal tool🎥"
52
+
53
+ description = r"""Please note that if your video file is long (or having high amount of frames) the result may be shorter than input video because of the GPU timeout.<br>
54
+ In this case consider trying Fast mode."""
55
+
56
+ examples = [['./input.mp4'],]
57
+
58
+ iface = gr.Interface(
59
+ fn=doo,
60
+ inputs=["video", gr.components.Radio(['Normal', 'Fast'], label='Select mode', value='Normal', info= 'Normal is more accurate, but takes longer. | Fast has lower accuracy so the process will be faster.')],
61
+ outputs="video", examples=examples, title=title, description=description
62
+ )
63
+ iface.launch()
input.mp4 ADDED
Binary file (267 kB). View file
 
requirements.txt ADDED
@@ -0,0 +1,8 @@
 
 
 
 
 
 
 
 
 
1
+ torch==2.3.0
2
+ torchvision
3
+ opencv-python
4
+ timm==0.9.1
5
+ tqdm
6
+ kornia
7
+ gdown
8
+ transparent-background