当前位置:网站首页>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 .
边栏推荐
- 手机股票账号开户安全吗?是靠谱的吗?
- 先写API文档还是先写代码?
- 挑选智能音箱时,首选“智能”还是“音质”?这篇文章给你答案
- [software testing] basic knowledge of software testing you need to know
- 抖音最新Xbogus,signature生成js逆向分析
- ONEFLOW source code parsing: automatic inference of operator signature
- 漏洞复现----37、Apache Unomi 远程代码执行漏洞 (CVE-2020-13942)
- Oneortwo bugs in "software testing" are small things, but security vulnerabilities are big things. We must pay attention to them
- TiDB Dashboard里面可以写sql执行吗
- Tsinghua only ranks third? 2022 release of AI major ranking of Chinese Universities of soft science
猜你喜欢
How to use AI technology to optimize the independent station customer service system? Listen to the experts!
Dependencies tool to view exe and DLL dependencies
Volcano engine was selected into the first "panorama of edge computing industry" in China
Geoffrey Hinton:我的五十年深度学习生涯与研究心法
AI首席架构师10-AICA-蓝翔 《飞桨框架设计与核心技术》
Vulnerability recurrence ----- 38. Thinkphp5 5.0.23 Remote Code Execution Vulnerability
Hcip (Huawei Senior Network Security Engineer) (Experiment 8) (MPLS basic experiment)
Sword finger offer 17 Print from 1 to maximum n digits
System integration project management engineer certification high frequency examination site: prepare project scope management plan
Another CVPR 2022 paper was accused of plagiarism, and Ping An insurance researchers sued IBM Zurich team
随机推荐
先写API文档还是先写代码?
Rust 操控大疆可编程无人机 tello
100 examples of bug records of unity development (the first example) -- shader failure or bug after packaging
【合集- 行业解决方案】如何搭建高性能的数据加速与数据编排平台
ONEFLOW source code parsing: automatic inference of operator signature
Hcip (Huawei Senior Network Security Engineer) (Experiment 8) (MPLS basic experiment)
hdfs上的数据导入到clickhouse用什么方式最快呢?spark通过jdbc导入,还是hdfs
Troubleshooting MySQL for update deadlock
Vulnerability recurrence ----37. Apache unomi Remote Code Execution Vulnerability (cve-2020-13942)
Rhai - Rust 的嵌入式脚本引擎
Unity开发bug记录100例子(第1例)——打包后shader失效或者bug
Do you write API documents or code first?
Glacier teacher's book
VScode 状态条 StatusBar
《被讨厌的勇气:“自我启发之父”阿德勒的哲学课》
秉持'家在中国'理念 2022 BMW儿童交通安全训练营启动
The new Post-00 Software Test Engineer in 2022 is a champion
《所谓情商高,就是会说话》读书笔记
Type ~ storage ~ variable in C #
程序员女友给我做了一个疲劳驾驶检测