Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
@@ -6,8 +6,45 @@ import numpy as np
|
|
6 |
from PIL import Image
|
7 |
from moviepy.editor import *
|
8 |
|
9 |
-
def
|
10 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
11 |
|
12 |
image = np.array(image)
|
13 |
|
@@ -18,14 +55,47 @@ def get_canny_filter(image):
|
|
18 |
image = image[:, :, None]
|
19 |
image = np.concatenate([image, image, image], axis=2)
|
20 |
image = Image.fromarray(image)
|
21 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
22 |
|
23 |
def infer(imported_gif):
|
24 |
clip = VideoFileClip(imported_gif.name)
|
25 |
clip.write_videofile("my_gif_video.mp4")
|
26 |
|
27 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
28 |
|
29 |
inputs = [gr.File(label="import a GIF instead", file_types=['.gif'])]
|
30 |
-
outputs = [gr.Video()]
|
31 |
gr.Interface(fn=infer, inputs=inputs, outputs=outputs).launch()
|
|
|
6 |
from PIL import Image
|
7 |
from moviepy.editor import *
|
8 |
|
9 |
+
def get_frames(video_in):
|
10 |
+
frames = []
|
11 |
+
#resize the video
|
12 |
+
clip = VideoFileClip(video_in)
|
13 |
+
|
14 |
+
#check fps
|
15 |
+
if clip.fps > 30:
|
16 |
+
print("vide rate is over 30, resetting to 30")
|
17 |
+
clip_resized = clip.resize(height=512)
|
18 |
+
clip_resized.write_videofile("video_resized.mp4", fps=30)
|
19 |
+
else:
|
20 |
+
print("video rate is OK")
|
21 |
+
clip_resized = clip.resize(height=512)
|
22 |
+
clip_resized.write_videofile("video_resized.mp4", fps=clip.fps)
|
23 |
+
|
24 |
+
print("video resized to 512 height")
|
25 |
+
|
26 |
+
# Opens the Video file with CV2
|
27 |
+
cap= cv2.VideoCapture("video_resized.mp4")
|
28 |
+
|
29 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
30 |
+
print("video fps: " + str(fps))
|
31 |
+
i=0
|
32 |
+
while(cap.isOpened()):
|
33 |
+
ret, frame = cap.read()
|
34 |
+
if ret == False:
|
35 |
+
break
|
36 |
+
cv2.imwrite('kang'+str(i)+'.jpg',frame)
|
37 |
+
frames.append('kang'+str(i)+'.jpg')
|
38 |
+
i+=1
|
39 |
+
|
40 |
+
cap.release()
|
41 |
+
cv2.destroyAllWindows()
|
42 |
+
print("broke the video into frames")
|
43 |
+
|
44 |
+
return frames, fps
|
45 |
+
|
46 |
+
def get_canny_filter(i):
|
47 |
+
image = Image.open(i)
|
48 |
|
49 |
image = np.array(image)
|
50 |
|
|
|
55 |
image = image[:, :, None]
|
56 |
image = np.concatenate([image, image, image], axis=2)
|
57 |
image = Image.fromarray(image)
|
58 |
+
image.save("canny_frame_" + str(i) + ".jpeg")
|
59 |
+
return "canny_frame_" + str(i) + ".jpeg"
|
60 |
+
|
61 |
+
def create_video(frames, fps, type):
|
62 |
+
print("building video result")
|
63 |
+
clip = ImageSequenceClip(frames, fps=fps)
|
64 |
+
clip.write_videofile(type + "_result.mp4", fps=fps)
|
65 |
+
|
66 |
+
return type + "_result.mp4"
|
67 |
+
|
68 |
|
69 |
def infer(imported_gif):
|
70 |
clip = VideoFileClip(imported_gif.name)
|
71 |
clip.write_videofile("my_gif_video.mp4")
|
72 |
|
73 |
+
# 1. break video into frames and get FPS
|
74 |
+
break_vid = get_frames("my_gif_video.mp4")
|
75 |
+
frames_list= break_vid[0]
|
76 |
+
fps = break_vid[1]
|
77 |
+
n_frame = int(trim_value*fps)
|
78 |
+
|
79 |
+
if n_frame >= len(frames_list):
|
80 |
+
print("video is shorter than the cut value")
|
81 |
+
n_frame = len(frames_list)
|
82 |
+
|
83 |
+
# 2. prepare frames result arrays
|
84 |
+
result_frames = []
|
85 |
+
print("set stop frames to: " + str(n_frame))
|
86 |
+
|
87 |
+
for i in frames_list[0:int(n_frame)]:
|
88 |
+
canny_frame = get_canny_filter(i)
|
89 |
+
result_frames.append(canny_frame)
|
90 |
+
print("frame " + i + "/" + str(n_frame) + ": done;")
|
91 |
+
|
92 |
+
|
93 |
+
final_vid = create_video(result_frames, fps, "final")
|
94 |
+
|
95 |
+
files = [final_vid]
|
96 |
+
|
97 |
+
return final_vid, files
|
98 |
|
99 |
inputs = [gr.File(label="import a GIF instead", file_types=['.gif'])]
|
100 |
+
outputs = [gr.Video(),gr.Files()]
|
101 |
gr.Interface(fn=infer, inputs=inputs, outputs=outputs).launch()
|