|
import streamlit as st |
|
import numpy as np |
|
import cv2 |
|
import tempfile |
|
import os |
|
from io import BytesIO |
|
from matplotlib import pyplot as plt |
|
|
|
|
|
prototxt_path = "colorization_deploy_v2.prototxt" |
|
model_path = "colorization_release_v2.caffemodel" |
|
kernel_path = "pts_in_hull.npy" |
|
|
|
|
|
st.title("Video Colorization App") |
|
|
|
|
|
uploaded_video = st.file_uploader("Upload a black and white video", type=["mp4", "avi"]) |
|
|
|
if uploaded_video is not None: |
|
|
|
tfile = tempfile.NamedTemporaryFile(delete=False) |
|
tfile.write(uploaded_video.read()) |
|
video_path = tfile.name |
|
|
|
|
|
output_path = os.path.join(tempfile.gettempdir(), "colorized_video.mp4") |
|
|
|
|
|
net = cv2.dnn.readNetFromCaffe(prototxt_path, model_path) |
|
points = np.load(kernel_path) |
|
points = points.transpose().reshape(2, 313, 1, 1) |
|
net.getLayer(net.getLayerId("class8_ab")).blobs = [points.astype(np.float32)] |
|
net.getLayer(net.getLayerId("conv8_313_rh")).blobs = [np.full([1, 313], 2.686, dtype="float32")] |
|
|
|
|
|
cap = cv2.VideoCapture(video_path) |
|
|
|
|
|
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) |
|
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) |
|
fps = cap.get(cv2.CAP_PROP_FPS) |
|
|
|
|
|
fourcc = cv2.VideoWriter_fourcc(*"mp4v") |
|
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height)) |
|
|
|
frame_count = 0 |
|
stframe = st.empty() |
|
|
|
|
|
while True: |
|
ret, frame = cap.read() |
|
if not ret: |
|
break |
|
|
|
frame_count += 1 |
|
print(f"Frame: {frame_count}") |
|
|
|
normalized = frame.astype("float32") / 255.0 |
|
lab = cv2.cvtColor(normalized, cv2.COLOR_BGR2LAB) |
|
resized = cv2.resize(lab, (224, 224)) |
|
L = cv2.split(resized)[0] |
|
L -= 50 |
|
|
|
|
|
net.setInput(cv2.dnn.blobFromImage(L)) |
|
ab = net.forward()[0, :, :, :].transpose((1, 2, 0)) |
|
ab = cv2.resize(ab, (frame.shape[1], frame.shape[0])) |
|
|
|
|
|
L = cv2.split(lab)[0] |
|
colorized = np.concatenate((L[:, :, np.newaxis], ab), axis=2) |
|
colorized = cv2.cvtColor(colorized, cv2.COLOR_LAB2BGR) |
|
colorized = (255 * colorized).astype("uint8") |
|
|
|
|
|
|
|
|
|
out.write(colorized) |
|
|
|
|
|
cap.release() |
|
out.release() |
|
|
|
|
|
st.success("Video colorization completed!") |
|
with open(output_path, "rb") as file: |
|
btn = st.download_button(label="Download Colorized Video", data=file, file_name="colorized_video.mp4", mime="video/mp4") |
|
|