当前位置:网站首页>Dlib library for face key point detection (openCV Implementation)
Dlib library for face key point detection (openCV Implementation)
2022-06-30 18:50:00 【Keep_ Trying_ Go】
List of articles
- 1.dlib Realize real-time face detection
- 2.dlib The method of detecting human face is adopted 68 A key point
- 3. Download of relevant documents
- 4. Code combat
- (1) Import library
- (2) Integration of face key points
- (3) load dlib Face detection and face key point detection files of the database
- (4) Frame face
- (5) Rearrange the coordinates of the detected face key points
- (6) Draw points on the key points of the face
- (7) The overall code
- (9) Face key point detection of a single image
1.dlib Realize real-time face detection
https://mydreamambitious.blog.csdn.net/article/details/124851743
2.dlib The method of detecting human face is adopted 68 A key point

3. Download of relevant documents

Download address :http://dlib.net/files/
4. Code combat
(1) Import library
import os
import cv2
import dlib
import numpy as np
from collections import OrderedDict
(2) Integration of face key points
# about 68 Test points , Arrange several key points of the face in order , For later traversal
shape_predictor_68_face_landmark=OrderedDict([
('mouth',(48,68)),
('right_eyebrow',(17,22)),
('left_eye_brow',(22,27)),
('right_eye',(36,42)),
('left_eye',(42,48)),
('nose',(27,36)),
('jaw',(0,17))
])
(3) load dlib Face detection and face key point detection files of the database
# Load face detection and key point location
#http://dlib.net/python/index.html#dlib_pybind11.get_frontal_face_detector
detector = dlib.get_frontal_face_detector()
#http://dlib.net/python/index.html#dlib_pybind11.shape_predictor
criticPoints = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
(4) Frame face
# Draw face draw rectangle
def drawRectangle(detected,frame):
margin = 0.2
img_h,img_w,_=np.shape(frame)
if len(detected) > 0:
for i, locate in enumerate(detected):
x1, y1, x2, y2, w, h = locate.left(), locate.top(), locate.right() + 1, locate.bottom() + 1, locate.width(), locate.height()
xw1 = max(int(x1 - margin * w), 0)
yw1 = max(int(y1 - margin * h), 0)
xw2 = min(int(x2 + margin * w), img_w - 1)
yw2 = min(int(y2 + margin * h), img_h - 1)
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
face = frame[yw1:yw2 + 1, xw1:xw2 + 1, :]
cv2.putText(frame, 'Person', (locate.left(), locate.top() - 10),
cv2.FONT_HERSHEY_SIMPLEX, 1.2, (255, 0, 0), 3)
return frame
(5) Rearrange the coordinates of the detected face key points
# The face key point coordinates obtained after detection are transformed
def predict2Np(predict):
# establish 68*2 Two dimensional empty array of keys [(x1,y1),(x2,y2)……]
dims=np.zeros(shape=(predict.num_parts,2),dtype=np.int)
# Traverse each key point of the face to obtain two-dimensional coordinates
length=predict.num_parts
for i in range(0,length):
dims[i]=(predict.part(i).x,predict.part(i).y)
return dims
(6) Draw points on the key points of the face
# Traverse the prediction box , Draw the key points of the face
def drawCriticPoints(detected,frame):
for (step,locate) in enumerate(detected):
# Face key point detection is performed on the acquired face frame
# obtain 68 Coordinate values of key points
dims=criticPoints(frame,locate)
# Convert the obtained coordinate values into two dimensions
dims=predict2Np(dims)
# Draw the key points through the obtained key point coordinates
# from i->j All within this range are in the same area : For example, the upper nose is from 27->36
for (name,(i,j)) in shape_predictor_68_face_landmark.items():
# Draw points for each part
for (x,y) in dims[i:j]:
cv2.circle(img=frame,center=(x,y),
radius=2,color=(0,255,0),thickness=-1)
return frame
(7) The overall code
import os
import cv2
import dlib
import numpy as np
from collections import OrderedDict
#https://mydreamambitious.blog.csdn.net/article/details/123535760
# about 68 Test points , Arrange several key points of the face in order , For later traversal
shape_predictor_68_face_landmark=OrderedDict([
('mouth',(48,68)),
('right_eyebrow',(17,22)),
('left_eye_brow',(22,27)),
('right_eye',(36,42)),
('left_eye',(42,48)),
('nose',(27,36)),
('jaw',(0,17))
])
# Draw face draw rectangle
def drawRectangle(detected,frame):
margin = 0.2
img_h,img_w,_=np.shape(frame)
if len(detected) > 0:
for i, locate in enumerate(detected):
x1, y1, x2, y2, w, h = locate.left(), locate.top(), locate.right() + 1, locate.bottom() + 1, locate.width(), locate.height()
xw1 = max(int(x1 - margin * w), 0)
yw1 = max(int(y1 - margin * h), 0)
xw2 = min(int(x2 + margin * w), img_w - 1)
yw2 = min(int(y2 + margin * h), img_h - 1)
cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
face = frame[yw1:yw2 + 1, xw1:xw2 + 1, :]
cv2.putText(frame, 'Person', (locate.left(), locate.top() - 10),
cv2.FONT_HERSHEY_SIMPLEX, 1.2, (255, 0, 0), 3)
return frame
# The face key point coordinates obtained after detection are transformed
def predict2Np(predict):
# establish 68*2 Two dimensional empty array of keys [(x1,y1),(x2,y2)……]
dims=np.zeros(shape=(predict.num_parts,2),dtype=np.int)
# Traverse each key point of the face to obtain two-dimensional coordinates
length=predict.num_parts
for i in range(0,length):
dims[i]=(predict.part(i).x,predict.part(i).y)
return dims
# Load face detection and key point location
#http://dlib.net/python/index.html#dlib_pybind11.get_frontal_face_detector
detector = dlib.get_frontal_face_detector()
#http://dlib.net/python/index.html#dlib_pybind11.shape_predictor
criticPoints = dlib.shape_predictor("shape_predictor_68_face_landmarks.dat")
# Traverse the prediction box , Draw the key points of the face
def drawCriticPoints(detected,frame):
for (step,locate) in enumerate(detected):
# Face key point detection is performed on the acquired face frame
# obtain 68 Coordinate values of key points
dims=criticPoints(frame,locate)
# Convert the obtained coordinate values into two dimensions
dims=predict2Np(dims)
# Draw the key points through the obtained key point coordinates
# from i->j All within this range are in the same area : For example, the upper nose is from 27->36
for (name,(i,j)) in shape_predictor_68_face_landmark.items():
# Draw points for each part
for (x,y) in dims[i:j]:
cv2.circle(img=frame,center=(x,y),
radius=2,color=(0,255,0),thickness=-1)
return frame
# Face key point detection of a single image
def signal_detect(img_path='images/face1.jpg'):
img=cv2.imread(img_path)
detected=detector(img)
frame=drawRectangle(detected,img)
frame = drawCriticPoints(detected, img)
cv2.imshow('frame',frame)
cv2.waitKey(0)
cv2.destroyAllWindows()
# Real time face key point detection
def detect_time():
cap=cv2.VideoCapture(0)
while cap.isOpened():
ret,frame=cap.read()
detected = detector(frame)
frame = drawRectangle(detected, frame)
frame=drawCriticPoints(detected,frame)
cv2.imshow('frame', frame)
key=cv2.waitKey(1)
if key==27:
break
cap.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
print('Pycharm')
signal_detect()
# detect_time()
(9) Face key point detection of a single image

notes : Real time detection some readers can test by themselves .
边栏推荐
- 3.10 haas506 2.0开发教程-example-TFT
- CODING 正式入驻腾讯会议应用市场!
- 剑指 Offer 17. 打印从1到最大的n位数
- ForkJoinPool
- Deep learning compiler understanding
- [cloud resident co creation] Huawei iconnect enables IOT terminals to connect at one touch
- MySQL advanced - index optimization (super detailed)
- Adhering to the concept of 'home in China', 2022 BMW children's traffic safety training camp was launched
- Oneortwo bugs in "software testing" are small things, but security vulnerabilities are big things. We must pay attention to them
- 又一篇CVPR 2022论文被指抄袭,平安保险研究者控诉IBM苏黎世团队
猜你喜欢

漏洞复现----37、Apache Unomi 远程代码执行漏洞 (CVE-2020-13942)

Leader: who can use redis expired monitoring to close orders and get out of here!

Apple Watch无法开机怎么办?苹果手表不能开机解决方法!

医疗行业企业供应链系统解决方案:实现医疗数智化供应链协同可视

电子元器件招标采购商城:优化传统采购业务,提速企业数字化升级

Small program container technology to promote the operation efficiency of the park

PC端微信多开

3.10 haas506 2.0开发教程-example-TFT

countdownlatch 和 completableFuture 和 CyclicBarrier

教你30分钟快速搭建直播间
随机推荐
MySQL找不到mysql.sock文件的临时解
[零基础学IoT Pwn] 环境搭建
uni-app进阶之内嵌应用【day14】
Rust 书籍资料 - 芽之家书馆
Digital intelligent supplier management system solution for coal industry: data driven, supplier intelligent platform helps enterprises reduce costs and increase efficiency
Tensorflow2 ten must know for deep learning
如何做好软件系统的需求调研,七种武器让你轻松搞定
Openlayers roller shutter map
The online procurement system of the electronic components industry accurately matches the procurement demand and leverages the digital development of the electronic industry
医疗行业企业供应链系统解决方案:实现医疗数智化供应链协同可视
MRO industrial products procurement management system: enable MRO enterprise procurement nodes to build a new digital procurement system
MySQL n'a pas pu trouver MySQL. Solution temporaire pour le fichier Sock
云安全日报220630:IBM数据保护平台发现执行任意代码漏洞,需要尽快升级
【合集- 行业解决方案】如何搭建高性能的数据加速与数据编排平台
挖财账号开户安全吗?是靠谱的吗?
《Go题库·15》go struct 能不能比较?
《被讨厌的勇气:“自我启发之父”阿德勒的哲学课》
Summary of methods for offline installation of chrome extensions in China
使用excel快速生成sql语句
Hospital online consultation applet source code Internet hospital source code smart hospital source code