fffiloni commited on
Commit
5149f3a
1 Parent(s): 6596e7b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -6
app.py CHANGED
@@ -2,6 +2,9 @@ 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
  import cv2
6
  import paddlehub as hub
7
  import gradio as gr
@@ -9,28 +12,100 @@ import torch
9
  from PIL import Image, ImageOps
10
  import numpy as np
11
  import imageio
 
12
  os.mkdir("data")
13
  os.rename("best.ckpt", "models/best.ckpt")
14
  os.mkdir("dataout")
15
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
16
 
17
- def infer(img):
 
 
 
18
 
 
 
 
 
19
 
 
20
  img = Image.open(img)
21
  mask = Image.open("./masks/modelscope-mask.png")
22
  inverted_mask = ImageOps.invert(mask)
23
 
24
 
25
- imageio.imwrite("./data/data.png", img)
26
- imageio.imwrite("./data/data_mask.png", inverted_mask)
27
  os.system('python predict.py model.path=/home/user/app/ indir=/home/user/app/data/ outdir=/home/user/app/dataout/ device=cpu')
28
- return "./dataout/data_mask.png", "./data/data_mask.png"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
 
 
30
 
31
  inputs = [gr.Image(label="Input", source="upload", type="filepath")]
32
- outputs = [gr.outputs.Image(type="file", label="output"),
33
- gr.outputs.Image(type="file", label="Mask")]
34
  title = "LaMa Image Inpainting"
35
  description = "Gradio demo for LaMa: Resolution-robust Large Mask Inpainting with Fourier Convolutions. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below. Masks are generated by U^2net"
36
  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>"
 
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
 
12
  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_{i}.png", img)
74
+ imageio.imwrite(f"./data/data_mask_{i}.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_{i}.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
+ result_frames.append(lama_frame)
97
+ print("frame " + i + "/" + str(n_frame) + ": done;")
98
+
99
+
100
+ final_vid = create_video(result_frames, fps, "cleaned")
101
+
102
+ files = [final_vid]
103
 
104
+ return final_vid, files
105
 
106
  inputs = [gr.Image(label="Input", source="upload", type="filepath")]
107
+ outputs = [gr.Video(label="output"),
108
+ gr.Files(label="Download Video")]
109
  title = "LaMa Image Inpainting"
110
  description = "Gradio demo for LaMa: Resolution-robust Large Mask Inpainting with Fourier Convolutions. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below. Masks are generated by U^2net"
111
  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>"