Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,51 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import cv2
|
3 |
+
import gradio as gr
|
4 |
+
|
5 |
+
print("loading models.....")
|
6 |
+
net = cv2.dnn.readNetFromCaffe('colorization_deploy_v2.prototxt','colorization_release_v2.caffemodel')
|
7 |
+
pts = np.load('pts_in_hull.npy')
|
8 |
+
|
9 |
+
class8 = net.getLayerId("class8_ab")
|
10 |
+
conv8 = net.getLayerId("conv8_313_rh")
|
11 |
+
pts = pts.transpose().reshape(2,313,1,1)
|
12 |
+
|
13 |
+
net.getLayer(class8).blobs = [pts.astype("float32")]
|
14 |
+
net.getLayer(conv8).blobs = [np.full([1,313],2.606,dtype='float32')]
|
15 |
+
|
16 |
+
|
17 |
+
def colorize_image(image):
|
18 |
+
# Convert the PIL image to OpenCV format
|
19 |
+
image = np.array(image)
|
20 |
+
image = cv2.cvtColor(image, cv2.COLOR_RGB2BGR)
|
21 |
+
|
22 |
+
scaled = image.astype("float32")/255.0
|
23 |
+
lab = cv2.cvtColor(scaled, cv2.COLOR_BGR2LAB)
|
24 |
+
|
25 |
+
resized = cv2.resize(lab, (224, 224))
|
26 |
+
L = cv2.split(resized)[0]
|
27 |
+
L -= 50
|
28 |
+
|
29 |
+
net.setInput(cv2.dnn.blobFromImage(L))
|
30 |
+
ab = net.forward()[0, :, :, :].transpose((1, 2, 0))
|
31 |
+
ab = cv2.resize(ab, (image.shape[1], image.shape[0]))
|
32 |
+
|
33 |
+
L = cv2.split(lab)[0]
|
34 |
+
colorized = np.concatenate((L[:, :, np.newaxis], ab), axis=2)
|
35 |
+
colorized = cv2.cvtColor(colorized, cv2.COLOR_LAB2RGB)
|
36 |
+
colorized = np.clip(colorized, 0, 1)
|
37 |
+
colorized = (255 * colorized).astype("uint8")
|
38 |
+
|
39 |
+
return colorized
|
40 |
+
|
41 |
+
|
42 |
+
demo = gr.Interface(
|
43 |
+
colorize_image,
|
44 |
+
gr.Image(type="pil",label='Upload a black and white image'),
|
45 |
+
"image",
|
46 |
+
title="Image Colorization",
|
47 |
+
examples = ["Landscape.jpg","nnl.jpg","bw.jpg"]
|
48 |
+
)
|
49 |
+
|
50 |
+
if __name__ == "__main__":
|
51 |
+
demo.launch()
|