colorization.py 2.9 KB

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