1

I am trying to pass a jpeg image to detect faces using a hog classifier however I am getting an error that I don't understand.

Any help would be much-appreciated thanks.

I am using python 3.7 and open cv 4.3.0

import cv2
import matplotlib.pyplot as plt

face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades +"hogcascade_pedestrians.xml")

def detect(gray, frame):  
    faces = face_cascade.detectMultiScale(gray, 1.3, 5) 
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
        roi_gray = gray[y:y+h, x:x+w] 
        roi_color = frame[y:y+h, x:x+w]
        
        eyes = eye_cascade.detectMultiScale(roi_gray, 1.1, 18)
        for (ex, ey, ew, eh) in eyes:
            cv2.rectangle(roi_color, (ex, ey), (ex+ew, ey+eh), (0, 255, 0), 2)
            
    return frame

frame = cv2.imread('./data/images/cctv_still.JPG')  
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) 
canvas = detect(gray, frame)
plt.imshow(canvas)
plt.show()

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-40-26fd794bdcbb> in <module>
     19 frame = cv2.imread('./data/images/cctv_still.JPG')
     20 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
---> 21 canvas = detect(gray, frame)
     22 plt.imshow(canvas)
     23 plt.show()

<ipython-input-40-26fd794bdcbb> in detect(gray, frame)
      5 
      6 def detect(gray, frame):
----> 7     faces = face_cascade.detectMultiScale(gray, 1.3, 5)
      8     for (x, y, w, h) in faces:
      9         cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)

error: OpenCV(4.3.0) /io/opencv/modules/objdetect/src/cascadedetect.cpp:1689: error: (-215:Assertion failed) !empty() in function 'detectMultiScale'

Christoph Rackwitz
  • 11,317
  • 4
  • 27
  • 36
jack gell
  • 175
  • 1
  • 8
  • 1
    `assert os.path.exists(cv2.data.haarcascades +"hogcascade_pedestrians.xml")` will throw an error -- hog cascades aren't in the `haarcascades` directory. they're in an adjacent directory. use `cv.samples.findFile("hogcascades/hogcascade_pedestrians.xml")` -- there is a `HOGDescriptor` with `detectMultiScale` method but I can't get that to instantiate from a file – Christoph Rackwitz Jun 21 '22 at 15:25

1 Answers1

0

hogcascades are no more supported since opencv3.0, so it did not load your yml. (also a full-body pedestrian cascade is not feasible for face detection)

please choose one of the haar or lbp cascades instead

(with a more recent cv2 (4.6.0-pre here) you'd get a more informative error: The function/feature is not implemented) HOG cascade is not supported in 3.0 in function 'read' )

berak
  • 1,226
  • 2
  • 5
  • 7