1、识别效果
2、识别代码
import cv2 as cv
def face_detect_demo(img):
# 将图片灰度
gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
# 加载特征数据
face_detector = cv.CascadeClassifier(
"data//haarcascade_frontalface_alt.xml")
faces = face_detector.detectMultiScale(gray)
for x, y, w, h in faces:
cv.rectangle(img, (x, y), (x + w, y + h), color=(0, 0, 255), thickness=2)
cv.circle(img, center=(x + w // 2, y + h // 2), radius=(w // 2), color=(0, 255, 0), thickness=2)
cv.imshow('result', img)
# 读取视频
cap = cv.VideoCapture('video.MP4')
while True:
flag, frame = cap.read()
print('flag:', flag, 'frame.shape:', frame.shape)
if not flag:
break
face_detect_demo(frame)
if ord('q') == cv.waitKey(10):
break
cv.destroyAllWindows()
cap.release()
data//haarcascade_frontalface_alt.xml
需要下载到本地使用。大家可以去这篇文章中的百度网盘链接下载