0

I'm new to openCV and am trying to get openCV to work my USB webcam on Win7 with Python 3.8. I've got the basic tutorial from here modified from Raspberry Pi cam by the same author here. which is:

#!/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()

It should run out of the box, but I get the error below and I'm not sure why. CV_flag and gray have data and the other parameters are filled. Any ideas.

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'
Ghoul Fool
  • 6,249
  • 10
  • 67
  • 125
  • 1
    Sounds like it is this problem: https://stackoverflow.com/q/30508922/771848 – alecxe Dec 09 '19 at 22:02
  • you have to use full path to `xml` - lucky there is variable with folder name `cv2.data.haarcascades` - `cv2.CascadeClassifier( os.path.join(cv2.data.haarcascades, cascPath) )` – furas Dec 09 '19 at 22:39
  • Turns out that haarcascade.xml doesn't exist for 2.2 I'm using haarcascade_frontalface_default.xml instead. Let's hope that's ok. – Ghoul Fool Dec 10 '19 at 19:34

1 Answers1

1

The faceCascade classifier is empty, which means it was unable to retrieve the classifier from the path provided.

You can replace the line

cascPath = "haarcascade.xml"

with:

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

where you provide the full path of the xml file for cascPath.

Lalith Nag
  • 36
  • 2