I want to write to short code snippet in python, to determine which version of OpenCV has been installed in my System. How do i do it ? Thank you.
3
-
Anything more to this? Do you have the Python bindings to OpenCV? Is this for Windows and/or Linux and/or Mac, etc? Are you assuming that OpenCV's libraries will be installed to some specific location, or will the location of binaries be passed in?– wklApr 20, 2012 at 15:27
-
Hey, it is assumed that OpenCV is installed. But what if i had just cv installed. I think there is nothing called __version__ in cv– vijaym123Apr 20, 2012 at 15:57
-
I think is not conclusive enough, how about checking it first?– karlphillipApr 20, 2012 at 16:36
Add a comment
|
4 Answers
>>> from cv2 import __version__
>>> __version__
'$Rev: 4557 $'
If that doesn't work then, use cv
instead of cv2
.
-
3Hey, it is assumed that OpenCV is installed. But what if i had just cv installed. I think there is nothing called __version__ in cv. Apr 20, 2012 at 15:55
Convenient functions to check OpenCV version in run-time
def cv2():
return opencv_version("2")
def cv3():
return opencv_version("3")
def cv4():
return opencv_version("4")
def opencv_version(version):
import cv2
return cv2.__version__.startswith(version)
Useful when performing cv2.findContours()
since return signature varies by version
# Using OpenCV 2.X or OpenCV 4
if cv2() or cv4():
cnts, _ = cv2.findContours(image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Using OpenCV 3
elif cv3():
_, cnts, _ = cv2.findContours(image, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
In terminal write:
- for python 2.X
python2 -c 'import cv2; print cv2.__version__'
- for python 3.X
python3 -c 'import cv2; print(cv2.__version__)'