From 1850f43fe746215a0847a708f501a3efcd42decc Mon Sep 17 00:00:00 2001 From: Sebastian Schmidt Date: Thu, 10 Oct 2024 10:08:47 -0700 Subject: [PATCH] Internal PiperOrigin-RevId: 684485873 --- third_party/external_files.bzl | 10 +- third_party/prebuilts/BUILD | 1 + third_party/prebuilts/opencv_ios_source.BUILD | 97 +++++++++++++++++++ 3 files changed, 106 insertions(+), 2 deletions(-) create mode 100644 third_party/prebuilts/opencv_ios_source.BUILD diff --git a/third_party/external_files.bzl b/third_party/external_files.bzl index 500ab07651..cb07233293 100644 --- a/third_party/external_files.bzl +++ b/third_party/external_files.bzl @@ -288,8 +288,8 @@ def external_files(): http_file( name = "com_google_mediapipe_efficientdet_lite0_fp16_no_nms_tflite", - sha256 = "237a58389081333e5cf4154e42b593ce7dd357445536fcaf4ca5bc51c2c50f1c", - urls = ["https://storage.googleapis.com/mediapipe-assets/efficientdet_lite0_fp16_no_nms.tflite?generation=1693610182046114"], + sha256 = "bcda125c96d3767bca894c8cbe7bc458379c9974c9fd8bdc6204e7124a74082a", + urls = ["https://storage.googleapis.com/mediapipe-assets/efficientdet_lite0_fp16_no_nms.tflite?generation=1728573738871723"], ) http_file( @@ -1018,6 +1018,12 @@ def external_files(): urls = ["https://storage.googleapis.com/mediapipe-assets/ocr_text.jpg?generation=1681244241009078"], ) + http_file( + name = "com_google_mediapipe_opencv2_xcframework_zip", + sha256 = "6b625f564b72fd7c0946de2ae61507bed5daf84f3dbb5296ef4fef19da491160", + urls = ["https://storage.googleapis.com/mediapipe-assets/opencv2.xcframework.zip?generation=1728573757773068"], + ) + http_file( name = "com_google_mediapipe_palm_detection_full_tflite", sha256 = "1b14e9422c6ad006cde6581a46c8b90dd573c07ab7f3934b5589e7cea3f89a54", diff --git a/third_party/prebuilts/BUILD b/third_party/prebuilts/BUILD index d1c0da4ddf..5597763bce 100644 --- a/third_party/prebuilts/BUILD +++ b/third_party/prebuilts/BUILD @@ -13,6 +13,7 @@ mediapipe_files(srcs = [ "libopencv_core.3.4.darwin_arm64.a", "libopencv_imgproc.3.4.darwin.a", "libopencv_imgproc.3.4.darwin_arm64.a", + "opencv2.xcframework.zip", ]) cc_import( diff --git a/third_party/prebuilts/opencv_ios_source.BUILD b/third_party/prebuilts/opencv_ios_source.BUILD new file mode 100644 index 0000000000..42f0e1344a --- /dev/null +++ b/third_party/prebuilts/opencv_ios_source.BUILD @@ -0,0 +1,97 @@ +# Description: +# OpenCV xcframework for video/image processing on iOS. + +load( + "@//third_party:opencv_ios_source.bzl", + "select_headers", + "unzip_opencv_xcframework", +) +load( + "@build_bazel_rules_apple//apple:apple.bzl", + "apple_static_xcframework_import", +) + +licenses(["notice"]) # BSD license + +exports_files(["LICENSE"]) + +genrule( + name = "build_opencv_xcframework", + srcs = ["@mediapipe//third_party/prebuilts:opencv2.xcframework.zip"], + outs = ["opencv2.xcframework.zip"], + cmd = "cp $(SRCS) $(OUTS)", +) + +# Unzips `opencv2.xcframework.zip` built from source by `build_opencv_xcframework` +# genrule and returns an exhaustive list of all its files including symlinks. +unzip_opencv_xcframework( + name = "opencv2_unzipped_xcframework_files", + zip_file = "opencv2.xcframework.zip", +) + +# Imports the files of the unzipped `opencv2.xcframework` as an apple static +# framework which can be linked to iOS targets. +apple_static_xcframework_import( + name = "opencv_xcframework", + visibility = ["//visibility:public"], + xcframework_imports = [":opencv2_unzipped_xcframework_files"], +) + +# Filters the headers for each platform in `opencv2.xcframework` which will be +# used as headers in a `cc_library` that can be linked to C++ targets. +select_headers( + name = "opencv_xcframework_device_headers", + srcs = [":opencv_xcframework"], + platform = "ios-arm64", +) + +select_headers( + name = "opencv_xcframework_simulator_headers", + srcs = [":opencv_xcframework"], + platform = "ios-arm64_x86_64-simulator", +) + +# `cc_library` that can be linked to C++ targets to import opencv headers. +cc_library( + name = "opencv", + hdrs = select({ + "@//mediapipe:ios_x86_64": [ + ":opencv_xcframework_simulator_headers", + ], + "@//mediapipe:ios_sim_arm64": [ + ":opencv_xcframework_simulator_headers", + ], + "@//mediapipe:ios_arm64": [ + ":opencv_xcframework_device_headers", + ], + # A value from above is chosen arbitarily. + "//conditions:default": [ + ":opencv_xcframework_simulator_headers", + ], + }), + copts = [ + "-std=c++11", + "-x objective-c++", + ], + include_prefix = "opencv2", + linkopts = [ + "-framework AssetsLibrary", + "-framework CoreFoundation", + "-framework CoreGraphics", + "-framework CoreMedia", + "-framework Accelerate", + "-framework CoreImage", + "-framework AVFoundation", + "-framework CoreVideo", + "-framework QuartzCore", + ], + strip_include_prefix = select({ + "@//mediapipe:ios_x86_64": "opencv2.xcframework/ios-arm64_x86_64-simulator/opencv2.framework/Versions/A/Headers", + "@//mediapipe:ios_sim_arm64": "opencv2.xcframework/ios-arm64_x86_64-simulator/opencv2.framework/Versions/A/Headers", + "@//mediapipe:ios_arm64": "opencv2.xcframework/ios-arm64/opencv2.framework/Versions/A/Headers", + # Random value is selected for default cases. + "//conditions:default": "opencv2.xcframework/ios-arm64_x86_64-simulator/opencv2.framework/Versions/A/Headers", + }), + visibility = ["//visibility:public"], + deps = [":opencv_xcframework"], +)