程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python opencv for real-time face recognition and face distance measurement

編輯:Python

Python OpenCv Realize real-time face recognition and Facial ranging

Get ready
In the face Facial ranging Before development , It's up to you first Python Install separately in 4 Databases , Respectively cvzone library ,mediapipe library ,tensorflow library ,tensorflow-gpu library , In which cvzone Kuhe mediapipe Libraries are fast , You can quickly install by using the following statement

pip install cvzone
pip install mediapipe

install tensorflow Kuhe tensorflow-gpu Library because the library file is large , Regular method installation often fails to download halfway , The following commands are recommended , I personally tested that it can be installed quickly .

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple tensorflow-gpu

Ensure the above 4 After all the libraries are installed successfully, you can start writing code .

principle
OpenCv The principle of similar triangle is used to realize face distance measurement , See the picture below

Parameter description :
(1) The focal length of the camera f It's fixed ;
(2)w Pixel value distance of human eye after camera imaging , Can pass findFaceMesh( ) After the face grid is recognized, the pixel distance of the imaging human eye is measured w;
(3)W For the distance between the left eye and the right eye , The average pupillary distance of men is 64mm, The average pupil distance of a woman is 62mm, The development of this project takes the intermediate value 63mm;
(4)d To get the distance from the actual person to the camera , Through the principle of similar triangle , After changing the formula, you can calculate the distance formula from the person to the camera :d=(f*W)/w

notes :
Different cameras , The focal length is different , If you know the parameters of the camera itself is the best , It can be calculated accurately by directly substituting . If you don't know the camera parameters , You need to manually correct the focal length for many times f. Manual correction requires several more measurements , Then find the mean , To ensure focal length f The accuracy of , So as to improve the ranging accuracy . Manual calibration , First fix the distance between one eye and the camera , such as 50cm, Measure accurately with a tape measure , Measure the focal length several times f. Specific focal length measurement f See the following for the code of :

import cv2
import cvzone
from cvzone.FaceMeshModule import FaceMeshDetector # Import detector 
## Here is the import camera 
cap = cv2.VideoCapture(0)
detector = FaceMeshDetector(maxFaces = 1) # Face detection , And just a face , final 1 Indicates that only... Is detected at most 1 Zhang Renren 
while True:
success,img = cap.read()
img,faces = detector.findFaceMesh(img,draw = False) # Be a detector , Find face mesh , Back to image (img) And our faces (faces) ,draw = False With this sentence, you can't see the point 
if faces: # If you have a face (faces) You can use 
## Find the left eye and the right eye through the following statement 
face = faces[0] # Start with the first stage 
pointLeft = face[145] # The value on the left is basically the value 145
pointRight = face[374] # The value on the right is basically the value 374
### Here's how to find the focal length 
## Find the distance in pixels 
w,_ = detector.findDistance(pointLeft,pointRight) # Assign the distance from the position of the left eye point to the position of the right eye point to w w The following underline ignores other values 
W = 6.3 # This is the distance between the left eye and the right eye of the human eye 63mm, Take the intermediate value , What men do 64mm, A woman's behavior is 62mm
d = 50 # The distance is assumed to be 50cm
f = (w * d) / W # Substitute the formula for finding the focal length into 
print(f)
cv2.imshow("Iamge",img)
cv2.waitKey(1)

Measure the focal length several times through the above code f Calculating mean , The focal length of the camera I use is 300, So later in my code f = 300, Here, the measurement is substituted according to the actual focal length . Let's focus on f Insert the measurement formula d = (f*W) / w, See below for the specific code :

import cv2
import cvzone
from cvzone.FaceMeshModule import FaceMeshDetector # Import detector 
## Here is the import camera 
cap = cv2.VideoCapture(0)
detector = FaceMeshDetector(maxFaces = 1) # Face detection 
while True:
success,img = cap.read()
img,faces = detector.findFaceMesh(img,draw = False) # Be a detector , Find face mesh , Back to image (img) And our faces (faces) ,draw = False With this sentence, you can't see the grid 
if faces: # If you have a face (faces) You can use 
## Find the left eye and the right eye through the following statement 
face = faces[0] # Start with the first stage 
pointLeft = face[145] # The value on the left is basically the value 145
pointRight = face[374] # The value on the right is basically the value 374
## Here is the distance between the two points of the eye 
# cv2.line(img, pointLeft, pointRight, (0, 200, 0), 3) # Draw a line between the two points of the eye , The starting point is pointLeft, The end is pointRight, The line color is green , The line width is 3
# cv2.circle(img,pointLeft,5,(255,0,255),cv2.FILLED) # stay img Draw a circle on the image , The center point is pointLeft, The radius is 5, The color is purple , The final running result can mark purple dots on the left eye of the imager 
# cv2.circle(img,pointRight,5,(255,0,255),cv2.FILLED) # stay img Draw a circle on the image , The center point is pointRighe, The radius is 5, The color is purple , The final running result can mark purple dots in the right eye of the imager 
w, _ = detector.findDistance(pointLeft, pointRight) # Assign the distance from the position of the left eye point to the position of the right eye point to w w The following underline ignores other values 
W = 6.3 # This is the distance between the left eye and the right eye of the human eye 63mm, Take the intermediate value , What men do 64mm, A woman's behavior is 62mm
### Search distance 
## Pass above f = (w * d) / W The formula , Can roughly measure , When the human eye is away from the camera 50cm when , The focal length of the camera is 300 about 
## Then substitute the found focal length into the formula for calculating the distance , You can calculate the distance 
f = 300
d = (W * f) / w
print(d)
## The following is to output the text words of distance at the forehead position following the movement of the face 
cvzone.putTextRect(img,f'Depth:{
int(d)}cm',(face[10][0]-95,face[10][1]-5),scale = 1.8) # Display the distance text on the image , Display as a string , Unit is cm, The position displayed by the text value follows the movement of the human face and is displayed on the forehead ( On the forehead id yes 10, That is to say face[10], hinder face[10][0] Represents the first element ,face[10][1] For the second element ),
## above scale = 2 Indicates the size of the output text box on the picture 
##face[10][0] The changes are left and right ,face[10][1] What changes is the height of the display 
cv2.imshow("Iamge",img)
cv2.waitKey(1)

The final test result See below :

See the following for the code of multi person simultaneous ranging , I haven't optimized yet , When multiple people range at the same time, it will float unsteadily .

import cv2
import cvzone
from cvzone.FaceMeshModule import FaceMeshDetector # Import detector 
## Here is the import camera 
cap = cv2.VideoCapture(0)
detector = FaceMeshDetector(maxFaces=3)
while True:
sucess,img = cap.read()
img,faces1 = detector.findFaceMesh(img)
img,faces2 = detector.findFaceMesh(img)
img,faces3 = detector.findFaceMesh(img)
W = 6.3 # This is the distance between the left eye and the right eye of the human eye 63mm, Take the intermediate value , What men do 64mm, A woman's behavior is 62mm
if faces1: # If a face is detected 
face1 = faces1[0]
pointLeft1 = face1[145] # The facial value of the left eye is 145
pointRight1 = face1[374] # The face value of the right eye is 374
## Here is the distance between two eyes 
# cv2.line(img,pointLeft1,pointRight1,(0,200,0),3) # In the eyes 
# cv2.circle(img,pointLeft1,5,(255,0,255),cv2.FILLED) # stay img Draw a circle on the image , The center point is pointLeft, The radius is 5, The color is purple 
#cv2.circle(img,pointRight1,5,(255,0,255),cv2.FILLED) # stay img Draw a circle on the image , The center point is pointRight, The radius is 5, The color is purple 
w1, _ = detector.findDistance(pointLeft1, pointRight1) # Assign the position distance from the left eye to the right eye of the first face to w1 ,w1 The following underscore indicates that other values are ignored 
### Search distance 
## Pass above f = (w * d) / W The formula , Can roughly measure , When the human eye is away from the camera 50cm when , The focal length of the camera is 300 about 
## Then substitute the found focal length into the formula for calculating the distance , You can calculate the distance 
f = 300
d1 = (W * f) / w1
cvzone.putTextRect(img, f'Depth:{
int(d1)}cm', (face1[10][0] - 95, face1[10][1] - 5), scale = 1.8)
if faces2: ## This means that when the first face is tested , If the second face appears, the mark will be displayed 
face2 = faces2[0]
pointLeft2 = face2[145] # The facial value of the left eye is 145
pointRight2 = face2[374] # The face value of the right eye is 374
#cv2.line(img, pointLeft2, pointRight2, (0, 200, 0), 3) # In the eyes 
# cv2.circle(img, pointLeft2, 5, (255, 0, 255), cv2.FILLED) # stay img Draw a circle on the image , The center point is pointLeft, The radius is 5, The color is purple 
# cv2.circle(img, pointRight2, 5, (255, 0, 255), cv2.FILLED) # stay img Draw a circle on the image , The center point is pointRight, The radius is 5, The color is purple 
w2,_ = detector.findDistance(pointLeft2,pointRight2) # Assign the position distance from the left eye to the right eye of the first face to w1 ,w1 The following underscore indicates that other values are ignored 
### Search distance 
## Pass above f = (w * d) / W The formula , Can roughly measure , When the human eye is away from the camera 50cm when , The focal length of the camera is 300 about 
## Then substitute the found focal length into the formula for calculating the distance , You can calculate the distance 
f = 300
d2 = (W * f) / w2
cvzone.putTextRect(img, f'Depth:{
int(d2)}cm', (face2[10][0] - 95, face2[10][1] - 5), scale=1.8)
if faces3:
face3 = faces3[0]
pointLeft3 = face3[145] # The facial value of the left eye is 145
pointRight3 = face3[374] # The face value of the right eye is 374
# cv2.line(img, pointLeft3, pointRight3, (0, 200, 0), 3) # In the eyes 
# cv2.circle(img, pointLeft3, 5, (255, 0, 255), cv2.FILLED) # stay img Draw a circle on the image , The center point is pointLeft, The radius is 5, The color is purple 
# cv2.circle(img, pointRight3, 5, (255, 0, 255), cv2.FILLED) # stay img Draw a circle on the image , The center point is pointRight, The radius is 5, The color is purple 
w3, _ = detector.findDistance(pointLeft3, pointRight3) # Assign the position distance from the left eye to the right eye of the first face to w1 ,w1 The following underscore indicates that other values are ignored 
### Search distance 
## Pass above f = (w * d) / W The formula , Can roughly measure , When the human eye is away from the camera 50cm when , The focal length of the camera is 300 about 
## Then substitute the found focal length into the formula for calculating the distance , You can calculate the distance 
f = 300
d3 = (W * f) / w2
cvzone.putTextRect(img, f'Depth:{
int(d3)}cm', (face3[10][0] - 95, face3[10][1] - 5), scale=1.8)
cv2.imshow("img",img)
cv2.waitKey(1)

That's through Python OpenCv The process of realizing face recognition and face ranging , I wish all scientific researchers more articles , Lose less hair !


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved