Sitemap
Geek Culture

A new tech publication by Start it up (https://medium.com/swlh).

Creating a Web App to Colorize Images and YouTube Videos

10 min readAug 7, 2023

--

Photo by Greyson Joralemon on Unsplash
Final application of this tutorial — GIF by author

Table of Contents:

Part 1: Create the Hugging Face Space to host the application

Screenshot of the Hugging Face interface for creating a Space — Image by author
Screenshot of the initial Space interface — Image by author

Part 2: Build the Web Application with Streamlit

1. Creating app.py file

git clone https://huggingface.co/spaces/Username/space_name

2. Interface premises

Application overview — GIF by author
pip install streamlit
pip install streamlit-lottie
import streamlit as st
import requests
from streamlit_lottie import st_lottie

# Define a function that we can use to load lottie files from a link.
@st.cache_data()
def load_lottieurl(url: str):
r = requests.get(url)
if r.status_code != 200:
return None
return r.json()

st.set_page_config(page_title="Image & Video Colorizer", page_icon="🎨", layout="wide")

col1, col2 = st.columns([1, 3])
with col1:
lottie = load_lottieurl("https://assets5.lottiefiles.com/packages/lf20_RHdEuzVfEL.json")
st_lottie(lottie)

with col2:
st.write("""
## B&W Videos Colorizer
##### Input a YouTube black and white video link and get a colorized version of it.
###### ➠ This space is using CPU Basic so it might take a while to colorize a video.
###### ➠ If you want more models and GPU available please support this space by donating.""")
pip freeze > requirements.txt

git add .
git commit -m "Add requirements.txt and other files"
git push

3. User Inputs for models & YouTube videos

model = st.selectbox(
"Select Model (Both models have their pros and cons, I recommend trying both and keeping the best for your task)",
["ECCV16", "SIGGRAPH17"], index=0)
@st.cache_resource()
def change_model(current_model, model):
if current_model != model:
if model == "ECCV16":
loaded_model = eccv16(pretrained=True).eval()
elif model == "SIGGRAPH17":
loaded_model = siggraph17(pretrained=True).eval()
return loaded_model
else:
raise Exception("Model is the same as the current one.")

loaded_model = change_model(current_model, model)
st.write(f"Model is now {model}")
Model Selection — GIF by the author
link = st.text_input("YouTube Link (The longer the video, the longer the processing time)")
if st.button("Colorize"):
# Add code to handle user inputs
Model Selection + YouTube link — GIF by author

4. Colorizing the video

pip install pytube
from pytube import YouTube

# [...]

@st.cache_data()
def download_video(link):
yt = YouTube(link)
video = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first().download(filename="video.mp4")
return video
if st.button("Colorize"):
yt_video = download_video(link)
col1, col2 = st.columns([0.5, 0.5])
with col1:
st.markdown('<p style="text-align: center;">Before</p>', unsafe_allow_html=True)
st.video(yt_video)
pip install moviepy
pip install opencv-python
import cv2
import moviepy.editor as mp

# [...]

with col2:
st.markdown('<p style="text-align: center;">After</p>', unsafe_allow_html=True)
with st.spinner("Colorizing frames..."):
# Colorize video frames and store them in a list
output_frames = []

audio = mp.AudioFileClip("video.mp4")
video = cv2.VideoCapture("video.mp4")

total_frames = int(video.get(cv2.CAP_PROP_FRAME_COUNT))
fps = video.get(cv2.CAP_PROP_FPS)

progress_bar = st.progress(0) # Create a progress bar
start_time = time.time()
time_text = st.text("Time Remaining: ") # 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"Time Remaining: {format_time(time_remaining)}") # Update text value
else:
time_text.empty() # Remove text value
progress_bar.empty()
with st.spinner("Merging frames to video..."):
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="Download Colorized Video",
data=open(converted_filename, "rb").read(),
file_name="colorized_video.mp4"
)

# Close and delete the temporary file after processing
video.release()
Final interface for the colorization of YouTube videos — GIF by author

Part 3: Add Multiple Pages to the Application

Project file structure after Part 2 — Image by author
Directory ‘pages’ created — Image by author
Application not working anymore — Image by author
---
title: Tutorial Medium
emoji: 🐢
colorFrom: blue
colorTo: blue
sdk: streamlit
sdk_version: 1.21.0
app_file: app.py
pinned: false
---

Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
Basic multipage web application on Hugging Face with Streamlit — Image by author
Pleasing naming of application pages — Image by author

Bonus: Customize Streamlit’s theme and file upload size

[theme]
primaryColor="#F63366"
backgroundColor="#FFFFFF"
secondaryBackgroundColor="#F0F2F6"
textColor="#262730"
font="sans serif"
[server]
maxUploadSize=1028

Conclusion

Want to connect?

I’ve also written:

References

--

--

Clément Delteil 🌱
Clément Delteil 🌱

Written by Clément Delteil 🌱

Machine Learning Engineer 🌱 | French CS Engineer | Canadian MSc in AI | Data is my anchor in exploring all realms 🌍📊 | linkedin.com/in/clementdelteil/

No responses yet