Update app.py
Browse files
app.py
CHANGED
@@ -1,62 +1,62 @@
|
|
1 |
-
import streamlit as st
|
2 |
-
import numpy as np
|
3 |
-
import cv2
|
4 |
-
from PIL import Image
|
5 |
-
|
6 |
-
# Load model files
|
7 |
-
prototxt_path = "colorization_deploy_v2.prototxt"
|
8 |
-
model_path = "colorization_release_v2.caffemodel"
|
9 |
-
kernel_path = "pts_in_hull.npy"
|
10 |
-
|
11 |
-
# Load the model
|
12 |
-
net = cv2.dnn.readNetFromCaffe(prototxt_path, model_path)
|
13 |
-
points = np.load(kernel_path)
|
14 |
-
points = points.transpose().reshape(2, 313, 1, 1)
|
15 |
-
net.getLayer(net.getLayerId("class8_ab")).blobs = [points.astype(np.float32)]
|
16 |
-
net.getLayer(net.getLayerId("conv8_313_rh")).blobs = [np.full([1, 313], 2.686, dtype="float32")]
|
17 |
-
|
18 |
-
# Streamlit App
|
19 |
-
st.title("Black-and-White Image Colorization")
|
20 |
-
st.write("Upload a black-and-white image to colorize it.")
|
21 |
-
|
22 |
-
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
23 |
-
|
24 |
-
if uploaded_file is not None:
|
25 |
-
# Convert uploaded file to OpenCV format
|
26 |
-
image = Image.open(uploaded_file)
|
27 |
-
bw_image = np.array(image.convert("RGB"))
|
28 |
-
bw_image = cv2.cvtColor(bw_image, cv2.COLOR_RGB2BGR)
|
29 |
-
|
30 |
-
# Preprocessing for colorization
|
31 |
-
normalized = bw_image.astype("float32") / 255.0
|
32 |
-
lab = cv2.cvtColor(normalized, cv2.COLOR_BGR2LAB)
|
33 |
-
resized = cv2.resize(lab, (224, 224))
|
34 |
-
L = cv2.split(resized)[0]
|
35 |
-
L -=
|
36 |
-
|
37 |
-
# Predict color channels
|
38 |
-
net.setInput(cv2.dnn.blobFromImage(L))
|
39 |
-
ab = net.forward()[0, :, :, :].transpose((1, 2, 0))
|
40 |
-
ab = cv2.resize(ab, (bw_image.shape[1], bw_image.shape[0]))
|
41 |
-
|
42 |
-
# Combine L and ab channels
|
43 |
-
L = cv2.split(lab)[0]
|
44 |
-
colorized = np.concatenate((L[:, :, np.newaxis], ab), axis=2)
|
45 |
-
|
46 |
-
# Convert LAB to BGR
|
47 |
-
colorized = cv2.cvtColor(colorized, cv2.COLOR_LAB2BGR)
|
48 |
-
colorized = (255 * colorized).astype("uint8")
|
49 |
-
|
50 |
-
# Display results
|
51 |
-
st.image(colorized, channels="BGR", caption="Colorized Image")
|
52 |
-
|
53 |
-
# Provide download link
|
54 |
-
colorized_image = Image.fromarray(cv2.cvtColor(colorized, cv2.COLOR_BGR2RGB))
|
55 |
-
colorized_image.save("colorized_output.jpg")
|
56 |
-
with open("colorized_output.jpg", "rb") as file:
|
57 |
-
btn = st.download_button(
|
58 |
-
label="Download colorized image",
|
59 |
-
data=file,
|
60 |
-
file_name="colorized_image.jpg",
|
61 |
-
mime="image/jpeg"
|
62 |
-
)
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import numpy as np
|
3 |
+
import cv2
|
4 |
+
from PIL import Image
|
5 |
+
|
6 |
+
# Load model files
|
7 |
+
prototxt_path = "colorization_deploy_v2.prototxt"
|
8 |
+
model_path = "colorization_release_v2.caffemodel"
|
9 |
+
kernel_path = "pts_in_hull.npy"
|
10 |
+
|
11 |
+
# Load the model
|
12 |
+
net = cv2.dnn.readNetFromCaffe(prototxt_path, model_path)
|
13 |
+
points = np.load(kernel_path)
|
14 |
+
points = points.transpose().reshape(2, 313, 1, 1)
|
15 |
+
net.getLayer(net.getLayerId("class8_ab")).blobs = [points.astype(np.float32)]
|
16 |
+
net.getLayer(net.getLayerId("conv8_313_rh")).blobs = [np.full([1, 313], 2.686, dtype="float32")]
|
17 |
+
|
18 |
+
# Streamlit App
|
19 |
+
st.title("Black-and-White Image Colorization")
|
20 |
+
st.write("Upload a black-and-white image to colorize it.")
|
21 |
+
|
22 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
23 |
+
|
24 |
+
if uploaded_file is not None:
|
25 |
+
# Convert uploaded file to OpenCV format
|
26 |
+
image = Image.open(uploaded_file)
|
27 |
+
bw_image = np.array(image.convert("RGB"))
|
28 |
+
bw_image = cv2.cvtColor(bw_image, cv2.COLOR_RGB2BGR)
|
29 |
+
|
30 |
+
# Preprocessing for colorization
|
31 |
+
normalized = bw_image.astype("float32") / 255.0
|
32 |
+
lab = cv2.cvtColor(normalized, cv2.COLOR_BGR2LAB)
|
33 |
+
resized = cv2.resize(lab, (224, 224))
|
34 |
+
L = cv2.split(resized)[0]
|
35 |
+
L -= 50
|
36 |
+
|
37 |
+
# Predict color channels
|
38 |
+
net.setInput(cv2.dnn.blobFromImage(L))
|
39 |
+
ab = net.forward()[0, :, :, :].transpose((1, 2, 0))
|
40 |
+
ab = cv2.resize(ab, (bw_image.shape[1], bw_image.shape[0]))
|
41 |
+
|
42 |
+
# Combine L and ab channels
|
43 |
+
L = cv2.split(lab)[0]
|
44 |
+
colorized = np.concatenate((L[:, :, np.newaxis], ab), axis=2)
|
45 |
+
|
46 |
+
# Convert LAB to BGR
|
47 |
+
colorized = cv2.cvtColor(colorized, cv2.COLOR_LAB2BGR)
|
48 |
+
colorized = (255 * colorized).astype("uint8")
|
49 |
+
|
50 |
+
# Display results
|
51 |
+
st.image(colorized, channels="BGR", caption="Colorized Image")
|
52 |
+
|
53 |
+
# Provide download link
|
54 |
+
colorized_image = Image.fromarray(cv2.cvtColor(colorized, cv2.COLOR_BGR2RGB))
|
55 |
+
colorized_image.save("colorized_output.jpg")
|
56 |
+
with open("colorized_output.jpg", "rb") as file:
|
57 |
+
btn = st.download_button(
|
58 |
+
label="Download colorized image",
|
59 |
+
data=file,
|
60 |
+
file_name="colorized_image.jpg",
|
61 |
+
mime="image/jpeg"
|
62 |
+
)
|