<< return to Pixycam.com

Libpixyusb python module trouble - python 3 related?

Hi there thanks for looking at this,

I am a beginner at python and having trouble using the libpixyusb module.

I followed the instructions to build the module on my mac, and was able to build and run the demos via the command line.

Now I am trying to include the module in another project, and I am not sure where the “module” is or what files it comprises.

I need to get all of the pixy module dependencies together because my application is a game, and I need to package all the dependencies along with the python interpreter in the game itself.

I have tried copying all of the files in the ‘python_demos’ folder into my game folder, and “import pixy” in the game python code doesn’t throw an error - but none of the pixy attributes are defined or usable. In short, I’m stuck and in over my head - I’d appreciate any advice or new avenues to try.

Thanks in advance.

Hello,
Did you copy the ~/build/python_demos directory over? If those demos work from that directory, you should be able to copy them to different locations and run them. hmm…

Edward

Hi Edward, thank you for looking at this!

I copied the build/python_demos directory and I am able to run the demos in the new directory from the terminal python.

However even though all the python_demos directory content is being loaded by the game, and it is able to “import pixy” without an error or warning, trying to use anything from pixy in the game python causes attribute not defined errors.

The game’s python interpreter is python3.6 and it has a lib file to which I tried adding a new directory named “pixy” with the pixy demos contents, but that made no difference: “import pixy” doesn’t error but I can’t access the attributes. I have noticed that the other libraries mostly have an init file in them, but wasn’t sure if this was necessary in pixy’s case.

Hello,
I believe you need to copy the pixy.i, pixy.py and swig.dat files over as well.

Edward

Hi Edward, thanks for the tips I tried them out - I discovered there was no swig.dat file at all so began thinking my build process was screwed up.

I have done an entirely fresh build from master, and I think the issue now is with python3.

python2 can run the examples fine, but python3 throws the following error:

Traceback (most recent call last):

File “get_rgb_demo.py”, line 2, in <module>

import pixy

File “/Users/me/Downloads/pixy2-master-2/build/python_demos/pixy.py”, line 15, in <module>

import _pixy

ImportError: dlopen(/Users/me/Downloads/pixy2-master-2/build/python_demos/_pixy.so, 2): Symbol not found: _PyClass_Type

Referenced from: /Users/me/Downloads/pixy2-master-2/build/python_demos/_pixy.so

Expected in: flat namespace

in /Users/me/Downloads/pixy2-master-2/build/python_demos/_pixy.so

I tried modifying the swig flag in build_python_demos.sh from -python to -py3 but it made no difference - it works as expected when running on python 2.7.

Does it work with Python3 when you run from the python_demos directory? Or are you saying Python3 doesn’t work when you copy over everything from the python_demos directory somewhere else?

Edward

Hello,
A lot has changed since last year when I got the library to work with python3, so I tried to use the new code which works with python 2.7, but it failed on python3. So then I tried my old code, but it would not build.
Does anyone have any ideas as to how to run the current pixy2 code using python3? Here is the error I get when I try to build.

ImportError: /var/lib/cloud9/pixy/py3/pixy 2/build/python demos/pixy.so: undefined symbol: PyInstance_Type

I am using a BeagleBone Blue with the Stretch IoT Debian 9.9 image.

~Mr. R^2
P.S. I need to use python3 because the drive code for the robot requires python3 as opposed to 2.7.

Hi, sorry for the trouble! I understand that reusing Python .so files isn’t possible from version to version. Where did the pixy.so file that’s referenced in the error come from, and what version of Python was it compiled with?

Thanks,
Jesse

1 Like

Hello,
Thank you for your reply. I tried many of the branches on your git. They all seemed to fail with the same error. The most recent one (and the one that gave me the error) is from the main branch on your git. After building the code following the directions here, https://docs.pixycam.com/wiki/doku.php?id=wiki:v2:building_libpixyusb_as_a_python_module_on_linux
I ran using python 2.7 and it worked. My python 3 version is 3.5.3. I am trying to upgrade, but it seems Debian does not upgrade Python packages often. Is there a special command to compile for Python3 versus Python 2? I remember there was a file to change, but the code has updated recently and cannot find it.

I will update when the upgrade is done.
~Mr. R^2

Edit: The update did not work, however, I looked back at my old code and found the line that needs to be changed. It is line 32 in build_python_demos.sh.
python swig.dat build_ext --inplace -D__LINUX__ should be python3 swig.dat build_ext --inplace -D__LINUX__
I have not tested all the modules (and have not rebuilt the servo platform yet), but get_blocks_python_demo.py seems to work just fine. Thank you so much for your help.

PPS: I tried all the other python demos, and they seemed to work well. This is true of the pixy_set_lamp_demo.py too.

1 Like

Hello,
Sometimes modules get installed for Python 2 vs Python 3 and vice versa. It can create issues. I’m glad you figured it out. :slight_smile:

Edward

1 Like

Thank you. I realized, the issue was that the way the library is coded, it uses the default python install. However, on our beaglebone, the default is 2.7. So, it probably would have worked had we changed the default, but this solution literally only required adding one number to the build file to make it work.

I have a follow-up question, and sorry if it is too off-topic. If I wanted to follow an object using the servo platform mounted to a robot, do you recommend using the pan-offset as the input for a drive base PID? Would that get us close to centered? I am having difficulty wrapping my head around the two variables (the offset from center frame and the offset of the angle of the pan servo). What we are going for is a small robot that follows an object like you all have on the main page of the website.
Thank you again.
~Mr. R^2

Hello,
You might look at the pan-tilt code for the Arduino. It takes the object position in the image and generates offset (position) commands for the pan and tilt servos. The key to understanding this is that it’s a velocity controller, not a position controller. That is you want to control the velocity of the servos and not the position. You can control the velocity of the servo by taking the current position (currPos) and adding the desired velocity to the position. A small value will make the servo move slowly and a large value will make the servo move quickly.

Hope this helps!

Edward

2 Likes

Thank you. I think it does.

~Mr. R^2