Home > OS >  Why is it so difficult to download opencv?
Why is it so difficult to download opencv?

Time:05-17

To keep a long story short. I copied this code from https://www.geeksforgeeks.org/detect-an-object-with-opencv-python/ (not really import but I still mentioned it)

import cv2
from matplotlib import pyplot as plt
  
  
# Opening image
img = cv2.imread("image.jpg")
  
# OpenCV opens images as BRG 
# but we want it as RGB and 
# we also need a grayscale 
# version
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
  
# Creates the environment 
# of the picture and shows it
plt.subplot(1, 1, 1)
plt.imshow(img_rgb)
plt.show()

Anyways this line below has this error that says "Import "cv2" could not be resolved"

import cv2

When I run this code below to download opencv. I get the message that the "Requirement already satisfied: "

pip install opencv-python

Despit me closing vscode the reopening I still get that message that "Import "cv2" could not be resolved" Ive updated pip, I've printed the version of cv2 in cmd which I get 4.5.5, I've deleted python and python intelligence. I just cant to figure out why opencv isn't working. Any suggestions? (I'm a newbie lol)

CodePudding user response:

Two reasons come to mind:

1.) Either the path to the opencv library is not set in the editor so it cannot find it. That happens sometimes if you changed settings in the runtime environment.

2.) Or you created a virtual environment prior to installing opencv. In that case, install opencv for your virtual environment as well.

It is difficult to tell what is going on with so little information. The above causes are just the most likely. Check your runtime environment and your library paths.

CodePudding user response:

u need to do several things to clear it up, open cmd and follow this,

step 0: of course u must check python is added in ur system variable path,two path is essential, for me two dir is like this (add for ur installation, if have added this just skip this step)

C:\Python\Scripts\
C:\Python\

step 1: clear current package from python site package

pip uninstall opencv-python

step 2: clear cache from pip , for fresh install

pip cache dir

u will get a list of dir printed, now browse to that dir using explorer and delete everything in there.

step 3: check pip package install dir for ur python, it should be like "..\python\lib\site-packages" , to check this type into cmd

pip list -v

this will give u dir reference of all site packages, and u should check whether it is "..\python\lib\site-packages" or not.

step 4: reinstall the opencv , u can install community contrib version of opencv, this is extended package of opencv-python with extra modules

pip install opencv-python

or

pip install opencv-contrib-python

step 5: type python in cmd, if python idle responded in cmd, then ur system found python, then type import cv2 and make sure it is imported. If it is imported successfully then u need to make sure ur vs code python plugin in up to date and configured well, for me i have added python path to system variable and didn't have to configure the plugin, it works well.

let me know if u have the problem unsolved.

  • Related