当前位置:网站首页>Face detection based on OpenCV and dlib+fr and face alignment based on Dlib
Face detection based on OpenCV and dlib+fr and face alignment based on Dlib
2022-07-21 20:24:00 【CocoLoveOreo】
In the course of media big data , Senior brothers and sisters introduced face detection and face alignment based on various common databases , And did a series of experiments .
One 、 be based on OpenCV Face detection
1. install OpenCV:
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple opencv-python
2. Get the location of the classifier file :
see python The path of :
where python
stay Python Under the path /Lib/sitepackages/cv2/data/haarcascade_frontalface_default.xml' That is, the location of the classifier file
3. Code testing :
import cv2
filename = "test3.jpg"
### establish ⼀ Cascaded classifiers , load ⼀ individual .xml⽂ Pieces of
face_cascade =cv2.CascadeClassifier('D:\Anaconda3\Lib\site-packages\cv2\data\haarcascade_frontalface_default.xml')
### Load image
img = cv2.imread(filename)
### Convert to grayscale image
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
### send ⽤ On ⾯ Create a classifier to detect ⼈ face ,faces To recognize the rectangular box vector group of the face ,⼏ Every face has ⼏ Moments Shape frame , Vector group ⾥ There is ⼏ Group vectors , preservation ⼈ The coordinates of the face and ⼤⼩.
faces = face_cascade.detectMultiScale(gray,1.3,5)
### scaleFactor Express ⼈ The time chart of each iteration in the face detection process ⽚ Compression ratio of (1.3)
### minNeighbors: Every ⼈ The face rectangle retains the number of nearest neighbors ⽬ The most ⼩ value (5)
### Draw a rectangle on the original drawing
### Traverse faces Rectangular box vector group ⾥ Every time ⼀ Group rectangles ( That is, every ⼀ Open your face )
for (x, y, w, h) in faces:
img = cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)
cv2.imshow('Person Detected!', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
detection result :
Two 、 be based on face_recognition Face detection
Face-Recognition The emissary of ⽤ rely on Dlib, So you need to install it first Dlib:
1. Dlib install :
Install... With the following command shipment ⾏ Environmental Science :
## The environment that needs to be installed :python+opencv+dlib+face_recognition( On the first two ⾯ Installed )
1. pip install cmake
2. pip install dlib ( If an error , Refer to this link to ⾏ install )
https://blog.csdn.net/qq_58691861/article/details/119116148
https://zhuanlan.zhihu.com/p/464846060
3.pip install face_recognition
2. After installation , Run code :
import face_recognition
import cv2
### Load image ⽂ Pieces of (.jpg,.png etc. ), The data returned is Numpy Array , Record the figure ⽚ The eigenvectors of all pixels
image = face_recognition.load_image_file("test3.jpg")
### Locate all in the diagram ⼈ Pixel position of the face , The return value is in the form of a list , Every... In the list ⼀⾏ yes ⼀ Zhang ⼈ Position information of the face , Include 【top, right, bottom, left】 This is a ⼀ Group tuples .
face_locations_noCNN=face_recognition.face_locations(image)
face_recognition.face_locations(image,model='cnn')
print("face_location_noCNN:")
print(face_locations_noCNN)
face_num2=len(face_locations_noCNN)
print(face_num2)
### Look at the original picture
org = cv2.imread("test3.jpg")
img = cv2.imread("test3.jpg")
cv2.imshow("test3.jpg",img)
cv2.waitKey()
### Face detection
for i in range(0,face_num2):
top = face_locations_noCNN[i][0]
right = face_locations_noCNN[i][1]
bottom = face_locations_noCNN[i][2]
left = face_locations_noCNN[i][3]
start = (left, top)
end = (right, bottom)
color = (0,255,255)
thickness = 2
cv2.rectangle(org, start, end, color, thickness)
cv2.imshow("no cnn ",org)
cv2.waitKey(0)
cv2.destroyAllWindows()
detection result :
It can be seen that , Compared with OpenCV, be based on face_recognition The accuracy of face detection is higher
3、 ... and 、 Face feature point detection
import cv2
import dlib
path = "test4.jpg"
# Read graph ⽚
img = cv2.imread(path)
# graying
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# Get the face position detector
detector = dlib.get_frontal_face_detector()
dets = detector(gray, 1)
predictor =dlib.shape_predictor("D:\Anaconda3\Lib\site-packages\shape_predictor_68_face_landmarks.dat")
for face in dets:
shape = predictor(img, face) # seek ⼈ Facial 68 Points
for pt in shape.parts():
pt_pos = (pt.x, pt.y)
# Parameters : chart ⽚, round ⼼, radius , Typeface ⾊, The font size )
cv2.circle(img, pt_pos, 2, (0, 255, 0), 1)
cv2.imshow("image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
detection result :
边栏推荐
- Web. Config custom class reading
- Pytorch foundation module and Practice
- Nacos-注册中心原理解析
- Linux redis-6.2.6 stand alone deployment
- Yum install GCC error
- pycharm常见错误集锦
- 原码、反码、补码 AND 逻辑运算“与或非”
- 初学谷歌bert模型的预训练和fine-tuning微调
- Esp8266 firmware download and burning (include at firmware download address + firmware burning precautions)
- Data consistency of Nacos registry cluster
猜你喜欢
Data consistency of Nacos registry cluster
Linux redis-6.2.6 stand alone deployment
Exclusive locking of this profile failed. Another running VMware process may be using a profile.
Analyse du principe du Centre de configuration nacos
[record] the operation of optisystem is stuck, and it is unable to click to close, input variable values and other solutions
bert从入门到实践笔记本
合泰HT32--淘晶驰TJC--T0串口屏学习笔记(二)
(environment configuration) TDD net
pycharm常见错误集锦
[PCB] Based on stm32f103rct6 rocker - Bluetooth Module Development Board - drawing Board note arrangement
随机推荐
Idea2020 open run dashboard
Correlation function of CV2
Nacos-配置中心原理解析
PyTorch基础模块和实践
[404] the server starts successfully, and the default page cannot be accessed
PyTorch基础知识
ftp不能创建多级目录【循环创建问题】
Amy-Tabb机器人世界手眼标定(3、论文分析)
【PCB】Altium Designer 中(Via过孔)(文本string)(走线Linear Dimension)等项目修改默认设置
As7341 spectrum sensor measures color temperature color_ temperature_ Study notes
(笔记)吴恩达深度学习L4W2
pytorch的安装
AS7341光谱传感器测量色温color_temperature_学习笔记
Recursive and non recursive implementation of four traversals of binary trees
Web. Config custom class reading
Judge whether there are duplicate values in the "string []" array, and use the HashSet feature to check
【C语言】一些常用的字符串函数--学习笔记
分布式事务其中的那些坑
4. 10 lines of code MNIST handwritten numeral recognition of paddlepaddle
回型输入和回型输出