计算机视觉实战之歪头变正脸
在有的时候,我们需要正面的脸。但是经常拍到的图是有点侧面的,比如这样的:
头稍微有点歪,看一下预期效果:
右边的脸是不是有点扳正的感觉了。
代码如下:
import cv2
import matplotlib.pyplot as plt
import dlib
import numpy as np
import time
#读取图片
res="C:\\Users\Administrator\\Pictures\\Saved Pictures\\56565.jpg"
dis="C:\\Users\Administrator\\Pictures\\Saved Pictures\\"+str(time.time())+".png"
bgrImg=cv2.imread(res)
image = cv2.cvtColor(bgrImg, cv2.COLOR_BGR2RGB)
#获取detector
detector=dlib.get_frontal_face_detector()
#加载模型文件
mode_path="E:\\BaiduNetdiskDownload\\shape_predictor_68_face_landmarks.dat"
shape_predictor = dlib.shape_predictor(mode_path)
dets =detector(image , 1)
#获取landmarks
for detection in dets:
face_landmarks = [(item.x, item.y) for item in shape_predictor(image , detection).parts()]
x= [i[0] for i in face_landmarks]
y=[i[1] for i in face_landmarks]
#画带识别的原始图
plt.plot(x,y,'ro')
plt.imshow(image)
plt.show()
这里采用的是dlib,这里画出来的效果如下:
紧接着我们分别画出来看看
lm = np.array(face_landmarks)
lm_chin = lm[0 : 17] # 脸轮廓
lm_eyebrow_left = lm[17 : 22] # 左眉毛
lm_eyebrow_right = lm[22 : 27] # 右眉毛
lm_nose = lm[27 : 31] # 鼻子主线
lm_nostrils = lm[31 : 36] # 鼻子下轮廓
lm_eye_left = lm[36 : 42] # 左眼
lm_eye_right = lm[42 : 48] # 右眼
lm_mouth_outer = lm[48 : 60] # 画嘴巴外轮廓
def plot_part(part):
plt.plot([i[0] for i in part],[i[1] for i in part],'ro')
plt.imshow(image)
plt.show()
#画脸轮廓
plot_part(lm_chin)
#画左眉毛
plot_part(lm_eyebrow_left)
#画右眉毛
plot_part(lm_eyebrow_right)
#画鼻子主线
plot_part(lm_nose)
#画鼻子下轮廓
plot_part(lm_nostrils)
#画左眼
plot_part(lm_eye_left)
#画右眼
plot_part(lm_eye_right)
#画嘴巴外轮廓
plot_part(lm_mouth_outer)
#画嘴巴内轮廓
plot_part(lm_mouth_inner)
然后计算辅助的向量并画图看看
# 计算一些辅助向量
eye_left = np.mean(lm_eye_left, axis=0)
eye_right = np.mean(lm_eye_right, axis=0)
eye_avg = (eye_left + eye_right) * 0.5
eye_to_eye = eye_right - eye_left#瞳距
mouth_left = lm_mouth_outer[0]
mouth_right = lm_mouth_outer[6]
mouth_avg = (mouth_left + mouth_right) * 0.5
eye_to_mouth = mouth_avg - eye_avg#眼睛到最的距离
def plot_mean(part):
plt.plot(part[0],part[1],'bo')
plt.imshow(image)
plt.show()
plot_mean(eye_left)
plot_mean(eye_right)
plot_mean(eye_avg)
plot_mean(mouth_left)
plot_mean(mouth_right)
plot_mean(mouth_avg)
这里利用上面的辅助向量进行计算,这里注意的是需要重新读图,因为我用之前cv2读的图会错,也许是自己bug
import PIL.Image
img = PIL.Image.open(res).convert('RGBA').convert('RGB')
# Shrink.
output_size=1024
shrink = int(np.floor(qsize / output_size * 0.5))
print(shrink)
if shrink > 1:
rsize = (int(np.rint(float(img.size[0]) / shrink)), int(np.rint(float(img.size[1]) / shrink)))
img = img.resize(rsize, PIL.Image.ANTIALIAS)
quad /= shrink
qsize /= shrink
# Crop.
border = max(int(np.rint(qsize * 0.1)), 3)
crop = (int(np.floor(min(quad[:,0]))), int(np.floor(min(quad[:,1]))), int(np.ceil(max(quad[:,0]))), int(np.ceil(max(quad[:,1]))))
print(crop)
crop = (max(crop[0] - border, 0), max(crop[1] - border, 0), min(crop[2] + border, img.size[0]), min(crop[3] + border, img.size[1]))
if crop[2] - crop[0] < img.size[0] or crop[3] - crop[1] < img.size[1]:
img = img.crop(crop)
quad -= crop[0:2]
# Pad.
import scipy.ndimage
enable_padding=True
alpha=False
pad = (int(np.floor(min(quad[:,0]))), int(np.floor(min(quad[:,1]))), int(np.ceil(max(quad[:,0]))), int(np.ceil(max(quad[:,1]))))
pad = (max(-pad[0] + border, 0), max(-pad[1] + border, 0), max(pad[2] - img.size[0] + border, 0), max(pad[3] - img.size[1] + border, 0))
if enable_padding and max(pad) > border - 4:
pad = np.maximum(pad, int(np.rint(qsize * 0.3)))
img = np.pad(np.float32(img), ((pad[1], pad[3]), (pad[0], pad[2]), (0, 0)), 'reflect')
h, w, _ = img.shape
y, x, _ = np.ogrid[:h, :w, :1]
mask = np.maximum(1.0 - np.minimum(np.float32(x) / pad[0], np.float32(w-1-x) / pad[2]), 1.0 - np.minimum(np.float32(y) / pad[1], np.float32(h-1-y) / pad[3]))
blur = qsize * 0.02
img += (scipy.ndimage.gaussian_filter(img, [blur, blur, 0]) - img) * np.clip(mask * 3.0 + 1.0, 0.0, 1.0)
img += (np.median(img, axis=(0,1)) - img) * np.clip(mask, 0.0, 1.0)
img = np.uint8(np.clip(np.rint(img), 0, 255))
if alpha:
mask = 1-np.clip(3.0 * mask, 0.0, 1.0)
mask = np.uint8(np.clip(np.rint(mask*255), 0, 255))
img = np.concatenate((img, mask), axis=2)
img = PIL.Image.fromarray(img, 'RGBA')
else:
img = PIL.Image.fromarray(img, 'RGB')
# Transform.
transform_size=4096
img = img.transform((transform_size, transform_size), PIL.Image.QUAD, (quad + 0.5).flatten(), PIL.Image.BILINEAR)
if output_size < transform_size:
img = img.resize((output_size, output_size), PIL.Image.ANTIALIAS)
img.save(dis, 'PNG')
小结:挨着代码拷贝执行,就会得到想要的歪头转成正脸
还没有评论,来说两句吧...