SmartSpectra C++ SDK
Measure human vitals from video with SmartSpectra C++ SDK.
Loading...
Searching...
No Matches
frame_with_timestamp_recorder_impl.hpp
1
2// frame_with_timestamp_recorder_impl.hpp
3// Created by greg on 2/19/25.
4// Copyright (C) 2025 Presage Security, Inc.
5//
6// This program is free software; you can redistribute it and/or
7// modify it under the terms of the GNU Lesser General Public
8// License as published by the Free Software Foundation; either
9// version 3 of the License, or (at your option) any later version.
10//
11// This program is distributed in the hope that it will be useful,
12// but WITHOUT ANY WARRANTY; without even the implied warranty of
13// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14// Lesser General Public License for more details.
15//
16// You should have received a copy of the GNU Lesser General Public License
17// along with this program; if not, write to the Free Software Foundation,
18// Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20
21// === standard library includes (if any) ===
22// === third-party includes (if any) ===
23// === local includes (if any) ===
24#include "frame_with_timestamp_recorder.hpp"
25
26#pragma once
27namespace presage::smartspectra::grpc_bindings {
28
29
30template<container::settings::IntegrationMode TIntegrationMode>
31FrameWithTimestampRecorder<TIntegrationMode>::FrameWithTimestampRecorder(
32 std::shared_ptr<BackgroundContainer> container,
33 video_source::InputTransformMode input_transform_mode
34) :
35 container(std::move(container)),
36 input_transformer{input_transform_mode} {
37 StartRead(&this->frame_with_timestamp);
38};
39
40template<container::settings::IntegrationMode TIntegrationMode>
41void FrameWithTimestampRecorder<TIntegrationMode>::SwapContainer(std::shared_ptr<BackgroundContainer> new_container) {
42 this->container.reset();
43 this->container = std::move(new_container);
44}
45
46inline cv::Mat BuildOpenCVMatFromProtoBuf(const smartspectra::Mat& mat_protobuf) {
47 cv::Mat mat_cv(mat_protobuf.rows(), mat_protobuf.cols(), mat_protobuf.element_type());
48 size_t data_bytesize = mat_cv.rows * mat_cv.cols * mat_cv.elemSize();
49 auto data_protobuf = const_cast<char*>(mat_protobuf.data().data());
50 std::copy(reinterpret_cast<unsigned char*>(data_protobuf),
51 reinterpret_cast<unsigned char*>(data_protobuf + data_bytesize),
52 mat_cv.data);
53 return mat_cv;
54}
55
56template<container::settings::IntegrationMode TIntegrationMode>
57void FrameWithTimestampRecorder<TIntegrationMode>::OnReadDone(bool ok) {
58 if (ok) {
59 if (this->container == nullptr || !this->container->GraphIsRunning()) {
60 Finish(grpc::Status(grpc::StatusCode::FAILED_PRECONDITION,
61 "Physiology Preprocessing is not running, unable to add frame."));
62 } else {
63 cv::Mat frame = BuildOpenCVMatFromProtoBuf(frame_with_timestamp.frame());
64 frame = this->input_transformer.apply(frame);
65 rpc::StatusAdapter status_adapter = container
66 ->AddFrameWithTimestamp(frame, frame_with_timestamp.timestamp());
67 if (!status_adapter.ok()) {
68 Finish(status_adapter);
69 }
70 StartRead(&this->frame_with_timestamp);
71 }
72 } else {
73 Finish(grpc::Status(grpc::StatusCode::INTERNAL, "Unexpected failure reading frame with timestamp."));
74 }
75}
76
77} // namespace presage::smartspectra::grpc_bindings
InputTransformMode
Transformation applied to frames prior to processing.
Definition input_transform.hpp:19