import numpy as np
import cv2
import tensorflow as tf
from tensorflow import keras
from PIL import Image, ImageOps
net = cv2.dnn.readNetFromCaffe('colorization_deploy_v2.prototxt','colorization_release_v2.caffemodel')
pts = np.load('pts_in_hull.npy')
class8 = net.getLayerId("class8_ab")
conv8 = net.getLayerId("conv8_313_rh")
pts = pts.transpose().reshape(2,313,1,1)
net.getLayer(class8).blobs = [pts.astype("float32")]
net.getLayer(conv8).blobs = [np.full([1,313],2.606,dtype='float32')]
def infer(original_image):
#image = cv2.imread('bw.jpg')
image = keras.preprocessing.image.img_to_array(original_image)
scaled = image.astype("float32")/255.0
lab = cv2.cvtColor(scaled,cv2.COLOR_BGR2LAB)
#cv2.imshow("image",lab)
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, (image.shape[1],image.shape[0]))
L = cv2.split(lab)[0]
colorized = np.concatenate((L[:,:,np.newaxis], ab), axis=2)
colorized = cv2.cvtColor(colorized,cv2.COLOR_LAB2BGR)
colorized = np.clip(colorized,0,1)
colorized = (255 * colorized).astype("uint8")
#cv2_imshow(image)
#cv2_imshow(colorized)
color_coverted = cv2.cvtColor(colorized, cv2.COLOR_BGR2RGB)
colorized = Image.fromarray(color_coverted)
return colorized
cv2.waitKey(0)
import gradio as gr
examples=['bw.jpg','blw.jpg','boy.jpg']
iface = gr.Interface(
fn=infer,
title="Colourization",
description = "OpenCV implementation of Colorful Image Colorization paper presented in ECCV, 2016. 🌆🎆",
inputs=[gr.components.Image(label="image", type="pil")],
outputs="image",
examples=examples,
cache_examples=True,
article = "Authors: Vivek Narayan, Chiranjan,Srujan,Rohan Pawar,Pavan Karthik")
iface.queue(max_size=100)
iface.launch()