viveknarayan commited on
Commit
c7bc6df
β€’
1 Parent(s): d85678b

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +57 -0
app.py ADDED
@@ -0,0 +1,57 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ import cv2
3
+ import tensorflow as tf
4
+ from tensorflow import keras
5
+ from PIL import Image, ImageOps
6
+ net = cv2.dnn.readNetFromCaffe('colorization_deploy_v2.prototxt','drive/MyDrive/Lab2market2023/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
+
14
+ net.getLayer(class8).blobs = [pts.astype("float32")]
15
+ net.getLayer(conv8).blobs = [np.full([1,313],2.606,dtype='float32')]
16
+
17
+ def infer(original_image):
18
+ #image = cv2.imread('bw.jpg')
19
+ image = keras.preprocessing.image.img_to_array(original_image)
20
+ scaled = image.astype("float32")/255.0
21
+ lab = cv2.cvtColor(scaled,cv2.COLOR_BGR2LAB)
22
+ #cv2.imshow("image",lab)
23
+
24
+ resized = cv2.resize(lab,(224,224))
25
+ L = cv2.split(resized)[0]
26
+ L -= 50
27
+
28
+
29
+ net.setInput(cv2.dnn.blobFromImage(L))
30
+ ab = net.forward()[0, :, :, :].transpose((1,2,0))
31
+
32
+ ab = cv2.resize(ab, (image.shape[1],image.shape[0]))
33
+
34
+ L = cv2.split(lab)[0]
35
+ colorized = np.concatenate((L[:,:,np.newaxis], ab), axis=2)
36
+ colorized = cv2.cvtColor(colorized,cv2.COLOR_LAB2BGR)
37
+ colorized = np.clip(colorized,0,1)
38
+
39
+ colorized = (255 * colorized).astype("uint8")
40
+
41
+ cv2_imshow(image)
42
+ cv2_imshow(colorized)
43
+ color_coverted = cv2.cvtColor(colorized, cv2.COLOR_BGR2RGB)
44
+ colorized = Image.fromarray(color_coverted)
45
+ return colorized
46
+ cv2.waitKey(0)
47
+ import gradio as gr
48
+ examples=['bw.jpg','blw.jpg','boy.jpg']
49
+ iface = gr.Interface(
50
+ fn=infer,
51
+ title="Colourization",
52
+ description = "OpenCV implementation of Colorful Image Colorization paper presented in ECCV, 2016. πŸŒ†πŸŽ†",
53
+ inputs=[gr.inputs.Image(label="image", type="pil")],
54
+ outputs="image",
55
+ examples=examples,
56
+ cache_examples=True,
57
+ article = "Authors: <a href=\"https://github.com/Uviveknarayan\">Vivek Narayan</a>, <a href=\"https://github.com/chiranjan-7\">Chiranjan</a>,<a href=\"https://github.com/GangaSrujan\">Srujan</a>,<a href=\"https://github.com/RohanPawar3399\">Rohan Pawar</a>,<a href=\"https://github.com/pavankarthik77\">Pavan Karthik</a>").launch(enable_queue=True)