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 support for desktop examples on MacOS (Apple Silicon M1=arm64/ x86_64/ i386) #2591

Closed
wants to merge 3 commits into from

Conversation

walkoncross
Copy link

  1. add config "build:darwin_arm64" in .bazelrc;
  2. add "Build on MacOS" section in README.rd;
  3. add ‘config_setting(name = "macos_arm64", …)’ in mediapipe/BUILD, and set default apple_platform_type
    to ‘macos_x86_64’;
  4. add build_macos_desktop_examples.sh 

@google-cla
Copy link

google-cla bot commented Sep 24, 2021

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project (if not, look below for help). Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed (or fixed any issues), please reply here with @googlebot I signed it! and we'll verify it.


What to do if you already signed the CLA

Individual signers
Corporate signers

ℹ️ Googlers: Go here for more info.

@walkoncross
Copy link
Author

@googlebot I signed it!


2. Build all available examples under mediapipe/examples/desktop:
```bash
bash build_macos_desktop_examples.sh --cpu x86_64 -b
Copy link

@mgebhard mgebhard Dec 27, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Super helpful I finally got helloworld to build on my m1 but any help with:

In file included from mediapipe/util/annotation_renderer.cc:15:
In file included from ./mediapipe/util/annotation_renderer.h:20:
./mediapipe/framework/port/opencv_core_inc.h:18:10: fatal error: 'opencv2/core/version.hpp' file not found
#include <opencv2/core/version.hpp>
^~~~~~~~~~~~~~~~~~~~~~~~~~
1 error generated.
Error in child process '/usr/bin/xcrun'. 1
Target //mediapipe/examples/desktop/face_detection:face_detection_cpu failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 1.591s, Critical Path: 1.41s
INFO: 175 processes: 131 internal, 44 darwin-sandbox.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mgebhard try modifiy the third_party/opencv_macos.BUILD :

--- a/third_party/opencv_macos.BUILD
+++ b/third_party/opencv_macos.BUILD
@@ -19,8 +19,8 @@ cc_library(
             "lib/libopencv_videoio.dylib",
         ],
     ),
-    hdrs = glob(["include/opencv2/**/*.h*"]),
-    includes = ["include/"],
+    hdrs = glob(["include/opencv4/opencv2/**/*.h*"]),
+    includes = ["include/opencv4/"],
     linkstatic = 1,
     visibility = ["//visibility:public"],
 )

@a-hurst
Copy link

a-hurst commented Mar 30, 2022

@walkoncross Thanks a ton for this! Hopefully it gets merged soon.

NOTE: My original solution doesn't work because Bazel doesn't allow selects for repository paths, but the following solution works:

In WORKSPACE replace the macOS lines with these:

new_local_repository(
    name = "macos_opencv",
    build_file = "@//third_party:opencv_macos.BUILD",
    path = "/usr/local/opt/opencv@3",
)

new_local_repository(
    name = "macos_arm64_opencv",
    build_file = "@//third_party:opencv_macos.BUILD",
    path = "/opt/homebrew/opt/opencv@3",
)

new_local_repository(
    name = "macos_ffmpeg",
    build_file = "@//third_party:ffmpeg_macos.BUILD",
    path = "/usr/local/opt/ffmpeg@4",
)

new_local_repository(
    name = "macos_arm64_ffmpeg",
    build_file = "@//third_party:ffmpeg_macos.BUILD",
    path = "/opt/homebrew/opt/ffmpeg@4",
)

Then, in third_party/BUILD, add the platform-specific library paths for each macOS architecture like so:

alias(
    name = "opencv_binary",
    actual = select({
        "//mediapipe:android_x86": "@android_opencv//:libopencv_x86",
        "//mediapipe:android_x86_64": "@android_opencv//:libopencv_x86_64",
        "//mediapipe:android_armeabi": "@android_opencv//:libopencv_armeabi-v7a",
        "//mediapipe:android_arm": "@android_opencv//:libopencv_armeabi-v7a",
        "//mediapipe:android_arm64": "@android_opencv//:libopencv_arm64-v8a",
        "//mediapipe:ios": "@ios_opencv//:opencv",
        "//mediapipe:macos_x86_64": "@macos_opencv//:opencv",
        "//mediapipe:macos_arm64": "@macos_arm64_opencv//:opencv",
        "//mediapipe:windows": "@windows_opencv//:opencv",
        "//conditions:default": "@linux_opencv//:opencv",
    }),
)

cc_library(
    name = "libffmpeg",
    visibility = ["//visibility:public"],
    deps = select({
        "//mediapipe:android_x86": [],
        "//mediapipe:android_x86_64": [],
        "//mediapipe:android_armeabi": [],
        "//mediapipe:android_arm": [],
        "//mediapipe:android_arm64": [],
        "//mediapipe:ios": [],
        "//mediapipe:macos_x86_64": [
            "@macos_ffmpeg//:libffmpeg",
        ],
        "//mediapipe:macos_arm64": [
            "@macos_arm64_ffmpeg//:libffmpeg",
        ],
        "//conditions:default": [
            "@linux_ffmpeg//:libffmpeg",
        ],
    }),
)

Anyway, that lets you build easily on x86 or arm64 macOS without having to edit any build files, which should hopefully make it easier for the project to provide official macOS arm64 wheels.

That said, with the changes in this PR (and my edits above) I can install mediapipe with python setup.py install --link-opencv in a clean virtual environment just fine, but python setup.py bdist_wheel fails with an error about missing symbols when compiling OpenCV:

...
Scanning dependencies of target opencv_annotation
[ 99%] Building CXX object apps/annotation/CMakeFiles/opencv_annotation.dir/opencv_annotation.cpp.o
[ 99%] Linking CXX executable ../../bin/opencv_annotation
Undefined symbols for architecture arm64:
  "_jpeg_default_qtables", referenced from:
      cv::JpegEncoder::write(cv::Mat const&, std::__1::vector<int, std::__1::allocator<int> > const&) in libopencv_imgcodecs.a(grfmt_jpeg.cpp.o)
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Error in child process '/usr/bin/xcrun'. 1
/private/var/tmp/_bazel_austinhurst/c5b55272d6d862676b481c1414e3a50d/sandbox/darwin-sandbox/1778/execroot/mediapipe/external/local_config_cc/cc_wrapper.sh: line 69: 77421 Abort trap: 6           "$(/usr/bin/dirname "$0")"/wrapped_clang "$@"
make[2]: *** [bin/opencv_annotation] Error 134
make[1]: *** [apps/annotation/CMakeFiles/opencv_annotation.dir/all] Error 2

If I update the OpenCV source version to 3.4.16 I can get past it (see this OpenCV issue: opencv/opencv#18541), but then I run into the same bizarre ld: library not found for -ljpeg error reported in issue #1665.

@cansik
Copy link

cansik commented Apr 21, 2022

Would be great if mediapipe releases M1 packages officially. I managed to create a fully automated script to build version 0.8.9 with python wheels. I link opencv@3 and later delocate the wheel paths. The prebuilt wheels for version 3.8, 3.9 and 3.10 are already uploaded and after some testing I will release them on PyPi as well. Looking forward to feedback and suggestions.

Here the repository with the build: https://github.com/cansik/mediapipe-silicon

@a-hurst
Copy link

a-hurst commented Apr 21, 2022

Would be great if mediapipe releases M1 packages officially. I managed to create a fully automated script to build version 0.8.9 with python wheels. I link opencv@3 and later delocate the wheel paths. The prebuilt wheels for version 3.8, 3.9 and 3.10 are already uploaded and after some testing I will release them on PyPi as well. Looking forward to feedback and suggestions.

Here the repository with the build: https://github.com/cansik/mediapipe-silicon

Fantastic, thanks for putting this together! Will vastly simplify the setup and install process for anything that depends on mediapipe. For my use-case (extracting the x/y coordinates of face regions for cognitive face processing research), I can't realistically expect colleagues to set up and troubleshoot a compiler environment just to run a script, so having pre-built wheels is extremely useful.

@schmidt-sebastian
Copy link
Collaborator

Thank you for your contribution! We have integrated the required changes from your PR. We now support M1 for iOS and Android builds (Python support is available via Rosetta).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants