zolotl
zolotl

Reputation: 41

OpenCV Error: Can't open xml file in read mode using cv2.CascadeClassifier

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:[email protected]] 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

Upvotes: 1

Views: 16018

Answers (1)

zolotl
zolotl

Reputation: 41

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.

Upvotes: 1

Related Questions