当前位置:网站首页>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 .
边栏推荐
- MySQL advanced - basic index and seven joins
- MRO industrial products procurement management system: enable MRO enterprise procurement nodes to build a new digital procurement system
- 系统集成项目管理工程师认证高频考点:编制项目范围管理计划
- Infineon - GTM architecture -generic timer module
- Is it safe to open a mobile stock account? Is it reliable?
- 麻烦问下 Flink支持同步数据到 sqlserver么
- Openlayers roller shutter map
- NFT挖矿游GameFi链游系统开发搭建
- 「经验」浅谈聚类分析在工作中的应用
- [零基础学IoT Pwn] 环境搭建
猜你喜欢

Hospital online consultation applet source code Internet hospital source code smart hospital source code

领导:谁再用 Redis 过期监听实现关闭订单,立马滚蛋!

MySQL advanced - index optimization (super detailed)

Four tips tell you how to use SMS to promote business sales?

Troubleshooting MySQL for update deadlock

php利用队列解决迷宫问题

Helping the ultimate experience, best practice of volcano engine edge computing

分布式场景下,你知道有几种生成唯一ID的方式嘛?

医院在线问诊小程序源码 互联网医院源码 智慧医院源码

LeetCode之合并二叉树
随机推荐
Flink系列:checkpoint调优
Merged binary tree of leetcode
PHP uses queues to solve maze problems
Volcano engine was selected into the first "panorama of edge computing industry" in China
Rust 操控大疆可编程无人机 tello
Openlayers 卷帘地图
TeamTalk WinClient编译问题及解决方案
Solution of enterprise supply chain system in medical industry: realize collaborative visualization of medical digital intelligent supply chain
The company was jailed for nonstandard bug during the test ~ [cartoon version]
php利用队列解决迷宫问题
Vulnerability recurrence ----- 35. Uwsgi PHP directory traversal vulnerability (cve-2018-7490)
NFT挖矿游GameFi链游系统开发搭建
医院在线问诊小程序源码 互联网医院源码 智慧医院源码
Unity实战之一个脚本实现雷达图
[cloud resident co creation] Huawei iconnect enables IOT terminals to connect at one touch
Unity开发bug记录100例子(第1例)——打包后shader失效或者bug
分布式场景下,你知道有几种生成唯一ID的方式嘛?
漏洞复现----35、uWSGI PHP 目录遍历漏洞 (CVE-2018-7490)
Summary of methods for offline installation of chrome extensions in China
uni-app进阶之自定义【day13】