Spaces:
Runtime error
Runtime error
BilalSardar
commited on
Commit
·
f96b4fd
1
Parent(s):
ae4a895
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,58 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
# Import statements
|
3 |
+
import numpy as np
|
4 |
+
import argparse
|
5 |
+
import cv2
|
6 |
+
import os
|
7 |
+
import gradio as gr
|
8 |
+
|
9 |
+
PROTOTXT = "colorization_deploy_v2.prototxt"
|
10 |
+
POINTS = "pts_in_hull.npy"
|
11 |
+
MODEL = "colorization_release_v2.caffemodel"
|
12 |
+
|
13 |
+
|
14 |
+
# Load the Model
|
15 |
+
print("Load model")
|
16 |
+
net = cv2.dnn.readNetFromCaffe(PROTOTXT, MODEL)
|
17 |
+
pts = np.load(POINTS)
|
18 |
+
|
19 |
+
# Load centers for ab channel quantization used for rebalancing.
|
20 |
+
class8 = net.getLayerId("class8_ab")
|
21 |
+
conv8 = net.getLayerId("conv8_313_rh")
|
22 |
+
pts = pts.transpose().reshape(2, 313, 1, 1)
|
23 |
+
net.getLayer(class8).blobs = [pts.astype("float32")]
|
24 |
+
net.getLayer(conv8).blobs = [np.full([1, 313], 2.606, dtype="float32")]
|
25 |
+
|
26 |
+
def colorizedTheImage(image):
|
27 |
+
# Load the input image
|
28 |
+
scaled = image.astype("float32") / 255.0
|
29 |
+
lab = cv2.cvtColor(scaled, cv2.COLOR_BGR2LAB)
|
30 |
+
|
31 |
+
resized = cv2.resize(lab, (224, 224))
|
32 |
+
L = cv2.split(resized)[0]
|
33 |
+
L -= 50
|
34 |
+
|
35 |
+
print("Colorizing the image")
|
36 |
+
net.setInput(cv2.dnn.blobFromImage(L))
|
37 |
+
ab = net.forward()[0, :, :, :].transpose((1, 2, 0))
|
38 |
+
|
39 |
+
ab = cv2.resize(ab, (image.shape[1], image.shape[0]))
|
40 |
+
|
41 |
+
L = cv2.split(lab)[0]
|
42 |
+
colorized = np.concatenate((L[:, :, np.newaxis], ab), axis=2)
|
43 |
+
|
44 |
+
colorized = cv2.cvtColor(colorized, cv2.COLOR_LAB2BGR)
|
45 |
+
colorized = np.clip(colorized, 0, 1)
|
46 |
+
|
47 |
+
colorized = (255 * colorized).astype("uint8")
|
48 |
+
return colorized
|
49 |
+
# image = cv2.resize(image, (0,0), fx=0.5, fy=0.5)
|
50 |
+
# colorized = cv2.resize(colorized, (0,0), fx=0.5, fy=0.5)
|
51 |
+
# cv2.imshow("Original", image)
|
52 |
+
# cv2.imshow("Colorized", colorized)
|
53 |
+
# cv2.waitKey(0)
|
54 |
+
demo=gr.Interface(fn=colorizedTheImage,
|
55 |
+
inputs=["image"],
|
56 |
+
outputs=["image"],
|
57 |
+
examples=[["einstein.jpg"],["tiger.jpg"]],
|
58 |
+
title="Black&White To Color Image")
|