当前位置:网站首页>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 :
边栏推荐
- LeetCode:26. 删除有序数组中的重复项
- 多元聚类分析
- I-BERT
- Leetcode刷题题解2.1.1
- LeetCode:221. 最大正方形
- LeetCode:26. Remove duplicates from an ordered array
- Intel distiller Toolkit - Quantitative implementation 2
- [OC]-<UI入门>--常用控件-UIButton
- Revit secondary development Hof method calls transaction
- SAP ui5 date type sap ui. model. type. Analysis of the parsing format of date
猜你喜欢
I-BERT
[OC-Foundation框架]-<字符串And日期与时间>
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
UML圖記憶技巧
数学建模2004B题(输电问题)
Promise 在uniapp的简单使用
【图的三大存储方式】只会用邻接矩阵就out了
I-BERT
Navicat premium create MySQL create stored procedure
Variable length parameter
随机推荐
【剑指offer】序列化二叉树
Intel distiller Toolkit - Quantitative implementation 3
Leetcode: Sword Finger offer 42. Somme maximale des sous - tableaux consécutifs
Li Kou daily question 1 (2)
IJCAI2022论文合集(持续更新中)
I-BERT
Tdengine biweekly selection of community issues | phase III
BMINF的后训练量化实现
[embedded] cortex m4f DSP Library
LeetCode:34. 在排序数组中查找元素的第一个和最后一个位置
In depth analysis and encapsulation call of requests
Export IEEE document format using latex
LeetCode:39. 组合总和
LeetCode:836. 矩形重叠
Leetcode刷题题解2.1.1
Leetcode: Sword finger offer 42 Maximum sum of continuous subarrays
一改测试步骤代码就全写 为什么不试试用 Yaml实现数据驱动?
Intel Distiller工具包-量化实现3
Revit secondary development Hof method calls transaction
SAP ui5 date type sap ui. model. type. Analysis of the parsing format of date