大家新年快樂啊,時隔多日,今天又來給博客添磚加瓦了,話不多說,我們先上效果圖吧 



圖片左邊部分是原始的黑白影像,右邊是生產的彩色圖片,
這個專案是基于在加利福尼亞大學,伯克利,Richard Zhang,Phillip Isola和Alexei A. Efros開發的研究作業--Colorful Image Colorization
論文地址:https://arxiv.org/pdf/1603.08511.pdf
作者專案github地址:https://github.com/richzhang/colorization/tree/caffe
本人也是在看了之后試了效果的,基于OpenCV DNN模塊給黑白老照片上色(附Python/C++原始碼) (qq.com)
https://mp.weixin.qq.com/s/04_UtnP-56MaoOI3d1MSsg
然后,我這添油加醋的寫了一個黑白視頻轉換為彩色視頻的腳本,上代碼
import numpy as np
import matplotlib.pyplot as plt
import cv2
print(cv2.__version__)
# Path of our caffemodel, prototxt, and numpy files
prototxt = "./model/colorization_deploy_v2.prototxt"
caffe_model = "./model/colorization_release_v2.caffemodel"
pts_npy = "./model/pts_in_hull.npy"
# Loading our model
net = cv2.dnn.readNetFromCaffe(prototxt, caffe_model)
pts = np.load(pts_npy)
layer1 = net.getLayerId("class8_ab")
print(layer1)
layer2 = net.getLayerId("conv8_313_rh")
print(layer2)
pts = pts.transpose().reshape(2, 313, 1, 1)
net.getLayer(layer1).blobs = [pts.astype("float32")]
net.getLayer(layer2).blobs = [np.full([1, 313], 2.606, dtype="float32")]
video_path = r'./heibai.mp4'
cap = cv2.VideoCapture(video_path)
fps = 24 #保存視頻的FPS,可以適當調整
fourcc = cv2.VideoWriter_fourcc(*'XVID')
videoWriter = cv2.VideoWriter('video.avi',fourcc,fps,(1296,486))#最后一個是保存圖片的尺寸
i = 0
while cap.isOpened():
fps = cap.get(cv2.CAP_PROP_FPS) # 回傳視頻的fps--幀率
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH) # 回傳視頻的寬
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT) # 回傳視頻的高
print('fps:', fps, 'width:', width, 'height:', height)
ret, ori_frame = cap.read() # 讀取一幀視頻
if ret:
ori_frame = cv2.cvtColor(ori_frame, cv2.COLOR_BGR2GRAY)
# Convert image from gray scale to RGB format
ori_frame = cv2.cvtColor(ori_frame, cv2.COLOR_GRAY2RGB)
normalized = ori_frame.astype("float32") / 255.0
# Converting the image into LAB
lab_image = cv2.cvtColor(normalized, cv2.COLOR_RGB2LAB)
# Resizing the image
resized = cv2.resize(lab_image, (224, 224))
# Extracting the value of L for LAB image
L = cv2.split(resized)[0]
L -= 50 # OR we can write L = L - 50
######inference######
net.setInput(cv2.dnn.blobFromImage(L))
# Finding the values of 'a' and 'b'
ab = net.forward()[0, :, :, :].transpose((1, 2, 0))
# Resizing
ab = cv2.resize(ab, (ori_frame.shape[1], ori_frame.shape[0]))
# Combining L, a, and b channels
L = cv2.split(lab_image)[0]
# Combining L,a,b
LAB_colored = np.concatenate((L[:, :, np.newaxis], ab), axis=2)
## Converting LAB image to RGB
RGB_colored = cv2.cvtColor(LAB_colored, cv2.COLOR_LAB2RGB)
# Limits the values in array
RGB_colored = np.clip(RGB_colored, 0, 1)
# Changing the pixel intensity back to [0,255],as we did scaling during pre-processing and converted the pixel intensity to [0,1]
RGB_colored = (255 * RGB_colored).astype("uint8")
RGB_BGR = cv2.cvtColor(RGB_colored, cv2.COLOR_RGB2BGR)
finall_image = cv2.hconcat((ori_frame,RGB_BGR))
videoWriter.write(finall_image)
else:
break
cap.release()
videoWriter.release()
可以看到的是,運行這個腳本需要先下載3個檔案

為了方便大家運行代碼,所以我給大家準備好了這3個檔案和測驗用的視頻,放在百度云盤了,大家自行下載,
鏈接:https://pan.baidu.com/s/1d7ZDRG8AAWJY8G56tbok2Q
提取碼:gudo
好了,最后在展示一下最后合成的視頻,上鏈接
video
video20222161652361
好了,老規矩,上咩咩圖,
至此,敬禮,salute!!!!!!

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/425030.html
標籤:AI
上一篇:資料分析流程(學習)
