Spaces:
Runtime error
Runtime error
import os | |
import tempfile | |
import time | |
os.environ["IMAGEIO_FFMPEG_EXE"] = "/usr/bin/ffmpeg" | |
import cv2 | |
import moviepy.editor as mp | |
import numpy as np | |
import streamlit as st | |
from streamlit_lottie import st_lottie | |
from tqdm import tqdm | |
from models.deep_colorization.colorizers import eccv16 | |
from utils import load_lottieurl, format_time, colorize_frame, change_model | |
st.title("黑白影片著色器") | |
st.write(""" | |
##### 上傳黑白影片並獲得其彩色版本。 | |
###### ➠ 空間使用的是 CPU Basic,因此對影片進行彩色化可能需要一些時間。""") | |
#st.set_page_config(page_title="黑白影片著色器", page_icon="🎨", layout="wide") | |
loaded_model = eccv16(pretrained=True).eval() | |
current_model = "None" | |
def main(): | |
model = st.selectbox( | |
"選擇模型(兩種模型都有其優點和缺點,建議嘗試兩種模型並保持最適合您的任務)", | |
["ECCV16", "SIGGRAPH17"], index=0) | |
loaded_model = change_model(current_model, model) | |
st.write(f"現在是 {model}") | |
uploaded_file = st.file_uploader("在這裡上傳您的影片...", type=['mp4', 'mov', 'avi', 'mkv']) | |
if st.button("着色"): | |
if uploaded_file is not None: | |
file_extension = os.path.splitext(uploaded_file.name)[1].lower() | |
if file_extension in ['.mp4', '.avi', '.mov', '.mkv']: | |
# Save the video file to a temporary location | |
temp_file = tempfile.NamedTemporaryFile(delete=False) | |
temp_file.write(uploaded_file.read()) | |
audio = mp.AudioFileClip(temp_file.name) | |
# Open the video using cv2.VideoCapture | |
video = cv2.VideoCapture(temp_file.name) | |
# Get video information | |
fps = video.get(cv2.CAP_PROP_FPS) | |
col1, col2 = st.columns([0.5, 0.5]) | |
with col1: | |
st.markdown('<p style="text-align: center;">之前</p>', unsafe_allow_html=True) | |
st.video(temp_file.name) | |
with col2: | |
st.markdown('<p style="text-align: center;">之后</p>', unsafe_allow_html=True) | |
with st.spinner("上色中..."): | |
# Colorize video frames and store in a list | |
output_frames = [] | |
total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT)) | |
progress_bar = st.progress(0) # Create a progress bar | |
start_time = time.time() | |
time_text = st.text("剩餘時間:") # Initialize text value | |
for _ in tqdm(range(total_frames), unit='frame', desc="Progress"): | |
ret, frame = video.read() | |
if not ret: | |
break | |
colorized_frame = colorize_frame(frame, loaded_model) | |
output_frames.append((colorized_frame * 255).astype(np.uint8)) | |
elapsed_time = time.time() - start_time | |
frames_completed = len(output_frames) | |
frames_remaining = total_frames - frames_completed | |
time_remaining = (frames_remaining / frames_completed) * elapsed_time | |
progress_bar.progress(frames_completed / total_frames) # Update progress bar | |
if frames_completed < total_frames: | |
time_text.text(f"剩餘時間: {format_time(time_remaining)}") # Update text value | |
else: | |
time_text.empty() # Remove text value | |
progress_bar.empty() | |
with st.spinner("將幀合併到影片..."): | |
frame_size = output_frames[0].shape[:2] | |
output_filename = "output.mp4" | |
fourcc = cv2.VideoWriter_fourcc(*"mp4v") # Codec for MP4 video | |
out = cv2.VideoWriter(output_filename, fourcc, fps, (frame_size[1], frame_size[0])) | |
# Display the colorized video using st.video | |
for frame in output_frames: | |
frame_bgr = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR) | |
out.write(frame_bgr) | |
out.release() | |
# Convert the output video to a format compatible with Streamlit | |
converted_filename = "converted_output.mp4" | |
clip = mp.VideoFileClip(output_filename) | |
clip = clip.set_audio(audio) | |
clip.write_videofile(converted_filename, codec="libx264") | |
# Display the converted video using st.video() | |
st.video(converted_filename) | |
st.balloons() | |
# Add a download button for the colorized video | |
st.download_button( | |
label="下載彩色影片", | |
data=open(converted_filename, "rb").read(), | |
file_name="colorized_video.mp4" | |
) | |
# Close and delete the temporary file after processing | |
video.release() | |
temp_file.close() | |
if __name__ == "__main__": | |
main() | |