Spaces:
Sleeping
Sleeping
resize checked implemented, but even if resize not needed, still need to convert frame dtype to float64
Browse files- app.py +65 -21
- assets/default_driving_video.mp4 +2 -2
app.py
CHANGED
|
@@ -56,14 +56,17 @@ def save_video_from_upload(uploaded_file):
|
|
| 56 |
print(f"Error occurred: {e}")
|
| 57 |
|
| 58 |
def create_image_video_side_by_side(source, driving, generated=None, output_file='assets/output_video.mp4', fps=20, progress_bar=None):
|
|
|
|
| 59 |
total_driving_frames = len(driving)
|
| 60 |
progress_bar.progress(0)
|
| 61 |
#images = l
|
| 62 |
images = []
|
| 63 |
print("going through video")
|
|
|
|
| 64 |
for i in range(len(driving)):
|
| 65 |
|
| 66 |
cols = [source]
|
|
|
|
| 67 |
cols.append(driving[i])
|
| 68 |
if generated is not None:
|
| 69 |
#print("generated data length:"+str(len(generated)))
|
|
@@ -82,6 +85,19 @@ def create_image_video_side_by_side(source, driving, generated=None, output_file
|
|
| 82 |
np.uint8) if full_image.max() <= 1 else full_image.astype(np.uint8)
|
| 83 |
#print("full_image_rgb shape" + str(full_image_rgb.shape))
|
| 84 |
# Append to the list of images
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 85 |
images.append(full_image_rgb)
|
| 86 |
progress_percentage = (i + 1) / total_driving_frames
|
| 87 |
progress_bar.progress(progress_percentage)
|
|
@@ -128,16 +144,20 @@ def delete_file_if_exists(file_path):
|
|
| 128 |
|
| 129 |
|
| 130 |
def read_src_image():
|
|
|
|
| 131 |
st.session_state['source_image'] = imageio.imread(st.session_state['source_image_path'])
|
| 132 |
st.session_state['source_image'] = resize(st.session_state['source_image'],
|
| 133 |
(st.session_state['pixel'], st.session_state['pixel']))[..., :3]
|
| 134 |
|
| 135 |
-
|
|
|
|
| 136 |
def read_driving_video(progress_bar=None):
|
| 137 |
|
| 138 |
reader = imageio.get_reader(st.session_state['driving_video_path'])
|
| 139 |
st.session_state['fps'] = reader.get_meta_data()['fps']
|
| 140 |
st.session_state['duration'] = reader.get_meta_data()['duration']
|
|
|
|
|
|
|
| 141 |
print("st.session_state['duration']="+str(st.session_state['duration']))
|
| 142 |
st.session_state['driving_video'] = []
|
| 143 |
print("reading video")
|
|
@@ -150,8 +170,10 @@ def read_driving_video(progress_bar=None):
|
|
| 150 |
try:
|
| 151 |
video_frame_idx=0
|
| 152 |
for im in reader:
|
|
|
|
|
|
|
| 153 |
st.session_state['driving_video'].append(im)
|
| 154 |
-
progress_percentage = (video_frame_idx) / estimated_frame_count
|
| 155 |
# print(f"video_frame_idx = {video_frame_idx} estimated_frame_count={estimated_frame_count}")
|
| 156 |
progress_bar.progress(progress_percentage)
|
| 157 |
video_frame_idx = video_frame_idx+1
|
|
@@ -161,26 +183,43 @@ def read_driving_video(progress_bar=None):
|
|
| 161 |
print("finished reading video")
|
| 162 |
# st.session_state['driving_video'] = [resize(frame, (st.session_state['pixel'], st.session_state['pixel']))[..., :3]
|
| 163 |
# for frame in st.session_state['driving_video']]
|
| 164 |
-
|
| 165 |
-
|
| 166 |
progress_bar.progress(0)
|
| 167 |
# Process each frame, update progress bar along the way
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
def add_animation_to_image():
|
| 185 |
|
| 186 |
inference_status_placeholder.write("start inference")
|
|
@@ -191,6 +230,10 @@ def add_animation_to_image():
|
|
| 191 |
st.session_state['output_video_path']='assets/generated_video.mp4'
|
| 192 |
st.session_state['side_by_side_with_generated_video_path']='assets/src_image_driving_video_generated_video_side_by_side.mp4'
|
| 193 |
inference_status_placeholder.write("saving generated video")
|
|
|
|
|
|
|
|
|
|
|
|
|
| 194 |
imageio.mimsave(st.session_state['output_video_path'], [img_as_ubyte(frame) for frame in predictions], fps=st.session_state['fps'])
|
| 195 |
inference_status_placeholder.write("saving generated video done")
|
| 196 |
print("side_by_side_with_generated_video_path="+st.session_state['side_by_side_with_generated_video_path'])
|
|
@@ -283,7 +326,8 @@ if 'is_initialized' not in st.session_state:
|
|
| 283 |
print(f"Error occurred: {e}")
|
| 284 |
else:
|
| 285 |
current_status_placeholder.write("creating side by side video")
|
| 286 |
-
|
|
|
|
| 287 |
|
| 288 |
uploaded_src_image_file = st.file_uploader("Upload a source image... image must be square dimension", type=['jpg', 'jpeg', 'png'])
|
| 289 |
st.markdown(f'<a href="https://ikmtechnology.github.io/ikmtechnology/Kyla2.png" target="_blank">Alternative Source Image 1 download and then upload to above</a>', unsafe_allow_html=True)
|
|
|
|
| 56 |
print(f"Error occurred: {e}")
|
| 57 |
|
| 58 |
def create_image_video_side_by_side(source, driving, generated=None, output_file='assets/output_video.mp4', fps=20, progress_bar=None):
|
| 59 |
+
#st.image(source,caption='create_image_video_side_by_side src')
|
| 60 |
total_driving_frames = len(driving)
|
| 61 |
progress_bar.progress(0)
|
| 62 |
#images = l
|
| 63 |
images = []
|
| 64 |
print("going through video")
|
| 65 |
+
|
| 66 |
for i in range(len(driving)):
|
| 67 |
|
| 68 |
cols = [source]
|
| 69 |
+
|
| 70 |
cols.append(driving[i])
|
| 71 |
if generated is not None:
|
| 72 |
#print("generated data length:"+str(len(generated)))
|
|
|
|
| 85 |
np.uint8) if full_image.max() <= 1 else full_image.astype(np.uint8)
|
| 86 |
#print("full_image_rgb shape" + str(full_image_rgb.shape))
|
| 87 |
# Append to the list of images
|
| 88 |
+
# if i == 0:
|
| 89 |
+
# source_rgb = np.clip(source * 255, 0, 255).astype(
|
| 90 |
+
# np.uint8) if source.max() <= 1 else source.astype(np.uint8)
|
| 91 |
+
# st.image(source_rgb, caption="source_rgb")
|
| 92 |
+
# driving_0_rgb = np.clip(driving[i] * 255, 0, 255).astype(
|
| 93 |
+
# np.uint8) if driving[i].max() <= 1 else driving[i].astype(np.uint8)
|
| 94 |
+
# st.image(driving_0_rgb, caption="driving_0_rgb")
|
| 95 |
+
# cols_temp = [source_rgb]
|
| 96 |
+
# cols_temp.append(driving[i])
|
| 97 |
+
# full_image_temp = np.concatenate(cols_temp, axis=1)
|
| 98 |
+
# st.image(full_image_temp, caption="full_image_temp")
|
| 99 |
+
|
| 100 |
+
|
| 101 |
images.append(full_image_rgb)
|
| 102 |
progress_percentage = (i + 1) / total_driving_frames
|
| 103 |
progress_bar.progress(progress_percentage)
|
|
|
|
| 144 |
|
| 145 |
|
| 146 |
def read_src_image():
|
| 147 |
+
print("reading src imaage")
|
| 148 |
st.session_state['source_image'] = imageio.imread(st.session_state['source_image_path'])
|
| 149 |
st.session_state['source_image'] = resize(st.session_state['source_image'],
|
| 150 |
(st.session_state['pixel'], st.session_state['pixel']))[..., :3]
|
| 151 |
|
| 152 |
+
# st.session_state['source_image'] = np.clip(st.session_state['source_image'] * 255, 0, 255).astype(
|
| 153 |
+
# np.uint8) if st.session_state['source_image'].max() <= 1 else st.session_state['source_image'].astype(np.uint8)
|
| 154 |
def read_driving_video(progress_bar=None):
|
| 155 |
|
| 156 |
reader = imageio.get_reader(st.session_state['driving_video_path'])
|
| 157 |
st.session_state['fps'] = reader.get_meta_data()['fps']
|
| 158 |
st.session_state['duration'] = reader.get_meta_data()['duration']
|
| 159 |
+
video_width = reader.get_meta_data()['source_size'][0]
|
| 160 |
+
|
| 161 |
print("st.session_state['duration']="+str(st.session_state['duration']))
|
| 162 |
st.session_state['driving_video'] = []
|
| 163 |
print("reading video")
|
|
|
|
| 170 |
try:
|
| 171 |
video_frame_idx=0
|
| 172 |
for im in reader:
|
| 173 |
+
# im = np.clip(im * 255, 0, 255).astype(
|
| 174 |
+
# np.uint8) if im.max() <= 1 else im.astype(np.uint8)
|
| 175 |
st.session_state['driving_video'].append(im)
|
| 176 |
+
progress_percentage = (video_frame_idx) / (estimated_frame_count+1)
|
| 177 |
# print(f"video_frame_idx = {video_frame_idx} estimated_frame_count={estimated_frame_count}")
|
| 178 |
progress_bar.progress(progress_percentage)
|
| 179 |
video_frame_idx = video_frame_idx+1
|
|
|
|
| 183 |
print("finished reading video")
|
| 184 |
# st.session_state['driving_video'] = [resize(frame, (st.session_state['pixel'], st.session_state['pixel']))[..., :3]
|
| 185 |
# for frame in st.session_state['driving_video']]
|
| 186 |
+
print("check resize width ="+str(video_width))
|
| 187 |
+
# if video_width != 512:
|
| 188 |
progress_bar.progress(0)
|
| 189 |
# Process each frame, update progress bar along the way
|
| 190 |
+
if video_width != 512:
|
| 191 |
+
resized_frames = []
|
| 192 |
+
num_frames=len(st.session_state['driving_video'])
|
| 193 |
+
current_status_placeholder.write("resizing video")
|
| 194 |
+
for i, frame in enumerate(st.session_state['driving_video']):
|
| 195 |
+
if i == 0:
|
| 196 |
+
print("frame.dtype="+str(frame.dtype))
|
| 197 |
+
# Resize frame
|
| 198 |
+
resized_frame = resize(frame, (st.session_state['pixel'], st.session_state['pixel']))[..., :3]
|
| 199 |
+
if i == 0:
|
| 200 |
+
print("resized_frame.dtype="+str(resized_frame.dtype))
|
| 201 |
+
resized_frames.append(resized_frame)
|
| 202 |
+
|
| 203 |
+
# Update progress bar
|
| 204 |
+
progress_bar.progress((i + 1) / num_frames)
|
| 205 |
+
|
| 206 |
+
# Update the session state with the resized frames
|
| 207 |
+
st.session_state['driving_video'] = resized_frames
|
| 208 |
+
else:
|
| 209 |
+
for i, frame in enumerate(st.session_state['driving_video']):
|
| 210 |
+
if frame.dtype != np.float64:
|
| 211 |
+
# Convert to float64
|
| 212 |
+
frame_float64 = frame.astype(np.float64)
|
| 213 |
+
|
| 214 |
+
# Normalize the frame based on its original range
|
| 215 |
+
if frame.dtype == np.uint8:
|
| 216 |
+
frame_normalized = frame_float64 / 255.0
|
| 217 |
+
elif frame.dtype == np.uint16:
|
| 218 |
+
frame_normalized = frame_float64 / 65535.0
|
| 219 |
+
elif frame.dtype == np.float32:
|
| 220 |
+
# Assuming float32 range is 0.0 to 1.0, similar normalization might not be needed
|
| 221 |
+
frame_normalized = frame_float64
|
| 222 |
+
st.session_state['driving_video'][i] = frame_normalized
|
| 223 |
def add_animation_to_image():
|
| 224 |
|
| 225 |
inference_status_placeholder.write("start inference")
|
|
|
|
| 230 |
st.session_state['output_video_path']='assets/generated_video.mp4'
|
| 231 |
st.session_state['side_by_side_with_generated_video_path']='assets/src_image_driving_video_generated_video_side_by_side.mp4'
|
| 232 |
inference_status_placeholder.write("saving generated video")
|
| 233 |
+
# for i in range(len(predictions)):
|
| 234 |
+
# predictions[i] = np.clip(predictions[i] * 255, 0, 255).astype(
|
| 235 |
+
# np.uint8) if predictions[i].max() <= 1 else predictions[i].astype(np.uint8)
|
| 236 |
+
#st.image(predictions[0], caption="predictions[0]")
|
| 237 |
imageio.mimsave(st.session_state['output_video_path'], [img_as_ubyte(frame) for frame in predictions], fps=st.session_state['fps'])
|
| 238 |
inference_status_placeholder.write("saving generated video done")
|
| 239 |
print("side_by_side_with_generated_video_path="+st.session_state['side_by_side_with_generated_video_path'])
|
|
|
|
| 326 |
print(f"Error occurred: {e}")
|
| 327 |
else:
|
| 328 |
current_status_placeholder.write("creating side by side video")
|
| 329 |
+
|
| 330 |
+
st.session_state['side_by_side_video_path'] = create_image_video_side_by_side(st.session_state['source_image'], st.session_state['driving_video'], output_file=st.session_state['side_by_side_video_path'], fps=st.session_state['fps'], progress_bar=init_progress_bar)
|
| 331 |
|
| 332 |
uploaded_src_image_file = st.file_uploader("Upload a source image... image must be square dimension", type=['jpg', 'jpeg', 'png'])
|
| 333 |
st.markdown(f'<a href="https://ikmtechnology.github.io/ikmtechnology/Kyla2.png" target="_blank">Alternative Source Image 1 download and then upload to above</a>', unsafe_allow_html=True)
|
assets/default_driving_video.mp4
CHANGED
|
@@ -1,3 +1,3 @@
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
-
oid sha256:
|
| 3 |
-
size
|
|
|
|
| 1 |
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e8e32c30e583eea814895cfa3cfee20a0b8de02cb4a2041ee883463b5353a9c6
|
| 3 |
+
size 914115
|