Wu Yuxiong - born natural python learning Notes: python face recognition with Open CV

To identify a specific image, the key is to have a signature file to identify the object, OpenCV has built-in 
face recognition profile, we simply use the OpenCV CascadeClassifier class can be identified. 
Creating Cas cadeClas s ifier object syntax is:

 

 

import cv2

faceCascade = cv2.CascadeClassifier("E:\\haarcascade_frontalface_default.xml")
Can then be identified by facial recognition target detectMultiScale method, the syntax is:

 

 

 

 

For example, to identify the minimum set area (10, 10), the false detection rate is 5 , the normal pattern recognition, to recognize the 
image as Image, and the recognition result stored in the variable to the faces:

 

 

Identifying the position of the face 
to identify the face position in the image, and displays the number in the lower left corner of the face recognition.

 

 

import cv2 

casc_path = "E:\\haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(casc_path)
imagename = cv2.imread("F:\\pythonBase\\pythonex\\ch10\\media\\person1.jpg")
faces = faceCascade.detectMultiScale(imagename, scaleFactor=1.1, minNeighbors=5, minSize=(30,30), flags = cv2.CASCADE_SCALE_IMAGE)
#imagename.shape[0]:图片高度,imagename.shape[1]:图片宽度
cv2.rectangle(imagename, (10,imagename.shape[0]-20), (110,imagename.shape[0]), (0,0,0), -1)
cv2.putText(imagename,"Find " + str(len(faces)) + " face!", (10,imagename.shape[0]-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 2)
for (x,y,w,h) in faces:
    cv2.rectangle(imagename,(x,y),(x+w, y+h),(128,255,0),2)
cv2.namedWindow("facedetect")
cv2.imshow("facedetect", imagename)
cv2.waitKey(0)  
cv2.destroyWindow("facedetect")

detectMultiScale 方法可识别图片中的多个人脸,把图
片文件更改为 person3.jpg 或 person8.jpg 进行多个人脸的识别 。
import cv2 

casc_path = "E:\\haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(casc_path)
imagename = cv2.imread("F:\\pythonBase\\pythonex\\ch10\\media\\person3.jpg")
faces = faceCascade.detectMultiScale(imagename, scaleFactor=1.1, minNeighbors=5, minSize=(30,30), flags = cv2.CASCADE_SCALE_IMAGE)
#imagename.shape[0]:图片高度,imagename.shape[1]:图片宽度
cv2.rectangle(imagename, (10,imagename.shape[0]-20), (110,imagename.shape[0]), (0,0,0), -1)
cv2.putText(imagename,"Find " + str(len(faces)) + " face!", (10,imagename.shape[0]-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 2)
for (x,y,w,h) in faces:
    cv2.rectangle(imagename,(x,y),(x+w, y+h),(128,255,0),2)
cv2.namedWindow("facedetect")
cv2.imshow("facedetect", imagename)
cv2.waitKey(0)  
cv2.destroyWindow("facedetect")

 

 

import cv2 

casc_path = "E:\\haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(casc_path)
imagename = cv2.imread("F:\\pythonBase\\pythonex\\ch10\\media\\person8.jpg")
faces = faceCascade.detectMultiScale(imagename, scaleFactor=1.1, minNeighbors=5, minSize=(30,30), flags = cv2.CASCADE_SCALE_IMAGE)
#imagename.shape[0]:图片高度,imagename.shape[1]:图片宽度
cv2.rectangle(imagename, (10,imagename.shape[0]-20), (110,imagename.shape[0]), (0,0,0), -1)
cv2.putText(imagename,"Find " + str(len(faces)) + " face!", (10,imagename.shape[0]-5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255,255,255), 2)
for (x,y,w,h) in faces:
    cv2.rectangle(imagename,(x,y),(x+w, y+h),(128,255,0),2)
cv2.namedWindow("facedetect")
cv2.imshow("facedetect", imagename)
cv2.waitKey(0)  
cv2.destroyWindow("facedetect")

 

 

Guess you like

Origin www.cnblogs.com/tszr/p/12032890.html