How to import cv2 in python project "Visual Studio 2015"? - python

I would like to start with OpenCV . I followed some tutorials for installing OpenCV in Windows.
After downloading and extracting OpenCV
First, I create environment variable and path
Then I moved to python in Visual Studio. I installed python and I started with a very simple tutorial, but I faced some problems with python.
First problem was with numpy module was not found. solved
cv2 error I couldn't solved it, I followed instruction said that I need copy cv2.pyd file in ..\Python34\Lib\site-packages
afterword I tried to >>> import cv2
but I got this error
Thanks for all of you.

Related

Why the Python crashes on importing cv2?

I am just beginning to learn OpenCV in python for a face recognition project, I installed opencv-python in an python environment using pip and tried to run this simple program
import cv2
img = cv2.imread('.\\jerry.jpg',1)
cv2.imshow('jerry',img)
cv2.waitKey(0)
but as I executed this, the python stoped without any error. Like this:
(venv) C:\Users\ashok\Documents\_StudyMaterial\Code\Face Recognition - Open CV practice>python -u "c:\Users\ashok\Documents\_StudyMaterial\Code\Face Recognition - Open CV practice\1_read.py"
(venv) C:\Users\ashok\Documents\_StudyMaterial\Code\Face Recognition - Open CV practice>
When I run this program
a = 12
b = 2131
print(a+b)
import cv2
print(cv2.__version__())
It outputs
(venv) C:\Users\ashok\Documents\_StudyMaterial\Code\Face Recognition - Open CV practice>python -u "c:\Users\ashok\Documents\_StudyMaterial\Code\Face Recognition - Open CV practice\simple.py"
2143
(venv) C:\Users\ashok\Documents\_StudyMaterial\Code\Face Recognition - Open CV practice>
This confirmed python stopped working on statement import cv2
This same is also happening when i tried to import numpy
I tried searching for this, but all in vain. I reinstalled opencv-python, deleted and again created virtual environment, tried uninstalling and reinstalling numpy, but it didn't helped.
I am using windows 7 32-bit, python 3.8.10, these are the installed libraries in the environment
numpy==1.24.1
opencv-python==4.7.0.68
Please help me out, I am stuck here. If any other details are required, please let me know.
Thanks
Edit:
I have figured something out, It is being caused because of numpy, when cv2 imports numpy, it crashed there. This issue is being discussed at github
I tried to reinstall opencv-python, numpy. Destroyed and recreated virtual environment.

opencv is refusing to import because libcblas.so.3 is missing

I'm trying to build a text recognition system from images and I decided to use opencv and pytesseract to do this.
I'm new to using a raspberry pi so I'm using the thonny python ide that came preinstalled on the legacy os. I'm running the legacy os because I'm using a camera module to capture and save the images.
I pip installed both and both commands in the terminal stated that they were both successfully installed. Pytesseract appears to be working fine when I import it but opencv always throws an import error stating
libcblas.so.3: cannot open shared object file: no such file or directory
How can I fix this problem?

Visual Studio Code isn't working most of the modules ex. (PIL,pygame etc)

when I try to import PIL it's says "no module named PIL" error coming up. I did install PIL and try several things same thing happen with pygame earlier. please help me out.
I'm using MacOs Catalina 10.15.3
VS Code
Python 3.7.4
you need to select the right python environment, check Using Python environments in VS Code

How to import opencv in python (not from virtualenv) [UBUNTU]

I have problem about importing OpenCV to my project.
Not actually problem, but I didn't find how to do that. I know it's trivial, but I really don't know.
I have opencv downloaded and compiled in my home directory.
I know how to import it in virtualenv, but how to import it directly from original - non virtualenv python2.7?
To import opencv in your python project import cv2

Error: No module named cv2

I have already found few questions at SO but i am unable to solve this problem using the answers there.
I am new to python. I am having python in Ubuntu 12.04. In my /usr/local/lib, there are two python folders python 2.7 and python 3.2. python 2.7 contains dist-packages and site-packages while python 3.2 contains only dist-packages.
I am trying to run a very simple opencv example with the following code:
import cv2
import numpy as np
from matplotlib import pyplot as plt
img = cv2.imread('image.JPG')
kernel = np.ones((5,5),np.float32)/25
dst = cv2.filter2D(img,-1,kernel)
plt.subplot(121),plt.imshow(img),plt.title('Original')
plt.xticks([]), plt.yticks([])
plt.subplot(122),plt.imshow(dst),plt.title('Averaging')
plt.xticks([]), plt.yticks([])
plt.show()
Error: No module named cv2
NB : This answer is a short compilation of comments above. For more details, please refer to the comments below question.
Background : OP is using SPE Stani's python editor. OP has installed OpenCV /opt/ros/hydro/lib/python2.7/dist-packages which is not detected by the above mentioned editor. Adding this path to PYTHONPATH doesn't solve the issue.
Solution (any one of the below):
Add this path to sys.path and put it in every file.
import sys
sys.path.append('/opt/ros/hydro/lib/python2.7/dist-packages')
Copy cv2.so file to any directory in the sys.path.
I am able to resolve this problem of not loading cv2 dll by following below steps on my Win 64 machine.
Download the Unofficial Windows Binaries for Python Extension Packages from gohlke site.
In my case it is opencv_python-3.2.0-cp35-cp35m-win_amd64.whl downloaded it to some folder e.g. c:\py\lib
Install this .whl using below command
pip install c:\py\lib\opencv_python-3.2.0-cp35-cp35m-win_amd64.whl
Restart the kernel and error gone.
I had this issue and I fixed it by:
Finding where the openCV library was stored (did this through my IDE).
Deleting it.
Using "pip install opencv-python" again on the terminal in my IDE
Imported as normal
Hopefully this fixes other peoples issues too!

Categories