SkalskiP commited on
Commit
0ccb481
β€’
1 Parent(s): a59146c

Updated version with ProPainter integration

Browse files
Files changed (4) hide show
  1. Dockerfile +60 -0
  2. README.md +2 -4
  3. app.py +35 -14
  4. requirements.txt +0 -10
Dockerfile ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM pytorch/pytorch:2.0.1-cuda11.7-cudnn8-runtime
2
+
3
+ ENV DEBIAN_FRONTEND=noninteractive
4
+
5
+ #RUN apt-get update && apt-get install -y \
6
+ # git wget libgl1-mesa-glx libglib2.0-0 ffmpeg libx264-dev \
7
+ # && rm -rf /var/lib/apt/lists/*
8
+
9
+ RUN apt-get update && apt-get install -y \
10
+ git make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev \
11
+ libsqlite3-dev wget curl llvm libncursesw5-dev xz-utils tk-dev libxml2-dev \
12
+ libxmlsec1-dev libffi-dev liblzma-dev git-lfs ffmpeg libsm6 libxext6 cmake \
13
+ libgl1-mesa-glx \
14
+ && rm -rf /var/lib/apt/lists/* && git lfs install
15
+
16
+ RUN useradd -m -u 1000 user
17
+
18
+ USER user
19
+
20
+ ENV HOME=/home/user \
21
+ PATH=/home/user/.local/bin:$PATH \
22
+ PYTHONPATH=$HOME/app \
23
+ PYTHONUNBUFFERED=1 \
24
+ GRADIO_ALLOW_FLAGGING=never \
25
+ GRADIO_NUM_PORTS=1 \
26
+ GRADIO_SERVER_NAME=0.0.0.0 \
27
+ GRADIO_THEME=huggingface \
28
+ GRADIO_SHARE=False \
29
+ SYSTEM=spaces
30
+
31
+ # Set the working directory to the user's home directory
32
+ WORKDIR $HOME/app
33
+
34
+ # Clone your repository or add your code to the container
35
+ RUN git clone https://github.com/sczhou/ProPainter.git $HOME/app
36
+
37
+ # Install specific versions of PyTorch and TorchVision
38
+ RUN pip install torch==2.0.1+cu117 torchvision==0.15.2+cu117 -f https://download.pytorch.org/whl/torch_stable.html
39
+
40
+ # Install dependencies
41
+ RUN pip install --no-cache-dir -r requirements.txt \
42
+ gradio==3.50.2 opencv-python transformers supervision
43
+
44
+ # Download weights
45
+ RUN mkdir -p $HOME/app/weigths
46
+ RUN wget -c -O $HOME/app/weigths/i3d_rgb_imagenet.pt https://huggingface.co/camenduru/ProPainter/resolve/main/i3d_rgb_imagenet.pt
47
+ RUN wget -c -O $HOME/app/weights/raft-things.pth https://huggingface.co/camenduru/ProPainter/resolve/main/raft-things.pth
48
+ RUN wget -c -O $HOME/app/weights/recurrent_flow_completion.pth https://huggingface.co/camenduru/ProPainter/resolve/main/recurrent_flow_completion.pth
49
+ RUN wget -c -O $HOME/app/weights/ProPainter.pth https://huggingface.co/camenduru/ProPainter/resolve/main/ProPainter.pth
50
+
51
+ COPY app.py .
52
+
53
+ RUN find $HOME/app
54
+
55
+ # Set the environment variable to specify the GPU device
56
+ ENV CUDA_DEVICE_ORDER=PCI_BUS_ID
57
+ ENV CUDA_VISIBLE_DEVICES=0
58
+
59
+ # Run your app.py script
60
+ CMD ["python", "app.py"]
README.md CHANGED
@@ -1,11 +1,9 @@
1
  ---
2
  title: SAM And ProPainter
3
- emoji: 🎨
4
  colorFrom: pink
5
  colorTo: purple
6
- sdk: gradio
7
- sdk_version: 3.50.2
8
- app_file: app.py
9
  pinned: false
10
  ---
11
 
 
1
  ---
2
  title: SAM And ProPainter
3
+ emoji: πŸ‘¨β€πŸŽ¨
4
  colorFrom: pink
5
  colorTo: purple
6
+ sdk: docker
 
 
7
  pinned: false
8
  ---
9
 
app.py CHANGED
@@ -1,13 +1,13 @@
1
- import uuid
2
- from typing import Tuple, List
3
-
4
  import gradio as gr
5
  import numpy as np
 
6
  import supervision as sv
7
  import torch
 
8
  from PIL import Image
9
  from tqdm import tqdm
10
  from transformers import pipeline, CLIPModel, CLIPProcessor
 
11
 
12
  MARKDOWN = """
13
  # Auto ⚑ ProPainter πŸ§‘β€πŸŽ¨
@@ -17,7 +17,7 @@ This is a demo for automatic removal of objects from videos using
17
  [ProPainter](https://github.com/sczhou/ProPainter) combo.
18
 
19
  - [x] Automated object masking using SAM + MetaCLIP
20
- - [ ] Automated inpainting using ProPainter
21
  - [ ] Automated ⚑ object masking using FastSAM + MetaCLIP
22
  """
23
 
@@ -88,19 +88,35 @@ def mask_frame(frame: np.ndarray, prompt: str, confidence: float) -> np.ndarray:
88
  return np.repeat(mask[:, :, np.newaxis], 3, axis=2)
89
 
90
 
91
- def mask_video(source_video: str, prompt: str, confidence: float, name: str) -> str:
92
- video_info = sv.VideoInfo.from_video_path(source_video)
93
  frame_iterator = iter(sv.get_video_frames_generator(
94
  source_path=source_video, start=START_FRAME, end=END_FRAME))
95
 
96
- with sv.ImageSink(name, image_name_pattern="{:05d}.png") as image_sink:
97
- with sv.VideoSink(f"{name}.mp4", video_info=video_info) as video_sink:
98
  for _ in tqdm(range(TOTAL), desc="Masking frames"):
99
  frame = next(frame_iterator)
100
- annotated_frame = mask_frame(frame, prompt, confidence)
101
- video_sink.write_frame(annotated_frame)
102
- image_sink.save_image(annotated_frame)
103
- return f"{name}.mp4"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
104
 
105
 
106
  def process(
@@ -110,8 +126,13 @@ def process(
110
  progress=gr.Progress(track_tqdm=True)
111
  ) -> Tuple[str, str]:
112
  name = str(uuid.uuid4())
113
- masked_video = mask_video(source_video, prompt, confidence, name)
114
- return masked_video, masked_video
 
 
 
 
 
115
 
116
 
117
  with gr.Blocks() as demo:
 
 
 
 
1
  import gradio as gr
2
  import numpy as np
3
+ import subprocess
4
  import supervision as sv
5
  import torch
6
+ import uuid
7
  from PIL import Image
8
  from tqdm import tqdm
9
  from transformers import pipeline, CLIPModel, CLIPProcessor
10
+ from typing import Tuple, List
11
 
12
  MARKDOWN = """
13
  # Auto ⚑ ProPainter πŸ§‘β€πŸŽ¨
 
17
  [ProPainter](https://github.com/sczhou/ProPainter) combo.
18
 
19
  - [x] Automated object masking using SAM + MetaCLIP
20
+ - [x] Automated inpainting using ProPainter
21
  - [ ] Automated ⚑ object masking using FastSAM + MetaCLIP
22
  """
23
 
 
88
  return np.repeat(mask[:, :, np.newaxis], 3, axis=2)
89
 
90
 
91
+ def mask_video(source_video: str, prompt: str, confidence: float, frames_dir: str, masked_frames_dir: str) -> None:
 
92
  frame_iterator = iter(sv.get_video_frames_generator(
93
  source_path=source_video, start=START_FRAME, end=END_FRAME))
94
 
95
+ with sv.ImageSink(masked_frames_dir, image_name_pattern="{:05d}.png") as masked_frames_sink:
96
+ with sv.ImageSink(frames_dir, image_name_pattern="{:05d}.jpg") as frames_sink:
97
  for _ in tqdm(range(TOTAL), desc="Masking frames"):
98
  frame = next(frame_iterator)
99
+ frames_sink.save_image(frame)
100
+ masked_frame = mask_frame(frame, prompt, confidence)
101
+ masked_frames_sink.save_image(masked_frame)
102
+
103
+ return frames_dir, masked_frames_dir
104
+
105
+
106
+ def execute_command(command: str) -> None:
107
+ subprocess.run(command, check=True)
108
+
109
+
110
+ def paint_video(frames_dir: str, masked_frames_dir: str, results_dir: str) -> None:
111
+ command = [
112
+ f"python",
113
+ f"inference_propainter.py",
114
+ f"--video={frames_dir}",
115
+ f"--mask={masked_frames_dir}",
116
+ f"--output={results_dir}",
117
+ f"--save_fps={25}"
118
+ ]
119
+ execute_command(command)
120
 
121
 
122
  def process(
 
126
  progress=gr.Progress(track_tqdm=True)
127
  ) -> Tuple[str, str]:
128
  name = str(uuid.uuid4())
129
+ frames_dir = f"{name}/frames"
130
+ masked_frames_dir = f"{name}/masked_frames"
131
+ results_dir = f"{name}/results"
132
+
133
+ mask_video(source_video, prompt, confidence, frames_dir, masked_frames_dir)
134
+ paint_video(frames_dir, masked_frames_dir, results_dir)
135
+ return f"{name}/results/frames/masked_in.mp4", f"{name}/results/frames/inpaint_out.mp4"
136
 
137
 
138
  with gr.Blocks() as demo:
requirements.txt DELETED
@@ -1,10 +0,0 @@
1
- --extra-index-url https://download.pytorch.org/whl/cu118
2
- torch
3
- torchvision
4
-
5
- numpy
6
- opencv-python
7
- pillow
8
- gradio==3.50.2
9
- transformers
10
- supervision