atenpas / gpd

Detect 6-DOF grasp poses in point clouds
BSD 2-Clause "Simplified" License
598 stars 233 forks source link

Adding support for python bindings #120

Open patricknaughton01 opened 2 years ago

patricknaughton01 commented 2 years ago

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 commented 2 years ago

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

pvmilk commented 2 years ago

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 commented 2 years ago

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

patricknaughton01 commented 2 years ago

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?

pvmilk commented 2 years ago

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 commented 2 years ago

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).

tycoer commented 2 years ago

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"

patricknaughton01 commented 2 years ago

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

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

tycoer commented 2 years ago

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!!

anmakon commented 1 year ago

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.

pvmilk commented 1 year ago

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