Commit
·
578f3e2
1
Parent(s):
293e0b1
Update app.py
Browse files
app.py
CHANGED
|
@@ -26,20 +26,24 @@ mediapy.set_ffmpeg(ffmpeg_path)
|
|
| 26 |
|
| 27 |
SECRET_TOKEN = os.getenv('SECRET_TOKEN', 'default_secret')
|
| 28 |
|
|
|
|
| 29 |
def base64_to_video(base64_string, output_file):
|
| 30 |
video_data = base64.b64decode(base64_string)
|
| 31 |
with open(output_file, 'wb') as f:
|
| 32 |
f.write(video_data)
|
| 33 |
-
|
| 34 |
-
def do_interpolation(frame1, frame2, times_to_interpolate
|
|
|
|
| 35 |
input_frames = [frame1, frame2]
|
|
|
|
| 36 |
frames = list(
|
| 37 |
util.interpolate_recursively_from_files(
|
| 38 |
input_frames, times_to_interpolate, interpolator))
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
| 43 |
def get_frames(video_in, step, name):
|
| 44 |
frames = []
|
| 45 |
#resize the video
|
|
@@ -74,9 +78,10 @@ def get_frames(video_in, step, name):
|
|
| 74 |
ret, frame = cap.read()
|
| 75 |
if ret == False:
|
| 76 |
break
|
| 77 |
-
#
|
| 78 |
-
|
| 79 |
-
|
|
|
|
| 80 |
i+=1
|
| 81 |
|
| 82 |
cap.release()
|
|
@@ -99,37 +104,54 @@ def infer(secret_token, video_in_base64, interpolation, fps_output):
|
|
| 99 |
if secret_token != SECRET_TOKEN:
|
| 100 |
raise gr.Error(f'Invalid secret token. Please fork the original space if you want to use it for yourself.')
|
| 101 |
|
| 102 |
-
|
| 103 |
-
with tempfile.NamedTemporaryFile(delete=True, suffix=".mp4") as temp_video_in:
|
| 104 |
-
# Decode the base64 string to a video file
|
| 105 |
-
base64_to_video(video_in_base64, temp_video_in.name)
|
| 106 |
-
|
| 107 |
-
# 1. break video into frames and get FPS
|
| 108 |
-
break_vid = get_frames(temp_video_in.name, "vid_input_frame", "origin")
|
| 109 |
-
frames_list = break_vid[0]
|
| 110 |
-
fps = break_vid[1]
|
| 111 |
-
|
| 112 |
-
# print(f"ORIGIN FPS: {fps}")
|
| 113 |
-
n_frame = int(4 * fps) #limited to 4 seconds
|
| 114 |
-
# #n_frame = len(frames_list)
|
| 115 |
-
if n_frame >= len(frames_list):
|
| 116 |
-
n_frame = len(frames_list)
|
| 117 |
-
|
| 118 |
-
result_frames = []
|
| 119 |
-
for idx, frame in enumerate(frames_list[0:n_frame]):
|
| 120 |
-
if idx < len(frames_list) - 1:
|
| 121 |
-
next_frame = frames_list[idx + 1]
|
| 122 |
-
interpolated_frames = do_interpolation(frame, next_frame, interpolation, interpolator)
|
| 123 |
-
break_interpolated_video = get_frames(interpolated_frames, "interpol", f"{idx}_")
|
| 124 |
-
for j, img in enumerate(break_interpolated_video[0][:-1]):
|
| 125 |
-
os.rename(img, f"{frame}_to_{next_frame}_{j}.png")
|
| 126 |
-
result_frames.append(f"{frame}_to_{next_frame}_{j}.png")
|
| 127 |
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
|
| 134 |
title="""test space"""
|
| 135 |
|
|
|
|
| 26 |
|
| 27 |
SECRET_TOKEN = os.getenv('SECRET_TOKEN', 'default_secret')
|
| 28 |
|
| 29 |
+
|
| 30 |
def base64_to_video(base64_string, output_file):
|
| 31 |
video_data = base64.b64decode(base64_string)
|
| 32 |
with open(output_file, 'wb') as f:
|
| 33 |
f.write(video_data)
|
| 34 |
+
|
| 35 |
+
def do_interpolation(frame1, frame2, times_to_interpolate):
|
| 36 |
+
print(frame1, frame2)
|
| 37 |
input_frames = [frame1, frame2]
|
| 38 |
+
#times_to_interpolate = 2
|
| 39 |
frames = list(
|
| 40 |
util.interpolate_recursively_from_files(
|
| 41 |
input_frames, times_to_interpolate, interpolator))
|
| 42 |
+
|
| 43 |
+
#print(frames)
|
| 44 |
+
mediapy.write_video(f"{frame1}_to_{frame2}_out.mp4", frames, fps=12)
|
| 45 |
+
return f"{frame1}_to_{frame2}_out.mp4"
|
| 46 |
+
|
| 47 |
def get_frames(video_in, step, name):
|
| 48 |
frames = []
|
| 49 |
#resize the video
|
|
|
|
| 78 |
ret, frame = cap.read()
|
| 79 |
if ret == False:
|
| 80 |
break
|
| 81 |
+
# we could use png to avoid any compression artifact, but it takes much more space!
|
| 82 |
+
# alternatively, let's just bump the quality from 95 to 98 for now
|
| 83 |
+
cv2.imwrite(f"{name}_{step}{str(i)}.jpg", frame, [int(cv2.IMWRITE_JPEG_QUALITY), 98])
|
| 84 |
+
frames.append(f"{name}_{step}{str(i)}.jpg")
|
| 85 |
i+=1
|
| 86 |
|
| 87 |
cap.release()
|
|
|
|
| 104 |
if secret_token != SECRET_TOKEN:
|
| 105 |
raise gr.Error(f'Invalid secret token. Please fork the original space if you want to use it for yourself.')
|
| 106 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 107 |
|
| 108 |
+
# Decode the base64 string to a video file
|
| 109 |
+
video_in = "video_in.mp4" # or choose any other filename/path
|
| 110 |
+
base64_to_video(video_in_base64, video_in)
|
| 111 |
+
|
| 112 |
+
# 1. break video into frames and get FPS
|
| 113 |
+
break_vid = get_frames(video_in, "vid_input_frame", "origin")
|
| 114 |
+
frames_list= break_vid[0]
|
| 115 |
+
fps = break_vid[1]
|
| 116 |
+
print(f"ORIGIN FPS: {fps}")
|
| 117 |
+
n_frame = int(4*fps) #limited to 4 seconds
|
| 118 |
+
#n_frame = len(frames_list)
|
| 119 |
+
|
| 120 |
+
if n_frame >= len(frames_list):
|
| 121 |
+
print("video is shorter than the cut value")
|
| 122 |
+
n_frame = len(frames_list)
|
| 123 |
+
|
| 124 |
+
# 2. prepare frames result arrays
|
| 125 |
+
result_frames = []
|
| 126 |
+
# print("set stop frames to: " + str(n_frame))
|
| 127 |
+
|
| 128 |
+
|
| 129 |
+
for idx, frame in enumerate(frames_list[0:int(n_frame)]):
|
| 130 |
+
if idx < len(frames_list) - 1:
|
| 131 |
+
next_frame = frames_list[idx+1]
|
| 132 |
+
interpolated_frames = do_interpolation(frame, next_frame, interpolation) # should return a list of 3 interpolated frames
|
| 133 |
+
break_interpolated_video = get_frames(interpolated_frames, "interpol",f"{idx}_")
|
| 134 |
+
print(break_interpolated_video[0])
|
| 135 |
+
for j, img in enumerate(break_interpolated_video[0][0:len(break_interpolated_video[0])-1]):
|
| 136 |
+
#print(f"IMG:{img}")
|
| 137 |
+
# we could use png to avoid any compression artifact, but it takes much more space
|
| 138 |
+
# alternatively, let's just bump the quality from 95 to 98 for now
|
| 139 |
+
os.rename(img, f"{frame}_to_{next_frame}_{j}.jpg", [int(cv2.IMWRITE_JPEG_QUALITY), 98])
|
| 140 |
+
result_frames.append(f"{frame}_to_{next_frame}_{j}.jpg")
|
| 141 |
+
|
| 142 |
+
print("frames " + str(idx) + " & " + str(idx+1) + "/" + str(n_frame) + ": done;")
|
| 143 |
+
#print(f"CURRENT FRAMES: {result_frames}")
|
| 144 |
+
result_frames.append(f"{frames_list[n_frame-1]}")
|
| 145 |
+
final_vid = create_video(result_frames, fps_output, "interpolated")
|
| 146 |
+
|
| 147 |
+
encoded_string = ""
|
| 148 |
+
|
| 149 |
+
# Convert video to base64
|
| 150 |
+
with open(final_vid, "rb") as video_file:
|
| 151 |
+
encoded_string = base64.b64encode(video_file.read()).decode('utf-8')
|
| 152 |
+
|
| 153 |
+
return f"data:video/mp4;base64,{encoded_string}"
|
| 154 |
+
|
| 155 |
|
| 156 |
title="""test space"""
|
| 157 |
|