0

I'm currently working on a Python project which is a very simple image classifier that uses the kNN approach. I'm required to use Python version 3.8, so I'm using an Anaconda virtual environment for this.

I'm having trouble with the cv2 module, as I keep getting the following error:

Traceback (most recent call last): File "", line 6, in import cv2 ModuleNotFoundError: No module named 'cv2'

Here are all of my imports:

import argparse
import csv
import distutils.util
import os
import Dummy
import cv2

import numpy as np
from scipy.spatial.distance import hamming, cityblock
import math
from operator import itemgetter

Here is how I'm using the cv2 module:

def readAndResize(image_path, width=60, height=30):
    image = []
    resize = (width, height)
    
    #checking if image_path is a valid path
    if os.path.isfile(image_path):
        #IF TRUE: reading the image and storing in variable "readImage", resizing readImage and appending it to image list
        readImage = cv2.imread(image_path)
        resizedImage = cv2.resize(readImage, resize)
        image.append(resizedImage)
        return image
    #IF FALSE: return empty image list
    else:        
        return image

I really don't understand what could be causing this error.

Here's what I've tried so far:

  • pip installing opencv-Python

  • pip uninstalling and reinstalling opencv-python

  • Checking which directories Python is searching for installed packages, as well as the directory where the packages are being installed. Everything seems to check out.

  • I even tried deleting my old venv and creating a brand new one, installing all of the packages again, and still can't seem to solve or even figure out what is causing this error

I would really appreciate it if anyone can point me to any solutions or suggestions on how I can fix this issue. Any help would be greatly appreciated. Thank you!

Ajeet Verma
  • 2,938
  • 3
  • 13
  • 24
Smajy
  • 1
  • `opencv-python` requires python version `≥ 3.6` – Jordy Apr 18 '23 at 13:23
  • Have you checked whether your environment was activated? Your pip probably installed in some other venv, or your global python environment. – Souyama Apr 18 '23 at 13:25
  • 1
    Does this answer your question? [Cannot find module cv2 when using OpenCV](https://stackoverflow.com/questions/19876079/cannot-find-module-cv2-when-using-opencv) – Kulasangar Apr 18 '23 at 13:36
  • @Souyama Venv has definitely been activated. – Smajy Apr 18 '23 at 14:20

1 Answers1

0

Try installing it with Conda

conda install -c conda-forge opencv

For more infos go see Conda's documentation

MidKnight
  • 75
  • 7