Scroll Top

Videoflow (default)

C++ (Windows, Linux, MacOS / CUDA and Metal accelerated) port of VideoFlow. This is a bidirectional optical flow model making better use of temporal cues.

Example Input & Outputs

Inputs Outputs
Input
Frame 0
Input
Frame 1
Input
Frame 2
Input
Optical Flow Frame 1 to 0
Input
Optical Flow Frame 1 to 2

Demo Code

 1#include "blace_ai.h"
 2#include "videoflow_v1_default_v1_ALL_export_version_v17.h"
 3#include <opencv2/opencv.hpp>
 4
 5int main() {
 6  ::blace::workload_management::BlaceWorld blace;
 7
 8  // register model at server
 9  blace::util::registerModel(videoflow_v1_default_v1_ALL_export_version_v17,
10                             blace::util::getPathToExe());
11
12  auto exe_path = blace::util::getPathToExe();
13  auto frame_0_op = CONSTRUCT_OP(blace::ops::FromImageFileOp(
14      (exe_path / "videoflow_frame_0.png").string()));
15  auto frame_1_op = CONSTRUCT_OP(blace::ops::FromImageFileOp(
16      (exe_path / "videoflow_frame_1.png").string()));
17  auto frame_2_op = CONSTRUCT_OP(blace::ops::FromImageFileOp(
18      (exe_path / "videoflow_frame_2.png").string()));
19
20  // interpolate to half size to save memory
21  frame_0_op = CONSTRUCT_OP(blace::ops::Interpolate2DOp(
22      frame_0_op, 540, 960, blace::ml_core::BILINEAR, false, true));
23  frame_1_op = CONSTRUCT_OP(blace::ops::Interpolate2DOp(
24      frame_1_op, 540, 960, blace::ml_core::BILINEAR, false, true));
25  frame_2_op = CONSTRUCT_OP(blace::ops::Interpolate2DOp(
26      frame_2_op, 540, 960, blace::ml_core::BILINEAR, false, true));
27
28  // construct model inference arguments
29  blace::ml_core::InferenceArgsCollection infer_args;
30  infer_args.inference_args.device = blace::util::get_accelerator().value();
31  infer_args.inference_args.use_half = false;
32
33  // construct inference operation. the model returns two values, flow 1->0 and
34  // flow 1->2
35  auto flow_1_to_0 = CONSTRUCT_OP(blace::ops::InferenceOp(
36      videoflow_v1_default_v1_ALL_export_version_v17_IDENT,
37      {frame_0_op, frame_1_op, frame_2_op}, infer_args, 0));
38  auto flow_1_to_2 = CONSTRUCT_OP(blace::ops::InferenceOp(
39      videoflow_v1_default_v1_ALL_export_version_v17_IDENT,
40      {frame_0_op, frame_1_op, frame_2_op}, infer_args, 1));
41
42  // normalize optical flow to zero-one range for plotting. The model returns
43  // relative offsets in -1 to 1 pixel space, so the raw values are to small to
44  // plot
45  flow_1_to_0 = CONSTRUCT_OP(blace::ops::NormalizeToZeroOneOP(flow_1_to_0));
46  flow_1_to_0 =
47      CONSTRUCT_OP(blace::ops::ToColorOp(flow_1_to_0, blace::ml_core::RGB));
48
49  flow_1_to_2 = CONSTRUCT_OP(blace::ops::NormalizeToZeroOneOP(flow_1_to_2));
50  flow_1_to_2 =
51      CONSTRUCT_OP(blace::ops::ToColorOp(flow_1_to_2, blace::ml_core::RGB));
52
53  // construct evaluator and evaluate to opencv mat
54  blace::computation_graph::GraphEvaluator evaluator_0(flow_1_to_0);
55  auto flow_1_to_0_cv = evaluator_0.evaluateToCVMat().value();
56  blace::computation_graph::GraphEvaluator evaluator_1(flow_1_to_2);
57  auto flow_1_to_2_cv = evaluator_1.evaluateToCVMat().value();
58
59  // multipy for for plotting
60  flow_1_to_0_cv *= 255;
61  flow_1_to_2_cv *= 255;
62
63  cv::imwrite((exe_path / "optical_flow_1_to_0.png").string(), flow_1_to_0_cv);
64  cv::imwrite((exe_path / "optical_flow_1_to_2.png").string(), flow_1_to_2_cv);
65
66  return 0;
67}

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