-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathVideoColorizer.py
More file actions
96 lines (82 loc) · 3.6 KB
/
Copy pathVideoColorizer.py
File metadata and controls
96 lines (82 loc) · 3.6 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# USAGE
# python bw2color_video.py --prototxt model/colorization_deploy_v2.prototxt --model model/colorization_release_v2.caffemodel --points model/pts_in_hull.npy
# import the necessary packages
from imutils.video import VideoStream
import numpy as np
import argparse
import imutils
import time
import cv2
# construct the argument parser and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--input", type=str,
help="path to optional input video (webcam will be used otherwise)")
ap.add_argument("-p", "--prototxt", type=str, required=True,
help="path to Caffe prototxt file")
ap.add_argument("-m", "--model", type=str, required=True,
help="path to Caffe pre-trained model")
ap.add_argument("-c", "--points", type=str, required=True,
help="path to cluster center points")
ap.add_argument("-w", "--width", type=int, default=500,
help="input width dimension of frame")
args = vars(ap.parse_args())
print("[INFO] opening video file...")
vs = cv2.VideoCapture('video//GrayscaleVideo.mp4')
# load our serialized black and white colorizer model and cluster
# center points from disk
print("[INFO] loading model...")
net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])
pts = np.load(args["points"])
# add the cluster centers as 1x1 convolutions to the model
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")]
# loop over frames from the video stream
count = 1
while True:
# grab the next frame and handle if we are reading from either
# VideoCapture or VideoStream
frame = vs.read()
frame = frame[1]
# if we are viewing a video and we did not grab a frame then we
# have reached the end of the video
# resize the input frame, scale the pixel intensities to the
# range [0, 1], and then convert the frame from the BGR to Lab
# color space
frame = imutils.resize(frame, width=args["width"])
scaled = frame.astype("float32") / 255.0
lab = cv2.cvtColor(scaled, cv2.COLOR_BGR2LAB)
# resize the Lab frame 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
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 frame, then grab the 'L' channel from the *original* input
# frame (not the resized one) and concatenate the original 'L'
# channel with the predicted 'ab' channels
ab = cv2.resize(ab, (frame.shape[1], frame.shape[0]))
L = cv2.split(lab)[0]
colorized = np.concatenate((L[:, :, np.newaxis], ab), axis=2)
# convert the output frame from the Lab color space to RGB, clip
# any values that fall outside the range [0, 1], and then convert
# to an 8-bit unsigned integer ([0, 255] range)
colorized = cv2.cvtColor(colorized, cv2.COLOR_LAB2BGR)
colorized = np.clip(colorized, 0, 1)
colorized = (255 * colorized).astype("uint8")
# show the original and final colorized frames
cv2.imwrite("video//images//image%d.png" % count, colorized)
key = cv2.waitKey(1) & 0xFF
count += 1
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break
# close any open windows
#cv2.destroyAllWindows()