Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,89 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
import cv2
|
4 |
+
import tempfile
|
5 |
+
import os
|
6 |
+
from io import BytesIO
|
7 |
+
from matplotlib import pyplot as plt
|
8 |
+
|
9 |
+
# Load model files
|
10 |
+
prototxt_path = "colorization_deploy_v2.prototxt"
|
11 |
+
model_path = "colorization_release_v2.caffemodel"
|
12 |
+
kernel_path = "pts_in_hull.npy"
|
13 |
+
|
14 |
+
# Streamlit app title
|
15 |
+
st.title("Video Colorization App")
|
16 |
+
|
17 |
+
# File upload
|
18 |
+
uploaded_video = st.file_uploader("Upload a black and white video", type=["mp4", "avi"])
|
19 |
+
|
20 |
+
if uploaded_video is not None:
|
21 |
+
# Save uploaded video to a temporary file
|
22 |
+
tfile = tempfile.NamedTemporaryFile(delete=False)
|
23 |
+
tfile.write(uploaded_video.read())
|
24 |
+
video_path = tfile.name
|
25 |
+
|
26 |
+
# Output path for the colorized video
|
27 |
+
output_path = os.path.join(tempfile.gettempdir(), "colorized_video.mp4")
|
28 |
+
|
29 |
+
# Load the pre-trained model
|
30 |
+
net = cv2.dnn.readNetFromCaffe(prototxt_path, model_path)
|
31 |
+
points = np.load(kernel_path)
|
32 |
+
points = points.transpose().reshape(2, 313, 1, 1)
|
33 |
+
net.getLayer(net.getLayerId("class8_ab")).blobs = [points.astype(np.float32)]
|
34 |
+
net.getLayer(net.getLayerId("conv8_313_rh")).blobs = [np.full([1, 313], 2.686, dtype="float32")]
|
35 |
+
|
36 |
+
# Open the video file
|
37 |
+
cap = cv2.VideoCapture(video_path)
|
38 |
+
|
39 |
+
# Get video properties
|
40 |
+
frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
|
41 |
+
frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
|
42 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
43 |
+
|
44 |
+
# Create a VideoWriter object to save the colorized video
|
45 |
+
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
|
46 |
+
out = cv2.VideoWriter(output_path, fourcc, fps, (frame_width, frame_height))
|
47 |
+
|
48 |
+
frame_count = 0
|
49 |
+
stframe = st.empty() # Placeholder for showing frames in Streamlit
|
50 |
+
|
51 |
+
# Process each frame
|
52 |
+
while True:
|
53 |
+
ret, frame = cap.read()
|
54 |
+
if not ret:
|
55 |
+
break
|
56 |
+
|
57 |
+
frame_count += 1
|
58 |
+
print(f"Frame: {frame_count}")
|
59 |
+
# Convert frame to LAB color space and preprocess
|
60 |
+
normalized = frame.astype("float32") / 255.0
|
61 |
+
lab = cv2.cvtColor(normalized, cv2.COLOR_BGR2LAB)
|
62 |
+
resized = cv2.resize(lab, (224, 224))
|
63 |
+
L = cv2.split(resized)[0]
|
64 |
+
L -= 50
|
65 |
+
|
66 |
+
# Set the input and get the colorization
|
67 |
+
net.setInput(cv2.dnn.blobFromImage(L))
|
68 |
+
ab = net.forward()[0, :, :, :].transpose((1, 2, 0))
|
69 |
+
ab = cv2.resize(ab, (frame.shape[1], frame.shape[0]))
|
70 |
+
|
71 |
+
# Combine with the L channel
|
72 |
+
L = cv2.split(lab)[0]
|
73 |
+
colorized = np.concatenate((L[:, :, np.newaxis], ab), axis=2)
|
74 |
+
colorized = cv2.cvtColor(colorized, cv2.COLOR_LAB2BGR)
|
75 |
+
colorized = (255 * colorized).astype("uint8")
|
76 |
+
|
77 |
+
|
78 |
+
|
79 |
+
# Write colorized frame to output
|
80 |
+
out.write(colorized)
|
81 |
+
|
82 |
+
# Release resources
|
83 |
+
cap.release()
|
84 |
+
out.release()
|
85 |
+
|
86 |
+
# Provide a download link for the colorized video
|
87 |
+
st.success("Video colorization completed!")
|
88 |
+
with open(output_path, "rb") as file:
|
89 |
+
btn = st.download_button(label="Download Colorized Video", data=file, file_name="colorized_video.mp4", mime="video/mp4")
|