|
| 1 | +# Script is based on https://github.com/richzhang/colorization/colorize.py |
| 2 | +import numpy as np |
| 3 | +import argparse |
| 4 | +import cv2 as cv |
| 5 | + |
| 6 | +def parse_args(): |
| 7 | + parser = argparse.ArgumentParser(description='iColor: deep interactive colorization') |
| 8 | + parser.add_argument('--input', help='Path to image or video. Skip to capture frames from camera') |
| 9 | + parser.add_argument('--prototxt', help='Path to colorization_deploy_v2.prototxt', default='./models/colorization_release_v2.prototxt') |
| 10 | + parser.add_argument('--caffemodel', help='Path to colorization_release_v2.caffemodel', default='./models/colorization_release_v2.caffemodel') |
| 11 | + parser.add_argument('--kernel', help='Path to pts_in_hull.npy', default='./resources/pts_in_hull.npy') |
| 12 | + |
| 13 | + args = parser.parse_args() |
| 14 | + return args |
| 15 | + |
| 16 | +if __name__ == '__main__': |
| 17 | + W_in = 224 |
| 18 | + H_in = 224 |
| 19 | + imshowSize = (640, 480) |
| 20 | + |
| 21 | + args = parse_args() |
| 22 | + |
| 23 | + # Select desired model |
| 24 | + net = cv.dnn.readNetFromCaffe(args.prototxt, args.caffemodel) |
| 25 | + |
| 26 | + pts_in_hull = np.load(args.kernel) # load cluster centers |
| 27 | + |
| 28 | + # populate cluster centers as 1x1 convolution kernel |
| 29 | + pts_in_hull = pts_in_hull.transpose().reshape(2, 313, 1, 1) |
| 30 | + net.getLayer(long(net.getLayerId('class8_ab'))).blobs = [pts_in_hull.astype(np.float32)] |
| 31 | + net.getLayer(long(net.getLayerId('conv8_313_rh'))).blobs = [np.full([1, 313], 2.606, np.float32)] |
| 32 | + |
| 33 | + if args.input: |
| 34 | + cap = cv.VideoCapture(args.input) |
| 35 | + else: |
| 36 | + cap = cv.VideoCapture(0) |
| 37 | + |
| 38 | + while cv.waitKey(1) < 0: |
| 39 | + hasFrame, frame = cap.read() |
| 40 | + if not hasFrame: |
| 41 | + cv.waitKey() |
| 42 | + break |
| 43 | + |
| 44 | + img_rgb = (frame[:,:,[2, 1, 0]] * 1.0 / 255).astype(np.float32) |
| 45 | + |
| 46 | + img_lab = cv.cvtColor(img_rgb, cv.COLOR_RGB2Lab) |
| 47 | + img_l = img_lab[:,:,0] # pull out L channel |
| 48 | + (H_orig,W_orig) = img_rgb.shape[:2] # original image size |
| 49 | + |
| 50 | + # resize image to network input size |
| 51 | + img_rs = cv.resize(img_rgb, (W_in, H_in)) # resize image to network input size |
| 52 | + img_lab_rs = cv.cvtColor(img_rs, cv.COLOR_RGB2Lab) |
| 53 | + img_l_rs = img_lab_rs[:,:,0] |
| 54 | + img_l_rs -= 50 # subtract 50 for mean-centering |
| 55 | + |
| 56 | + net.setInput(cv.dnn.blobFromImage(img_l_rs)) |
| 57 | + ab_dec = net.forward('class8_ab')[0,:,:,:].transpose((1,2,0)) # this is our result |
| 58 | + |
| 59 | + (H_out,W_out) = ab_dec.shape[:2] |
| 60 | + ab_dec_us = cv.resize(ab_dec, (W_orig, H_orig)) |
| 61 | + img_lab_out = np.concatenate((img_l[:,:,np.newaxis],ab_dec_us),axis=2) # concatenate with original image L |
| 62 | + img_bgr_out = np.clip(cv.cvtColor(img_lab_out, cv.COLOR_Lab2BGR), 0, 1) |
| 63 | + |
| 64 | + frame = cv.resize(frame, imshowSize) |
| 65 | + cv.imshow('origin', frame) |
| 66 | + cv.imshow('gray', cv.cvtColor(frame, cv.COLOR_RGB2GRAY)) |
| 67 | + cv.imshow('colorized', cv.resize(img_bgr_out, imshowSize)) |
0 commit comments