Project import generated by Copybara.

GitOrigin-RevId: d8caa66de45839696f5bd0786ad3bfbcb9cff632
This commit is contained in:
MediaPipe Team
2020-12-09 22:43:33 -05:00
committed by chuoling
parent f15da632de
commit 2b58cceec9
750 changed files with 22901 additions and 9478 deletions
+1 -1
View File
@@ -143,7 +143,7 @@ bool AssetManager::ReadFile(const std::string& filename, std::string* output) {
return true;
}
::mediapipe::StatusOr<std::string> AssetManager::CachedFileFromAsset(
mediapipe::StatusOr<std::string> AssetManager::CachedFileFromAsset(
const std::string& asset_path) {
RET_CHECK(cache_dir_path_.size()) << "asset manager not initialized";
+19 -20
View File
@@ -25,10 +25,10 @@ static_assert(sizeof(off_t) == 8, "Large file support is required");
namespace mediapipe {
namespace file {
::mediapipe::Status RecursivelyCreateDir(absl::string_view path,
const file::Options& options) {
mediapipe::Status RecursivelyCreateDir(absl::string_view path,
const file::Options& options) {
if (path.empty()) {
return ::mediapipe::OkStatus();
return mediapipe::OkStatus();
}
std::vector<std::string> path_comp = absl::StrSplit(path, '/');
@@ -45,45 +45,44 @@ namespace file {
if (S_ISDIR(stat_buf.st_mode)) {
continue;
}
return ::mediapipe::Status(mediapipe::StatusCode::kInternal,
"Could not stat " + std::string(crpath));
return mediapipe::Status(mediapipe::StatusCode::kInternal,
"Could not stat " + std::string(crpath));
} else {
int mkval = mkdir(crpath, options.permissions());
if (mkval == -1) {
return ::mediapipe::Status(mediapipe::StatusCode::kInternal,
"Could not create " + std::string(crpath));
return mediapipe::Status(mediapipe::StatusCode::kInternal,
"Could not create " + std::string(crpath));
}
}
}
return ::mediapipe::OkStatus();
return mediapipe::OkStatus();
}
::mediapipe::Status Exists(absl::string_view path,
const file::Options& ignored) {
mediapipe::Status Exists(absl::string_view path, const file::Options& ignored) {
struct stat64 stat_buf;
int statval = lstat64(std::string(path).c_str(), &stat_buf);
if (statval == 0) {
return ::mediapipe::OkStatus();
return mediapipe::OkStatus();
} else {
return ::mediapipe::Status(mediapipe::StatusCode::kNotFound,
"Could not stat file.");
return mediapipe::Status(mediapipe::StatusCode::kNotFound,
"Could not stat file.");
}
}
::mediapipe::Status IsDirectory(absl::string_view path,
const file::Options& /*ignored*/) {
mediapipe::Status IsDirectory(absl::string_view path,
const file::Options& /*ignored*/) {
struct stat64 stat_buf;
int statval = lstat64(std::string(path).c_str(), &stat_buf);
bool is_dir = (statval == 0 && S_ISREG(stat_buf.st_mode));
if (is_dir) {
return ::mediapipe::OkStatus();
return mediapipe::OkStatus();
} else if (statval != 0) {
return ::mediapipe::Status(mediapipe::StatusCode::kNotFound,
"File does not exists");
return mediapipe::Status(mediapipe::StatusCode::kNotFound,
"File does not exists");
} else {
return ::mediapipe::Status(mediapipe::StatusCode::kNotFound,
"Not a directory");
return mediapipe::Status(mediapipe::StatusCode::kNotFound,
"Not a directory");
}
}
@@ -22,14 +22,13 @@
namespace mediapipe {
namespace file {
::mediapipe::Status RecursivelyCreateDir(absl::string_view path,
const file::Options& options);
mediapipe::Status RecursivelyCreateDir(absl::string_view path,
const file::Options& options);
::mediapipe::Status Exists(absl::string_view path,
const file::Options& options);
mediapipe::Status Exists(absl::string_view path, const file::Options& options);
::mediapipe::Status IsDirectory(absl::string_view path,
const file::Options& options);
mediapipe::Status IsDirectory(absl::string_view path,
const file::Options& options);
} // namespace file.
} // namespace mediapipe
+24 -27
View File
@@ -42,16 +42,16 @@ class FdCloser {
} // namespace
// Read contents of a file to a std::string.
::mediapipe::Status GetContents(int fd, std::string* output) {
mediapipe::Status GetContents(int fd, std::string* output) {
// Determine the length of the file.
struct stat buf;
if (fstat(fd, &buf) != 0) {
return ::mediapipe::Status(mediapipe::StatusCode::kUnknown,
"Failed to get file status");
return mediapipe::Status(mediapipe::StatusCode::kUnknown,
"Failed to get file status");
}
if (buf.st_size < 0 || buf.st_size > SIZE_MAX) {
return ::mediapipe::Status(mediapipe::StatusCode::kInternal,
"Invalid file size");
return mediapipe::Status(mediapipe::StatusCode::kInternal,
"Invalid file size");
}
size_t length = buf.st_size;
@@ -61,46 +61,43 @@ class FdCloser {
while (length != 0) {
const ssize_t nread = read(fd, output_ptr, length);
if (nread <= 0) {
return ::mediapipe::Status(mediapipe::StatusCode::kUnknown,
"Failed to read file");
return mediapipe::Status(mediapipe::StatusCode::kUnknown,
"Failed to read file");
}
output_ptr += nread;
length -= nread;
}
return ::mediapipe::OkStatus();
return mediapipe::OkStatus();
}
// Read contents of a file to a std::string.
::mediapipe::Status GetContents(absl::string_view file_name,
std::string* output,
const file::Options& /*options*/) {
mediapipe::Status GetContents(absl::string_view file_name, std::string* output,
const file::Options& /*options*/) {
int fd = open(std::string(file_name).c_str(), O_RDONLY);
if (fd < 0) {
return ::mediapipe::Status(
mediapipe::StatusCode::kUnknown,
"Failed to open file: " + std::string(file_name));
return mediapipe::Status(mediapipe::StatusCode::kUnknown,
"Failed to open file: " + std::string(file_name));
}
FdCloser closer(fd);
return GetContents(fd, output);
}
::mediapipe::Status GetContents(absl::string_view file_name,
std::string* output) {
mediapipe::Status GetContents(absl::string_view file_name,
std::string* output) {
return GetContents(file_name, output, file::Defaults());
}
::mediapipe::Status SetContents(absl::string_view file_name,
absl::string_view content,
const file::Options& options) {
mediapipe::Status SetContents(absl::string_view file_name,
absl::string_view content,
const file::Options& options) {
// Mode -rw-r--r--
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH;
int fd =
open(std::string(file_name).c_str(), O_WRONLY | O_CREAT | O_TRUNC, mode);
if (fd < 0) {
return ::mediapipe::Status(
mediapipe::StatusCode::kUnknown,
"Failed to open file: " + std::string(file_name));
return mediapipe::Status(mediapipe::StatusCode::kUnknown,
"Failed to open file: " + std::string(file_name));
}
int bytes_written = 0;
@@ -110,15 +107,15 @@ class FdCloser {
close(fd);
if (bytes_written == content.size()) {
return ::mediapipe::OkStatus();
return mediapipe::OkStatus();
} else {
return ::mediapipe::Status(mediapipe::StatusCode::kUnknown,
"Failed to write file");
return mediapipe::Status(mediapipe::StatusCode::kUnknown,
"Failed to write file");
}
}
::mediapipe::Status SetContents(absl::string_view file_name,
absl::string_view content) {
mediapipe::Status SetContents(absl::string_view file_name,
absl::string_view content) {
return SetContents(file_name, content, file::Defaults());
}
+9 -11
View File
@@ -25,25 +25,23 @@ namespace mediapipe {
namespace file {
// Read contents of a file to a std::string.
::mediapipe::Status GetContents(absl::string_view file_name,
std::string* output,
const file::Options& options);
mediapipe::Status GetContents(absl::string_view file_name, std::string* output,
const file::Options& options);
// Read contents of a file to a std::string with default file options.
::mediapipe::Status GetContents(absl::string_view file_name,
std::string* output);
mediapipe::Status GetContents(absl::string_view file_name, std::string* output);
// Read contents of a file to a std::string from an open file descriptor.
::mediapipe::Status GetContents(int fd, std::string* output);
mediapipe::Status GetContents(int fd, std::string* output);
// Write std::string to file.
::mediapipe::Status SetContents(absl::string_view file_name,
absl::string_view content,
const file::Options& options);
mediapipe::Status SetContents(absl::string_view file_name,
absl::string_view content,
const file::Options& options);
// Write std::string to file with default file options.
::mediapipe::Status SetContents(absl::string_view file_name,
absl::string_view content);
mediapipe::Status SetContents(absl::string_view file_name,
absl::string_view content);
} // namespace file
} // namespace mediapipe
+26 -10
View File
@@ -16,6 +16,7 @@
#include <math.h>
#include <algorithm>
#include <cmath>
#include "mediapipe/framework/port/logging.h"
@@ -37,6 +38,11 @@ using Rectangle = RenderAnnotation::Rectangle;
using RoundedRectangle = RenderAnnotation::RoundedRectangle;
using Text = RenderAnnotation::Text;
int ClampThickness(int thickness) {
constexpr int kMaxThickness = 32767; // OpenCV MAX_THICKNESS
return std::clamp(thickness, 1, kMaxThickness);
}
bool NormalizedtoPixelCoordinates(double normalized_x, double normalized_y,
int image_width, int image_height, int* x_px,
int* y_px) {
@@ -152,7 +158,8 @@ void AnnotationRenderer::DrawRectangle(const RenderAnnotation& annotation) {
}
const cv::Scalar color = MediapipeColorToOpenCVColor(annotation.color());
const int thickness = round(annotation.thickness() * scale_factor_);
const int thickness =
ClampThickness(round(annotation.thickness() * scale_factor_));
if (rectangle.rotation() != 0.0) {
const auto& rect = RectangleToOpenCVRotatedRect(left, top, right, bottom,
rectangle.rotation());
@@ -231,7 +238,8 @@ void AnnotationRenderer::DrawRoundedRectangle(
}
const cv::Scalar color = MediapipeColorToOpenCVColor(annotation.color());
const int thickness = round(annotation.thickness() * scale_factor_);
const int thickness =
ClampThickness(round(annotation.thickness() * scale_factor_));
const int corner_radius =
round(annotation.rounded_rectangle().corner_radius() * scale_factor_);
const int line_type = annotation.rounded_rectangle().line_type();
@@ -336,9 +344,11 @@ void AnnotationRenderer::DrawOval(const RenderAnnotation& annotation) {
cv::Point center((left + right) / 2, (top + bottom) / 2);
cv::Size size((right - left) / 2, (bottom - top) / 2);
const double rotation = enclosing_rectangle.rotation() / M_PI * 180.f;
const cv::Scalar color = MediapipeColorToOpenCVColor(annotation.color());
const int thickness = round(annotation.thickness() * scale_factor_);
cv::ellipse(mat_image_, center, size, 0, 0, 360, color, thickness);
const int thickness =
ClampThickness(round(annotation.thickness() * scale_factor_));
cv::ellipse(mat_image_, center, size, rotation, 0, 360, color, thickness);
}
void AnnotationRenderer::DrawFilledOval(const RenderAnnotation& annotation) {
@@ -364,8 +374,9 @@ void AnnotationRenderer::DrawFilledOval(const RenderAnnotation& annotation) {
cv::Point center((left + right) / 2, (top + bottom) / 2);
cv::Size size(std::max(0, (right - left) / 2),
std::max(0, (bottom - top) / 2));
const double rotation = enclosing_rectangle.rotation() / M_PI * 180.f;
const cv::Scalar color = MediapipeColorToOpenCVColor(annotation.color());
cv::ellipse(mat_image_, center, size, 0, 0, 360, color, -1);
cv::ellipse(mat_image_, center, size, rotation, 0, 360, color, -1);
}
void AnnotationRenderer::DrawArrow(const RenderAnnotation& annotation) {
@@ -392,7 +403,8 @@ void AnnotationRenderer::DrawArrow(const RenderAnnotation& annotation) {
cv::Point arrow_start(x_start, y_start);
cv::Point arrow_end(x_end, y_end);
const cv::Scalar color = MediapipeColorToOpenCVColor(annotation.color());
const int thickness = round(annotation.thickness() * scale_factor_);
const int thickness =
ClampThickness(round(annotation.thickness() * scale_factor_));
// Draw the main arrow line.
cv::line(mat_image_, arrow_start, arrow_end, color, thickness);
@@ -431,7 +443,8 @@ void AnnotationRenderer::DrawPoint(const RenderAnnotation& annotation) {
cv::Point point_to_draw(x, y);
const cv::Scalar color = MediapipeColorToOpenCVColor(annotation.color());
const int thickness = round(annotation.thickness() * scale_factor_);
const int thickness =
ClampThickness(round(annotation.thickness() * scale_factor_));
cv::circle(mat_image_, point_to_draw, thickness, color, -1);
}
@@ -458,7 +471,8 @@ void AnnotationRenderer::DrawLine(const RenderAnnotation& annotation) {
cv::Point start(x_start, y_start);
cv::Point end(x_end, y_end);
const cv::Scalar color = MediapipeColorToOpenCVColor(annotation.color());
const int thickness = round(annotation.thickness() * scale_factor_);
const int thickness =
ClampThickness(round(annotation.thickness() * scale_factor_));
cv::line(mat_image_, start, end, color, thickness);
}
@@ -484,7 +498,8 @@ void AnnotationRenderer::DrawGradientLine(const RenderAnnotation& annotation) {
const cv::Point start(x_start, y_start);
const cv::Point end(x_end, y_end);
const int thickness = round(annotation.thickness() * scale_factor_);
const int thickness =
ClampThickness(round(annotation.thickness() * scale_factor_));
const cv::Scalar color1 = MediapipeColorToOpenCVColor(line.color1());
const cv::Scalar color2 = MediapipeColorToOpenCVColor(line.color2());
cv_line2(mat_image_, start, end, color1, color2, thickness);
@@ -509,7 +524,8 @@ void AnnotationRenderer::DrawText(const RenderAnnotation& annotation) {
cv::Point origin(left, baseline);
const cv::Scalar color = MediapipeColorToOpenCVColor(annotation.color());
const int thickness = round(annotation.thickness() * scale_factor_);
const int thickness =
ClampThickness(round(annotation.thickness() * scale_factor_));
const int font_face = text.font_face();
const double font_scale = ComputeFontScale(font_face, font_size, thickness);
+20 -20
View File
@@ -344,7 +344,7 @@ mediapipe::Status AudioPacketProcessor::Open(int id, AVStream* stream) {
id_ = id;
avcodec_ = avcodec_find_decoder(stream->codecpar->codec_id);
if (!avcodec_) {
return ::mediapipe::InvalidArgumentError("Failed to find codec");
return mediapipe::InvalidArgumentError("Failed to find codec");
}
avcodec_ctx_ = avcodec_alloc_context3(avcodec_);
avcodec_parameters_to_context(avcodec_ctx_, stream->codecpar);
@@ -588,18 +588,18 @@ int64 AudioPacketProcessor::MaybeCorrectPtsForRollover(int64 media_pts) {
AudioDecoder::AudioDecoder() { av_register_all(); }
AudioDecoder::~AudioDecoder() {
::mediapipe::Status status = Close();
mediapipe::Status status = Close();
if (!status.ok()) {
LOG(ERROR) << "Encountered error while closing media file: "
<< status.message();
}
}
::mediapipe::Status AudioDecoder::Initialize(
mediapipe::Status AudioDecoder::Initialize(
const std::string& input_file,
const mediapipe::AudioDecoderOptions options) {
if (options.audio_stream().empty()) {
return ::mediapipe::InvalidArgumentError(
return mediapipe::InvalidArgumentError(
"At least one audio_stream must be defined in AudioDecoderOptions");
}
std::map<int, int> stream_index_to_audio_options_index;
@@ -611,7 +611,7 @@ AudioDecoder::~AudioDecoder() {
}
Cleanup<std::function<void()>> decoder_closer([this]() {
::mediapipe::Status status = Close();
mediapipe::Status status = Close();
if (!status.ok()) {
LOG(ERROR) << "Encountered error while closing media file: "
<< status.message();
@@ -620,12 +620,12 @@ AudioDecoder::~AudioDecoder() {
avformat_ctx_ = avformat_alloc_context();
if (avformat_open_input(&avformat_ctx_, input_file.c_str(), NULL, NULL) < 0) {
return ::mediapipe::InvalidArgumentError(
return mediapipe::InvalidArgumentError(
absl::StrCat("Could not open file: ", input_file));
}
if (avformat_find_stream_info(avformat_ctx_, NULL) < 0) {
return ::mediapipe::InvalidArgumentError(absl::StrCat(
return mediapipe::InvalidArgumentError(absl::StrCat(
"Could not find stream information of file: ", input_file));
}
@@ -686,10 +686,10 @@ AudioDecoder::~AudioDecoder() {
is_first_packet_.resize(avformat_ctx_->nb_streams, true);
decoder_closer.release();
return ::mediapipe::OkStatus();
return mediapipe::OkStatus();
}
::mediapipe::Status AudioDecoder::GetData(int* options_index, Packet* data) {
mediapipe::Status AudioDecoder::GetData(int* options_index, Packet* data) {
while (true) {
for (auto& item : audio_processor_) {
while (item.second && item.second->HasData()) {
@@ -697,7 +697,7 @@ AudioDecoder::~AudioDecoder() {
is_first_packet_[item.first] = false;
*options_index =
FindOrDie(stream_id_to_audio_options_index_, item.first);
::mediapipe::Status status = item.second->GetData(data);
mediapipe::Status status = item.second->GetData(data);
// Ignore packets which are out of the requested timestamp range.
if (start_time_ != Timestamp::Unset()) {
if (is_first_packet && data->Timestamp() > start_time_) {
@@ -735,10 +735,10 @@ AudioDecoder::~AudioDecoder() {
}
MP_RETURN_IF_ERROR(ProcessPacket());
}
return ::mediapipe::OkStatus();
return mediapipe::OkStatus();
}
::mediapipe::Status AudioDecoder::Close() {
mediapipe::Status AudioDecoder::Close() {
for (auto& item : audio_processor_) {
if (item.second) {
item.second->Close();
@@ -749,10 +749,10 @@ AudioDecoder::~AudioDecoder() {
if (avformat_ctx_) {
avformat_close_input(&avformat_ctx_);
}
return ::mediapipe::OkStatus();
return mediapipe::OkStatus();
}
::mediapipe::Status AudioDecoder::FillAudioHeader(
mediapipe::Status AudioDecoder::FillAudioHeader(
const AudioStreamOptions& stream_option, TimeSeriesHeader* header) const {
const std::unique_ptr<AudioPacketProcessor>* processor_ptr_ = FindOrNull(
audio_processor_,
@@ -760,10 +760,10 @@ AudioDecoder::~AudioDecoder() {
RET_CHECK(processor_ptr_ && *processor_ptr_) << "audio stream is not open.";
MP_RETURN_IF_ERROR((*processor_ptr_)->FillHeader(header));
return ::mediapipe::OkStatus();
return mediapipe::OkStatus();
}
::mediapipe::Status AudioDecoder::ProcessPacket() {
mediapipe::Status AudioDecoder::ProcessPacket() {
std::unique_ptr<AVPacket, AVPacketDeleter> av_packet(new AVPacket());
av_init_packet(av_packet.get());
av_packet->size = 0;
@@ -785,14 +785,14 @@ AudioDecoder::~AudioDecoder() {
} else {
VLOG(3) << "Ignoring packet for stream " << stream_id;
}
return ::mediapipe::OkStatus();
return mediapipe::OkStatus();
}
VLOG(1) << "Demuxing returned error (or EOF): " << AvErrorToString(ret);
if (ret == AVERROR(EAGAIN)) {
// EAGAIN is used to signify that the av_packet should be skipped
// (maybe the demuxer is trying to re-sync). This definitely
// occurs in the FLV and MpegT demuxers.
return ::mediapipe::OkStatus();
return mediapipe::OkStatus();
}
// Unrecoverable demuxing error with details in avformat_ctx_->pb->error.
@@ -819,8 +819,8 @@ AudioDecoder::~AudioDecoder() {
"Failed to read a frame: retval = $0 ($1)", ret, AvErrorToString(ret));
}
::mediapipe::Status AudioDecoder::Flush() {
std::vector<::mediapipe::Status> statuses;
mediapipe::Status AudioDecoder::Flush() {
std::vector<mediapipe::Status> statuses;
for (auto& item : audio_processor_) {
if (item.second) {
statuses.push_back(item.second->Flush());
+8 -8
View File
@@ -194,19 +194,19 @@ class AudioDecoder {
AudioDecoder();
~AudioDecoder();
::mediapipe::Status Initialize(const std::string& input_file,
const mediapipe::AudioDecoderOptions options);
mediapipe::Status Initialize(const std::string& input_file,
const mediapipe::AudioDecoderOptions options);
::mediapipe::Status GetData(int* options_index, Packet* data);
mediapipe::Status GetData(int* options_index, Packet* data);
::mediapipe::Status Close();
mediapipe::Status Close();
::mediapipe::Status FillAudioHeader(const AudioStreamOptions& stream_option,
TimeSeriesHeader* header) const;
mediapipe::Status FillAudioHeader(const AudioStreamOptions& stream_option,
TimeSeriesHeader* header) const;
private:
::mediapipe::Status ProcessPacket();
::mediapipe::Status Flush();
mediapipe::Status ProcessPacket();
mediapipe::Status Flush();
std::map<int, int> stream_id_to_audio_options_index_;
std::map<int, int> stream_index_to_stream_id_;
+2 -2
View File
@@ -38,12 +38,12 @@ namespace {
constexpr uint32 kBufferLength = 64;
::mediapipe::StatusOr<std::string> GetFilePath(int cpu) {
mediapipe::StatusOr<std::string> GetFilePath(int cpu) {
return absl::Substitute(
"/sys/devices/system/cpu/cpu$0/cpufreq/cpuinfo_max_freq", cpu);
}
::mediapipe::StatusOr<uint64> GetCpuMaxFrequency(int cpu) {
mediapipe::StatusOr<uint64> GetCpuMaxFrequency(int cpu) {
auto path_or_status = GetFilePath(cpu);
if (!path_or_status.ok()) {
return path_or_status.status();
+2
View File
@@ -34,6 +34,7 @@ cc_test(
deps = [
":low_pass_filter",
"//mediapipe/framework/port:gtest_main",
"@com_google_absl//absl/memory",
],
)
@@ -56,6 +57,7 @@ cc_test(
":relative_velocity_filter",
"//mediapipe/framework/port:gtest_main",
"//mediapipe/framework/port:logging",
"@com_google_absl//absl/memory",
"@com_google_absl//absl/time",
],
)
@@ -14,6 +14,7 @@
#include "mediapipe/util/filtering/low_pass_filter.h"
#include "absl/memory/memory.h"
#include "mediapipe/framework/port/gtest.h"
namespace mediapipe {
@@ -18,6 +18,7 @@
#include <cmath>
#include <vector>
#include "absl/memory/memory.h"
#include "absl/time/time.h"
#include "mediapipe/framework/port/gtest.h"
#include "mediapipe/framework/port/logging.h"
@@ -25,7 +26,7 @@
namespace mediapipe {
using DistanceEstimationMode =
::mediapipe::RelativeVelocityFilter::DistanceEstimationMode;
mediapipe::RelativeVelocityFilter::DistanceEstimationMode;
absl::Duration DurationFromNanos(int64_t nanos) {
return absl::FromChrono(std::chrono::nanoseconds{nanos});
+6 -6
View File
@@ -19,8 +19,8 @@
namespace mediapipe {
::mediapipe::Status CopyInputHeadersToOutputs(const InputStreamSet& inputs,
const OutputStreamSet& outputs) {
mediapipe::Status CopyInputHeadersToOutputs(const InputStreamSet& inputs,
const OutputStreamSet& outputs) {
for (auto id = inputs.BeginId(); id < inputs.EndId(); ++id) {
std::pair<std::string, int> tag_index = inputs.TagAndIndexFromId(id);
auto output_id = outputs.GetId(tag_index.first, tag_index.second);
@@ -29,11 +29,11 @@ namespace mediapipe {
}
}
return ::mediapipe::OkStatus();
return mediapipe::OkStatus();
}
::mediapipe::Status CopyInputHeadersToOutputs(const InputStreamShardSet& inputs,
OutputStreamShardSet* outputs) {
mediapipe::Status CopyInputHeadersToOutputs(const InputStreamShardSet& inputs,
OutputStreamShardSet* outputs) {
for (auto id = inputs.BeginId(); id < inputs.EndId(); ++id) {
std::pair<std::string, int> tag_index = inputs.TagAndIndexFromId(id);
auto output_id = outputs->GetId(tag_index.first, tag_index.second);
@@ -42,7 +42,7 @@ namespace mediapipe {
}
}
return ::mediapipe::OkStatus();
return mediapipe::OkStatus();
}
} // namespace mediapipe
+4 -4
View File
@@ -22,11 +22,11 @@ namespace mediapipe {
// Copies headers from |inputs| into |outputs| respectively. The size of
// |inputs| and |outputs| must be equal.
::mediapipe::Status CopyInputHeadersToOutputs(const InputStreamSet& inputs,
const OutputStreamSet& outputs);
mediapipe::Status CopyInputHeadersToOutputs(const InputStreamSet& inputs,
const OutputStreamSet& outputs);
::mediapipe::Status CopyInputHeadersToOutputs(const InputStreamShardSet& inputs,
OutputStreamShardSet* outputs);
mediapipe::Status CopyInputHeadersToOutputs(const InputStreamShardSet& inputs,
OutputStreamShardSet* outputs);
} // namespace mediapipe
+5 -6
View File
@@ -27,14 +27,13 @@ ABSL_FLAG(
namespace mediapipe {
::mediapipe::StatusOr<std::string> PathToResourceAsFile(
const std::string& path) {
return ::mediapipe::file::JoinPath(FLAGS_resource_root_dir.CurrentValue(),
path);
mediapipe::StatusOr<std::string> PathToResourceAsFile(const std::string& path) {
return mediapipe::file::JoinPath(FLAGS_resource_root_dir.CurrentValue(),
path);
}
::mediapipe::Status GetResourceContents(const std::string& path,
std::string* output) {
mediapipe::Status GetResourceContents(const std::string& path,
std::string* output) {
return mediapipe::file::GetContents(path, output);
}
+3 -4
View File
@@ -39,13 +39,12 @@ namespace mediapipe {
// accepts file paths. Code that can access data as a stream or as a buffer
// should read from an asset directly on Android; an API for this will be
// provided later. TODO.
::mediapipe::StatusOr<std::string> PathToResourceAsFile(
const std::string& path);
mediapipe::StatusOr<std::string> PathToResourceAsFile(const std::string& path);
// Reads the entire contents of a resource. The search path is as in
// PathToResourceAsFile.
::mediapipe::Status GetResourceContents(const std::string& path,
std::string* output);
mediapipe::Status GetResourceContents(const std::string& path,
std::string* output);
} // namespace mediapipe
+6 -7
View File
@@ -24,14 +24,13 @@
namespace mediapipe {
namespace {
::mediapipe::StatusOr<std::string> PathToResourceAsFileInternal(
mediapipe::StatusOr<std::string> PathToResourceAsFileInternal(
const std::string& path) {
return Singleton<AssetManager>::get()->CachedFileFromAsset(path);
}
} // namespace
::mediapipe::StatusOr<std::string> PathToResourceAsFile(
const std::string& path) {
mediapipe::StatusOr<std::string> PathToResourceAsFile(const std::string& path) {
// Return full path.
if (absl::StartsWith(path, "/")) {
return path;
@@ -57,8 +56,8 @@ namespace {
}
}
::mediapipe::Status GetResourceContents(const std::string& path,
std::string* output) {
mediapipe::Status GetResourceContents(const std::string& path,
std::string* output) {
if (absl::StartsWith(path, "/")) {
return file::GetContents(path, output, file::Defaults());
}
@@ -66,12 +65,12 @@ namespace {
if (absl::StartsWith(path, "content://")) {
MP_RETURN_IF_ERROR(
Singleton<AssetManager>::get()->ReadContentUri(path, output));
return ::mediapipe::OkStatus();
return mediapipe::OkStatus();
}
RET_CHECK(Singleton<AssetManager>::get()->ReadFile(path, output))
<< "could not read asset: " << path;
return ::mediapipe::OkStatus();
return mediapipe::OkStatus();
}
} // namespace mediapipe
+5 -6
View File
@@ -24,7 +24,7 @@
namespace mediapipe {
namespace {
::mediapipe::StatusOr<std::string> PathToResourceAsFileInternal(
mediapipe::StatusOr<std::string> PathToResourceAsFileInternal(
const std::string& path) {
NSString* ns_path = [NSString stringWithUTF8String:path.c_str()];
Class mediapipeGraphClass = NSClassFromString(@"MPPGraph");
@@ -39,8 +39,7 @@ namespace {
}
} // namespace
::mediapipe::StatusOr<std::string> PathToResourceAsFile(
const std::string& path) {
mediapipe::StatusOr<std::string> PathToResourceAsFile(const std::string& path) {
// Return full path.
if (absl::StartsWith(path, "/")) {
return path;
@@ -66,15 +65,15 @@ namespace {
}
}
::mediapipe::Status GetResourceContents(const std::string& path,
std::string* output) {
mediapipe::Status GetResourceContents(const std::string& path,
std::string* output) {
ASSIGN_OR_RETURN(std::string full_path, PathToResourceAsFile(path));
std::ifstream input_file(full_path);
std::stringstream buffer;
buffer << input_file.rdbuf();
buffer.str().swap(*output);
return ::mediapipe::OkStatus();
return mediapipe::OkStatus();
}
} // namespace mediapipe
+5 -5
View File
@@ -168,14 +168,14 @@ Status TensorsToDetections(const ::tensorflow::Tensor& num_detections,
TensorToDetection(boxes_mat(i, 0), boxes_mat(i, 1), boxes_mat(i, 2),
boxes_mat(i, 3), score, class_id);
} else {
if (!::mediapipe::ContainsKey(label_map, class_id)) {
if (!mediapipe::ContainsKey(label_map, class_id)) {
return InvalidArgumentError(StrFormat(
"Input label_map does not contain entry for integer label: %d",
class_id));
}
detection = TensorToDetection(
boxes_mat(i, 0), boxes_mat(i, 1), boxes_mat(i, 2), boxes_mat(i, 3),
score, ::mediapipe::FindOrDie(label_map, class_id));
detection = TensorToDetection(boxes_mat(i, 0), boxes_mat(i, 1),
boxes_mat(i, 2), boxes_mat(i, 3), score,
mediapipe::FindOrDie(label_map, class_id));
}
// Adding keypoints
LocationData* location_data = detection.mutable_location_data();
@@ -201,7 +201,7 @@ Status TensorsToDetections(const ::tensorflow::Tensor& num_detections,
}
detections->emplace_back(detection);
}
return ::mediapipe::OkStatus();
return mediapipe::OkStatus();
}
} // namespace mediapipe
+2 -2
View File
@@ -28,7 +28,7 @@ Detection TensorToDetection(
const ::tensorflow::TTypes<const float>::Vec& box, float score,
const ::absl::variant<int, std::string>& class_label);
::mediapipe::Status TensorsToDetections(
mediapipe::Status TensorsToDetections(
const ::tensorflow::Tensor& num_detections,
const ::tensorflow::Tensor& boxes, const ::tensorflow::Tensor& scores,
const ::tensorflow::Tensor& classes,
@@ -36,7 +36,7 @@ Detection TensorToDetection(
std::vector<Detection>* detections);
// Use this version if keypoints or masks are available.
::mediapipe::Status TensorsToDetections(
mediapipe::Status TensorsToDetections(
const ::tensorflow::Tensor& num_detections,
const ::tensorflow::Tensor& boxes, const ::tensorflow::Tensor& scores,
const ::tensorflow::Tensor& classes, const ::tensorflow::Tensor& keypoints,
+14
View File
@@ -98,3 +98,17 @@ cc_library(
],
}) + ["@org_tensorflow//tensorflow/lite/core/api"],
)
cc_library(
name = "tflite_model_loader",
srcs = ["tflite_model_loader.cc"],
hdrs = ["tflite_model_loader.h"],
deps = [
"//mediapipe/framework:packet",
"//mediapipe/framework/port:ret_check",
"//mediapipe/framework/port:status",
"//mediapipe/framework/port:statusor",
"//mediapipe/util:resource_util",
"@org_tensorflow//tensorflow/lite:framework",
],
)
+33 -2
View File
@@ -41,6 +41,34 @@ namespace tflite {
namespace gpu {
namespace {
// TODO: Find a better place for these utility functions.
void UpdateShapes(const tflite::Interpreter& interpreter,
const std::vector<int>& indices,
std::vector<std::vector<int>>* shapes) {
shapes->resize(indices.size());
for (int i = 0; i < indices.size(); ++i) {
const TfLiteTensor* tensor = interpreter.tensor(indices[i]);
shapes->at(i).resize(tensor->dims->size);
for (int j = 0; j < tensor->dims->size; ++j) {
shapes->at(i)[j] = tensor->dims->data[j];
}
}
}
absl::Status InitializeShapes(const tflite::FlatBufferModel& flatbuffer,
const tflite::OpResolver& op_resolver,
std::vector<std::vector<int>>* input_shapes,
std::vector<std::vector<int>>* output_shapes) {
std::unique_ptr<tflite::Interpreter> interpreter;
tflite::InterpreterBuilder interpreter_builder(flatbuffer, op_resolver);
if (interpreter_builder(&interpreter) != kTfLiteOk || !interpreter) {
return absl::InternalError("Unable to prepare TfLite interpreter.");
}
UpdateShapes(*interpreter, interpreter->inputs(), input_shapes);
UpdateShapes(*interpreter, interpreter->outputs(), output_shapes);
return absl::OkStatus();
}
ObjectDef GetSSBOObjectDef(int channels) {
ObjectDef gpu_object_def;
gpu_object_def.data_type = DataType::FLOAT32;
@@ -77,12 +105,15 @@ mediapipe::Status TFLiteGPURunner::InitializeWithModel(
for (const auto& output : graph_gl_->outputs()) {
output_shapes_.push_back(output->tensor.shape);
}
MP_RETURN_IF_ERROR(InitializeShapes(flatbuffer, op_resolver,
&input_shape_from_model_,
&output_shape_from_model_));
return absl::OkStatus();
}
mediapipe::StatusOr<int64_t> TFLiteGPURunner::GetInputElements(int id) {
if (id >= input_shapes_.size()) {
return ::mediapipe::InternalError("Wrong input tensor id.");
return mediapipe::InternalError("Wrong input tensor id.");
} else {
return input_shapes_[id].DimensionsProduct();
}
@@ -90,7 +121,7 @@ mediapipe::StatusOr<int64_t> TFLiteGPURunner::GetInputElements(int id) {
mediapipe::StatusOr<int64_t> TFLiteGPURunner::GetOutputElements(int id) {
if (id >= output_shapes_.size()) {
return ::mediapipe::InternalError("Wrong output tensor id.");
return mediapipe::InternalError("Wrong output tensor id.");
} else {
return output_shapes_[id].DimensionsProduct();
}
+13
View File
@@ -75,6 +75,13 @@ class TFLiteGPURunner {
std::vector<BHWC> GetInputShapes() { return input_shapes_; }
std::vector<BHWC> GetOutputShapes() { return output_shapes_; }
std::vector<std::vector<int>> GetTFLiteInputShapes() {
return input_shape_from_model_;
}
std::vector<std::vector<int>> GetTFLiteOutputShapes() {
return output_shape_from_model_;
}
#ifdef __ANDROID__
void SetSerializedBinaryCache(std::vector<uint8_t>&& cache) {
serialized_binary_cache_ = std::move(cache);
@@ -110,6 +117,12 @@ class TFLiteGPURunner {
std::vector<BHWC> input_shapes_;
std::vector<BHWC> output_shapes_;
// Input/output shapes above belong to the internal graph representation. It
// is handy in certain situations to have the original tflite model's
// input/output shapes, which differ conceptually.
std::vector<std::vector<int>> input_shape_from_model_;
std::vector<std::vector<int>> output_shape_from_model_;
bool opencl_is_forced_ = false;
bool opengl_is_forced_ = false;
};
@@ -0,0 +1,34 @@
// 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 "mediapipe/util/tflite/tflite_model_loader.h"
#include "mediapipe/framework/port/ret_check.h"
#include "mediapipe/util/resource_util.h"
namespace mediapipe {
mediapipe::StatusOr<Packet> TfLiteModelLoader::LoadFromPath(
const std::string& path) {
std::string model_path = path;
ASSIGN_OR_RETURN(model_path, mediapipe::PathToResourceAsFile(model_path));
auto model = tflite::FlatBufferModel::BuildFromFile(model_path.c_str());
RET_CHECK(model) << "Failed to load model from path " << model_path;
return MakePacket<TfLiteModelPtr>(TfLiteModelPtr(
model.release(), [](tflite::FlatBufferModel* model) { delete model; }));
}
} // namespace mediapipe
@@ -0,0 +1,38 @@
// 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.
#ifndef MEDIAPIPE_UTIL_TFLITE_TFLITE_MODEL_LOADER_H_
#define MEDIAPIPE_UTIL_TFLITE_TFLITE_MODEL_LOADER_H_
#include "mediapipe/framework/packet.h"
#include "mediapipe/framework/port/status.h"
#include "mediapipe/framework/port/statusor.h"
#include "tensorflow/lite/model.h"
namespace mediapipe {
// Represents a TfLite model as a FlatBuffer.
using TfLiteModelPtr =
std::unique_ptr<tflite::FlatBufferModel,
std::function<void(tflite::FlatBufferModel*)>>;
class TfLiteModelLoader {
public:
// Returns a Packet containing a TfLiteModelPtr, pointing to a model loaded
// from the specified file path.
static mediapipe::StatusOr<Packet> LoadFromPath(const std::string& path);
};
} // namespace mediapipe
#endif // MEDIAPIPE_UTIL_TFLITE_TFLITE_MODEL_LOADER_H_
+1 -1
View File
@@ -308,7 +308,7 @@ class TimeSeriesCalculatorTest : public ::testing::Test {
AppendInputPacket(payload, Timestamp(timestamp), input_tag);
}
::mediapipe::Status RunGraph() { return runner_->Run(); }
mediapipe::Status RunGraph() { return runner_->Run(); }
bool HasInputHeader(const size_t input_index = 0) const {
return input(input_index)
+7 -7
View File
@@ -62,10 +62,10 @@ bool LogWarningIfTimestampIsInconsistent(const Timestamp& current_timestamp,
}
}
::mediapipe::Status IsTimeSeriesHeaderValid(const TimeSeriesHeader& header) {
mediapipe::Status IsTimeSeriesHeaderValid(const TimeSeriesHeader& header) {
if (header.has_sample_rate() && header.sample_rate() >= 0 &&
header.has_num_channels() && header.num_channels() >= 0) {
return ::mediapipe::OkStatus();
return mediapipe::OkStatus();
} else {
std::string error_message =
"TimeSeriesHeader is missing necessary fields: "
@@ -77,8 +77,8 @@ bool LogWarningIfTimestampIsInconsistent(const Timestamp& current_timestamp,
}
}
::mediapipe::Status FillTimeSeriesHeaderIfValid(const Packet& header_packet,
TimeSeriesHeader* header) {
mediapipe::Status FillTimeSeriesHeaderIfValid(const Packet& header_packet,
TimeSeriesHeader* header) {
CHECK(header);
if (header_packet.IsEmpty()) {
return tool::StatusFail("No header found.");
@@ -90,7 +90,7 @@ bool LogWarningIfTimestampIsInconsistent(const Timestamp& current_timestamp,
return IsTimeSeriesHeaderValid(*header);
}
::mediapipe::Status FillMultiStreamTimeSeriesHeaderIfValid(
mediapipe::Status FillMultiStreamTimeSeriesHeaderIfValid(
const Packet& header_packet, MultiStreamTimeSeriesHeader* header) {
CHECK(header);
if (header_packet.IsEmpty()) {
@@ -107,7 +107,7 @@ bool LogWarningIfTimestampIsInconsistent(const Timestamp& current_timestamp,
return IsTimeSeriesHeaderValid(header->time_series_header());
}
::mediapipe::Status IsMatrixShapeConsistentWithHeader(
mediapipe::Status IsMatrixShapeConsistentWithHeader(
const Matrix& matrix, const TimeSeriesHeader& header) {
if (header.has_num_samples() && matrix.cols() != header.num_samples()) {
return tool::StatusInvalid(absl::StrCat(
@@ -119,7 +119,7 @@ bool LogWarningIfTimestampIsInconsistent(const Timestamp& current_timestamp,
"Matrix size is inconsistent with header. Expected ",
header.num_channels(), " rows, but found ", matrix.rows()));
}
return ::mediapipe::OkStatus();
return mediapipe::OkStatus();
}
int64 SecondsToSamples(double time_in_seconds, double sample_rate) {
+10 -10
View File
@@ -45,25 +45,25 @@ bool LogWarningIfTimestampIsInconsistent(const Timestamp& current_timestamp,
// Returns mediapipe::status::OK if the header is valid. Otherwise, returns a
// Status object with an error message.
::mediapipe::Status IsTimeSeriesHeaderValid(const TimeSeriesHeader& header);
mediapipe::Status IsTimeSeriesHeaderValid(const TimeSeriesHeader& header);
// Fills header and returns mediapipe::status::OK if the header is non-empty and
// valid. Otherwise, returns a Status object with an error message.
::mediapipe::Status FillTimeSeriesHeaderIfValid(const Packet& header_packet,
TimeSeriesHeader* header);
mediapipe::Status FillTimeSeriesHeaderIfValid(const Packet& header_packet,
TimeSeriesHeader* header);
// Fills header and returns mediapipe::status::OK if the header contains a
// non-empty and valid TimeSeriesHeader. Otherwise, returns a Status object with
// an error message.
::mediapipe::Status FillMultiStreamTimeSeriesHeaderIfValid(
mediapipe::Status FillMultiStreamTimeSeriesHeaderIfValid(
const Packet& header_packet, MultiStreamTimeSeriesHeader* header);
// Returns::mediapipe::Status::OK iff options contains an extension of type
// Returnsmediapipe::Status::OK iff options contains an extension of type
// OptionsClass.
template <typename OptionsClass>
::mediapipe::Status HasOptionsExtension(const CalculatorOptions& options) {
mediapipe::Status HasOptionsExtension(const CalculatorOptions& options) {
if (options.HasExtension(OptionsClass::ext)) {
return ::mediapipe::OkStatus();
return mediapipe::OkStatus();
}
std::string error_message = "Options proto does not contain extension ";
absl::StrAppend(&error_message,
@@ -72,15 +72,15 @@ template <typename OptionsClass>
// Avoid lite proto APIs on mobile targets.
absl::StrAppend(&error_message, " : ", options.DebugString());
#endif
return ::mediapipe::InvalidArgumentError(error_message);
return mediapipe::InvalidArgumentError(error_message);
}
// Returns::mediapipe::Status::OK if the shape of 'matrix' is consistent
// Returnsmediapipe::Status::OK if the shape of 'matrix' is consistent
// with the num_samples and num_channels fields present in 'header'.
// The corresponding matrix dimensions of unset header fields are
// ignored, so e.g. an empty header (which is not valid according to
// FillTimeSeriesHeaderIfValid) is considered consistent with any matrix.
::mediapipe::Status IsMatrixShapeConsistentWithHeader(
mediapipe::Status IsMatrixShapeConsistentWithHeader(
const Matrix& matrix, const TimeSeriesHeader& header);
template <typename OptionsClass>
+1
View File
@@ -522,6 +522,7 @@ cc_library(
"//mediapipe/framework/port:opencv_imgproc",
"//mediapipe/framework/port:opencv_video",
"//mediapipe/framework/port:vector",
"@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:node_hash_set",
"@com_google_absl//absl/memory",
"@eigen_archive//:eigen",
@@ -26,6 +26,7 @@
#include <utility>
#include "Eigen/Core"
#include "absl/container/flat_hash_map.h"
#include "absl/container/node_hash_set.h"
#include "absl/memory/memory.h"
#include "mediapipe/framework/port/logging.h"
@@ -557,7 +558,7 @@ struct RegionFlowComputation::LongTrackData {
float motion_mag = 0; // Smoothed average motion. -1 for unknown.
};
std::unordered_map<int, TrackInfo> track_info;
absl::flat_hash_map<int, TrackInfo> track_info;
};
template <class T>
@@ -21,7 +21,7 @@
namespace {
using ::mediapipe::TrackedDetection;
using mediapipe::TrackedDetection;
// Checks if a point is out of view.
// x and y should both be in [0, 1] to be considered in view.