sofianhw commited on
Commit
d6f15c1
·
verified ·
1 Parent(s): 10d5ee5

update some deprecated from gradio v3

Browse files
Files changed (1) hide show
  1. app.py +72 -73
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
- os.mkdir("data")
 
 
 
 
17
  os.rename("best.ckpt", "models/best.ckpt")
18
- os.mkdir("dataout")
 
19
 
20
  def get_frames(video_in):
21
  frames = []
22
- #resize the video
23
  clip = VideoFileClip(video_in)
24
-
25
- #check fps
26
  if clip.fps > 30:
27
- print("vide rate is over 30, resetting to 30")
28
  clip_resized = clip.resize(height=256)
29
  clip_resized.write_videofile("video_resized.mp4", fps=30)
30
  else:
31
- print("video rate is OK")
32
  clip_resized = clip.resize(height=256)
33
  clip_resized.write_videofile("video_resized.mp4", fps=clip.fps)
34
-
35
- print("video resized to 512 height")
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("video fps: " + str(fps))
42
- i=0
43
- while(cap.isOpened()):
44
  ret, frame = cap.read()
45
- if ret == False:
46
  break
47
- cv2.imwrite('kang'+str(i)+'.jpg',frame)
48
- frames.append('kang'+str(i)+'.jpg')
49
- i+=1
 
50
 
51
  cap.release()
52
  cv2.destroyAllWindows()
53
- print("broke the video into frames")
54
-
55
  return frames, fps
56
 
57
- def create_video(frames, fps, type):
58
- print("building video result")
59
  clip = ImageSequenceClip(frames, fps=fps)
60
- clip.write_videofile(type + "_result.mp4", fps=fps)
61
-
62
- return type + "_result.mp4"
63
 
64
-
65
- def magic_lama(img):
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(f"./data/data.png", img)
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 f"./dataout/data_mask.png"
77
 
78
  def infer(video_in):
79
- # 1. break video into frames and get FPS
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
- print("set stop frames to: " + str(n_frame))
93
-
94
- for i in frames_list[0:int(n_frame)]:
95
- lama_frame = magic_lama(i)
96
- lama_frame = Image.open(lama_frame)
97
- imageio.imwrite(f"cleaned_frame_{i}", lama_frame)
98
- result_frames.append(f"cleaned_frame_{i}")
99
- print("frame " + i + "/" + str(n_frame) + ": done;")
100
 
101
-
102
- final_vid = create_video(result_frames, fps, "cleaned")
103
 
104
- files = [final_vid]
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
105
 
106
- return final_vid
 
 
 
 
 
 
 
 
107
 
108
- inputs = [gr.Video(label="Input", source="upload", type="filepath")]
109
- outputs = [gr.Video(label="output")]
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)