From 03ff268ac6d60c2125499f1b806b14e742000d1e Mon Sep 17 00:00:00 2001 From: rodgomesc Date: Sun, 7 Apr 2024 23:22:34 -0300 Subject: [PATCH 1/2] chore: update libs and fix macos build script --- README.md | 28 ++++++++++++++-------------- build-aarch64-android.sh | 2 +- build-aarch64-macos.sh | 4 ++-- build-x86_64-linux.sh | 2 +- build-x86_64-macos.sh | 2 +- build-x86_64-windows.sh | 2 +- 6 files changed, 20 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index 640eda2..8a17517 100644 --- a/README.md +++ b/README.md @@ -14,14 +14,14 @@ 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 . ``` @@ -29,7 +29,7 @@ cmake --install . #### Run the build script ``` cd -./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 @@ -43,12 +43,12 @@ cd - 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 -./build-x86_64-windows.ps1 --version v0.8.11 --config release --opencv_dir +./build-x86_64-windows.ps1 --version v0.10.11 --config release --opencv_dir ``` ### MacOS @@ -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 . ``` @@ -79,7 +79,7 @@ cmake --install . ``` cd -./build--macos.sh --version v0.8.11 --config release --opencv_dir /usr/local +./build--macos.sh --version v0.10.11 --config release --opencv_dir /usr/local ``` ### Android @@ -100,7 +100,7 @@ export ANDROID_NDK_HOME= #### Run the build script ``` cd -./build-aarch64-android.sh --version v0.8.11 --config release +./build-aarch64-android.sh --version v0.10.11 --config release ``` ## Usage diff --git a/build-aarch64-android.sh b/build-aarch64-android.sh index e3d3d85..ff12a8f 100755 --- a/build-aarch64-android.sh +++ b/build-aarch64-android.sh @@ -3,7 +3,7 @@ set -e shopt -s nullglob -VERSION="v0.8.11" +VERSION="v0.10.11" OPENCV_DIR="" CONFIG="debug" diff --git a/build-aarch64-macos.sh b/build-aarch64-macos.sh index 1c73919..64be44b 100755 --- a/build-aarch64-macos.sh +++ b/build-aarch64-macos.sh @@ -3,7 +3,7 @@ set -e shopt -s nullglob -VERSION="v0.8.11" +VERSION="v0.10.11" OPENCV_DIR="" CONFIG="debug" @@ -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" diff --git a/build-x86_64-linux.sh b/build-x86_64-linux.sh index cdc2638..300a45a 100755 --- a/build-x86_64-linux.sh +++ b/build-x86_64-linux.sh @@ -3,7 +3,7 @@ set -e shopt -s nullglob -VERSION="v0.8.11" +VERSION="v0.10.11" OPENCV_DIR="" CONFIG="debug" diff --git a/build-x86_64-macos.sh b/build-x86_64-macos.sh index 7630a5e..e49fbfc 100755 --- a/build-x86_64-macos.sh +++ b/build-x86_64-macos.sh @@ -3,7 +3,7 @@ set -e shopt -s nullglob -VERSION="v0.8.11" +VERSION="v0.10.11" OPENCV_DIR="" CONFIG="debug" diff --git a/build-x86_64-windows.sh b/build-x86_64-windows.sh index 23ec84a..46ccaf8 100644 --- a/build-x86_64-windows.sh +++ b/build-x86_64-windows.sh @@ -1,7 +1,7 @@ set -e shopt -s nullglob -VERSION="v0.8.11" +VERSION="v0.10.11" OPENCV_DIR="" CONFIG="debug" From 46209a3459061bb65a4aa92a01b1f639c5e55b03 Mon Sep 17 00:00:00 2001 From: rodgomesc Date: Sun, 7 Apr 2024 23:24:35 -0300 Subject: [PATCH 2/2] feat: add bounding box bindings --- c/mediapipe.cpp | 37 +++++++++++++++++++++++++++++++++++++ c/mediapipe.h | 22 ++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/c/mediapipe.cpp b/c/mediapipe.cpp index 07a0137..97064a2 100644 --- a/c/mediapipe.cpp +++ b/c/mediapipe.cpp @@ -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" @@ -59,6 +60,33 @@ struct mp_packet { mediapipe::Packet packet; }; + +template +static mp_bounding_box_list* get_bounding_boxes(mp_packet* packet) { + const auto& detections = packet->packet.Get>(); + + 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(bbox.xmin()), + static_cast(bbox.ymin()), + static_cast(bbox.width()), + static_cast(bbox.height()) + }; + } + } + + return new mp_bounding_box_list { + boxes_array, + static_cast(detections.size()) + }; +} + template static mp_multi_face_landmark_list* get_multi_face_landmarks(mp_packet* packet) { const auto& mp_data = packet->packet.template Get>(); @@ -355,6 +383,10 @@ MEDIAPIPE_API mp_multi_face_landmark_list* mp_get_multi_face_landmarks(mp_packet return get_multi_face_landmarks(packet); } +MEDIAPIPE_API mp_bounding_box_list* mp_get_bounding_boxes(mp_packet* packet) { + return get_bounding_boxes(packet); +} + MEDIAPIPE_API mp_multi_face_landmark_list* mp_get_norm_multi_face_landmarks(mp_packet* packet) { return get_multi_face_landmarks(packet); } @@ -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(packet); } diff --git a/c/mediapipe.h b/c/mediapipe.h index c1a57fd..dff37ea 100644 --- a/c/mediapipe.h +++ b/c/mediapipe.h @@ -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 { @@ -222,6 +237,10 @@ 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); @@ -229,6 +248,9 @@ MEDIAPIPE_API mp_multi_face_landmark_list* mp_get_norm_multi_face_landmarks(mp_p /// 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);