Spaces:
Sleeping
Sleeping
app is mcp compatible
Browse files
app.py
CHANGED
@@ -6,16 +6,24 @@ import numpy as np
|
|
6 |
from PIL import Image
|
7 |
from moviepy.editor import *
|
8 |
|
|
|
9 |
openpose = OpenposeDetector.from_pretrained('lllyasviel/ControlNet')
|
10 |
|
11 |
def get_frames(video_in):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
12 |
frames = []
|
13 |
-
#resize the video
|
14 |
clip = VideoFileClip(video_in)
|
15 |
|
16 |
-
#check fps
|
17 |
if clip.fps > 30:
|
18 |
-
print("
|
19 |
clip_resized = clip.resize(height=512)
|
20 |
clip_resized.write_videofile("video_resized.mp4", fps=30)
|
21 |
else:
|
@@ -23,97 +31,103 @@ def get_frames(video_in):
|
|
23 |
clip_resized = clip.resize(height=512)
|
24 |
clip_resized.write_videofile("video_resized.mp4", fps=clip.fps)
|
25 |
|
26 |
-
|
27 |
-
|
28 |
-
# Opens the Video file with CV2
|
29 |
-
cap= cv2.VideoCapture("video_resized.mp4")
|
30 |
-
|
31 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
32 |
-
|
33 |
-
i=0
|
34 |
while(cap.isOpened()):
|
35 |
ret, frame = cap.read()
|
36 |
-
if ret
|
37 |
break
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
|
|
42 |
cap.release()
|
43 |
cv2.destroyAllWindows()
|
44 |
-
print("broke the video into frames")
|
45 |
-
|
46 |
return frames, fps
|
47 |
|
48 |
def get_openpose_filter(i):
|
49 |
-
image
|
50 |
|
51 |
-
|
52 |
-
|
|
|
|
|
|
|
|
|
|
|
53 |
image = openpose(image)
|
54 |
-
|
55 |
-
image.save(
|
56 |
-
return
|
57 |
|
58 |
def create_video(frames, fps, type):
|
59 |
-
|
60 |
-
clip = ImageSequenceClip(frames, fps=fps)
|
61 |
-
clip.write_videofile(type + "_result.mp4", fps=fps)
|
62 |
|
63 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
64 |
|
65 |
def convertG2V(imported_gif):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
66 |
clip = VideoFileClip(imported_gif.name)
|
67 |
clip.write_videofile("my_gif_video.mp4")
|
68 |
return "my_gif_video.mp4"
|
69 |
-
|
70 |
def infer(video_in):
|
|
|
71 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
|
73 |
-
|
|
|
|
|
|
|
74 |
break_vid = get_frames(video_in)
|
75 |
-
frames_list= break_vid[0]
|
76 |
fps = break_vid[1]
|
77 |
-
#n_frame = int(trim_value*fps)
|
78 |
n_frame = len(frames_list)
|
79 |
|
80 |
-
if n_frame >= len(frames_list):
|
81 |
-
print("video is shorter than the cut value")
|
82 |
-
n_frame = len(frames_list)
|
83 |
-
|
84 |
-
# 2. prepare frames result arrays
|
85 |
result_frames = []
|
86 |
-
|
87 |
-
|
88 |
-
for i in frames_list[0:int(n_frame)]:
|
89 |
openpose_frame = get_openpose_filter(i)
|
90 |
result_frames.append(openpose_frame)
|
91 |
-
print("frame " + i + "/" + str(n_frame) + ": done;")
|
92 |
|
93 |
-
|
94 |
final_vid = create_video(result_frames, fps, "openpose")
|
95 |
-
|
96 |
files = [final_vid]
|
97 |
|
98 |
return final_vid, files
|
99 |
|
100 |
-
|
|
|
101 |
<div style="text-align: center; max-width: 500px; margin: 0 auto;">
|
102 |
-
|
103 |
-
style="
|
104 |
-
display: inline-flex;
|
105 |
-
align-items: center;
|
106 |
-
gap: 0.8rem;
|
107 |
-
font-size: 1.75rem;
|
108 |
-
margin-bottom: 10px;
|
109 |
-
"
|
110 |
-
>
|
111 |
-
<h1 style="font-weight: 600; margin-bottom: 7px;">
|
112 |
-
Video to OpenPose
|
113 |
-
</h1>
|
114 |
-
</div>
|
115 |
-
|
116 |
</div>
|
|
|
117 |
"""
|
118 |
|
119 |
with gr.Blocks() as demo:
|
@@ -122,14 +136,13 @@ with gr.Blocks() as demo:
|
|
122 |
with gr.Row():
|
123 |
with gr.Column():
|
124 |
video_input = gr.Video(sources=["upload"])
|
125 |
-
gif_input = gr.File(label="
|
126 |
gif_input.change(fn=convertG2V, inputs=gif_input, outputs=video_input)
|
127 |
submit_btn = gr.Button("Submit")
|
128 |
-
|
129 |
with gr.Column():
|
130 |
video_output = gr.Video()
|
131 |
file_output = gr.Files()
|
132 |
|
133 |
submit_btn.click(fn=infer, inputs=[video_input], outputs=[video_output, file_output])
|
134 |
|
135 |
-
demo.launch()
|
|
|
6 |
from PIL import Image
|
7 |
from moviepy.editor import *
|
8 |
|
9 |
+
# Load the OpenPose detector
|
10 |
openpose = OpenposeDetector.from_pretrained('lllyasviel/ControlNet')
|
11 |
|
12 |
def get_frames(video_in):
|
13 |
+
"""Extract frames from a video and resize to height 512px.
|
14 |
+
|
15 |
+
Args:
|
16 |
+
video_in: Path to the input video file.
|
17 |
+
|
18 |
+
Returns:
|
19 |
+
frames: List of paths to extracted frame images.
|
20 |
+
fps: Frames per second of the original video.
|
21 |
+
"""
|
22 |
frames = []
|
|
|
23 |
clip = VideoFileClip(video_in)
|
24 |
|
|
|
25 |
if clip.fps > 30:
|
26 |
+
print("video rate is over 30, resetting to 30")
|
27 |
clip_resized = clip.resize(height=512)
|
28 |
clip_resized.write_videofile("video_resized.mp4", fps=30)
|
29 |
else:
|
|
|
31 |
clip_resized = clip.resize(height=512)
|
32 |
clip_resized.write_videofile("video_resized.mp4", fps=clip.fps)
|
33 |
|
34 |
+
cap = cv2.VideoCapture("video_resized.mp4")
|
|
|
|
|
|
|
|
|
35 |
fps = cap.get(cv2.CAP_PROP_FPS)
|
36 |
+
i = 0
|
|
|
37 |
while(cap.isOpened()):
|
38 |
ret, frame = cap.read()
|
39 |
+
if not ret:
|
40 |
break
|
41 |
+
frame_path = f'frame_{i}.jpg'
|
42 |
+
cv2.imwrite(frame_path, frame)
|
43 |
+
frames.append(frame_path)
|
44 |
+
i += 1
|
45 |
+
|
46 |
cap.release()
|
47 |
cv2.destroyAllWindows()
|
|
|
|
|
48 |
return frames, fps
|
49 |
|
50 |
def get_openpose_filter(i):
|
51 |
+
"""Apply OpenPose filter to a single image frame.
|
52 |
|
53 |
+
Args:
|
54 |
+
i: Path to the image frame.
|
55 |
+
|
56 |
+
Returns:
|
57 |
+
output_path: Path to the OpenPose-processed image.
|
58 |
+
"""
|
59 |
+
image = Image.open(i)
|
60 |
image = openpose(image)
|
61 |
+
output_path = f"openpose_frame_{os.path.basename(i)}.jpeg"
|
62 |
+
image.save(output_path)
|
63 |
+
return output_path
|
64 |
|
65 |
def create_video(frames, fps, type):
|
66 |
+
"""Create a video from a sequence of image frames.
|
|
|
|
|
67 |
|
68 |
+
Args:
|
69 |
+
frames: List of image frame paths.
|
70 |
+
fps: Frames per second for the output video.
|
71 |
+
type: A string used as the prefix for naming the result video.
|
72 |
+
|
73 |
+
Returns:
|
74 |
+
video_path: Path to the resulting video file.
|
75 |
+
"""
|
76 |
+
clip = ImageSequenceClip(frames, fps=fps)
|
77 |
+
video_path = f"{type}_result.mp4"
|
78 |
+
clip.write_videofile(video_path, fps=fps)
|
79 |
+
return video_path
|
80 |
|
81 |
def convertG2V(imported_gif):
|
82 |
+
"""Convert a GIF file to a standard MP4 video.
|
83 |
+
|
84 |
+
Args:
|
85 |
+
imported_gif: The input GIF file object.
|
86 |
+
|
87 |
+
Returns:
|
88 |
+
Path to the converted MP4 video file.
|
89 |
+
"""
|
90 |
clip = VideoFileClip(imported_gif.name)
|
91 |
clip.write_videofile("my_gif_video.mp4")
|
92 |
return "my_gif_video.mp4"
|
93 |
+
|
94 |
def infer(video_in):
|
95 |
+
"""Generate an OpenPose-filtered video from an input video.
|
96 |
|
97 |
+
This function performs the following steps:
|
98 |
+
1. Extracts frames from the input video and retrieves the original frame rate (FPS).
|
99 |
+
2. Applies the OpenPose model to each frame to extract pose data.
|
100 |
+
3. Reconstructs a new video from the OpenPose-processed frames using the original FPS.
|
101 |
+
|
102 |
+
Args:
|
103 |
+
video_in: The uploaded input video file (MP4 or converted GIF).
|
104 |
|
105 |
+
Returns:
|
106 |
+
final_vid: The path to the OpenPose-filtered output video.
|
107 |
+
files: A list containing the output video file (for download).
|
108 |
+
"""
|
109 |
break_vid = get_frames(video_in)
|
110 |
+
frames_list = break_vid[0]
|
111 |
fps = break_vid[1]
|
|
|
112 |
n_frame = len(frames_list)
|
113 |
|
|
|
|
|
|
|
|
|
|
|
114 |
result_frames = []
|
115 |
+
for i in frames_list[:n_frame]:
|
|
|
|
|
116 |
openpose_frame = get_openpose_filter(i)
|
117 |
result_frames.append(openpose_frame)
|
|
|
118 |
|
|
|
119 |
final_vid = create_video(result_frames, fps, "openpose")
|
|
|
120 |
files = [final_vid]
|
121 |
|
122 |
return final_vid, files
|
123 |
|
124 |
+
# UI layout
|
125 |
+
title = """
|
126 |
<div style="text-align: center; max-width: 500px; margin: 0 auto;">
|
127 |
+
<div style="display: inline-flex; align-items: center; gap: 0.8rem; font-size: 1.75rem; margin-bottom: 10px;">
|
128 |
+
<h1 style="font-weight: 600; margin-bottom: 7px;">Video to OpenPose</h1>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
129 |
</div>
|
130 |
+
</div>
|
131 |
"""
|
132 |
|
133 |
with gr.Blocks() as demo:
|
|
|
136 |
with gr.Row():
|
137 |
with gr.Column():
|
138 |
video_input = gr.Video(sources=["upload"])
|
139 |
+
gif_input = gr.File(label="Import a GIF instead", file_types=['.gif'])
|
140 |
gif_input.change(fn=convertG2V, inputs=gif_input, outputs=video_input)
|
141 |
submit_btn = gr.Button("Submit")
|
|
|
142 |
with gr.Column():
|
143 |
video_output = gr.Video()
|
144 |
file_output = gr.Files()
|
145 |
|
146 |
submit_btn.click(fn=infer, inputs=[video_input], outputs=[video_output, file_output])
|
147 |
|
148 |
+
demo.launch(mcp_server=True)
|