Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/303.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 断言在打开CV时失败_Python_Python 3.x_Opencv - Fatal编程技术网

Python 断言在打开CV时失败

Python 断言在打开CV时失败,python,python-3.x,opencv,Python,Python 3.x,Opencv,我是openCV的新手,正在尝试让openCV在Win7和Python 3.8上使用我的USB网络摄像头。我得到了由同一作者修改的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

我是openCV的新手,正在尝试让openCV在Win7和Python 3.8上使用我的USB网络摄像头。我得到了由同一作者修改的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_标志和灰色有数据,其他参数已填充。任何想法

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'
faceCascade分类器为空,这意味着它无法从提供的路径检索分类器

你可以换线

cascPath = "haarcascade.xml"
与:


在这里,您为cascPath提供xml文件的完整路径

听起来像是这个问题:您必须使用xml的完整路径-幸运的是,有一个文件夹名为cv2.data.haarcascades的变量-cv2.cascadeOS.path.joincv2.data.haarcascades,cascPath证明haarcascade.xml在2.2中不存在,我使用的是haarcascade\u frontalface\u default.xml。希望没问题。
cascPath = '../../haarcascade.xml'