1

I just started learning AI using OpenCV and am creating an AI to detect pedestrians. I ran into the problem of not being able to open and read the "hogcascade_pedestrain.xml" file.

Things I have tried:

  1. Downloading the xml file into the project directory
  2. Giving the full path as the argument
  3. Using cv2.data.haarcascade + "hogcascade_pedestrain.xml" as the argument

However, it would still give the same error:

[ERROR:0@0.019] global persistence.cpp:505 cv::FileStorage::Impl::open Can't open file: 'C:\Python\Python311\Lib\site-packages\cv2\data\hogcascade_pedestrain.xml' in read mode

Here is my code

import cv2

# Load the pre-trained classifier
pedestrain_classifier = cv2.CascadeClassifier(cv2.data.haarcascades + "hogcascade_pedestrains.xml")

frame = cv2.imread("pedestrain.jpg")

# Convert frame to grayscale
grayscaled_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Detect pedestrain
pedestrain_coordinates = pedestrain_classifier.detectMultiScale(grayscaled_img)

# Draw rectangle
for (x, y, w, h) in pedestrain_coordinates:
    cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

# Dispay frame
cv2.imshow("Clever road detector", frame)

# Wait
cv2.waitKey()

enter image description here

zolotl
  • 41
  • 1
  • 7
  • Remove here cv2.waitKey()here to cv2.waitKey() – toyota Supra Jan 04 '23 at 13:09
  • My bad that the 'here' wasn't in my original code. But I still have a problem with opening the xml file – zolotl Jan 04 '23 at 15:34
  • Again. Remove type import cv2 to import cv2. Can you post original image? – toyota Supra Jan 04 '23 at 16:50
  • Can you check whether the file really exists in the given path? It will likely be in a path similar to 'C:\Python\Python311\Lib\site-packages\cv2\data\hogcascade_pedestrain.xml' Also check whether you have permission to access the file, if it exists in the given location. – Hiran Hasanka Jan 05 '23 at 11:29
  • Thank you guys for the help :). I did more research online and stumbled upon a post saying that hogcascades are no loner supported by Open CV version above 3.0. I changed it to a haarcascade and it had no problems opening the file. Once again, thanks for your help and time! – zolotl Jan 05 '23 at 16:49

1 Answers1

1

OpenCV versions 3.0 and later DO NOT support hogcascades anymore.

See here for original post: https://stackoverflow.com/a/72701276/20926636

Changed the hogcascade to a haarcascade and it worked fine opening the file and running the rest of the code.

zolotl
  • 41
  • 1
  • 7