Face recognition technology has been able to accurately locate the position of the face, even including the position of the eyes and nose, we can obtain the movement of the face through the face positioning technology, and the detection of the movement can be used as a virtual input, such as controlling the game character through the face, controlling the movement of the machine through the face, in life, when our hands are occupied, using the face action is an option.
2.Part SourcingWe use OpenCV to extract images from the camera, recognize faces in the image through face recognition technology, the language adopts Python, install the OpenCV Python library, and the camera can use Intel RealSense:
1) Intel realsense
2) opencv
3) opencv python lib
3. BuildingWe use the pip tool to install, using the command: pip install opencv-python, and then you can import cv2 to use the opencv library, opencv library has face recognition function functions, you can use cv2. CascadeClassifier("haarcascade_frontalface_default.xml") function to build a face recognition model, haarcascade_frontalface_ The default .xml file is the face recognition model parameters, using the CascadeClassifier recognition model, you can train the model based on other face data and generate customized model parameter information.
4.Coding # coding: utf-8
import cv2
capture = cv2.VideoCapture(0)
facecascade = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
eyecascade = cv2.CascadeClassifier("haarcascade_eye.xml")
oldX=0
oldY=0
while(capture.isOpened()):
retval,image = capture.read()
#eyes = eyecascade.detectMultiScale(image,1.15)
faces = facecascade.detectMultiScale(image,1.15)
#for (x,y,w,h) in eyes:
# cv2.rectangle(image,(x,y),(x+w,y+h),(0,0,255),5)
for (x,y,w,h) in faces:
cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),5)
nowX = x+w/2
nowY = y+h/2
range = (oldX-nowX)*(oldX-nowX)+(oldY-nowY)*(oldY-nowY)
if range >20:
h = nowX-oldX
v = nowY-oldY
print(oldX,oldY,h,v,range)
else:
h = 0
v = 0
oldX = nowX
oldY = nowY
cv2.imshow("video",image)
key = cv2.waitKey(1)
if key == 32:
break
if key == 97:
cv2.imwrite("face.jpg",image)
capture.release()
cv2.destroyAllWindows()
Comments