opencv 打开摄像头 出问题

opencv 打开摄像头 出问题

import matplotlib.pyplot as plt
import cv2

face_detector = cv2.CascadeClassifier("./opencv/haarcascade_frontalface_alt.xml")

cap = cv2.VideoCapture(0)
flag, frame = cap.read()
if not flag:
    print("摄像头调用失败")
    
while flag:
    face_zone = face_detector.detectMultiScale(frame, scaleFactor=1.1, minNeighbors=3)
    
    for x,y,w,h in face_zone:
        cv2.rectangle(frame, ptl=(x,y), pt2=(x+w, y+h), color=[0,0,255], thickness=2)
        
        cv2.imshow('capture', frame)
        if cv2.waitKey(1000//24) == ord('q'):
            break
        flag, frame = cap.read()
        
cv2.waitKey(0)
cv2.destoryALLWindows()

OpenCV(3.4.3) C:\projects\opencv-python\opencv\modules\objdetect\src\cascadedetect.cpp:1698: error: (-215:Assertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

不是读取摄像头的问题,应该是xml文件没有读取到,检查路径是否正确,写成绝对路径试试。
https://datascience.stackexchange.com/questions/44871/cv2-error-opencv3-4-3-215assertion-failed-empty-in-function