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 |
![]() |
![]() |
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_v25.h"
6
7using namespace blace;
8
9int main() {
10 ::workload_management::BlaceWorld blace;
11
12 // load image into op
13 auto exe_path = util::getPathToExe();
14 std::filesystem::path photo_path = exe_path / "person.jpg";
15 auto world_tensor_orig =
16 CONSTRUCT_OP(ops::FromImageFileOp(photo_path.string()));
17
18 // interpolate to size consumable by model
19 auto interpolated = CONSTRUCT_OP(ops::Interpolate2DOp(
20 world_tensor_orig, 512, 896, ml_core::BICUBIC, false, true));
21
22 // construct model inference arguments
23 ml_core::InferenceArgsCollection infer_args;
24 infer_args.inference_args.backends = {
25 ml_core::TORCHSCRIPT_CUDA_FP16, ml_core::TORCHSCRIPT_MPS_FP16,
26 ml_core::TORCHSCRIPT_CUDA_FP32, ml_core::TORCHSCRIPT_MPS_FP32,
27 ml_core::ONNX_DML_FP32, ml_core::TORCHSCRIPT_CPU_FP32};
28
29 // construct inference operation
30 auto infer_op = sghm_v7_default_v1_ALL_export_version_v25_run(
31 interpolated, 0, infer_args, util::getPathToExe().string());
32
33 // normalize matte to zero-one range
34 auto normalized_matte =
35 CONSTRUCT_OP(ops::MapToRangeOp(infer_op, ml_core::ZERO_TO_ONE));
36
37 // construct evaluator and evaluate to cv::Mat
38 computation_graph::GraphEvaluator evaluator(normalized_matte);
39 auto [return_code, cv_result] = evaluator.evaluateToCVMat();
40
41 // multiply for plotting
42 cv_result *= 255.;
43
44 // save to disk and return
45 auto out_file = exe_path / "matte.png";
46 cv::imwrite(out_file.string(), cv_result);
47
48 return 0;
49}
Tested on version v0.9.96 of blace.ai sdk. Might also work on newer or older releases (check if release notes of blace.ai state breaking changes).
Quickstart
- Download blace.ai SDK and unzip. In the bootstrap script
build_run_demos.ps1(Windows) orbuild_run_demos.sh(Linux/MacOS) set theBLACE_AI_CMAKE_DIRenvironment variable to thecmakefolder inside the unzipped SDK, e.g.export BLACE_AI_CMAKE_DIR="<unzip_folder>/package/cmake". - Download the model payload(s) (
.binfiles) from below and place in the same folder as the bootstrapper scripts. - Then run the bootstrap script with
powershell build_run_demo.ps1(Windows)
sh build_run_demo.sh(Linux and MacOS).
This will build and execute the demo.
Supported Backends
| Torchscript CPU | Torchscript CUDA FP16 * | Torchscript CUDA FP32 * | Torchscript MPS FP16 * | Torchscript MPS FP32 * | ONNX CPU FP32 | ONNX DirectML FP32 * |
|---|---|---|---|---|---|---|
| ✅ | ✅ | ✅ | ✅ | ✅ | ❌ | ❌ |
(*: Hardware Accelerated)
Artifacts
| Torchscript Payload | Demo Project | Header |



