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]) ...

Get Python Game Programming By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.