<< return to Pixycam.com

User Tools

Site Tools


wiki:v2:building_libpixyusb_as_a_python_module_on_linux

**This is an old revision of the document!**

Using the Python API on Linux

1.0. Introduction

Pixy2 now has a Python wrapper for libpixyusb2. For now, this is only available for Linux-based controllers (e.g. Raspberry Pi, BeagleBone). The libpixyusb2 library provides a software application programming interface (API) for communicating with Pixy2 over USB. The following procedure will outline the steps to build libpixyusb2 as a Python module.

This procedure assumes the following:

  • Your system has Linux Mint installed
  • Your system is connected to the internet

Although this procedure uses Linux Mint, it should work with any Linux distribution that uses the Debian package manager (e.g. Ubuntu).

2.0. Procedure

2.1. Install dependencies for building Python SWIG module

Start a terminal application. In the terminal window type:

# sudo apt-get install swig libusb-1.0-0-dev g++ git build-essential

2.2. Download Pixy2 source code

In the terminal window type:

# git clone https://github.com/charmedlabs/pixy2.git

2.3. Build the Pixy2 SWIG module

In the terminal window type:

# cd pixy2/scripts
# ./build_python_demos.sh

2.4. Try it out!

Change directory to the build folder. In your terminal window type:

# cd ../build/python_demos

2.4.1. Run the get blocks demo

Make sure your Pixy2 is connected to a USB port on your computer. Then in a terminal window type:

# python get_blocks_python_demo.py

If all goes well you should see blocks:

Pixy2 Python SWIG Example -- Get Blocks
frame 0:
[BLOCK: SIG=1 X=157 Y=103 WIDTH=314 HEIGHT=207]
frame 1:
[BLOCK: SIG=1 X=157 Y=103 WIDTH=314 HEIGHT=207]
frame 2:
[BLOCK: SIG=1 X=157 Y=103 WIDTH=314 HEIGHT=207]
frame 3:
[BLOCK: SIG=1 X=157 Y=103 WIDTH=314 HEIGHT=207]

If you don't see any block information, try running as superuser:

# sudo python get_blocks_python_demo.py

2.4.2. Run the get lines demo

Make sure your Pixy2 is connected to a USB port on your computer. Then in a terminal window type:

# python get_lines_python_demo.py

Example output:

Pixy2 Python SWIG Example -- Get Line Features
frame 0:
[VECTOR: INDEX=1 X0= 50 Y0= 15 X1= 50 Y1= 5]
[VECTOR: INDEX=2 X0= 56 Y0= 8 X1= 67 Y1= 4]
frame 1:
[VECTOR: INDEX=0 X0= 55 Y0= 27 X1= 51 Y1= 2]
frame 2:
[VECTOR: INDEX=0 X0= 55 Y0= 27 X1= 51 Y1= 2]
frame 3:
[VECTOR: INDEX=1 X0= 57 Y0= 31 X1= 53 Y1= 21]

If you don't see any line feature information, try running as superuser:

# sudo python get_lines_python_demo.py

Errors

If you are getting an error like:

ImportError: No module named _pixy

Python is having trouble finding the Pixy Python library. This may be because you are running in another directory. Just copy over the _pixy.so and pixy.py files in the pixy2/build/python_demos directory to your directory and it should work.

If you still get an error, you may be running into a Python version issue. Please see the next section.

Python2 vs Python3

This library will support both Python2 and Python3, but sometimes it will compile fine but provide import errors when you try to run one of the Python demos.

If you want Python3 support, for example, make sure that your default “python” is Python3 before running buildpythondemos.sh. That is, make sure that Python3 is the version of Python that runs when you type in the “python” command in your shell. You can test this by running “python –version” at the shell prompt.
If it's not version 3 (or higher), change the default version to be Python3. The recommended way to do this may depend on your Linux distribution.

wiki/v2/building_libpixyusb_as_a_python_module_on_linux.1565290297.txt.gz · Last modified: 2019/08/08 18:51 by pixycam