-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
67 lines (52 loc) · 2.17 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import numpy as np
import cv2
import pickle
print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe("./model/model/colorization_deploy_v2.prototxt","./model/model/colorization_release_v2.caffeModel")
pts= np.load("./model/model/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")]
image = cv2.imread("./model/test8.jpg")
scaled = image.astype("float32") / 255.0
lab = cv2.cvtColor(scaled, cv2.COLOR_BGR2LAB)
# resize the Lab image to 224x224 (the dimensions the colorization
# network accepts), split channels, extract the 'L' channel, and then
# perform mean centering
resized = cv2.resize(lab, (224, 224))
L = cv2.split(resized)[0]
L -= 50
# pass the L channel through the network which will *predict* the 'a'
# and 'b' channel values
'print("[INFO] colorizing image...")'
net.setInput(cv2.dnn.blobFromImage(L))
ab = net.forward()[0, :, :, :].transpose((1, 2, 0))
# resize the predicted 'ab' volume to the same dimensions as our
# input image
ab = cv2.resize(ab, (image.shape[1], image.shape[0]))
# grab the 'L' channel from the *original* input image (not the
# resized one) and concatenate the original 'L' channel with the
# predicted 'ab' channels
L = cv2.split(lab)[0]
colorized = np.concatenate((L[:, :, np.newaxis], ab), axis=2)
# convert the output image from the Lab color space to RGB, then
# clip any values that fall outside the range [0, 1]
colorized = cv2.cvtColor(colorized, cv2.COLOR_LAB2BGR)
colorized = np.clip(colorized, 0, 1)
# the current colorized image is represented as a floating point
# data type in the range [0, 1] -- let's convert to an unsigned
# 8-bit integer representation in the range [0, 255]
colorized = (255 * colorized).astype("uint8")
# show the original and output colorized images
# cv2.imshow("Original", image)
image_final=cv2.imshow("Colorized", colorized)
import os
directory="./model/"
os.chdir(directory)
file="coloured8.jpg"
cv2.imwrite(file,colorized)
cv2.waitKey(0)
# import anvil.server
# anvil.server.connect("4SLRBQ3JKSJ2JQYHYBPSKSL2-LLEPCAEWLSZJ4YFA")