gpd icon indicating copy to clipboard operation
gpd copied to clipboard

Adding support for python bindings

Open patricknaughton01 opened this issue 3 years ago • 11 comments

Can build shared library for python bindings by specifying -DBUILD_PYTHON=ON when calling cmake. This requires pybind11 to be cloned into build's parent directory. The python bindings expose a very minimal interface: it allows construction of a GraspDetector and calling detectGrasps which accepts a previously instantiated GraspDetector and a point cloud as a numpy array (of shape (n, 3)). This function returns a numpy array of shape (number of grasps, 14) where the columns specify the position, orientation (rotation matrix in row-major order), the width of the gripper, and the score of the grasp.

patricknaughton01 avatar Nov 05 '21 15:11 patricknaughton01

One remark is that it seems like the Eigen predictor segfaults if the weights file is not found.

patricknaughton01 avatar Nov 05 '21 21:11 patricknaughton01

I am just another user of the repo that looking into these features recently.

Wondering what is the difference between this implementation and the one in https://github.com/atenpas/gpd/blob/master/src/detect_grasps_python.cpp.

P.S. I am not familiar with cpp2python binding at all, so please excuse me for some silly questions.

pvmilk avatar Nov 15 '21 14:11 pvmilk

I guess this stackoverflow is the answer to https://github.com/atenpas/gpd/blob/master/src/detect_grasps_python.cpp.

pvmilk avatar Nov 15 '21 15:11 pvmilk

I was unaware actually of that method you linked. I just used pybind because I've used it before and it works nicely with numpy arrays. I haven't tried it, but do you know if the cdll/the existing python code will let you pass in a numpy array?

patricknaughton01 avatar Nov 15 '21 15:11 patricknaughton01

I am testing it at the moment. Still cannot get it compiled. But will report back if I got anything from it. But looking from here, maybe it is ok.

pvmilk avatar Nov 15 '21 16:11 pvmilk

FYI, the provide binding detect_grasps_python.cpp works with some modification, and numpy array can be pass through that ctypes python interface. Can provide the patch if anyone needed it (Update as of July 2023).

pvmilk avatar Nov 19 '21 20:11 pvmilk

Can build shared library for python bindings by specifying -DBUILD_PYTHON=ON when calling cmake. This requires pybind11 to be cloned into build's parent directory. The python bindings expose a very minimal interface: it allows construction of a GraspDetector and calling detectGrasps which accepts a previously instantiated GraspDetector and a point cloud as a numpy array (of shape (n, 3)). This function returns a numpy array of shape (number of grasps, 14) where the columns specify the position, orientation (rotation matrix in row-major order), the width of the gripper, and the score of the grasp.

Hi, after I install your branch, I use the commend 'python3 test.py', the python shows "No module named gpd_python".

I use the following commend when I install your branch: mkdir build && cd build cmake -DBUILD_PYTHON=ON .. make -j8 sudo make install

I already installed pybind11 under the parent dir, and no error occurred during compiling.

However, "No module named gpd_python".

Maybe there should be a "setup.py" file that I can "python setup.py develop" to install "gpd_python"?

or

Any step I wrongly ran caused "No module named gpd_python"

tycoer avatar Jul 17 '22 07:07 tycoer

The library might be inside the build directory in which case you might be able to solve via any one of the following:

  • Move either test.py or the built shared library into the same directory.
  • Add the build directory to the python path (sys.path.append("build/"))
  • Change the import to from build import gpd_python as gpd

Let me know if these don't work. Thanks.

patricknaughton01 avatar Jul 19 '22 15:07 patricknaughton01

The library might be inside the build directory in which case you might be able to solve via any one of the following:

  • Move either test.py or the built shared library into the same directory.
  • Add the build directory to the python path (sys.path.append("build/"))
  • Change the import to from build import gpd_python as gpd

Let me know if these don't work. Thanks.

It works, Thanks!!

tycoer avatar Jul 27 '22 11:07 tycoer

FYI, the provide binding detect_grasps_python.cpp works with some modification, and numpy array can be pass through that ctypes python interface. Can provide the patch if anyone needed it.

Hi @pvmilk, do you by any chance still have that patch? I'm looking into using GPD in python with an indexed pointcloud, which I think should be possible with the function calcGraspDescriptorsAtIndices, but I can't quite get it to compile yet.

anmakon avatar Jul 17 '23 12:07 anmakon

@anmakon Sorry for the late response. I searched through my system and cannot find the patch anymore. Sorry to give a false expectation.

pvmilk avatar Jul 26 '23 06:07 pvmilk