使用opencv实现人脸识别

Python 2018-07-26 1202

代码如下:

import cv2  
from PIL import Image

casc_path = "C:\\Python36\\haarcascade_frontalface_default.xml"  
faceCascade = cv2.CascadeClassifier(casc_path)  
imagename = cv2.imread("D:\\qqqq.jpg")  
faces = faceCascade.detectMultiScale(imagename, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30),  
                                     flags=cv2.CASCADE_SCALE_IMAGE)  
cv2.rectangle(imagename, (10, imagename.shape[0]-20), (110, imagename.shape[0]), (0, 0, 0), -1)  
cv2.putText(imagename, "Find" + str(len(faces)) + "faces!", (10, imagename.shape[0]-5),  
            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)  
count = 1  
for (x, y, w, h) in faces:  
    cv2.rectangle(imagename, (x, y), (x+w, y+h), (128, 255, 0), 2)  
    filename = "D:\\face" + str(count) + ".jpg"  
    image1 = Image.fromarray(imagename)  # 使用.fromarray替换open,避免错误  
    image2 = image1.crop((x, y, x+w, y+h))  
    image3 = image2.resize((200, 200), Image.ANTIALIAS)  
    image3.save(filename)  
    count += 1  
cv2.namedWindow("facedetect")  
cv2.imshow("facedetect", imagename)  
cv2.waitKey(0)  
cv2.destroyWindow("facedetect")  

 

标签:Python

文章评论

评论列表

已有0条评论