【问题标题】:Assertion fail with Open CVOpen CV 断言失败
【发布时间】:2020-04-03 01:16:57
【问题描述】:

我是 openCV 的新手,正在尝试让 openCV 在 Win7 上使用 Python 3.8 运行我的 USB 网络摄像头。我有来自here 的基本教程,由同一作者here 修改自Raspberry Pi cam。 即:

#!/usr/bin/python3

import time
import numpy as np
import cv2


#point to the haar cascade file in the directory
cascPath = "haarcascade.xml"
faceCascade = cv2.CascadeClassifier(cascPath)

#start the camera
video_capture = cv2.VideoCapture(0)

#give camera time to warm up
time.sleep(0.1)

#start video frame capture loop
while True:
  # take the frame, convert it to black and white, and look for facial features
  ret, frame = video_capture.read()

  gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

  # use appropriate flag based on version of OpenCV
  if int(cv2.__version__.split('.')[0]) >= 3:
    cv_flag = cv2.CASCADE_SCALE_IMAGE
  else:
    cv_flag = cv2.cv.CV_HAAR_SCALE_IMAGE

  faces = faceCascade.detectMultiScale(
    gray,
    scaleFactor=1.1,
    minNeighbors=5,
    minSize=(30, 30),
    flags=cv_flag
  )

  #for each face, draw a green rectangle around it and append to the image
  for (x, y, w, h) in faces:
    cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

  #display the resulting image
  cv2.imshow('Video', frame)

  #set "q" as the key to exit the program when pressed
  if cv2.waitKey(1) & 0xFF == ord('q'):
    break


# clear the stream capture
video_capture.release()
cv2.destroyAllWindows()

它应该开箱即用,但我收到下面的错误,我不知道为什么。 CV_flag 和 gray 有数据,其他参数填充。任何想法。

C:\Users\Ghoul>py D:\LearnPython\open_cv_face_track_test.py -3.8
[ WARN:0] global C:\projects\opencv-python\opencv\modules\videoio\src\cap_msmf.cpp (674) 
SourceReaderCB::~SourceReaderCB
 terminating async callback
Traceback (most recent call last):
  File "D:\LearnPython\open_cv_face_track_test.py", line 31, in <module>
    faces = faceCascade.detectMultiScale(
cv2.error: OpenCV(4.1.2) C:\projects\opencv- 
python\opencv\modules\objdetect\src\cascadedetect.cpp:1689: error: (-215:Ass
ertion failed) !empty() in function 'cv::CascadeClassifier::detectMultiScale'

【问题讨论】:

  • 听起来是这个问题:stackoverflow.com/q/30508922/771848
  • 你必须使用xml的完整路径 - 幸运的是文件夹名称为cv2.data.haarcascades - cv2.CascadeClassifier( os.path.join(cv2.data.haarcascades, cascPath) )的变量
  • 原来 haarcascade.xml 不存在 2.2 我正在使用 haarcascade_frontalface_default.xml。希望一切都好。

标签: python python-3.x opencv


【解决方案1】:

faceCascade 分类器为空,这意味着它无法从提供的路径检索分类器。

你可以换行

cascPath = "haarcascade.xml"

与:

cascPath = '../../haarcascade.xml'

您在此处提供 cascPath 的 xml 文件的完整路径。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 2021-11-08
    • 1970-01-01
    • 2015-05-20
    • 2020-08-21
    • 2017-12-24
    相关资源
    最近更新 更多