当前位置:网站首页>Opencv+dlib realizes "matching" glasses for Mona Lisa
Opencv+dlib realizes "matching" glasses for Mona Lisa
2022-07-06 09:07:00 【Guozhou questioner】
opencv+dlib To Mona Lisa “ with ” glasses
This case uses opencv+dlib To achieve the Mona Lisa wearing glasses .
The main principle is to use dlib Feature point extraction effect of face recognition , And use feature points to add glasses to the face .
Match Mona Lisa with glasses
Import toolkit
import cv2
import numpy as np
import dlib
from PIL import Image, ImageDraw, ImageFont
from imutils import face_utils, translate, rotate, resize
# Import python mapping matplotlib
import matplotlib.pyplot as plt
# Use ipython The magic method of , Embed the drawn image directly in notebook In the cell
%matplotlib inline
# Define visual image functions
def look_img(img):
'''opencv The format of the read image is BGR,matplotlib The visualization format is RGB, So we need to BGR turn RGB'''
img_RGB = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
plt.imshow(img_RGB)
plt.show()
Import model
# Create a face detector
det_face = dlib.get_frontal_face_detector()
# Load the marker detector
det_landmarks = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") # 68 spot
Single picture processing
max_width = 500
img=cv2.imread('mnls.jpg')
img=resize(img,width=max_width)
deal = Image.open("0.png") # Glasses pictures
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()
# A predictor used to detect the position and direction of the current face
shape = det_landmarks(img_gray, rect)
shape = face_utils.shape_to_np(shape)
# Capture the outline of each eye from the input image
leftEye = shape[36:42]
rightEye = shape[42:48]
# Calculate the center of each eye
leftEyeCenter = leftEye.mean(axis=0).astype("int")
rightEyeCenter = rightEye.mean(axis=0).astype("int")
# Calculate the included angle between the eyes
dY = leftEyeCenter[1] - rightEyeCenter[1]
dX = leftEyeCenter[0] - rightEyeCenter[0]
angle = np.rad2deg(np.arctan2(dY, dX))
# Picture rewriting
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 # Parameter adjustment
glasses_on=1 # Parameter adjustment
current_y = int(current_animation / glasses_on * left_eye_y)
img.paste(current_deal, (left_eye_x, current_y-20), current_deal) # Adjust the position of glasses
display(img)
Complete code
# Complete code :
import cv2
import numpy as np
import dlib
from PIL import Image, ImageDraw, ImageFont
from imutils import face_utils, translate, rotate, resize
# Import python mapping matplotlib
import matplotlib.pyplot as plt
# Use ipython The magic method of , Embed the drawn image directly in notebook In the cell
%matplotlib inline
# Define visual image functions
def look_img(img):
'''opencv The format of the read image is BGR,matplotlib The visualization format is RGB, So we need to BGR turn RGB'''
img_RGB = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
plt.imshow(img_RGB)
plt.show()
# Create a face detector
det_face = dlib.get_frontal_face_detector()
# Load the marker detector
det_landmarks = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat") # 68 spot
max_width = 500
img=cv2.imread('mnls.jpg') # Face photos
img=resize(img,width=max_width)
deal = Image.open("./Glasses/1.png") # Glasses pictures
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()
# A predictor used to detect the position and direction of the current face
shape = det_landmarks(img_gray, rect)
shape = face_utils.shape_to_np(shape)
# Capture the outline of each eye from the input image
leftEye = shape[36:42]
rightEye = shape[42:48]
# Calculate the center of each eye
leftEyeCenter = leftEye.mean(axis=0).astype("int")
rightEyeCenter = rightEye.mean(axis=0).astype("int")
# Calculate the included angle between the eyes
dY = leftEyeCenter[1] - rightEyeCenter[1]
dX = leftEyeCenter[0] - rightEyeCenter[0]
angle = np.rad2deg(np.arctan2(dY, dX))
# Picture rewriting
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 # Parameter adjustment
glasses_on=0.8 # Parameter adjustment
current_y = int(current_animation / glasses_on * left_eye_y)
img.paste(current_deal, (left_eye_x, current_y-20), current_deal) # Adjust the position of glasses
#PIL Image to CV2 Images
cv2_img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
look_img(cv2_img)
Picture synthesis 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' # Data folder
images=os.listdir(data_path)
img_paths=[]
for i in images:
img_paths+=[os.path.join(data_path,i)]
compose_gif()
Effect display :
边栏推荐
- Intel Distiller工具包-量化实现3
- ant-design的走马灯(Carousel)组件在TS(typescript)环境中调用prev以及next方法
- [OC foundation framework] - [set array]
- 力扣每日一题(二)
- Advanced Computer Network Review(3)——BBR
- Post training quantification of bminf
- Leetcode刷题题解2.1.1
- Philosophical enlightenment from single point to distributed
- [MySQL] limit implements paging
- pytorch查看张量占用内存大小
猜你喜欢
什么是MySQL?MySql的学习之路是怎样的
The ECU of 21 Audi q5l 45tfsi brushes is upgraded to master special adjustment, and the horsepower is safely and stably increased to 305 horsepower
Selenium+Pytest自动化测试框架实战(下)
Compétences en mémoire des graphiques UML
LeetCode:498. Diagonal traversal
Advanced Computer Network Review(3)——BBR
Selenium+pytest automated test framework practice (Part 2)
如何正确截取字符串(例:应用报错信息截取入库操作)
BMINF的後訓練量化實現
Improved deep embedded clustering with local structure preservation (Idec)
随机推荐
SimCLR:NLP中的对比学习
Variable length parameter
CUDA realizes focal_ loss
Problems encountered in connecting the database of the project and their solutions
SAP ui5 date type sap ui. model. type. Analysis of the parsing format of date
LeetCode:236. The nearest common ancestor of binary tree
Simple use of promise in uniapp
Different data-driven code executes the same test scenario
[sword finger offer] serialized binary tree
Super efficient! The secret of swagger Yapi
Mongodb installation and basic operation
LeetCode:387. The first unique character in the string
Advance Computer Network Review(1)——FatTree
MongoDB 的安装和基本操作
Intel distiller Toolkit - Quantitative implementation 3
【文本生成】论文合集推荐丨 斯坦福研究者引入时间控制方法 长文本生成更流畅
【剑指offer】序列化二叉树
Leetcode刷题题解2.1.1
LeetCode:673. Number of longest increasing subsequences
LeetCode:26. Remove duplicates from an ordered array