繁体   English   中英

OpenCV 4.7.0:系统错误:<class 'cv2.cascadeclassifier'> 返回带有异常集的结果</class>

[英]OpenCV 4.7.0: SystemError: <class 'cv2.CascadeClassifier'> returned a result with an exception set

这是代码的开头。 我正在关注使用 OpenCV 和 Python 的面部识别项目教程。这意味着使用 haar 级联面部检测拍摄面部照片,然后存储在我拥有的数据集文件中。

import cv2
import os

cam = cv2.VideoCapture(0)
cam.set(3, 640) # set video width
cam.set(4, 480) # set video height


face_detector = cv2.CascadeClassifier('/Users/*********/workspace/Facial Recognition Project/haarcascade_frontalface_default.xml')

# For each person, enter one numeric face id
face_id = input('\n enter user id end press <return> ==>  ')

print("\n [INFO] Initializing face capture. Look the camera and wait ...")
# Initialize individual sampling face count
count = 0

while(True):

    ret, img = cam.read()
    img = cv2.flip(img, -1) # flip video image vertically
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_detector.detectMultiScale(gray, 1.3, 5)

    for (x,y,w,h) in faces:

        cv2.rectangle(img, (x,y), (x+w,y+h), (255,0,0), 2)     
        count += 1

        # Save the captured image into the datasets folder
        cv2.imwrite("dataset/User." + str(face_id) + '.' + str(count) + ".jpg", gray[y:y+h,x:x+w])

        cv2.imshow('image', img)

    k = cv2.waitKey(100) & 0xff # Press 'ESC' for exiting video
    if k == 27:
        break
    elif count >= 30: # Take 30 face sample and stop video
         break

# Do a bit of cleanup
print("\n [INFO] Exiting Program and cleanup stuff")
cam.release()
cv2.destroyAllWindows()```

这是错误:

cv2.error: OpenCV(4.7.0) /Users/opencv-cn/GHA-OCV-1/_work/opencv-python/opencv-python/opencv/modules/core/src/persistence.cpp:681: 错误: ( -215:断言失败) buf in function 'open'

上述异常是以下异常的直接原因:

回溯(最近调用最后):文件“/Users/sashuponnaganti/workspace/Facial Recognition Project/01_face_dataset.py”,第 9 行,在 face_detector = cv2.CascadeClassifier('/Users/*********/ workspace/Facial Recognition Project/haarcascade_frontalface_default.xml') SystemError: <class 'cv2.CascadeClassifier'> 返回了一个带有异常集的结果

我相信我正在正确使用 cv2.CascadeClassifier function 并且我知道 xml 文件的文件路径是正确的所以我很困惑为什么我会收到错误。

我想你不需要强制 cv2 在某些特定路径上找到 xml ...所以,尝试使用

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

代替。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM