#导入cv模块
import cv2 as cv
#检测函数
def face_detect_demo(img):
    #将图片转换为灰度图片
    gary = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
    #自带的分类器
    face_detect = cv.CascadeClassifier('D:/PyCharm 2024.1.1/opencv/sources/data/haarcascades/haarcascade_frontalface_default.xml')
    face = face_detect.detectMultiScale(gary)
    for (x,y,w,h) in face:
        cv.rectangle(img,(x,y),(x+w,y+h),color=(0,0,255),thickness=2)
    # 显示图像
    cv.imshow('result', img)

#读取摄像头
#cap = cv.VideoCapture(0)
cap = cv.VideoCapture("output/1.mp4")

#循环
#等待
while True:
    flag,frame = cap.read()
    if not flag:
        break
    face_detect_demo(frame)
    if ord('q') == cv.waitKey(0):
        break
#>0 等待delay毫秒 =0 无限等待 <0 等待键盘单击
#释放内存
cv.destroyAllWindows() #删除所以窗口
cap.release()