当前位置:网站首页>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 :
边栏推荐
猜你喜欢
Booking of tourism products in Gansu quadrupled: "green horse" became popular, and one room of B & B around Gansu museum was hard to find
[OC foundation framework] - string and date and time >
甘肃旅游产品预订增四倍:“绿马”走红,甘肃博物馆周边民宿一房难求
Navicat Premium 创建MySql 创建存储过程
CUDA implementation of self defined convolution attention operator
IJCAI2022论文合集(持续更新中)
CUDA实现focal_loss
[OC]-<UI入门>--常用控件-提示对话框 And 等待提示器(圈)
SimCLR:NLP中的对比学习
Once you change the test steps, write all the code. Why not try yaml to realize data-driven?
随机推荐
Philosophical enlightenment from single point to distributed
Super efficient! The secret of swagger Yapi
Show slave status \ read in G_ Master_ Log_ POS and relay_ Log_ The (size) relationship of POS
七层网络体系结构
注意力机制的一种卷积替代方式
pytorch查看张量占用内存大小
MySQL uninstallation and installation methods
Using C language to complete a simple calculator (function pointer array and callback function)
Once you change the test steps, write all the code. Why not try yaml to realize data-driven?
MYSQL卸载方法与安装方法
[Hacker News Weekly] data visualization artifact; Top 10 Web hacker technologies; Postman supports grpc
[oc]- < getting started with UI> -- common controls uibutton
TDengine 社区问题双周精选 | 第三期
KDD 2022 paper collection (under continuous update)
requests的深入刨析及封装调用
After reading the programmer's story, I can't help covering my chest...
[today in history] February 13: the father of transistors was born The 20th anniversary of net; Agile software development manifesto was born
LeetCode:剑指 Offer 42. 连续子数组的最大和
LeetCode:剑指 Offer 04. 二维数组中的查找
LeetCode:498. Diagonal traversal