Scroll Top

Semantic Guided Human Matting (default)

C++ (Windows, Linux, MacOS / CUDA and Metal accelerated) port of https://github.com/cxgincsu/SemanticGuidedHumanMatting.

Example Input & Outputs

Inputs Outputs
Input
Image Input
Input
Matte

Demo Code

 1#include "blace_ai.h"
 2#include <opencv2/opencv.hpp>
 3
 4// include the models you want to use
 5#include "sghm_v7_default_v1_ALL_export_version_v17.h"
 6
 7int main() {
 8  ::blace::workload_management::BlaceWorld blace;
 9  // register model at server
10  blace::util::registerModel(sghm_v7_default_v1_ALL_export_version_v17,
11                             blace::util::getPathToExe());
12
13  // load image into op
14  auto exe_path = blace::util::getPathToExe();
15  std::filesystem::path photo_path = exe_path / "person.jpg";
16  auto world_tensor_orig =
17      CONSTRUCT_OP(blace::ops::FromImageFileOp(photo_path.string()));
18
19  // interpolate to size consumable by model
20  auto interpolated = CONSTRUCT_OP(blace::ops::Interpolate2DOp(
21      world_tensor_orig, 512, 896, blace::ml_core::BICUBIC, false, true));
22
23  // construct model inference arguments
24  blace::ml_core::InferenceArgsCollection infer_args;
25  infer_args.inference_args.device = blace::util::get_accelerator().value();
26
27  // construct inference operation
28  auto infer_op = CONSTRUCT_OP(
29      blace::ops::InferenceOp(sghm_v7_default_v1_ALL_export_version_v17_IDENT,
30                              {interpolated}, infer_args, 0));
31
32  // normalize matte to zero-one range
33  auto normalized_matte = CONSTRUCT_OP(
34      blace::ops::MapToRangeOp(infer_op, blace::ml_core::ZERO_TO_ONE));
35
36  // construct evaluator and evaluate to cv::Mat
37  blace::computation_graph::GraphEvaluator evaluator(normalized_matte);
38  auto cv_result = evaluator.evaluateToCVMat().value();
39
40  // multiply for plotting
41  cv_result *= 255.;
42
43  // save to disk and return
44  auto out_file = exe_path / "matte.png";
45  cv::imwrite(out_file.string(), cv_result);
46
47  return 0;
48}

Follow the 5 minute instructions to build and run the demo.

Tested on version v0.9.51 of blace.ai sdk. Might also work on newer or older releases (check if release notes of blace.ai state breaking changes).

Artifacts

Payload Demo Project Header

License