[PYTHON] Create a wheel of your own OpenCV module

I'm grafi working part-time at ALBERT Co., Ltd. I haven't posted to Qiita before, but I'll post it for the first time.

Motivation

When doing data analysis, I often read images with Python and perform simple processing, so I would like OpenCV. Most people're done with Anaconda, but I personally don't want to leave the Python ecosystem, and I'd love to specify which libraries to link with.

That's why you're building OpenCV. I think there are explanations about how to build it here and there, so I will omit it. This will create a file such as cv2. Somehow .so as a Python module. To install it globally, just do make install.

However, from the environment created with virtualenv (or venv), this cv2 module cannot be seen unless --system-site-packages is added. If I somehow copy cv2.so to site-packages in the virtualenv environment, it works, but it's just not cool.

So I wondered if I could make this a wheel.

How to do

What I really want to do is "I want to make one pre-built extension module into a wheel", but it seems that this is not supported.

Therefore, I made a lot of mistakes, but I managed to do it. I mainly referred to

There are three.

First, the directory structure is as follows.

--+-- cv2 --+-- _native --+-- cv2.somehow.so
  |         |             |
  |         |             +-- __init__.py
  |         | 
  |         +-- __init__.py
  |
  +-- setup.py
  |
  +-- MANIFEST.in

In __init__.py directly under the cv2 directory

import * from _native.cv2

Write. The __init__.py under the _native directory can be empty.

setup.py is as follows

from setuptools import setup
from setuptools.dist import Distribution

class BinaryDistribution(Distribution):
    def has_ext_modules(foo):
        return True

setup(
    name='cv2',
    version='1.0',
    packages=['cv2'],
    include_package_data=True,
    distclass=BinaryDistribution,
)

Also, MANIFEST.in is

include cv2/_native/cv2.somehow.so

Write.

Now when you run python setup.py bdist_wheel, you will have a wheel with a file name like cv2-1.0-cp36-cp36m-linux_x86_64.whl under dist. I'm happy.

Commentary

It's simple to do, just create a dummy package and export all the definitions in the original cv2 module. I'd like to hide the original cv2 module for the time being, but if I renamecv2. Somehow .so to _cv2. Somehow .so, it will not work, so I solved it by putting it in another package. did.

By forcibly telling you that the extension module is included by setting has_ext_modules = True, you can make the wheel so that the installation will be rejected if you bring it to another OS.

application

numpy and scipy will not perform well unless you compile them yourself and link them to a high-performance matrix operation library, but it is also convenient to have the compiled result as a wheel. I will write this detail later.

Recommended Posts

Create a wheel of your own OpenCV module
Create your own exception
Create a Python module
Create your own Django middleware
(Note) How to pass the path of your own module
Reinforcement learning 23 Create and use your own module with Colaboratory
[Python] logging in your own module
How to create your own Transform
Create your own name resolution service
[Django] Create your own 403, 404, 500 error pages
Create a compatibility judgment program with the random module of python.
Create your own Linux commands in Python
[LLDB] Create your own command in Python
Create your own DNS server with Twisted
Create a model for your Django schedule
Create a python environment on your Mac
Make a relation diagram of Python module
Create your own Composite Value with SQLAlchemy
Create a graph using the Sympy module
Create your own virtual camera with Python + OpenCV and apply original effects
To import your own module with jupyter
[Machine learning] Create a machine learning model by performing transfer learning with your own data set
[python] Create a list of various character types
Make your own module quickly with setuptools (python)
[Python] A rough understanding of the logging module
Create a table of contents with IPython notebook
Flow of creating your own package with setup.py with python
Create a dataset of images to use for learning
Create your own Big Data in Python for validation
[Python] 2 Create a risk-return map for your asset portfolio
Scraping your Qiita articles to create a word cloud
Make your cursor a photo of your choice on Linux
Create a batch of images and inflate with ImageDataGenerator
Call your own python module from the ROS package
Create a shape on the trajectory of an object