import cv2 face_detect = cv2.CascadeClassifier(r'C:\Users\1\Desktop\haarcascade_frontalface_alt.xml') # 识别电脑摄像头,并打开 cap = cv2.VideoCapture(0) fourcc = cv2.VideoWriter_fourcc(*'XVID') out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480),True) font = cv2.FONT_HERSHEY_SIMPLEX while True: ret, img = cap.read() gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) faces = face_detect.detectMultiScale(gray, 1.1, 3) for (x, y, w, h) in faces: # 参数分别为 图片、左上角坐标,右下角坐标,颜色,厚度 face = img[y:y + h, x:x + w] # 裁剪坐标为[y0:y1, x0:x1] mask_face = face_detect.detectMultiScale(gray, 1.1, 5) #ret, frame = cap.read() cv2.putText(img, 'Not Wearing A Mask', (0, 130), font, 2, (0, 0, 255), 2, cv2.LINE_AA) out.write(img) for (x2, y2, w2, h2) in mask_face: cv2.rectangle(img, (x2, y2), (x2 + w2, y2 + h2), (0, 0, 255), 2) cv2.imshow('Cheney', img) print(img) cv2.waitKey(3) cv2.destroyAllWindows()