<< return to Pixycam.com

How to get many blocks with the pantilt python program?

Hi all,

Using the Pixy pantilt (CMUcam5), I understand that there are 2 python examples, each use different pixy module :

  1. get_blocks.py : detect blocks without moving and store them into a BlockArray struct;
  2. pantilt.py : detect blocks, but choose only one to track

Then, I would like to know how we can get more than one block with the python pantilt program (I’m beginner at python).

The goal is to get all the blocks and passing them through a neural network which would allow to choose only one to track, but without going into details, increasing the size of the buffer shows that it detects several targets but still returns a single block (I guess the biggest one). This seems logical since the program pantilt use the struct Block() instead of struct BlockArray().

Grouping the 2 pixy modules and import them as 2 different names in my python program didn’t solve my problem since the functions that work depend on the pixy_init() that is initiate …

So if you have any suggest in how I could obtain a one module pixy which is simply the pixy module for tracking with the possibility to get many objects.

Thanks for your help.

Hi Marino,
You can replace the line in the pantilt program that says:

count = pixy_get_blocks(BLOCK_BUFFER_SIZE, block)

with

count = pixy_get_blocks(100, blocks)

while also declaring blocks as an array of 100

blocks = BlockArray(100)

The program will now get up to 100 blocks and put them in the block array. You will then need to edit the rest of the program to use the blocks array instead of the single block.

Hope this helps!

Edward

Hi edge,

thank you for your reply.

I already tried to do your solution but it returns an Error when I then run pan_tilt.py : ‘BlockArray’ is not defined …


  • Pixy Tracking Demo Started +
    Traceback (most recent call last):
    File “pan_tilt.py”, line 180, in
    main()
    File “pan_tilt.py”, line 119, in main
    blocks = BlockArray(100)
    NameError: global name ‘BlockArray’ is not defined

And indeed, in the pixy.py (located in the same folder as pan_tilt.py) there is no BlockArray defined.

I tried then to copy the definition of the class BlockArray from the pixy.py from the folder libpixyusb_swig to the pixy.py of the pan_tilt … but it didn’t work either


  • Traceback (most recent call last):
    File “pan_tilt.py”, line 20, in
    from pixy import *
    File “/home/leat/pixy/build/pantilt_in_python/pixy.py”, line 93, in
    class BlockArray(_object):
    File “/home/leat/pixy/build/pantilt_in_python/pixy.py”, line 106, in BlockArray
    swig_destroy = _pixy.delete_BlockArray
    AttributeError: ‘module’ object has no attribute ‘delete_BlockArray’

Maybe I’m wrong, but I guess the 2 programs use 2 different pixy.py modules which use also 2 different _pixy.so files …

Hi Marino,
I just did some copying and pasting from the get_blocks example here:

I didn’t do a complete job though, just trying to help you, provide hints. It looks like you have some simple programming issues with your program :grinning:

Have you tried running the get_blocks example? If you familarize yourself with the code, I think you’ll get find the modifications easier.

Edward

Hi,

Thank you again Edward, I try each solution you gave me.
But ok, if I resume here:

Using only the pan_tilt.py program to do tracking later:
1. How can I integrate and use BlockArray() class in the pan_tilt.py program ?

  • copying the definition of BlockArray and replacing Block() by BlockArray() didn’t work
  • importing it directly from the get_blocks.py directory lets me create BlockArray object in the pantilt program but …

It sends me to a second problem which is :
2. How can I make the pixy_get_blocks() in the “pan_tilt.py” work with BlockArray() since it only works with Block() object ?

  • Replacing/importing this specific function from the get_blocks.py directory didn’t solve the problem… indeed, it always returns 0 (= no blocks detected, even if they are truly present)
  • I guess the pixy_init() of the pan_tilt.py doesn’t allow the use of pixy_get_blocks() from get_blocks.py , that’s why the 0 return.

It looks like I have to modify the pixy.py file, ok it’s still fine. But since this one depends on some other files like _pixy (?) or ‘pixy_wrap.cxx’ which translates c++ functions and classes into python, I get lost … Maybe you have some advices/simplier solutions to solve it.

Marino

Hi Marino,
Do you know someone who feels comfortable with Python that can help you? Stack Overflow is also a great resource for programming-related questions.

We can’t write/rewrite the programs for you (sorry!)

Edward

Hi Edward,

ok then, I will continue to find solutions by myself :pensive:.

Thank you for your help. If you have or get some advices about my problem, let me know I’m still open to take it :wink:

Marino

Nevermind, I finally get it work ! :+1::+1::+1:

For those who may need it, it is just to modify the ‘pixy.i’ from the pixy/src/host/pantilt_in_python BEFORE building the build_pantilt_python_demo.sh:


%module pixy

%include "stdint.i"
%include "carrays.i"

%{
#define SWIG_FILE_WITH_INIT
#include "pixy.h"
%}

%array_class(struct Block, BlockArray);

int  pixy_init();
void pixy_close();
void pixy_error(int error_code);
int  pixy_blocks_are_new();
int pixy_get_blocks(uint16_t max_blocks, BlockArray *blocks);
int  pixy_rcs_set_position(uint8_t channel, uint16_t position);

struct Block
{
  uint16_t type;
  uint16_t signature;
  uint16_t x;
  uint16_t y;
  uint16_t width;
  uint16_t height;
  int16_t  angle;
};

then, don’t forget to make changes in the pan_tilt.py like:
_ to replace Block() by BlockArray()
_ to index the block before trying to get its information : block[0].x block[0].y …

Marino

Nice work! Thanks for sharing :slight_smile: