AttributeError: partially initialized module 'cv2' has no attribute 'CascadeClassifier' (most likely due to a circular import)

Question:

I’ve been going round in circles for ages trying to figure this out. Why am I getting this attribute error? I’ve tried using absolute references, and get the same issue. PyCharm is also highlighting CascadeClassifier, cvtColor and COLOR_BGR2GRAY saying it cannot find reference in cv2.py. I’m not sure if more information is relevant to solving this problem, so please ask if more is needed.

import cv2
face_cascade = cv2.CascadeClassifier('read_only/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('read_only/haarcascade_eye.xml')
grayed_images = []
for x in np_images:
    gray_img = cv2.cvtColor(x, cv2.COLOR_BGR2GRAY)
    grayed_images.append(gray_img)
    print(x)
Asked By: drwie

||

Answers:

If I am correct, you are using an environment different from where you run.

Step1: In command line/terminal where you see the opencv-python when you run pip list:

run python command. Copy your code, check if it works (you can simply import cv2 alternatively)

If it works, my idea should be correct. Otherwise, there is something bigger.

Step 2: (Assuming step1 works.) In Pycharm, Under Run > Edit Configurations, change python interpreter to whichever interpreter is you are that has opencv.

Step2 better alternative: On Pycharm, open the terminal, pip install opencv-python. After that you should have the opencv.

Answered By: smttsp

Downgrade opencv to the version 4.5.5.64.

You can install opencv using
pip install opencv-python==4.5.5.64.

This works for me

Answered By: Muhammed Anees V
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.