安装 OpenCV
pip install opencv-python
pip install opencv-contrib-python
完整代码
import cv2 as cv
# 读取图片
img = cv.imread("test.jpg")
# 人脸检测
retval, faces = cv.face.getFacesHAAR(img, "haarcascade_frontalface_alt2.xml")
# 画矩形
for [(x, y, w, h)] in faces:
cv.rectangle(img, (x, y), (x + w, y + h), (0, 0, 255), 2)
# 显示图片
cv.imshow("Display window", img)
k = cv.waitKey(0)