计算机视觉之人脸识别看起来很高大上,但是用OpenCV解决很方便,OpenCV真的太强大了。。
直接上代码:
#!/usr/bin/env python3
import cv2
image = cv2.imread("demo.jpg")
gray_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
classifier = cv2.CascadeClassifier()
classifier.load(
r"C:\Program Files\OpenCV\sources\data\haarcascades\haarcascade_frontalface_default.xml")
faces = classifier.detectMultiScale(gray_img, 1.2, 6)
for (x, y, w, h) in faces:
src = cv2.rectangle(image, (x, y), (x+w, y+h), (0, 255, 255), 2)
cv2.imshow('Result Image', image)
cv2.waitKey()
cv2.destroyAllWindows()
运行结果: