Book Image

Python Game Programming By Example

Book Image

Python Game Programming By Example

Overview of this book

Table of Contents (14 chapters)
Python Game Programming By Example
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Supporting multiple versions of OpenCV


OpenCV 2.x and OpenCV 3.x both use the name cv2 for their top-level Python module. However, inside this module, some classes, functions, and constants have been renamed in OpenCV 3.x. Moreover, some functionality is entirely new in OpenCV 3.x, but our project relies only on functionality that is present in both major versions.

To bridge a few of the naming differences between versions 2.x and 3.x, we create a module, CVBackwardCompat. It begins by importing cv2:

import cv2

OpenCV's version string is stored in cv2.__version__. For example, its value may be 2.4.11 or 3.0.0. We can use the following line of code to get the major version number as an integer, such as 2 or 3:

CV_MAJOR_VERSION = int(cv2.__version__.split('.')[0])

For OpenCV 2.x (or earlier), we will inject new names into the imported cv2 module so that all the necessary OpenCV 3.x names will be present. Specifically, we need to create aliases for several constants that have new names in OpenCV...