Python import cv2 runs forever - python

My OS is ubuntu 16.04, Python 2
I installed opencv followed this link, the only difference is that I am not using an virtual python environment.
After installation is done, there is a cv2.so file in both /usr/lib/python2.7/dist-packages and /usr/local/lib/python2.7/dist-packages.
However, when I try to import cv2 in Python, it will keep importing forever like this
Any ideas?
Update: After waiting for a loooooong time, cv2 was imported successfully

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.

Failed to execute script pyi_rth_win32comgenpy after packing with pyinstaller

I'm trying to pack a script that contains these external imports:
import keyboards
import win32gui
from PIL import ImageGrab
import pytesseract
I have a virtual environment set up with PyCharm, so I make sure to add the site-packages directory to the -paths option when running pyinstaller.
I am at a loss for what to do now since this error has never been fully answered. Some other resources (here and here) say to simply uninstall and then reinstall all packages relating to win32. I did that, but it's still not successful. Any ideas?
After playing around with it for about an hour, I've come to a solution. I added --hidden-import "pywin32" to the pyinstaller command, and it worked! So if uninstalling and reinstalling doesn't work, try adding "pywin32" as a hidden import.

VSC does not import installed python libraries

I recently got a new computer and installed VSC as well as the official python on it. I installed some extra libraries for python on it with pip but whenever I want to run a script it gives me the error: ModuleNotFoundError: No module named 'numpy', or any other library installed afterwards. The libraries that come with python do work.
When I run the same script in IDLE for example, all the libraries are found and it works fine, but I would like to be able to code in VSC.
For example:
import numpy as np
import pygame as pg
import time
Rest of the code...
The error occurs right when it tries to import the libraries.
Any ideas on what might be causing the problem?

Unresolved reference "cv2" inside cv2 (cv2.cv2)

I've looked around and people seem to have similar problems but none described my case exactly, and solutions that worked for them didn't seem to work for me (or there was no answer to the question at all).
Using pycharm, after having installed opencv-python and opencv-contrib-python I noticed that import cv2 works, but when I tried accessing cv2.imread() pycharm complained about not being able to find it.
So I went to the cv2 init file, which looks like this:
import importlib
from .cv2 import *
from .data import *
# wildcard import above does not import "private" variables like __version__
# this makes them available
globals().update(importlib.import_module('cv2.cv2').__dict__)
Pycharm detects an unresolved reference on the from .cv2 import * line and I imagine the same problem happens on the last line - I tried doing the following in a python console:
import cv2
print(__version__)
But I got a NameError, which seems to confirm my suspicion.
As I wrote, I have tried installing opencv-contrib-python but that didn't seem to do anything and frankly I'm already out of ideas.
Notes:
- I'm on Windows 10 x64.
- I'm using Python 3.6 x64.
- I have a virtual environment set up on my Pycharm project.
I'm no expert but the following line worked for me:
import cv2.cv2 as cv2
Everything seems to work afterwards. Autocompletion is also back
Have you installed opencv via terminal?
For example, like this.
$ pip install opencv-python
$ pip install opencv-contrib-python
I also experienced the same problem.
If you use pycharm, you should install opencv via pycharm.
File -> Settings... -> Project interpreter -> +
I got this same issue. You need to try a couple of things.
You need to simply import cv2 instead of cv2.cv2. Simply write "import cv2"
If you have installed any other library such as cv before, then uninstall it first.
The library that we need to install is opencv-python
You need to install it via IDE not with the terminal. Steps are as follows:
File -> Settings -> (Click on your project) -> Project Interpreter -> + -> (Type opencv-python) -> Download and install it -> It should work now.
From Python Interpreter download the version of opencv-python 4.5.4.60 and opencv-contrib-python 4.5.5.64.

Importing OpenCV with python 2.7 in Virtualenv and PyCharm

I am struggling with installing opencv for python 2.7.11 on OSX for almost three days now.
After some failures, I accomplished a first success by following the instructions here. So my basic setup is python 2.7.11 running from ~/.virtualenvs/cv_env/bin/python and I have a cv2.so located in ~/.virtualenvs/cv/lib/python2.7/site-packages/.
So good so far. Using source ~/.virtualenvs/cv_env/bin/activate, I can activate the virtualenv and than use import cv2. For some reasons, this does not work always. From time to time, I have to deactivate first and than reactivate (any guesses?).
Next, I wanted to use opencv in PyCharm. Under "preferences - Project interpreter", I selected the virtualenv interpreter and this is also working. I can import other moduals like numpy and pandas (previously installed into the vortualenv using pip). But for some reasons, I am not able to import opencv (import cv2). It always gives me
ImportError: No module named cv2
So my question is, why I am able to import opencv in terminal (at least sometimes) but not in PyCharm. Any ideas are welcomed.
Best,
Fabian
Your cv2.so located in a different directory. And you activating from another directory. I mean cv_env and cv.

Categories