Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add bindings for bounding box detection #7

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 14 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,22 @@ This produces a shared library that can subsequently be used in CMake/Visual Stu
- Bazel 5.2.0: https://bazel.build/install/ubuntu
- Numpy: ```pip3 install numpy```

#### Build and install OpenCV 4.7.0
#### Build and install OpenCV 4.9.0
```
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.7.0.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.7.0.zip
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.9.0.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.9.0.zip
unzip opencv.zip
unzip opencv_contrib.zip
mkdir -p opencv_build && cd opencv_build
cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.7.0/modules ../opencv-4.7.0
cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.9.0/modules ../opencv-4.9.0
cmake --build .
cmake --install .
```

#### Run the build script
```
cd <path to libmediapipe>
./build-x86_64-linux.sh --version v0.8.11 --config release --opencv_dir /usr/local
./build-x86_64-linux.sh --version v0.10.11 --config release --opencv_dir /usr/local
```

### Windows
Expand All @@ -43,12 +43,12 @@ cd <path to libmediapipe>
- Git with Bash
- Numpy: ```pip install numpy```

#### Install OpenCV 4.7.0
The installer can be downloaded from https://github.com/opencv/opencv/releases/download/4.7.0/opencv-4.7.0-windows.exe
#### Install OpenCV 4.9.0
The installer can be downloaded from https://github.com/opencv/opencv/releases/download/4.9.0/opencv-4.9.0-windows.exe

#### Run the build script
```cd <path to libmediapipe>
./build-x86_64-windows.ps1 --version v0.8.11 --config release --opencv_dir <path to opencv build>
./build-x86_64-windows.ps1 --version v0.10.11 --config release --opencv_dir <path to opencv build>
```

### MacOS
Expand All @@ -59,14 +59,14 @@ The installer can be downloaded from https://github.com/opencv/opencv/releases/d
- Xcode
- Numpy: ```pip install numpy``` or ```pip3 install numpy```

#### Build and install OpenCV 4.7.0
#### Build and install OpenCV 4.9.0
```
curl -L -o opencv.zip https://github.com/opencv/opencv/archive/4.7.0.zip
curl -L -o opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.7.0.zip
curl -L -o opencv.zip https://github.com/opencv/opencv/archive/4.9.0.zip
curl -L -o opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.9.0.zip
unzip opencv.zip
unzip opencv_contrib.zip
mkdir -p opencv_build && cd opencv_build
cmake -DBUILD_ZLIB=false -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.7.0/modules ../opencv-4.7.0
cmake -DBUILD_ZLIB=false -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.9.0/modules ../opencv-4.9.0
cmake --build . -j6
cmake --install .
```
Expand All @@ -79,7 +79,7 @@ cmake --install .

```
cd <path to libmediapipe>
./build-<arch>-macos.sh --version v0.8.11 --config release --opencv_dir /usr/local
./build-<arch>-macos.sh --version v0.10.11 --config release --opencv_dir /usr/local
```

### Android
Expand All @@ -100,7 +100,7 @@ export ANDROID_NDK_HOME=<path to android ndk>
#### Run the build script
```
cd <path to libmediapipe>
./build-aarch64-android.sh --version v0.8.11 --config release
./build-aarch64-android.sh --version v0.10.11 --config release
```

## Usage
Expand Down
2 changes: 1 addition & 1 deletion build-aarch64-android.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -e
shopt -s nullglob

VERSION="v0.8.11"
VERSION="v0.10.11"
OPENCV_DIR=""
CONFIG="debug"

Expand Down
4 changes: 2 additions & 2 deletions build-aarch64-macos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -e
shopt -s nullglob

VERSION="v0.8.11"
VERSION="v0.10.11"
OPENCV_DIR=""
CONFIG="debug"

Expand Down Expand Up @@ -149,7 +149,7 @@ echo "Done"

echo -n "Patching dynamic library - "
DYLIB_PATH=mediapipe/bazel-bin/mediapipe/c/libmediapipe.dylib
mv mediapipe/bazel-bin/mediapipe/c/libmediapipe.so "$DYLIB_PATH"
mv mediapipe/bazel-bin/mediapipe/c/libmediapipe.dylib "$DYLIB_PATH"
chmod +w "$DYLIB_PATH"
install_name_tool -id @rpath/libmediapipe.dylib "$DYLIB_PATH"
echo "Done"
Expand Down
2 changes: 1 addition & 1 deletion build-x86_64-linux.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -e
shopt -s nullglob

VERSION="v0.8.11"
VERSION="v0.10.11"
OPENCV_DIR=""
CONFIG="debug"

Expand Down
2 changes: 1 addition & 1 deletion build-x86_64-macos.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
set -e
shopt -s nullglob

VERSION="v0.8.11"
VERSION="v0.10.11"
OPENCV_DIR=""
CONFIG="debug"

Expand Down
2 changes: 1 addition & 1 deletion build-x86_64-windows.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
set -e
shopt -s nullglob

VERSION="v0.8.11"
VERSION="v0.10.11"
OPENCV_DIR=""
CONFIG="debug"

Expand Down
37 changes: 37 additions & 0 deletions c/mediapipe.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include "mediapipe/framework/calculator_framework.h"
#include "mediapipe/framework/port/parse_text_proto.h"
#include "mediapipe/framework/formats/image_frame.h"
#include "mediapipe/framework/formats/detection.pb.h"
#include "mediapipe/framework/formats/landmark.pb.h"
#include "mediapipe/framework/formats/rect.pb.h"
#include "mediapipe/framework/tool/options_util.h"
Expand Down Expand Up @@ -59,6 +60,33 @@ struct mp_packet {
mediapipe::Packet packet;
};


template<typename Detection>
static mp_bounding_box_list* get_bounding_boxes(mp_packet* packet) {
const auto& detections = packet->packet.Get<std::vector<Detection>>();

auto* boxes_array = new mp_bounding_box[detections.size()];

for (int i = 0; i < detections.size(); i++) {
const Detection& detection = detections[i];
if (detection.location_data().format() == mediapipe::LocationData::RELATIVE_BOUNDING_BOX) {
const auto& bbox = detection.location_data().relative_bounding_box();
boxes_array[i] = {
detection.score_size() > 0 ? detection.score(0) : 0,
static_cast<float>(bbox.xmin()),
static_cast<float>(bbox.ymin()),
static_cast<float>(bbox.width()),
static_cast<float>(bbox.height())
};
}
}

return new mp_bounding_box_list {
boxes_array,
static_cast<int>(detections.size())
};
}

template<typename List, typename Landmark>
static mp_multi_face_landmark_list* get_multi_face_landmarks(mp_packet* packet) {
const auto& mp_data = packet->packet.template Get<std::vector<List>>();
Expand Down Expand Up @@ -355,6 +383,10 @@ MEDIAPIPE_API mp_multi_face_landmark_list* mp_get_multi_face_landmarks(mp_packet
return get_multi_face_landmarks<mediapipe::LandmarkList, mediapipe::Landmark>(packet);
}

MEDIAPIPE_API mp_bounding_box_list* mp_get_bounding_boxes(mp_packet* packet) {
return get_bounding_boxes<mediapipe::Detection>(packet);
}

MEDIAPIPE_API mp_multi_face_landmark_list* mp_get_norm_multi_face_landmarks(mp_packet* packet) {
return get_multi_face_landmarks<mediapipe::NormalizedLandmarkList, mediapipe::NormalizedLandmark>(packet);
}
Expand All @@ -368,6 +400,11 @@ MEDIAPIPE_API void mp_destroy_multi_face_landmarks(mp_multi_face_landmark_list*
delete multi_face_landmarks;
}

MEDIAPIPE_API void mp_destroy_bounding_boxes(mp_bounding_box_list* bounding_boxes) {
delete[] bounding_boxes->boxes;
delete bounding_boxes;
}

MEDIAPIPE_API mp_rect_list* mp_get_rects(mp_packet* packet) {
return get_rects<mediapipe::Rect>(packet);
}
Expand Down
22 changes: 22 additions & 0 deletions c/mediapipe.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,21 @@ typedef struct {
int length;
} mp_rect_list;

struct mp_bounding_box {
float score;
float xmin;
float ymin;
float width;
float height;
};

// A list of bounding boxes
struct mp_bounding_box_list {
mp_bounding_box* boxes;
int length;
};


/// Hand landmark indices in a mp_landmark_list.
/// For more information, see: https://google.github.io/mediapipe/solutions/hands.html#hand-landmark-model
typedef enum {
Expand Down Expand Up @@ -222,13 +237,20 @@ MEDIAPIPE_API void mp_copy_packet_image(mp_packet* packet, uint8_t* out_data);
/// The list should be destroyed with mp_destroy_multi_face_landmarks.
MEDIAPIPE_API mp_multi_face_landmark_list* mp_get_multi_face_landmarks(mp_packet* packet);

/// Returns the multiface bounding box and score of a packet
/// this list should be destroyed with mp_destroy_bounding_boxes
MEDIAPIPE_API mp_bounding_box_list* mp_get_bounding_boxes(mp_packet* packet);

/// Returns the normalized multi-face landmarks of a packet.
/// The list should be destroyed with mp_destroy_multi_face_landmarks.
MEDIAPIPE_API mp_multi_face_landmark_list* mp_get_norm_multi_face_landmarks(mp_packet* packet);

/// Deallocates a multi-face landmark list.
MEDIAPIPE_API void mp_destroy_multi_face_landmarks(mp_multi_face_landmark_list* multi_face_landmarks);

// Dealocates a multiface bounding box list
MEDIAPIPE_API void mp_destroy_bounding_boxes(mp_bounding_box_list* bounding_boxes);

/// Returns the rectangles of a packet.
/// The list should be destroyed with mp_destroy_rects.
MEDIAPIPE_API mp_rect_list* mp_get_rects(mp_packet* packet);
Expand Down