How to install cv and cv2 both python packages on Ubuntu? - python

I have got a python code from open source community where it imports cv as well as cv2. After installing opencv using conda 'import cv2' resolved. But still I have issue with 'import cv', where it shows 'ImportError: No module named cv'.
Few other things I have tried:
1. sudo apt-get install python-opencv
2. pip install opencv
3. import cv2.cv as cv
Can any one help me how to resolve "No module named cv"?
Note that I am using python version 2.7.13
In my code I am using as below:
import cv2
import cv
And getting the following error:
import cv
ImportError: No module named cv

The cv module was removed actually before OpenCV 3.0. Regardless, most things accessible through the cv module are still accessible. The cv.CV_CAP_PROP_FPS setting is now cv2.CAP_PROP_FPS.
Basically, the constants given by CV_XXX_XXX used to be accessible through the old cv module via cv2.cv.CV_XXX_XXX but now it's not necessary---just remove the prepended CV_ and use the cv2 module.

Related

Import Cv2 not working despite opencv installed

I have installed OpenCV with the command pip install opencv-contrib-python and when I run the command pip list I see opencv-contrib-python has been installed and yet import cv2 does not work for me and it throws
Traceback (most recent call last):
File "\[filename here\]", line 1, in \<module\>
import cv2
ImportError: No module named cv2
and I have tried uninstalling and reinstalling OpenCV and it still does not work. I tried different development environments like Pycharm and visual studio code. I also have my pip upgraded and my OpenCV is version 4.5.5 which is the latest version. I have tried using python 3.10 and 3.7 but the same error still shows. I have also tried using
import sys
sys.path.append('/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages')
which was recommended in some answers and I have tried just installing opencv-python and using that and the same error still shows. I checked and I have cv2 in
my folder
but despite this, it keeps showing no module named cv2 every time I try to run import cv2
I use macOS Monterey version 12.2.1 and for my terminal, I use Darwan OS which is a Linux distro.
I tried:
pip install opencv-python
pip install opencv-contrib-python
putting this at the first lines of code:
import sys
sys.path.append('/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/site-packages')
You have to use python2.7. As the manual says: "Currently, only the Python 2 version of the cv2 module is built and included in the latest release."
You can only use python3 if you manually build from source code.

Pycharm imported Opencv-python lib can not use cv2 module directly

I have installed the opencv-python lib (Here the installed lib) but when I use a function (cv2.VideoCapture(0)) of the imported cv2 (import cv2) module, pycharm raised the error:
AttributeError: module 'cv2' has no attribute 'VideoCapture'
While in the python basic IDE (IDLE) it works great.
I saw the solution for pycharm was to import cv2 this way:
from cv2 import cv2
But I don't understand why I should import it like that in pycharm

ImportError: No module named scenedetect on Linux Ubuntu

I'm trying to use pyscenedetect library on python for videos but I get this error when using the python interface and when I use the command line interface I get the error "ModuleNotFoundError: No module named 'cv2'"
even though I believe I installed both correctly according to the documentations.
I have trying to look for different ways to import opencv for the second error but to no avail. As for the first error i can't find any answers to my problem.
import cv2
import numpy as numpy
import os
import scenedetect
from scenedetect.video_manager import VideoManager
from scenedetect.scene_manager import SceneManager
from scenedetect.frame_timecode import FrameTimecode
from scenedetect.stats_manager import StatsManager
from scenedetect.detectors import ContentDetector
If you have pip you can try
pip install opencv-python
If you have anaconoda, you can try
conda install -c conda-forge opencv
it's probable that you installed it on a different python installation in your PC.
To know where your python installation is you can launch python and:
import sys
sys.path
To get the list of everything you have installed you can:
pip freeze > installed_modules.txt
Try only running
import cv2
So that you can test it
I found the problem. As Ivan was pointing out, the problem was with openCV.
I used the following command:
sudo apt install python3-opencv

Why can't I import opencv3 even though the package is installed?

I currently am running Python 3.5 and using Spyder from Anaconda as my IDE. I am running this on a Windows machine.
When I write import cv3 at the top of my code, it returns the error ImportError: No module named 'cv3'
I attempted to install opencv3 again with the command conda install -c https://conda.binstar.org/menpo opencv3 in the Command Prompt. It is apparently already installed because it returned
Fetching package metabase...............
Solving package specifications: .
# All requested packages already installed.
# packages in environment at C:\Users\Joey\Anaconda3:
# opencv3 3.1.0 py35_0 https://conda.binstar.org/menpo
Am I importing cv3 wrong? How do I fix this error?
Update: Tried import cv3 instead of import cv2 but got the following error: ImportError: cannot import name 'cv2'. The wording on the two errors is different, so python must acknowledge there is opencv installed but it does not work for some reason. Any ideas?
Ironically enough, the module is still called cv2 because it doesn't represent the version of opencv but the actual C++ API underneath which is, to be contrasted from the C API, named - cv2... So try with: import cv2
Problem solved by using command pip uninstall opencv-python in the Command Prompt.
I have attempted several installations of opencv and I suppose one may have downloaded badly and Anaconda was attempting to read that one. I looked into the build of some of the other installations I attempted and some were for Python 2.7. Maybe that contributed to the error.
Thankfully, this worked. Now import cv2 works perfectly. No errors.
I used the same approach to install the package. However, I could not import the library using the name opencv3. I had to use cv2 which worked for me.
Elaborating on #zwer's answer, check the version of OpenCV after import cv2.
>>> cv2.__version__
'3.1.0'
So basically it's calling the OpenCV3 library.

Import media on python gives Import Error

Here is the piece of code:
from PIL import Image
from pygraphics import media
toy_story = media.Movie()
I get the following error:
ImportError: No module named 'Image'
If I comment out the from pygraphics import media line it works. But then I need the media package. what's going on here. I have installed all the required packages by doing pip install <module name>. If I comment out from PIL import Image I still get the same error. What is going on here?. I have looked at other similar questions on here. But none of them answer this question.
TL;DR import media doesn't work. It gives the error ImportError: No module named 'Image'. But I have already installed Image via PIL. Help!!
I am on a mac and using python 3.5.1.
In your terminal, run python3 -m pip install <module_name>. pip has likely installed the module for python2, which you can verify by running help("modules") in python2 and python3. Running pip -V will also tell you which version of Python pip is installing modules for.

Categories