识别图像人脸

import cv2
# Get user supplied values
# path=input("请输入你图片的路径:")
path="C:\\Users\\Administrator\\Desktop\\3.jpg"
cascPath = "haarcascade_frontalface_default.xml"

# Create the haar cascade
faceCascade = cv2.CascadeClassifier(cascPath)

# Read the image
image = cv2.imread(path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Detect faces in the image
faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.1,
    minNeighbors=5,
    minSize=(30, 30),
    flags=cv2.CASCADE_SCALE_IMAGE
)

print("Found {0} faces!".format(len(faces)))

# Draw a rectangle around the faces
for (x, y, w, h) in faces:
    cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 0), 2)

cv2.imshow("facesFound", image)
cv2.imwrite("2.jpg",image)
cv2.waitKey(0)
cv2.destroyAllWindows()
cv2.destroyWindow("facesFound")

猜你喜欢

转载自blog.csdn.net/liaoqingjian/article/details/108648190