What is the different between opencv-python library and cv2 library? - python

I am trying to use openCV. Normally, When I run command panel on desktop, I can see version of my openCV:
But when I couldn't import to my project. Normally, I was adding opencv-python library from interpreter but I builded cv2 library which version is 4.4.0 to use gpu. So I haven't got a opencv-python library at site-packages but I have cv2 file in site-packages. Unfortunately I couldn't import it to my project. How can I use this opencv version or file which name is cv2.cp37-win_amd64.pyd?
I can use all other libraries in site-packages. But when I couldn't add cv2 library. Here the a basic image read from file :
The interesting part of the problem is, all cv2 function cannot be found but program is working :D I am really confused.

cv2 is the module import name for opencv-python, "Unofficial pre-built CPU-only OpenCV packages for Python". The traditional OpenCV has many complicated steps involving building the module from scratch, which is unnecessary. I would recommend remaining with the opencv-python library.
The IDE probably doesn't recognize the cv2 commands because you are using a 64 bit version of opencv-python on a 32 bit version of Python or vice-versa. I believe this can be resolved by uninstalling your current OpenCV installation using pip uninstall opencv-python and then reinstalling it with the correct version following the linked tutorial.
Sources / Additional Links:
Install Tutorial: https://www.youtube.com/watch?v=Z78zbnLlPUA

Related

Symbol not found when importing cv2

I am attempting to import cv2 using python. When I use the import cv2 function in jupyter notebook I get a string of code indicating that the symbol is not found and therefore cv2 cannot be imported. enter image description here
I have tried updating opencv code using various code in my terminal including pip install opencv-python in both my terminal and my jupyter environment. Each time it said that the opencv is installed and that both environments are running on anaconda3 as expected.
Either upgrade your OS-X or install an older version of OpenCV (and python)

How can I modify the OpenCV installation path as the one built from source instead of the one installed with pip in an environment?

I built the OpenCV from the source to access GPU inference on Cuda using this great tutorial on windows. I tested the yolov3 inference (using cv2.dnn method) on GPU and it worked fine.
but after I installed another package named mediapipe using pip, the cv2 throw an error DLL load failed error when importing cv2, which of course is because of the fact that mediapipe installation also includes the opencv-python-contrib package.
Now I was wondering if there is any probable solution to fix this problem that avoids building OpenCV again from the source and does the things all over again.
I would appreciate it if you could specify solutions in detail.
OK, I fixed the problem using these steps:
after activating the environment, I uninstalled both opencv-python and opencv-python-contrib using pip. please note that mediapipe was not removed.
I rebuilt the OpenCV library by setting the python interpreter in this environment as the OpenCV CmakeListVars file which can be done using CMake software.
at the end both mediapipe and OpenCV ran with no issue.
So I did not find any shortcut to this issue, and we should run all the building process again.

OpenCV in Python using Pycharm [duplicate]

I have opencv-python installed and the .pyd file is added in the site-packages and the DLLs. The code works with images. When I want to read, show, write an image it works. But I get a warning that the functions' references cannot be found in init.py . Due to this, I can not use the auto-complete feature. Could someone help me out? I am using opencv 3.4.0 and python 3.6.4 in pycharm. I downloaded opencv via pip in the command prompt.
The problem is caused by CV2 and how __init__.py does the imports. Just ignore the warnings the program will work all the same, or you can do an import with an alias like:
import cv2.cv2 as cv2
If you have a warning on it press Alt+Enter to install and fix it. Now you will have the code completion and no other warnings (about that) on the project.
Switching to an older version of opencv solved this problem for me.
Apparently pycharm sometime breaks depending on the version of opencv. For me, the newest version was, 4.6.X unstalling and installing 4.5.X did the trick.
I use python 3.10 and my newest version for opencv is 4.6.0.66 ,by changing opencv version to 4.5.5.62 and with an alias: import cv2.cv2 as cv2 my problem has solved.
Import cv2 as follows:
from cv2 import cv2
I installed version 4.5.5.64 and imported with an alias.
import cv2.cv2 as cvv2
I was using Python 3.10.2288.0 and OpenCV 1.6.0.66.
I resolved the issue by rolling back the OpenCV version to 4.5.5.62.

Unable to import cv2 OpenCV 2.4.13 in python 3.6

import cv2
On executing the above code, it shows the following error.
Error:
Traceback (most recent call last) in
()
----> 1 import cv2
ImportError: DLL load failed: The specified module could not be found.
Unable to import cv2 in python
I have installed OpenCV 2.4.13 and Anaconda3 with python 3.6.4.
OpenCV location:C:\Users\harsh\Anaconda3
Anaconda location:C:\Users\harsh\opencv.
I have also added cv2.pyd in C:\Users\harsh\Anaconda3\Lib\site-packages.
Try pip install opencv-python instead of pip install cv2. Although the name of the package changes, you can still import it as import cv2, It will work.
Have also experienced some issues with OpenCV and Anaconda. Anaconda can fetch only OpenCV 2.4, that might not suit your versions. You should try install OpenCV this way
pip install opencv-python
This can happen if you are using windows 10 N distribution, the N distributions does not come pre installed with windows media feature pack, which is required after OpenCV version 3.4 and onwards.
The preferred solution is to install the feature pack at : https://www.microsoft.com/en-us/software-download/mediafeaturepack
Be careful to choose the version that works with your current version of windows.
If that is not an option, fall back to an earlier version of OpenCV that does not have dll dependencies, you can do that by:pip install opencv-python=3.3.0.9
If the problem still persists try using Dependency walker to find out where specifically your problems stem from and then try fixing them individually.
Since windows rolled out it's N version this problem has been seen at many places, and has many impacts across the windows environment, the fastest way to identify if you have this problem is open youtube in Edge browser, if it says HTML5 media plugin not found, this is the problem.
If the problem still persists feel free to add comments.

How do I add cv2 as a requirement in a python package?

I'm trying to make my own package which uses OpenCV Python module cv2. However when using PyCharm, it warns that the
Package requirement is not satisfied.
I suspect this is because I used the recommended method of copy/pasting cv2.pyd into my python dir. Note that pip install cv2 doesn't work.
What's the right method to ensure that requirements are met when this package is brought in?
EDIT:
My setup.py file is as follows
from setuptools import setup
setup(name='image_processing',
version='0.1',
install_requires=['numpy', 'scipy', 'cv2'],
description='Collection of useful image processing functions',
url='',
author='Bill',
license='MIT',
packages=['image_processing'],
zip_safe=False)
This is where the error shows up when trying to package my code. Normally I have no issues importing numpy or cv2. I installed Numpy using pip, and cv2 via the method mentioned above. Everything works if I just run scripts using cv2, but it's this packaging that's tricking me up.
You need to add opencv-python to requirements.
The package is here: https://pypi.python.org/pypi/opencv-python
Then, your requirements.txt file is OK, but sometimes PyCharm keeps whining. If this is the case, then next to the "Install requirement" label there is "Ignore requirement". The latter is your way to go.
(current - 2018.2 PyCharm version does not complain about cv2 when opencv-python is specified in requirements)
You have to specify the version of the opencv python package.
"pip install opencv-python==2.4.9"
Find the proper version from here.
If you want to install latest opencv python package, use
"pip install opencv-python"

Categories

Resources