当前位置:网站首页>opencv+dlib实现给蒙娜丽莎“配”眼镜
opencv+dlib实现给蒙娜丽莎“配”眼镜
2022-07-06 08:48:00 【果州做题家】
opencv+dlib实现给蒙娜丽莎“配”眼镜
本案例利用opencv+dlib实现了给蒙娜丽莎佩戴眼镜。
主要原理就是利用dlib人脸识别的特征点提取效果,并利用特征点给人脸加上一个眼镜。
给蒙诺丽莎配眼镜
导入工具包
import cv2
import numpy as np
import dlib
from PIL import Image, ImageDraw, ImageFont
from imutils import face_utils, translate, rotate, resize
#导入python绘图matplotlib
import matplotlib.pyplot as plt
#使用ipython的魔法方法,将绘制出的图像直接嵌入在notebook单元格中
%matplotlib inline
#定义可视化图像函数
def look_img(img):
'''opencv读入图像格式为BGR,matplotlib可视化格式为RGB,因此需将BGR转RGB'''
img_RGB = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
plt.imshow(img_RGB)
plt.show()
导入模型
# 创建人脸检测器
det_face = dlib.get_frontal_face_detector()
# 加载标志点检测器
det_landmarks = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") # 68点
单张图片处理
max_width = 500
img=cv2.imread('mnls.jpg')
img=resize(img,width=max_width)
deal = Image.open("0.png") #眼镜图片
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
rects = det_face(img_gray, 0)
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
for rect in rects:
face = {
}
shades_width = rect.right() - rect.left()
# 用于检测当前人脸所在位置方向的预测器
shape = det_landmarks(img_gray, rect)
shape = face_utils.shape_to_np(shape)
# 从输入图像中抓取每只眼睛的轮廓
leftEye = shape[36:42]
rightEye = shape[42:48]
# 计算每只眼睛的中心
leftEyeCenter = leftEye.mean(axis=0).astype("int")
rightEyeCenter = rightEye.mean(axis=0).astype("int")
# 计算眼心之间的夹角
dY = leftEyeCenter[1] - rightEyeCenter[1]
dX = leftEyeCenter[0] - rightEyeCenter[0]
angle = np.rad2deg(np.arctan2(dY, dX))
# 图片重写
current_deal = deal.resize((shades_width, int(shades_width * deal.size[1] / deal.size[0])),
resample=Image.Resampling.LANCZOS)
current_deal = current_deal.rotate(angle, expand=True)
current_deal = current_deal.transpose(Image.Transpose.FLIP_TOP_BOTTOM)
face['glasses_image'] = current_deal
left_eye_x = leftEye[0,0] - shades_width // 4
left_eye_y = leftEye[0,1] - shades_width // 6
face['final_pos'] = (left_eye_x, left_eye_y)
current_animation=1 #参数调节
glasses_on=1 #参数调节
current_y = int(current_animation / glasses_on * left_eye_y)
img.paste(current_deal, (left_eye_x, current_y-20), current_deal) #调节眼镜位置
display(img)
完整代码
# 完整代码:
import cv2
import numpy as np
import dlib
from PIL import Image, ImageDraw, ImageFont
from imutils import face_utils, translate, rotate, resize
#导入python绘图matplotlib
import matplotlib.pyplot as plt
#使用ipython的魔法方法,将绘制出的图像直接嵌入在notebook单元格中
%matplotlib inline
#定义可视化图像函数
def look_img(img):
'''opencv读入图像格式为BGR,matplotlib可视化格式为RGB,因此需将BGR转RGB'''
img_RGB = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
plt.imshow(img_RGB)
plt.show()
# 创建人脸检测器
det_face = dlib.get_frontal_face_detector()
# 加载标志点检测器
det_landmarks = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") # 68点
max_width = 500
img=cv2.imread('mnls.jpg') #人脸照片
img=resize(img,width=max_width)
deal = Image.open("./Glasses/1.png") #眼镜图片
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
rects = det_face(img_gray, 0)
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
for rect in rects:
face = {
}
shades_width = rect.right() - rect.left()
# 用于检测当前人脸所在位置方向的预测器
shape = det_landmarks(img_gray, rect)
shape = face_utils.shape_to_np(shape)
# 从输入图像中抓取每只眼睛的轮廓
leftEye = shape[36:42]
rightEye = shape[42:48]
# 计算每只眼睛的中心
leftEyeCenter = leftEye.mean(axis=0).astype("int")
rightEyeCenter = rightEye.mean(axis=0).astype("int")
# 计算眼心之间的夹角
dY = leftEyeCenter[1] - rightEyeCenter[1]
dX = leftEyeCenter[0] - rightEyeCenter[0]
angle = np.rad2deg(np.arctan2(dY, dX))
# 图片重写
current_deal = deal.resize((shades_width, int(shades_width * deal.size[1] / deal.size[0])),
resample=Image.Resampling.LANCZOS)
current_deal = current_deal.rotate(angle, expand=True)
current_deal = current_deal.transpose(Image.Transpose.FLIP_TOP_BOTTOM)
face['glasses_image'] = current_deal
left_eye_x = leftEye[0,0] - shades_width // 4
left_eye_y = leftEye[0,1] - shades_width // 6
face['final_pos'] = (left_eye_x, left_eye_y)
current_animation=1 #参数调节
glasses_on=0.8 #参数调节
current_y = int(current_animation / glasses_on * left_eye_y)
img.paste(current_deal, (left_eye_x, current_y-20), current_deal) #调节眼镜位置
#PIL图像转CV2图像
cv2_img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
look_img(cv2_img)
图片合成gif
import imageio
def compose_gif():
gif_images = []
for path in img_paths:
gif_images.append(imageio.imread(path))
imageio.mimsave("test.gif",gif_images,fps=1)
data_path='./output' #数据文件夹
images=os.listdir(data_path)
img_paths=[]
for i in images:
img_paths+=[os.path.join(data_path,i)]
compose_gif()
效果展示:
边栏推荐
猜你喜欢
FairGuard游戏加固:游戏出海热潮下,游戏安全面临新挑战
【ROS】usb_ Cam camera calibration
Swagger setting field required is mandatory
电脑清理,删除的系统文件
After PCD is converted to ply, it cannot be opened in meshlab, prompting error details: ignored EOF
What is CSRF (Cross Site Request Forgery)?
可变长参数
View computer devices in LAN
Process of obtaining the electronic version of academic qualifications of xuexin.com
Computer graduation design PHP Zhiduo online learning platform
随机推荐
visdom可视化实现与检查介绍
Unsupported operation exception
[embedded] cortex m4f DSP Library
个人电脑好用必备软件(使用过)
移位运算符
LeetCode:剑指 Offer 48. 最长不含重复字符的子字符串
R language ggplot2 visualization: place the title of the visualization image in the upper left corner of the image (customize Title position in top left of ggplot2 graph)
How to conduct interface test? What are the precautions? Nanny level interpretation
Charging interface docking tutorial of enterprise and micro service provider platform
vb.net 随窗口改变,缩放控件大小以及保持相对位置
Purpose of computer F1-F12
JS pure function
被破解毁掉的国产游戏之光
Research Report on supply and demand and development prospects of China's high purity aluminum market (2022 Edition)
Sublime text in CONDA environment plt Show cannot pop up the problem of displaying pictures
704 binary search
Excellent software testers have these abilities
win10系统中的截图,win+prtSc保存位置
Roguelike game into crack the hardest hit areas, how to break the bureau?
Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges