Project import generated by Copybara.

GitOrigin-RevId: afeb9cf5a8c069c0a566d16e1622bbb086170e4d
This commit is contained in:
MediaPipe Team
2020-05-21 13:37:51 -04:00
committed by chuoling
parent b6e680647c
commit b133b0f200
258 changed files with 4146 additions and 5147 deletions
+33 -188
View File
@@ -14,7 +14,7 @@
# limitations under the License.
#
load("//mediapipe/framework/port:build_config.bzl", "mediapipe_cc_proto_library", "mediapipe_py_proto_library")
load("//mediapipe/framework/port:build_config.bzl", "mediapipe_proto_library")
licenses(["notice"]) # Apache 2.0
@@ -33,10 +33,10 @@ exports_files([
"encode_binary_proto.bzl",
])
proto_library(
mediapipe_proto_library(
name = "calculator_proto",
srcs = ["calculator.proto"],
visibility = ["//visibility:public"],
visibility = [":mediapipe_internal"],
deps = [
"//mediapipe/framework:calculator_options_proto",
"//mediapipe/framework:mediapipe_options_proto",
@@ -48,40 +48,46 @@ proto_library(
],
)
proto_library(
mediapipe_proto_library(
name = "calculator_options_proto",
srcs = ["calculator_options.proto"],
visibility = ["//visibility:public"],
)
proto_library(
mediapipe_proto_library(
name = "calculator_contract_test_proto",
testonly = 1,
srcs = ["calculator_contract_test.proto"],
visibility = ["//mediapipe/framework:__subpackages__"],
deps = ["//mediapipe/framework:calculator_proto"],
deps = [
"//mediapipe/framework:calculator_options_proto",
"//mediapipe/framework:calculator_proto",
],
)
proto_library(
mediapipe_proto_library(
name = "calculator_profile_proto",
srcs = ["calculator_profile.proto"],
visibility = ["//mediapipe/framework:__subpackages__"],
deps = ["//mediapipe/framework:calculator_proto"],
visibility = [":mediapipe_internal"],
deps = [
"//mediapipe/framework:calculator_options_proto",
"//mediapipe/framework:calculator_proto",
],
)
proto_library(
mediapipe_proto_library(
name = "mediapipe_options_proto",
srcs = ["mediapipe_options.proto"],
visibility = ["//mediapipe/framework:__subpackages__"],
)
proto_library(
mediapipe_proto_library(
name = "packet_factory_proto",
srcs = ["packet_factory.proto"],
visibility = ["//mediapipe/framework:__subpackages__"],
)
proto_library(
mediapipe_proto_library(
name = "packet_generator_proto",
srcs = ["packet_generator.proto"],
visibility = [
@@ -89,206 +95,45 @@ proto_library(
],
)
proto_library(
mediapipe_proto_library(
name = "packet_test_proto",
testonly = 1,
srcs = ["packet_test.proto"],
visibility = ["//mediapipe/framework:__subpackages__"],
)
proto_library(
mediapipe_proto_library(
name = "status_handler_proto",
srcs = ["status_handler.proto"],
visibility = ["//mediapipe/framework:__subpackages__"],
deps = ["//mediapipe/framework:mediapipe_options_proto"],
)
proto_library(
mediapipe_proto_library(
name = "stream_handler_proto",
srcs = ["stream_handler.proto"],
visibility = ["//mediapipe/framework:__subpackages__"],
deps = ["//mediapipe/framework:mediapipe_options_proto"],
)
proto_library(
mediapipe_proto_library(
name = "test_calculators_proto",
testonly = 1,
srcs = ["test_calculators.proto"],
visibility = ["//visibility:public"],
deps = ["//mediapipe/framework:calculator_proto"],
deps = [
"//mediapipe/framework:calculator_options_proto",
"//mediapipe/framework:calculator_proto",
],
)
proto_library(
mediapipe_proto_library(
name = "thread_pool_executor_proto",
srcs = ["thread_pool_executor.proto"],
visibility = ["//mediapipe/framework:__subpackages__"],
deps = ["//mediapipe/framework:mediapipe_options_proto"],
)
mediapipe_cc_proto_library(
name = "calculator_cc_proto",
srcs = ["calculator.proto"],
cc_deps = [
":calculator_options_cc_proto",
":mediapipe_options_cc_proto",
":packet_factory_cc_proto",
":packet_generator_cc_proto",
":status_handler_cc_proto",
":stream_handler_cc_proto",
"@com_google_protobuf//:cc_wkt_protos",
],
visibility = ["//visibility:public"],
deps = [":calculator_proto"],
)
mediapipe_cc_proto_library(
name = "calculator_contract_test_cc_proto",
testonly = 1,
srcs = ["calculator_contract_test.proto"],
cc_deps = [":calculator_cc_proto"],
visibility = ["//mediapipe/framework:__subpackages__"],
deps = [":calculator_contract_test_proto"],
)
mediapipe_cc_proto_library(
name = "calculator_options_cc_proto",
srcs = ["calculator_options.proto"],
visibility = ["//visibility:public"],
deps = [":calculator_options_proto"],
)
mediapipe_cc_proto_library(
name = "calculator_profile_cc_proto",
srcs = ["calculator_profile.proto"],
cc_deps = [":calculator_cc_proto"],
visibility = [
"//mediapipe/framework:__subpackages__",
"//mediapipe/java/com/google/mediapipe/framework:__subpackages__",
],
deps = [":calculator_profile_proto"],
)
mediapipe_cc_proto_library(
name = "mediapipe_options_cc_proto",
srcs = [":mediapipe_options.proto"],
visibility = ["//mediapipe/framework:__subpackages__"],
deps = [":mediapipe_options_proto"],
)
mediapipe_cc_proto_library(
name = "packet_factory_cc_proto",
srcs = ["packet_factory.proto"],
visibility = ["//mediapipe/framework:__subpackages__"],
deps = [":packet_factory_proto"],
)
mediapipe_cc_proto_library(
name = "packet_generator_cc_proto",
srcs = ["packet_generator.proto"],
visibility = [
"//mediapipe:__subpackages__",
],
deps = [":packet_generator_proto"],
)
mediapipe_cc_proto_library(
name = "packet_test_cc_proto",
testonly = 1,
srcs = ["packet_test.proto"],
visibility = ["//mediapipe/framework:__subpackages__"],
deps = [":packet_test_proto"],
)
mediapipe_cc_proto_library(
name = "status_handler_cc_proto",
srcs = ["status_handler.proto"],
cc_deps = [":mediapipe_options_cc_proto"],
visibility = ["//mediapipe/framework:__subpackages__"],
deps = [":status_handler_proto"],
)
mediapipe_cc_proto_library(
name = "stream_handler_cc_proto",
srcs = ["stream_handler.proto"],
cc_deps = [":mediapipe_options_cc_proto"],
visibility = ["//mediapipe/framework:__subpackages__"],
deps = [":stream_handler_proto"],
)
mediapipe_cc_proto_library(
name = "test_calculators_cc_proto",
testonly = 1,
srcs = ["test_calculators.proto"],
cc_deps = [":calculator_cc_proto"],
visibility = ["//visibility:public"],
deps = [":test_calculators_proto"],
)
mediapipe_cc_proto_library(
name = "thread_pool_executor_cc_proto",
srcs = ["thread_pool_executor.proto"],
cc_deps = [":mediapipe_options_cc_proto"],
visibility = ["//mediapipe/framework:__subpackages__"],
deps = [":thread_pool_executor_proto"],
)
mediapipe_py_proto_library(
name = "mediapipe_options_py_pb2",
srcs = ["mediapipe_options.proto"],
api_version = 2,
proto_deps = [":mediapipe_options_proto"],
visibility = ["//mediapipe:__subpackages__"],
)
mediapipe_py_proto_library(
name = "stream_handler_py_pb2",
srcs = ["stream_handler.proto"],
api_version = 2,
proto_deps = [":stream_handler_proto"],
py_proto_deps = [":mediapipe_options_py_pb2"],
visibility = ["//mediapipe:__subpackages__"],
)
mediapipe_py_proto_library(
name = "packet_generator_py_pb2",
srcs = ["packet_generator.proto"],
api_version = 2,
proto_deps = [":packet_generator_proto"],
py_proto_deps = [":mediapipe_options_py_pb2"],
visibility = ["//mediapipe:__subpackages__"],
)
mediapipe_py_proto_library(
name = "calculator_py_pb2",
srcs = [
"calculator.proto",
"calculator_options.proto",
"packet_factory.proto",
"status_handler.proto",
],
api_version = 2,
proto_deps = [":calculator_proto"],
py_proto_deps = [
":mediapipe_options_py_pb2",
":packet_generator_py_pb2",
":stream_handler_py_pb2",
],
visibility = ["//mediapipe:__subpackages__"],
)
java_lite_proto_library(
name = "calculator_java_proto_lite",
strict_deps = 0,
visibility = [":mediapipe_internal"],
deps = [":calculator_proto"],
)
java_lite_proto_library(
name = "calculator_profile_java_proto_lite",
visibility = [":mediapipe_internal"],
deps = [":calculator_profile_proto"],
)
cc_library(
name = "calculator_base",
srcs = ["calculator_base.cc"],
@@ -473,8 +318,8 @@ cc_library(
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:str_format",
"@com_google_absl//absl/synchronization",
"//mediapipe/framework:calculator_node",
"//mediapipe/framework:output_side_packet_impl",
":calculator_node",
":output_side_packet_impl",
"//mediapipe/framework/profiler:graph_profiler",
"//mediapipe/framework/tool:fill_packet_set",
"//mediapipe/framework/tool:status_util",
@@ -633,8 +478,8 @@ cc_library(
hdrs = ["collection.h"],
visibility = [":mediapipe_internal"],
deps = [
":collection_item_id",
":type_map",
"//mediapipe/framework:collection_item_id",
"//mediapipe/framework/port:logging",
"//mediapipe/framework/tool:tag_map",
"//mediapipe/framework/tool:validate_name",
@@ -803,11 +648,11 @@ cc_library(
":collection_item_id",
":input_stream_manager",
":input_stream_shard",
":mediapipe_profiling",
":packet",
":packet_set",
":packet_type",
"//mediapipe/framework:mediapipe_options_cc_proto",
"//mediapipe/framework:mediapipe_profiling",
"//mediapipe/framework/deps:registration",
"//mediapipe/framework/port:ret_check",
"//mediapipe/framework/port:status",
@@ -1060,10 +905,10 @@ cc_library(
hdrs = ["packet_type.h"],
visibility = ["//visibility:public"],
deps = [
":collection",
":packet",
":packet_set",
":type_map",
"//mediapipe/framework:collection",
"//mediapipe/framework/port:logging",
"//mediapipe/framework/port:map_util",
"//mediapipe/framework/port:ret_check",
@@ -1345,8 +1190,8 @@ cc_test(
linkstatic = 1,
deps = [
":calculator_framework",
":calculator_node",
"//mediapipe/calculators/core:pass_through_calculator",
"//mediapipe/framework:calculator_node",
"//mediapipe/framework/port:gtest_main",
"//mediapipe/framework/port:logging",
"//mediapipe/framework/port:parse_text_proto",
+4 -1
View File
@@ -304,7 +304,10 @@ cc_library(
cc_library(
name = "threadpool",
srcs = ["threadpool.cc"],
srcs = select({
"//mediapipe:windows": ["threadpool_std_thread_impl.cc"],
"//conditions:default": ["threadpool_pthread_impl.cc"],
}),
hdrs = ["threadpool.h"],
# Use this library through "mediapipe/framework/port:threadpool".
visibility = ["//mediapipe/framework/port:__pkg__"],
@@ -17,14 +17,16 @@
#include <stdlib.h> // for free(), aligned_alloc(),
#if defined(__ANDROID__)
#include <malloc.h> // for memalign()
#if defined(__ANDROID__) || defined(_WIN32)
#include <malloc.h> // for memalign() on Android, _aligned_alloc() on Windows
#endif
inline void *aligned_malloc(size_t size, int minimum_alignment) {
#if defined(__ANDROID__) || defined(OS_ANDROID)
return memalign(minimum_alignment, size);
#else // !__ANDROID__ && !OS_ANDROID
#elif _WIN32
return _aligned_malloc(size, minimum_alignment);
#else // !__ANDROID__ && !OS_ANDROID && !_WIN32
void *ptr = nullptr;
// posix_memalign requires that the requested alignment be at least
// sizeof(void*). In this case, fall back on malloc which should return memory
@@ -38,6 +40,12 @@ inline void *aligned_malloc(size_t size, int minimum_alignment) {
#endif
}
inline void aligned_free(void *aligned_memory) { free(aligned_memory); }
inline void aligned_free(void *aligned_memory) {
#ifdef _WIN32
_aligned_free(aligned_memory);
#else
free(aligned_memory);
#endif // _WIN32
}
#endif // MEDIAPIPE_DEPS_ALIGNED_MALLOC_AND_FREE_H_
+124 -44
View File
@@ -14,7 +14,11 @@
#include "mediapipe/framework/deps/file_helpers.h"
#ifdef _WIN32
#include <Windows.h>
#else
#include <dirent.h>
#endif // _WIN32
#include <stdio.h>
#include <string.h>
#include <sys/stat.h>
@@ -27,6 +31,110 @@
namespace mediapipe {
namespace file {
namespace {
// Helper class that returns all entries (files, directories) in a directory,
// except "." and "..". Example usage:
//
// DirectoryListing listing("/tmp");
// while (listing.HasNextEntry()) {
// std::cout << listing.NextEntry() << std::endl;
// }
#ifndef _WIN32
class DirectoryListing {
public:
explicit DirectoryListing(const std::string& directory) {
dir_ = opendir(directory.c_str());
if (dir_) {
ReadNextEntry();
}
}
~DirectoryListing() {
if (dir_) {
closedir(dir_);
}
}
// Returns true if another entry in this directory listing is available.
bool HasNextEntry() { return dir_ != nullptr && next_entry_ != nullptr; }
// Returns the next entry in this directory listing and advances to the entry
// after the one that is returned, if it exists.
std::string NextEntry() {
if (HasNextEntry()) {
std::string result = std::string(next_entry_->d_name);
ReadNextEntry();
return result;
} else {
return std::string();
}
}
private:
void ReadNextEntry() {
next_entry_ = readdir(dir_);
while (next_entry_ && (std::string(next_entry_->d_name) == "." ||
std::string(next_entry_->d_name) == "..")) {
next_entry_ = readdir(dir_);
}
}
DIR* dir_ = nullptr;
struct dirent* next_entry_ = nullptr;
};
#else
class DirectoryListing {
public:
explicit DirectoryListing(const std::string& directory) {
directory_ = directory;
std::string search_string = directory + "\\*.*";
find_handle_ = FindFirstFile(search_string.c_str(), &find_data_);
}
~DirectoryListing() {
if (find_handle_ != INVALID_HANDLE_VALUE) {
FindClose(find_handle_);
}
}
// Returns true if another entry in this directory listing is available.
bool HasNextEntry() { return find_handle_ != INVALID_HANDLE_VALUE; }
// Returns the next entry in this directory listing and advances to the entry
// after the one that is returned, if it exists.
std::string NextEntry() {
if (HasNextEntry()) {
std::string result =
std::string(directory_ + "\\" + find_data_.cFileName);
ReadNextEntry();
return result;
} else {
return std::string();
}
}
private:
void ReadNextEntry() {
int find_result = FindNextFile(find_handle_, &find_data_);
while (find_result != 0 && (std::string(find_data_.cFileName) == "." ||
std::string(find_data_.cFileName) == "..")) {
find_result = FindNextFile(find_handle_, &find_data_);
}
if (find_result == 0) {
FindClose(find_handle_);
find_handle_ = INVALID_HANDLE_VALUE;
}
}
std::string directory_;
HANDLE find_handle_ = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA find_data_;
};
#endif // _WIN32
} // namespace
::mediapipe::Status GetContents(absl::string_view file_name,
std::string* output) {
FILE* fp = fopen(file_name.data(), "r");
@@ -70,62 +178,34 @@ namespace file {
::mediapipe::Status MatchInTopSubdirectories(
const std::string& parent_directory, const std::string& file_name,
std::vector<std::string>* results) {
DIR* dir = opendir(parent_directory.c_str());
CHECK(dir);
// Iterates through the parent direcotry.
while (true) {
struct dirent* dir_ent = readdir(dir);
if (dir_ent == nullptr) {
break;
}
if (std::string(dir_ent->d_name) == "." ||
std::string(dir_ent->d_name) == "..") {
continue;
}
std::string subpath =
JoinPath(parent_directory, std::string(dir_ent->d_name));
DIR* sub_dir = opendir(subpath.c_str());
// Iterates through the subdirecotry to find file matches.
while (true) {
struct dirent* dir_ent_2 = readdir(sub_dir);
if (dir_ent_2 == nullptr) {
break;
}
if (std::string(dir_ent_2->d_name) == "." ||
std::string(dir_ent_2->d_name) == "..") {
continue;
}
if (absl::EndsWith(std::string(dir_ent_2->d_name), file_name)) {
results->push_back(JoinPath(subpath, std::string(dir_ent_2->d_name)));
DirectoryListing parent_listing(parent_directory);
while (parent_listing.HasNextEntry()) {
std::string subdirectory =
JoinPath(parent_directory, parent_listing.NextEntry());
DirectoryListing subdirectory_listing(subdirectory);
while (subdirectory_listing.HasNextEntry()) {
std::string next_entry = subdirectory_listing.NextEntry();
if (absl::EndsWith(next_entry, file_name)) {
results->push_back(JoinPath(subdirectory, next_entry));
}
}
closedir(sub_dir);
}
closedir(dir);
return ::mediapipe::OkStatus();
}
::mediapipe::Status MatchFileTypeInDirectory(
const std::string& directory, const std::string& file_suffix,
std::vector<std::string>* results) {
DIR* dir = opendir(directory.c_str());
CHECK(dir);
// Iterates through the direcotry.
while (true) {
struct dirent* dir_ent = readdir(dir);
if (dir_ent == nullptr) {
break;
}
if (std::string(dir_ent->d_name) == "." ||
std::string(dir_ent->d_name) == "..") {
continue;
}
DirectoryListing directory_listing(directory);
if (absl::EndsWith(std::string(dir_ent->d_name), file_suffix)) {
results->push_back(JoinPath(directory, std::string(dir_ent->d_name)));
while (directory_listing.HasNextEntry()) {
std::string next_entry = directory_listing.NextEntry();
if (absl::EndsWith(next_entry, file_suffix)) {
results->push_back(JoinPath(directory, next_entry));
}
}
closedir(dir);
return ::mediapipe::OkStatus();
}
@@ -12,8 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "mediapipe/framework/deps/threadpool.h"
#include <errno.h>
#include <pthread.h>
#include <string.h>
@@ -22,6 +20,7 @@
#include "absl/strings/str_cat.h"
#include "absl/strings/str_join.h"
#include "mediapipe/framework/deps/threadpool.h"
#include "mediapipe/framework/port/logging.h"
namespace mediapipe {
@@ -0,0 +1,156 @@
// Copyright 2020 The MediaPipe Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <errno.h>
#include <string.h>
#include <thread> // NOLINT(build/c++11)
#include "mediapipe/framework/deps/threadpool.h"
#ifdef _WIN32
#include <windows.h>
#else
#include <sys/syscall.h>
#include <unistd.h>
#endif
#include "absl/strings/str_cat.h"
#include "absl/strings/str_join.h"
#include "mediapipe/framework/port/logging.h"
namespace mediapipe {
class ThreadPool::WorkerThread {
public:
// Creates and starts a thread that runs pool->RunWorker().
WorkerThread(ThreadPool* pool, const std::string& name_prefix);
// REQUIRES: Join() must have been called.
~WorkerThread();
// Joins with the running thread.
void Join();
private:
static void* ThreadBody(void* arg);
ThreadPool* pool_;
std::string name_prefix_;
std::thread thread_;
};
ThreadPool::WorkerThread::WorkerThread(ThreadPool* pool,
const std::string& name_prefix)
: pool_(pool), name_prefix_(name_prefix) {
thread_ = std::thread(ThreadBody, this);
}
ThreadPool::WorkerThread::~WorkerThread() {}
void ThreadPool::WorkerThread::Join() { thread_.join(); }
void* ThreadPool::WorkerThread::ThreadBody(void* arg) {
auto thread = reinterpret_cast<WorkerThread*>(arg);
int nice_priority_level =
thread->pool_->thread_options().nice_priority_level();
const std::set<int> selected_cpus = thread->pool_->thread_options().cpu_set();
if (nice_priority_level != 0 || !selected_cpus.empty()) {
LOG(ERROR) << "Thread priority and processor affinity feature aren't "
"supported by the std::thread threadpool implementation.";
}
thread->pool_->RunWorker();
return nullptr;
}
ThreadPool::ThreadPool(int num_threads) {
num_threads_ = (num_threads == 0) ? 1 : num_threads;
}
ThreadPool::ThreadPool(const std::string& name_prefix, int num_threads)
: name_prefix_(name_prefix) {
num_threads_ = (num_threads == 0) ? 1 : num_threads;
}
ThreadPool::ThreadPool(const ThreadOptions& thread_options,
const std::string& name_prefix, int num_threads)
: name_prefix_(name_prefix), thread_options_(thread_options) {
num_threads_ = (num_threads == 0) ? 1 : num_threads;
}
ThreadPool::~ThreadPool() {
mutex_.Lock();
stopped_ = true;
condition_.SignalAll();
mutex_.Unlock();
for (int i = 0; i < threads_.size(); ++i) {
threads_[i]->Join();
delete threads_[i];
}
threads_.clear();
}
void ThreadPool::StartWorkers() {
for (int i = 0; i < num_threads_; ++i) {
threads_.push_back(new WorkerThread(this, name_prefix_));
}
}
void ThreadPool::Schedule(std::function<void()> callback) {
mutex_.Lock();
tasks_.push_back(std::move(callback));
condition_.Signal();
mutex_.Unlock();
}
int ThreadPool::num_threads() const { return num_threads_; }
void ThreadPool::RunWorker() {
mutex_.Lock();
while (true) {
if (!tasks_.empty()) {
std::function<void()> task = std::move(tasks_.front());
tasks_.pop_front();
mutex_.Unlock();
task();
mutex_.Lock();
} else {
if (stopped_) {
break;
} else {
condition_.Wait(&mutex_);
}
}
}
mutex_.Unlock();
}
const ThreadOptions& ThreadPool::thread_options() const {
return thread_options_;
}
namespace internal {
std::string CreateThreadName(const std::string& prefix, int thread_id) {
std::string name = absl::StrCat(prefix, "/", thread_id);
constexpr size_t kMaxThreadNameLength = 15;
name.resize(std::min(name.length(), kMaxThreadNameLength));
return name;
}
} // namespace internal
} // namespace mediapipe
@@ -63,6 +63,9 @@ message ImageFormat {
// One float per pixel.
VEC32F1 = 9;
// Two floats per pixel.
VEC32F2 = 12;
// LAB, interleaved: one byte for L, then one byte for a, then one
// byte for b for each pixel.
LAB8 = 10;
@@ -277,6 +277,8 @@ int ImageFrame::NumberOfChannelsForFormat(ImageFormat::Format format) {
return 4;
case ImageFormat::VEC32F1:
return 1;
case ImageFormat::VEC32F2:
return 2;
case ImageFormat::LAB8:
return 3;
case ImageFormat::SBGRA:
@@ -304,6 +306,8 @@ int ImageFrame::ChannelSizeForFormat(ImageFormat::Format format) {
return sizeof(uint16);
case ImageFormat::VEC32F1:
return sizeof(float);
case ImageFormat::VEC32F2:
return sizeof(float);
case ImageFormat::LAB8:
return sizeof(uint8);
case ImageFormat::SBGRA:
@@ -331,6 +335,8 @@ int ImageFrame::ByteDepthForFormat(ImageFormat::Format format) {
return 2;
case ImageFormat::VEC32F1:
return 4;
case ImageFormat::VEC32F2:
return 4;
case ImageFormat::LAB8:
return 1;
case ImageFormat::SBGRA:
@@ -56,6 +56,9 @@ int GetMatType(const mediapipe::ImageFormat::Format format) {
case mediapipe::ImageFormat::VEC32F1:
type = CV_32F;
break;
case mediapipe::ImageFormat::VEC32F2:
type = CV_32FC2;
break;
case mediapipe::ImageFormat::LAB8:
type = CV_8U;
break;
@@ -81,19 +81,22 @@ TEST(ImageFrameOpencvTest, ImageFormats) {
const int i_width = 123, i_height = 45;
ImageFrame frame_g8(ImageFormat::GRAY8, i_width, i_height);
ImageFrame frame_g16(ImageFormat::GRAY16, i_width, i_height);
ImageFrame frame_f32(ImageFormat::VEC32F1, i_width, i_height);
ImageFrame frame_v32f1(ImageFormat::VEC32F1, i_width, i_height);
ImageFrame frame_v32f2(ImageFormat::VEC32F2, i_width, i_height);
ImageFrame frame_c3(ImageFormat::SRGB, i_width, i_height);
ImageFrame frame_c4(ImageFormat::SRGBA, i_width, i_height);
cv::Mat mat_g8 = formats::MatView(&frame_g8);
cv::Mat mat_g16 = formats::MatView(&frame_g16);
cv::Mat mat_f32 = formats::MatView(&frame_f32);
cv::Mat mat_v32f1 = formats::MatView(&frame_v32f1);
cv::Mat mat_v32f2 = formats::MatView(&frame_v32f2);
cv::Mat mat_c3 = formats::MatView(&frame_c3);
cv::Mat mat_c4 = formats::MatView(&frame_c4);
EXPECT_EQ(mat_g8.type(), CV_8UC1);
EXPECT_EQ(mat_g16.type(), CV_16UC1);
EXPECT_EQ(mat_f32.type(), CV_32FC1);
EXPECT_EQ(mat_v32f1.type(), CV_32FC1);
EXPECT_EQ(mat_v32f2.type(), CV_32FC2);
EXPECT_EQ(mat_c3.type(), CV_8UC3);
EXPECT_EQ(mat_c4.type(), CV_8UC4);
}
+106 -20
View File
@@ -327,6 +327,8 @@ namespace packet_internal {
template <typename T>
class Holder;
template <typename T>
class ForeignHolder;
class HolderBase {
public:
@@ -353,10 +355,112 @@ class HolderBase {
// Downcasts this to Holder<T>. Returns nullptr if deserialization
// failed or if the requested type is not what is stored.
template <typename T>
Holder<T>* As();
inline Holder<T>* As(
typename std::enable_if<
(!std::is_base_of<proto_ns::MessageLite, T>::value &&
!std::is_base_of<proto_ns::Message, T>::value) ||
(std::is_same<proto_ns::MessageLite, T>::value ||
std::is_same<proto_ns::Message, T>::value)>::type* = 0) {
if (HolderIsOfType<Holder<T>>() || HolderIsOfType<ForeignHolder<T>>()) {
return static_cast<Holder<T>*>(this);
}
// Does not hold a T.
return nullptr;
}
// For proto Message/MessageLite subclasses.
// When holder data is a concrete proto, the method downcasts this to
// Holder<T> if the requested type is what is stored.
// When holder data is a generic proto Message/MessageLite and a concrete
// proto type T is requested, the method will downcast the HolderBase to
// Holder<T> if the proto data is an instance of T.
template <typename T>
inline Holder<T>* As(
typename std::enable_if<
(std::is_base_of<proto_ns::MessageLite, T>::value ||
std::is_base_of<proto_ns::Message, T>::value) &&
(!std::is_same<proto_ns::MessageLite, T>::value &&
!std::is_same<proto_ns::Message, T>::value)>::type* = 0) {
// Holder data is an instance of subclass type T.
if (HolderIsOfType<Holder<T>>() || HolderIsOfType<ForeignHolder<T>>()) {
return static_cast<Holder<T>*>(this);
}
// Holder data is a generic proto Message/MessageLite and a subclass type T
// is requested.
if (HolderIsOfType<Holder<proto_ns::Message>>() ||
HolderIsOfType<ForeignHolder<proto_ns::Message>>() ||
HolderIsOfType<Holder<proto_ns::MessageLite>>() ||
HolderIsOfType<ForeignHolder<proto_ns::MessageLite>>()) {
// TODO: Holder<proto_ns::Message/MessageLite> cannot be
// legally downcast to Holder<T>, even though that downcast works in
// practice. Need to propose a better way to do the downcast.
Holder<T>* holder = static_cast<Holder<T>*>(this);
T tmp;
VLOG(2) << "Holder proto data type: " << holder->data().GetTypeName()
<< " vs requested proto type: " << tmp.GetTypeName();
if (tmp.GetTypeName() == holder->data().GetTypeName()) {
return holder;
}
}
// Does not hold a T.
return nullptr;
}
// Same as non-const As() function.
template <typename T>
const Holder<T>* As() const;
inline const Holder<T>* As(
typename std::enable_if<
(!std::is_base_of<proto_ns::MessageLite, T>::value &&
!std::is_base_of<proto_ns::Message, T>::value) ||
(std::is_same<proto_ns::MessageLite, T>::value ||
std::is_same<proto_ns::Message, T>::value)>::type* = 0) const {
if (HolderIsOfType<Holder<T>>() || HolderIsOfType<ForeignHolder<T>>()) {
return static_cast<const Holder<T>*>(this);
}
// Does not hold a T.
return nullptr;
}
// For proto Message/MessageLite subclasses.
// When holder data is a concrete proto, the method downcasts this to
// Holder<T> if the requested type is what is stored.
// When holder data is a generic proto Message/MessageLite and a concrete
// proto type T is requested, the method will downcast the HolderBase to
// Holder<T> if the proto data is an instance of T.
template <typename T>
inline const Holder<T>* As(
typename std::enable_if<
(std::is_base_of<proto_ns::MessageLite, T>::value ||
std::is_base_of<proto_ns::Message, T>::value) &&
(!std::is_same<proto_ns::MessageLite, T>::value &&
!std::is_same<proto_ns::Message, T>::value)>::type* = 0) const {
if (HolderIsOfType<Holder<T>>() || HolderIsOfType<ForeignHolder<T>>()) {
return static_cast<const Holder<T>*>(this);
}
// Holder data is a generic proto Message/MessageLite and a subclass type T
// is requested.
if (HolderIsOfType<Holder<proto_ns::Message>>() ||
HolderIsOfType<ForeignHolder<proto_ns::Message>>() ||
HolderIsOfType<Holder<proto_ns::MessageLite>>() ||
HolderIsOfType<ForeignHolder<proto_ns::MessageLite>>()) {
// TODO: Holder<proto_ns::Message/MessageLite> cannot be
// legally downcast to Holder<T>, even though that downcast works in
// practice. Need to propose a better way to do the downcast.
Holder<T>* holder = static_cast<const Holder<T>*>(this);
T tmp;
VLOG(2) << "Holder proto data type: " << holder->data().GetTypeName()
<< " vs requested proto type: " << tmp.GetTypeName();
if (tmp.GetTypeName() == holder->data().GetTypeName()) {
return holder;
}
}
// Does not hold a T.
return nullptr;
}
// Returns the pointer to MessageLite type for the data in holder, if
// underlying object is protocol buffer type, otherwise, nullptr is returned.
@@ -518,24 +622,6 @@ class ForeignHolder : public Holder<T> {
}
};
template <typename T>
Holder<T>* HolderBase::As() {
if (HolderIsOfType<Holder<T>>() || HolderIsOfType<ForeignHolder<T>>()) {
return static_cast<Holder<T>*>(this);
}
// Does not hold a T.
return nullptr;
}
template <typename T>
const Holder<T>* HolderBase::As() const {
if (HolderIsOfType<Holder<T>>() || HolderIsOfType<ForeignHolder<T>>()) {
return static_cast<const Holder<T>*>(this);
}
// Does not hold a T.
return nullptr;
}
} // namespace packet_internal
inline Packet::Packet(const Packet& packet)
+51 -1
View File
@@ -164,7 +164,7 @@ TEST(PacketTest, TypeRegistrationDebugString) {
"mediapipe::(anonymous namespace)::UnregisteredPairStruct");
}
TEST(PacketTest, CanContainProtobufMessage) {
TEST(PacketTest, ReturnGenericProtobufMessage) {
std::unique_ptr<::mediapipe::PacketTestProto> proto_ptr(
new ::mediapipe::PacketTestProto);
proto_ptr->add_x(123);
@@ -174,6 +174,56 @@ TEST(PacketTest, CanContainProtobufMessage) {
.x(0));
}
TEST(PacketTest, ReturnProtobufMessageSubType) {
std::unique_ptr<::mediapipe::PacketTestProto> proto_ptr(
new ::mediapipe::PacketTestProto);
proto_ptr->add_x(123);
Packet packet = Adopt(static_cast<proto_ns::Message*>(proto_ptr.release()));
EXPECT_EQ(123, packet.Get<::mediapipe::PacketTestProto>().x(0));
EXPECT_EQ(123, packet.Get<const ::mediapipe::PacketTestProto>().x(0));
}
TEST(PacketTest, TryWrongProtobufMessageSubType) {
// Packet of PacketTestProto.
std::unique_ptr<::mediapipe::PacketTestProto> proto_ptr(
new ::mediapipe::PacketTestProto);
proto_ptr->add_x(123);
Packet packet = Adopt(proto_ptr.release());
EXPECT_FALSE(packet.ValidateAsType<::mediapipe::SimpleProto>().ok());
EXPECT_TRUE(packet.ValidateAsType<::mediapipe::PacketTestProto>().ok());
// Packet of proto_ns::Message.
proto_ptr.reset(new ::mediapipe::PacketTestProto);
proto_ptr->add_x(456);
Packet packet2 = Adopt(static_cast<proto_ns::Message*>(proto_ptr.release()));
EXPECT_FALSE(packet2.ValidateAsType<::mediapipe::SimpleProto>().ok());
EXPECT_TRUE(packet2.ValidateAsType<::mediapipe::PacketTestProto>().ok());
EXPECT_EQ(123, packet.Get<::mediapipe::PacketTestProto>().x(0));
}
TEST(PacketTest, ReturnProtobufMessageLiteSubType) {
std::unique_ptr<::mediapipe::PacketTestProto> proto_ptr(
new ::mediapipe::PacketTestProto);
proto_ptr->add_x(123);
Packet packet =
Adopt(static_cast<proto_ns::MessageLite*>(proto_ptr.release()));
EXPECT_EQ(123, packet.Get<::mediapipe::PacketTestProto>().x(0));
EXPECT_EQ(123, packet.Get<const ::mediapipe::PacketTestProto>().x(0));
}
TEST(PacketTest, TryWrongProtobufMessageLiteSubType) {
// Packet of PacketTestProto.
std::unique_ptr<::mediapipe::PacketTestProto> proto_ptr(
new ::mediapipe::PacketTestProto);
// Packet of proto_ns::MessageLite.
proto_ptr->add_x(456);
Packet packet =
Adopt(static_cast<proto_ns::MessageLite*>(proto_ptr.release()));
EXPECT_FALSE(packet.ValidateAsType<::mediapipe::SimpleProto>().ok());
EXPECT_TRUE(packet.ValidateAsType<::mediapipe::PacketTestProto>().ok());
EXPECT_EQ(456, packet.Get<::mediapipe::PacketTestProto>().x(0));
}
TEST(PacketTest, GetProtoBase) {
std::unique_ptr<::mediapipe::PacketTestProto> proto_ptr(
new ::mediapipe::PacketTestProto);
+2 -2
View File
@@ -96,7 +96,7 @@ cc_library(
visibility = ["//visibility:public"],
deps = [
"//mediapipe/framework:port",
"@com_github_glog_glog//:glog",
"//third_party:glog",
],
)
@@ -177,7 +177,7 @@ cc_library(
visibility = ["//visibility:public"],
deps = [
"//mediapipe/framework:port",
"@com_github_glog_glog//:glog",
"//third_party:glog",
"@com_google_absl//absl/time",
],
)
+45 -20
View File
@@ -9,31 +9,44 @@ def provided_args(**kwargs):
"""Returns the keyword arguments omitting None arguments."""
return {k: v for k, v in kwargs.items() if v != None}
def allowed_args(**kwargs):
"""Returns the keyword arguments allowed for proto_library().
def replace_suffix(string, old, new):
"""Returns a string with an old suffix replaced by a new suffix."""
return string.endswith(old) and string[:-len(old)] + new or string
def replace_deps(deps, old, new, drop_google_protobuf = True):
"""Returns deps with an old suffix replaced by a new suffix.
Args:
**kwargs: the specified keyword arguments.
deps: the specified dep targets.
old: the suffix to remove.
new: the suffix to insert.
drop_google_protobuf: if true, omit google/protobuf deps.
Returns:
the allowed keyword arguments.
the modified dep targets.
"""
result = dict(kwargs)
result.pop("cc_api_version", None)
return result
if drop_google_protobuf:
deps = [dep for dep in deps if not dep.startswith("@com_google_protobuf//")]
deps = [replace_suffix(dep, "any_proto", "cc_wkt_protos") for dep in deps]
deps = [replace_suffix(dep, old, new) for dep in deps]
return deps
# TODO: load this macro from a common helper file.
def mediapipe_proto_library(
name,
srcs,
deps = [],
exports = None,
visibility = None,
testonly = 0,
compatible_with = [],
testonly = None,
compatible_with = None,
def_proto = True,
def_cc_proto = True,
def_py_proto = True,
def_java_lite_proto = True,
def_portable_proto = True,
def_objc_proto = True,
def_java_proto = True,
def_jspb_proto = True,
portable_deps = None):
"""Defines the proto_library targets needed for all mediapipe platforms.
@@ -41,6 +54,7 @@ def mediapipe_proto_library(
name: the new proto_library target name.
srcs: the ".proto" source files to compile.
deps: the proto_library targets for all referenced protobufs.
exports: deps that are published with "import public".
portable_deps: the portable_proto_library targets for all referenced protobufs.
visibility: visibility of this target.
testonly: true means the proto can be used for testing only.
@@ -50,27 +64,30 @@ def mediapipe_proto_library(
def_py_proto: define the py_proto_library target
def_java_lite_proto: define the java_lite_proto_library target
def_portable_proto: define the portable_proto_library target
def_objc_proto: define the objc_proto_library target
def_java_proto: define the java_proto_library target
def_jspb_proto: define the jspb_proto_library target
"""
_ignore = [def_portable_proto, portable_deps]
_ignore = [def_portable_proto, def_objc_proto, def_java_proto, def_jspb_proto, portable_deps]
# The proto_library targets for the compiled ".proto" source files.
proto_deps = [":" + name]
if def_proto:
native.proto_library(**allowed_args(**provided_args(
native.proto_library(**provided_args(
name = name,
srcs = srcs,
deps = deps,
exports = exports,
visibility = visibility,
testonly = testonly,
cc_api_version = 2,
compatible_with = compatible_with,
)))
))
if def_cc_proto:
cc_deps = [dep.replace("_proto", "_cc_proto") for dep in deps]
cc_deps = replace_deps(deps, "_proto", "_cc_proto", False)
mediapipe_cc_proto_library(**provided_args(
name = name.replace("_proto", "_cc_proto"),
name = replace_suffix(name, "_proto", "_cc_proto"),
srcs = srcs,
deps = proto_deps,
cc_deps = cc_deps,
@@ -79,22 +96,25 @@ def mediapipe_proto_library(
))
if def_py_proto:
py_deps = [dep.replace("_proto", "_py_pb2") for dep in deps]
py_deps = replace_deps(deps, "_proto", "_py_pb2")
mediapipe_py_proto_library(**provided_args(
name = name.replace("_proto", "_py_pb2"),
name = replace_suffix(name, "_proto", "_py_pb2"),
srcs = srcs,
proto_deps = proto_deps,
py_proto_deps = py_deps,
visibility = visibility,
api_version = 2,
visibility = visibility,
testonly = testonly,
))
if def_java_lite_proto:
native.java_lite_proto_library(**provided_args(
name = name.replace("_proto", "_java_proto_lite"),
name = replace_suffix(name, "_proto", "_java_proto_lite"),
deps = proto_deps,
strict_deps = 0,
visibility = visibility,
testonly = testonly,
compatible_with = compatible_with,
))
def mediapipe_py_proto_library(
@@ -103,14 +123,18 @@ def mediapipe_py_proto_library(
visibility,
py_proto_deps = [],
proto_deps = None,
api_version = None):
api_version = None,
testonly = 0):
"""Generate py_proto_library for mediapipe open source version.
Args:
name: the name of the py_proto_library.
api_version: api version for bazel use only.
srcs: the .proto files of the py_proto_library for Bazel use.
visibility: visibility of this target.
py_proto_deps: a list of dependency labels for Bazel use; must be py_proto_library.
proto_deps: a list of dependency labels for bazel use.
testonly: test only proto or not.
"""
_ignore = [api_version, proto_deps]
py_proto_library(**provided_args(
@@ -120,6 +144,7 @@ def mediapipe_py_proto_library(
default_runtime = "@com_google_protobuf//:protobuf_python",
protoc = "@com_google_protobuf//:protoc",
deps = py_proto_deps + ["@com_google_protobuf//:protobuf_python"],
testonly = testonly,
))
def mediapipe_cc_proto_library(name, srcs, visibility, deps = [], cc_deps = [], testonly = 0):
+4 -4
View File
@@ -27,10 +27,10 @@ typedef int16_t int16;
typedef int32_t int32;
typedef int64_t int64;
typedef u_int8_t uint8;
typedef u_int16_t uint16;
typedef u_int32_t uint32;
typedef u_int64_t uint64;
typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;
typedef uint64_t uint64;
typedef signed int char32;
typedef unsigned long uword_t;
+4
View File
@@ -16,6 +16,10 @@
#define MEDIAPIPE_PORT_LOGGING_H_
#include "absl/time/time.h"
#ifdef _WIN32
#define GLOG_NO_ABBREVIATED_SEVERITIES
#endif
#include "glog/logging.h"
namespace mediapipe {
-19
View File
@@ -17,23 +17,4 @@
#include "absl/base/port.h"
namespace mediapipe {
inline void *aligned_malloc(size_t size, int minimum_alignment) {
void *ptr = nullptr;
// posix_memalign requires that the requested alignment be at least
// sizeof(void*). In this case, fall back on malloc which should return memory
// aligned to at least the size of a pointer.
const int required_alignment = sizeof(void *);
if (minimum_alignment < required_alignment) return malloc(size);
if (posix_memalign(&ptr, static_cast<size_t>(minimum_alignment), size) != 0)
return nullptr;
else
return ptr;
}
inline void aligned_free(void *aligned_memory) { free(aligned_memory); }
} // namespace mediapipe
#endif // MEDIAPIPE_PORT_PORT_H_
@@ -428,7 +428,13 @@ void GraphProfiler::SetCloseRuntime(const CalculatorContext& calculator_context,
void GraphProfiler::AddTimeSample(int64 start_time_usec, int64 end_time_usec,
TimeHistogram* histogram) {
CHECK_GE(end_time_usec, start_time_usec);
if (end_time_usec < start_time_usec) {
LOG(ERROR) << absl::Substitute(
"end_time_usec ($0) is < start_time_usec ($1)", end_time_usec,
start_time_usec);
return;
}
int64 time_usec = end_time_usec - start_time_usec;
histogram->set_total(histogram->total() + time_usec);
int64 interval_index = time_usec / histogram->interval_size_usec();
+25
View File
@@ -94,5 +94,30 @@ std::string CanonicalNodeName(const CalculatorGraphConfig& graph_config,
return absl::StrCat(node_name, "_", sequence + 1);
}
std::string ParseNameFromStream(const std::string& stream) {
std::string tag, name;
int index;
MEDIAPIPE_CHECK_OK(tool::ParseTagIndexName(stream, &tag, &index, &name));
return name;
}
std::pair<std::string, int> ParseTagIndexFromStream(const std::string& stream) {
std::string tag, name;
int index;
MEDIAPIPE_CHECK_OK(tool::ParseTagIndexName(stream, &tag, &index, &name));
return {tag, index};
}
std::string CatTag(const std::string& tag, int index) {
return absl::StrCat(tag, index <= 0 ? "" : absl::StrCat(":", index));
}
std::string CatStream(const std::pair<std::string, int>& tag_index,
const std::string& name) {
std::string tag = CatTag(tag_index.first, tag_index.second);
tag = tag.empty() ? tag : absl::StrCat(tag, ":");
return absl::StrCat(tag, name);
}
} // namespace tool
} // namespace mediapipe
+13
View File
@@ -73,6 +73,19 @@ std::string GetUnusedNodeName(const CalculatorGraphConfig& config,
std::string CanonicalNodeName(const CalculatorGraphConfig& graph_config,
int node_id);
// Parses the name from a "tag:index:name".
std::string ParseNameFromStream(const std::string& stream);
// Parses the TagIndex from a "tag:index:name".
std::pair<std::string, int> ParseTagIndexFromStream(const std::string& stream);
// Formats to "tag:index".
std::string CatTag(const std::string& tag, int index);
// Concatenates "tag:index:name" into a single std::string.
std::string CatStream(const std::pair<std::string, int>& tag_index,
const std::string& name);
} // namespace tool
} // namespace mediapipe
+3 -3
View File
@@ -40,13 +40,13 @@ template <typename T>
struct IsExtension {
private:
template <typename U>
static decltype(U::ext, std::true_type()) test(int);
static char test(decltype(&U::ext));
template <typename>
static std::false_type test(...);
static int test(...);
public:
static constexpr bool value = decltype(test<T>(0))();
static constexpr bool value = (sizeof(test<T>(0)) == sizeof(char));
};
// A map from object type to object.
@@ -79,7 +79,11 @@ mediapipe::Status ReadFile(const std::string& proto_source, bool read_text,
// Write a proto to a text or a binary file.
mediapipe::Status WriteFile(const std::string& proto_output, bool write_text,
const proto_ns::Message& message) {
std::ofstream ofs(proto_output, std::ofstream::out | std::ofstream::trunc);
std::ios_base::openmode mode = std::ios_base::out | std::ios_base::trunc;
if (!write_text) {
mode |= std::ios_base::binary;
}
std::ofstream ofs(proto_output, mode);
proto_ns::io::OstreamOutputStream out(&ofs);
MP_RETURN_IF_ERROR(WriteProto(message, write_text, proto_output, &out));
return mediapipe::OkStatus();