当前位置:网站首页>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()
效果展示:
边栏推荐
- sublime text没关闭其他运行就使用CTRL+b运行另外的程序问题
- LeetCode:836. 矩形重叠
- Sublime text using ctrl+b to run another program without closing other runs
- 同一局域网的手机和电脑相互访问,IIS设置
- Generator parameters incoming parameters
- Fairguard game reinforcement: under the upsurge of game going to sea, game security is facing new challenges
- 按位逻辑运算符
- pcd转ply后在meshlab无法打开,提示 Error details: Unespected eof
- POI add write excel file
- LeetCode:劍指 Offer 42. 連續子數組的最大和
猜你喜欢
pcd转ply后在meshlab无法打开,提示 Error details: Unespected eof
[embedded] print log using JLINK RTT
Computer cleaning, deleted system files
Mobile phones and computers on the same LAN access each other, IIS settings
Deep analysis of C language data storage in memory
sublime text的编写程序时的Tab和空格缩进问题
Charging interface docking tutorial of enterprise and micro service provider platform
C language double pointer -- classic question type
Delay initialization and sealing classes
Trying to use is on a network resource that is unavailable
随机推荐
被破解毁掉的国产游戏之光
Hutool gracefully parses URL links and obtains parameters
Mobile phones and computers on the same LAN access each other, IIS settings
LeetCode:221. 最大正方形
Niuke winter vacation training 6 maze 2
Process of obtaining the electronic version of academic qualifications of xuexin.com
@Jsonbackreference and @jsonmanagedreference (solve infinite recursion caused by bidirectional references in objects)
Navicat premium create MySQL create stored procedure
Rviz仿真时遇到机器人瞬间回到世界坐标原点的问题及可能原因
LeetCode:劍指 Offer 42. 連續子數組的最大和
The harm of game unpacking and the importance of resource encryption
LeetCode:41. 缺失的第一个正数
Research Report on supply and demand and development prospects of China's high purity aluminum market (2022 Edition)
pytorch查看张量占用内存大小
POI add write excel file
Excellent software testers have these abilities
Unsupported operation exception
【嵌入式】使用JLINK RTT打印log
UnsupportedOperationException异常
gcc动态库fPIC和fpic编译选项差异介绍