Spaces:
Runtime error
Runtime error
update some deprecated from gradio v3
Browse files
app.py
CHANGED
@@ -1,10 +1,4 @@
|
|
1 |
import os
|
2 |
-
os.system("wget https://huggingface.co/akhaliq/lama/resolve/main/best.ckpt")
|
3 |
-
os.system("pip install imageio")
|
4 |
-
os.system("pip install albumentations==0.5.2")
|
5 |
-
os.system("pip install opencv-python")
|
6 |
-
os.system("pip install ffmpeg-python")
|
7 |
-
os.system("pip install moviepy")
|
8 |
import cv2
|
9 |
import paddlehub as hub
|
10 |
import gradio as gr
|
@@ -13,103 +7,108 @@ from PIL import Image, ImageOps
|
|
13 |
import numpy as np
|
14 |
import imageio
|
15 |
from moviepy.editor import *
|
16 |
-
|
|
|
|
|
|
|
|
|
17 |
os.rename("best.ckpt", "models/best.ckpt")
|
18 |
-
os.
|
|
|
19 |
|
20 |
def get_frames(video_in):
|
21 |
frames = []
|
22 |
-
#resize the video
|
23 |
clip = VideoFileClip(video_in)
|
24 |
-
|
25 |
-
#
|
26 |
if clip.fps > 30:
|
27 |
-
print("
|
28 |
clip_resized = clip.resize(height=256)
|
29 |
clip_resized.write_videofile("video_resized.mp4", fps=30)
|
30 |
else:
|
31 |
-
print("
|
32 |
clip_resized = clip.resize(height=256)
|
33 |
clip_resized.write_videofile("video_resized.mp4", fps=clip.fps)
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
# Opens the Video file with CV2
|
38 |
-
cap= cv2.VideoCapture("video_resized.mp4")
|
39 |
-
|
40 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
41 |
-
print("
|
42 |
-
i=0
|
43 |
-
while
|
44 |
ret, frame = cap.read()
|
45 |
-
if ret
|
46 |
break
|
47 |
-
|
48 |
-
|
49 |
-
|
|
|
50 |
|
51 |
cap.release()
|
52 |
cv2.destroyAllWindows()
|
53 |
-
print("
|
54 |
-
|
55 |
return frames, fps
|
56 |
|
57 |
-
def create_video(frames, fps,
|
58 |
-
print("
|
59 |
clip = ImageSequenceClip(frames, fps=fps)
|
60 |
-
|
61 |
-
|
62 |
-
return
|
63 |
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
i = img
|
68 |
-
img = Image.open(img)
|
69 |
mask = Image.open("./masks/modelscope-mask.png")
|
70 |
inverted_mask = ImageOps.invert(mask)
|
71 |
-
|
72 |
-
|
73 |
-
imageio.imwrite(
|
74 |
-
imageio.imwrite(f"./data/data_mask.png", inverted_mask)
|
75 |
os.system('python predict.py model.path=/home/user/app/ indir=/home/user/app/data/ outdir=/home/user/app/dataout/ device=cpu')
|
76 |
-
return
|
77 |
|
78 |
def infer(video_in):
|
79 |
-
|
80 |
-
break_vid = get_frames(video_in)
|
81 |
-
frames_list= break_vid[0]
|
82 |
-
fps = break_vid[1]
|
83 |
-
#n_frame = int(trim_value*fps)
|
84 |
n_frame = len(frames_list)
|
85 |
|
86 |
-
if n_frame >= len(frames_list):
|
87 |
-
print("video is shorter than the cut value")
|
88 |
-
n_frame = len(frames_list)
|
89 |
-
|
90 |
-
# 2. prepare frames result arrays
|
91 |
result_frames = []
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
lama_frame
|
96 |
-
|
97 |
-
|
98 |
-
result_frames.append(f"cleaned_frame_{i}")
|
99 |
-
print("frame " + i + "/" + str(n_frame) + ": done;")
|
100 |
|
101 |
-
|
102 |
-
|
103 |
|
104 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
105 |
|
106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
107 |
|
108 |
-
|
109 |
-
|
110 |
-
title = "LaMa Video Watermark Remover"
|
111 |
-
description = "<p style='text-align: center'>LaMa: Resolution-robust Large Mask Inpainting with Fourier Convolutions. <br />This demo in meant to be used as a watermark remover on Modelscope generated videos. <br />Simply upload your modelscope video and hit Submit</p>"
|
112 |
-
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2109.07161' target='_blank'>Resolution-robust Large Mask Inpainting with Fourier Convolutions</a> | <a href='https://github.com/saic-mdal/lama' target='_blank'>Github Repo</a></p>"
|
113 |
-
examples = ["./examples/modelscope-astronaut-horse.mp4", "./examples/modelscope-panda.mp4", "./examples/modelscope-spiderman-surfing.mp4"]
|
114 |
-
gr.Interface(infer, inputs, outputs, title=title,
|
115 |
-
description=description, article=article, examples=examples).launch()
|
|
|
1 |
import os
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
import cv2
|
3 |
import paddlehub as hub
|
4 |
import gradio as gr
|
|
|
7 |
import numpy as np
|
8 |
import imageio
|
9 |
from moviepy.editor import *
|
10 |
+
|
11 |
+
# Download and set up necessary files and folders
|
12 |
+
os.system("wget https://huggingface.co/akhaliq/lama/resolve/main/best.ckpt")
|
13 |
+
os.system("pip install imageio albumentations==0.5.2 opencv-python ffmpeg-python moviepy")
|
14 |
+
os.makedirs("models", exist_ok=True)
|
15 |
os.rename("best.ckpt", "models/best.ckpt")
|
16 |
+
os.makedirs("data", exist_ok=True)
|
17 |
+
os.makedirs("dataout", exist_ok=True)
|
18 |
|
19 |
def get_frames(video_in):
|
20 |
frames = []
|
|
|
21 |
clip = VideoFileClip(video_in)
|
22 |
+
|
23 |
+
# Resize and adjust FPS
|
24 |
if clip.fps > 30:
|
25 |
+
print("Video rate is over 30, resetting to 30")
|
26 |
clip_resized = clip.resize(height=256)
|
27 |
clip_resized.write_videofile("video_resized.mp4", fps=30)
|
28 |
else:
|
29 |
+
print("Video rate is OK")
|
30 |
clip_resized = clip.resize(height=256)
|
31 |
clip_resized.write_videofile("video_resized.mp4", fps=clip.fps)
|
32 |
+
|
33 |
+
# Extract frames
|
34 |
+
cap = cv2.VideoCapture("video_resized.mp4")
|
|
|
|
|
|
|
35 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
36 |
+
print("Video fps:", fps)
|
37 |
+
i = 0
|
38 |
+
while cap.isOpened():
|
39 |
ret, frame = cap.read()
|
40 |
+
if not ret:
|
41 |
break
|
42 |
+
frame_path = f'kang_{i}.jpg'
|
43 |
+
cv2.imwrite(frame_path, frame)
|
44 |
+
frames.append(frame_path)
|
45 |
+
i += 1
|
46 |
|
47 |
cap.release()
|
48 |
cv2.destroyAllWindows()
|
49 |
+
print("Video broken into frames")
|
|
|
50 |
return frames, fps
|
51 |
|
52 |
+
def create_video(frames, fps, type_name):
|
53 |
+
print("Building video result")
|
54 |
clip = ImageSequenceClip(frames, fps=fps)
|
55 |
+
output_file = f"{type_name}_result.mp4"
|
56 |
+
clip.write_videofile(output_file, fps=fps)
|
57 |
+
return output_file
|
58 |
|
59 |
+
def magic_lama(img_path):
|
60 |
+
img = Image.open(img_path)
|
|
|
|
|
|
|
61 |
mask = Image.open("./masks/modelscope-mask.png")
|
62 |
inverted_mask = ImageOps.invert(mask)
|
63 |
+
|
64 |
+
imageio.imwrite("./data/data.png", img)
|
65 |
+
imageio.imwrite("./data/data_mask.png", inverted_mask)
|
|
|
66 |
os.system('python predict.py model.path=/home/user/app/ indir=/home/user/app/data/ outdir=/home/user/app/dataout/ device=cpu')
|
67 |
+
return "./dataout/data_mask.png"
|
68 |
|
69 |
def infer(video_in):
|
70 |
+
frames_list, fps = get_frames(video_in)
|
|
|
|
|
|
|
|
|
71 |
n_frame = len(frames_list)
|
72 |
|
|
|
|
|
|
|
|
|
|
|
73 |
result_frames = []
|
74 |
+
for i, frame_path in enumerate(frames_list[:n_frame]):
|
75 |
+
lama_frame = magic_lama(frame_path)
|
76 |
+
cleaned_frame_path = f"cleaned_frame_{i}.jpg"
|
77 |
+
Image.open(lama_frame).save(cleaned_frame_path)
|
78 |
+
result_frames.append(cleaned_frame_path)
|
79 |
+
print(f"Processed frame {i + 1}/{n_frame}")
|
|
|
|
|
80 |
|
81 |
+
final_video = create_video(result_frames, fps, "cleaned")
|
82 |
+
return final_video
|
83 |
|
84 |
+
# Gradio Interface
|
85 |
+
inputs = gr.Video(label="Input", source="upload", type="filepath")
|
86 |
+
outputs = gr.Video(label="Output")
|
87 |
+
title = "LaMa Video Watermark Remover"
|
88 |
+
description = (
|
89 |
+
"<p style='text-align: center'>LaMa: Resolution-robust Large Mask Inpainting with Fourier Convolutions.<br />"
|
90 |
+
"This demo is meant to be used as a watermark remover on Modelscope generated videos.<br />"
|
91 |
+
"Simply upload your Modelscope video and hit Submit.</p>"
|
92 |
+
)
|
93 |
+
article = (
|
94 |
+
"<p style='text-align: center'><a href='https://arxiv.org/abs/2109.07161' target='_blank'>Resolution-robust Large Mask Inpainting with Fourier Convolutions</a> | "
|
95 |
+
"<a href='https://github.com/saic-mdal/lama' target='_blank'>Github Repo</a></p>"
|
96 |
+
)
|
97 |
+
examples = [
|
98 |
+
"./examples/modelscope-astronaut-horse.mp4",
|
99 |
+
"./examples/modelscope-panda.mp4",
|
100 |
+
"./examples/modelscope-spiderman-surfing.mp4"
|
101 |
+
]
|
102 |
|
103 |
+
demo = gr.Interface(
|
104 |
+
fn=infer,
|
105 |
+
inputs=inputs,
|
106 |
+
outputs=outputs,
|
107 |
+
title=title,
|
108 |
+
description=description,
|
109 |
+
article=article,
|
110 |
+
examples=examples
|
111 |
+
)
|
112 |
|
113 |
+
# Launch with prevent_thread_lock in case it's needed for async compatibility in v4
|
114 |
+
demo.launch(prevent_thread_lock=True)
|
|
|
|
|
|
|
|
|
|
|
|