Image Style freeze fix

This commit is contained in:
mslight 2022-07-22 13:20:19 +04:00
parent 1fee386316
commit 4dc5793f44
4 changed files with 59 additions and 65 deletions

View File

@ -42,13 +42,6 @@ namespace mediapipe
constexpr char kMaskTag[] = "MASK";
constexpr char kImageFrameTag[] = "IMAGE";
enum
{
ATTRIB_VERTEX,
ATTRIB_TEXTURE_POSITION,
NUM_ATTRIBUTES
};
inline bool HasImageTag(mediapipe::CalculatorContext *cc) { return false; }
} // namespace
@ -252,8 +245,7 @@ namespace mediapipe
{
image_mat = absl::make_unique<cv::Mat>(
150, 150, CV_8UC4,
cv::Scalar(255, 255,
255));
cv::Scalar(cv::Scalar::all(255)));
*target_format = ImageFormat::SRGBA;
}
@ -272,8 +264,6 @@ namespace mediapipe
upper_lips_mask = cv::Mat::zeros(mat_image__.size(), CV_32F);
lower_lips_mask = cv::Mat::zeros(mat_image__.size(), CV_32F);
//__android_log_print(ANDROID_LOG_ERROR, "OVERSEAS", "%d ", mask_vec[1].size().height);
upper_lips_mask = mask_vec.find("UPPER_LIP")->second;
lower_lips_mask = mask_vec.find("LOWER_LIP")->second;

View File

@ -66,15 +66,6 @@ namespace mediapipe
static const std::vector<cv::Point> FACEMESH_FACE_OVAL{
{10, 338}, {338, 297}, {297, 332}, {332, 284}, {284, 251}, {251, 389}, {389, 356}, {356, 454}, {454, 323}, {323, 361}, {361, 288}, {288, 397}, {397, 365}, {365, 379}, {379, 378}, {378, 400}, {400, 377}, {377, 152}, {152, 148}, {148, 176}, {176, 149}, {149, 150}, {150, 136}, {136, 172}, {172, 58}, {58, 132}, {132, 93}, {93, 234}, {234, 127}, {127, 162}, {162, 21}, {21, 54}, {54, 103}, {103, 67}, {67, 109}, {109, 10}};
enum
{
ATTRIB_VERTEX,
ATTRIB_TEXTURE_POSITION,
NUM_ATTRIBUTES
};
// Round up n to next multiple of m.
size_t RoundUp(size_t n, size_t m) { return ((n + m - 1) / m) * m; } // NOLINT
inline bool HasImageTag(mediapipe::CalculatorContext *cc) { return false; }
bool NormalizedtoPixelCoordinates(double normalized_x, double normalized_y,
@ -150,8 +141,6 @@ namespace mediapipe
source1ch = source.reshape(1, 5);
target1ch = target.reshape(1, 5);
//std::cout << "R (numpy) = " << std::endl << cv::format(source, cv::Formatter::FMT_NUMPY) << std::endl << std::endl;
cv::SVD::compute(source1ch.t() * target1ch, w, u, vt);
rotation = (u * vt).t();
@ -252,6 +241,9 @@ namespace mediapipe
};
std::unique_ptr<cv::Mat> image_mat;
cv::Mat mat_image_;
int image_width_;
int image_height_;
};
REGISTER_CALCULATOR(FastUtilsCalculator);
@ -298,9 +290,6 @@ namespace mediapipe
{
image_frame_available_ = true;
}
else
{
}
// Set the output header based on the input header (if present).
const char *tag = kImageFrameTag;
@ -322,28 +311,25 @@ namespace mediapipe
{
return absl::OkStatus();
}
if (cc->Inputs().HasTag(kLandmarksTag) &&
cc->Inputs().Tag(kLandmarksTag).IsEmpty())
{
return absl::OkStatus();
}
if (cc->Inputs().HasTag(kNormLandmarksTag) &&
cc->Inputs().Tag(kNormLandmarksTag).IsEmpty())
{
return absl::OkStatus();
}
// Initialize render target, drawn with OpenCV.
ImageFormat::Format target_format;
std::vector<std::vector<cv::Point2f>> lms_out;
MP_RETURN_IF_ERROR(CreateRenderTargetCpu(cc, image_mat, &target_format));
mat_image_ = *image_mat.get();
image_width_ = image_mat->cols;
image_height_ = image_mat->rows;
if (cc->Inputs().HasTag(kNormLandmarksTag) &&
!cc->Inputs().Tag(kNormLandmarksTag).IsEmpty())
{
MP_RETURN_IF_ERROR(Call(cc, image_mat, target_format, lms_out));
cv::Mat source_lm = cv::Mat(lms_out[0]);
MP_RETURN_IF_ERROR(Align(image_mat, source_lm));
}
uchar *image_mat_ptr = image_mat->data;
MP_RETURN_IF_ERROR(RenderToCpu(cc, target_format, image_mat_ptr, image_mat));
@ -360,13 +346,10 @@ namespace mediapipe
CalculatorContext *cc, const ImageFormat::Format &target_format,
uchar *data_image, std::unique_ptr<cv::Mat> &image_mat)
{
cv::Mat mat_image_ = *image_mat.get();
auto output_frame = absl::make_unique<ImageFrame>(
target_format, mat_image_.cols, mat_image_.rows);
target_format, image_mat->cols, image_mat->rows);
output_frame->CopyPixelData(target_format, mat_image_.cols, mat_image_.rows, data_image,
output_frame->CopyPixelData(target_format, image_mat->cols, image_mat->rows, data_image,
ImageFrame::kDefaultAlignmentBoundary);
if (cc->Outputs().HasTag(kImageFrameTag))
@ -441,11 +424,6 @@ namespace mediapipe
ImageFormat::Format &target_format,
std::vector<std::vector<cv::Point2f>> &lms_out)
{
cv::Mat mat_image_ = *image_mat.get();
int image_width_ = image_mat->cols;
int image_height_ = image_mat->rows;
std::vector<cv::Point2f> kps, landmarks;
if (cc->Inputs().HasTag(kNormLandmarksTag))
@ -500,8 +478,6 @@ namespace mediapipe
cv::Mat target_lm, cv::Size size,
float extend, std::tuple<float, float, float, float> roi)
{
cv::Mat mat_image_ = *image_mat.get();
cv::Mat source, target;
source_lm.convertTo(source, CV_32F);
target_lm.convertTo(target, CV_32F);

View File

@ -13,7 +13,6 @@
// limitations under the License.
#include <vector>
#include <chrono>
#include <iostream>
#include "absl/strings/str_format.h"
@ -66,9 +65,6 @@ namespace
NUM_ATTRIBUTES
};
std::chrono::steady_clock::time_point begin;
std::chrono::steady_clock::time_point end;
// Commonly used to compute the number of blocks to launch in a kernel.
int NumGroups(const int size, const int group_size)
{ // NOLINT
@ -236,7 +232,7 @@ namespace mediapipe
{
cc->SetOffset(TimestampDiff(0));
bool use_gpu = false;
begin = std::chrono::steady_clock::now();
if (CanUseGpu())
{
#if !MEDIAPIPE_DISABLE_GPU
@ -327,10 +323,6 @@ namespace mediapipe
absl::Status TensorsToSegmentationCalculator::Close(CalculatorContext *cc)
{
end = std::chrono::steady_clock::now();
std::cout << "Time difference = " << std::chrono::duration_cast<std::chrono::microseconds>(end - begin).count() << "[µs]" << std::endl;
std::cout << "Time difference = " << std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin).count() << "[ns]" << std::endl;
#if !MEDIAPIPE_DISABLE_GPU
gpu_helper_.RunInGlContext([this]

View File

@ -0,0 +1,36 @@
# Image Style
This graph performs face aligning.
## Getting started
Clone branch.
1. Desktop-CPU
Build with:
```
bazel build -c opt --define MEDIAPIPE_DISABLE_GPU=1 mediapipe/examples/desktop/image_style:image_style_cpu
```
Run with (using your camera):
```
bazel-bin/mediapipe/examples/desktop/image_style/image_style_cpu --calculator_graph_config_file=mediapipe/graphs/image_style/image_style_cpu.pbtxt
```
Run with (using video):
```
bazel-bin/mediapipe/examples/desktop/image_style/image_style_cpu --calculator_graph_config_file=mediapipe/graphs/image_style/image_style_cpu.pbtxt
--input_video_path=/path/video.mp4
--output_video_path=/path/outvideo.mp4
```
2. Mobile (Android)
Build with:
```
bazel build -c opt --config=android_arm64 mediapipe/examples/android/src/java/com/google/mediapipe/apps/imagestylegpu:imagestylegpu
```
Install with:
```
adb install bazel-bin/mediapipe/examples/android/src/java/com/google/mediapipe/apps/imagestylegpu/imagestylegpu.apk
```