2020-03-25 17:31:22 +00:00
|
|
|
/*
|
|
|
|
* Modern effects for a modern Streamer
|
|
|
|
* Copyright (C) 2017 Michael Fabian Dirks
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "filter-nv-face-tracking.hpp"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <filesystem>
|
|
|
|
#include <util/platform.h>
|
2021-04-25 22:37:15 +00:00
|
|
|
#include "nvidia/cuda/nvidia-cuda-context.hpp"
|
2020-03-25 17:31:22 +00:00
|
|
|
#include "obs/gs/gs-helper.hpp"
|
2020-04-10 13:40:12 +00:00
|
|
|
#include "obs/obs-tools.hpp"
|
2021-09-07 02:22:46 +00:00
|
|
|
#include "util/util-logging.hpp"
|
|
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
#define ST_PREFIX "<%s> "
|
|
|
|
#define D_LOG_ERROR(x, ...) P_LOG_ERROR(ST_PREFIX##x, __FUNCTION_SIG__, __VA_ARGS__)
|
|
|
|
#define D_LOG_WARNING(x, ...) P_LOG_WARN(ST_PREFIX##x, __FUNCTION_SIG__, __VA_ARGS__)
|
|
|
|
#define D_LOG_INFO(x, ...) P_LOG_INFO(ST_PREFIX##x, __FUNCTION_SIG__, __VA_ARGS__)
|
|
|
|
#define D_LOG_DEBUG(x, ...) P_LOG_DEBUG(ST_PREFIX##x, __FUNCTION_SIG__, __VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define ST_PREFIX "<filter::face_tracking> "
|
|
|
|
#define D_LOG_ERROR(...) P_LOG_ERROR(ST_PREFIX __VA_ARGS__)
|
|
|
|
#define D_LOG_WARNING(...) P_LOG_WARN(ST_PREFIX __VA_ARGS__)
|
|
|
|
#define D_LOG_INFO(...) P_LOG_INFO(ST_PREFIX __VA_ARGS__)
|
|
|
|
#define D_LOG_DEBUG(...) P_LOG_DEBUG(ST_PREFIX __VA_ARGS__)
|
|
|
|
#endif
|
2020-03-25 17:31:22 +00:00
|
|
|
|
2021-06-04 03:00:20 +00:00
|
|
|
#define ST_I18N "Filter.NVIDIA.FaceTracking"
|
|
|
|
#define ST_I18N_ROI ST_I18N ".ROI"
|
|
|
|
#define ST_I18N_ROI_ZOOM ST_I18N_ROI ".Zoom"
|
|
|
|
#define ST_I18N_ROI_OFFSET ST_I18N_ROI ".ROI.Offset"
|
|
|
|
#define ST_I18N_ROI_OFFSET_X ST_I18N_ROI_OFFSET ".X"
|
|
|
|
#define ST_I18N_ROI_OFFSET_Y ST_I18N_ROI_OFFSET ".Y"
|
|
|
|
#define ST_I18N_ROI_STABILITY ST_I18N_ROI ".Stability"
|
|
|
|
|
|
|
|
#define ST_KEY_ROI_ZOOM "ROI.Zoom"
|
|
|
|
#define ST_KEY_ROI_OFFSET_X "ROI.Offset.X"
|
|
|
|
#define ST_KEY_ROI_OFFSET_Y "ROI.Offset.Y"
|
|
|
|
#define ST_KEY_ROI_STABILITY "ROI.Stability"
|
2020-03-25 17:31:22 +00:00
|
|
|
|
2020-04-05 16:52:06 +00:00
|
|
|
using namespace streamfx::filter::nvidia;
|
|
|
|
|
2020-04-10 13:40:12 +00:00
|
|
|
void ar_feature_deleter(NvAR_FeatureHandle v)
|
2020-03-25 17:31:22 +00:00
|
|
|
{
|
2020-04-05 16:52:06 +00:00
|
|
|
face_tracking_factory::get()->get_ar()->destroy(v);
|
2020-03-25 17:31:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 16:52:06 +00:00
|
|
|
face_tracking_instance::face_tracking_instance(obs_data_t* settings, obs_source_t* self)
|
2020-04-10 13:40:12 +00:00
|
|
|
: obs::source_instance(settings, self),
|
|
|
|
|
|
|
|
_rt_is_fresh(false), _rt(),
|
|
|
|
|
2020-05-09 21:40:17 +00:00
|
|
|
_cfg_zoom(1.0), _cfg_offset({0., 0.}), _cfg_stability(1.0),
|
2020-04-10 13:40:12 +00:00
|
|
|
|
2020-05-09 21:40:17 +00:00
|
|
|
_geometry(), _filters(), _values(),
|
2020-04-10 13:40:12 +00:00
|
|
|
|
2021-06-16 10:15:13 +00:00
|
|
|
_cuda(::streamfx::nvidia::cuda::obs::get()), _cuda_stream(),
|
2020-04-10 13:40:12 +00:00
|
|
|
|
2020-05-09 21:40:17 +00:00
|
|
|
_ar_library(face_tracking_factory::get()->get_ar()), _ar_loaded(false), _ar_feature(), _ar_is_tracking(false),
|
|
|
|
_ar_bboxes_confidence(), _ar_bboxes_data(), _ar_bboxes(), _ar_texture(), _ar_texture_cuda_fresh(false),
|
|
|
|
_ar_texture_cuda(), _ar_texture_cuda_mem(), _ar_image(), _ar_image_bgr(), _ar_image_temp()
|
2020-03-25 17:31:22 +00:00
|
|
|
{
|
2020-04-25 23:04:04 +00:00
|
|
|
#ifdef ENABLE_PROFILING
|
2020-03-25 17:31:22 +00:00
|
|
|
// Profiling
|
2021-06-08 02:18:02 +00:00
|
|
|
_profile_capture = streamfx::util::profiler::create();
|
|
|
|
_profile_capture_realloc = streamfx::util::profiler::create();
|
|
|
|
_profile_capture_copy = streamfx::util::profiler::create();
|
|
|
|
_profile_ar_realloc = streamfx::util::profiler::create();
|
|
|
|
_profile_ar_copy = streamfx::util::profiler::create();
|
|
|
|
_profile_ar_transfer = streamfx::util::profiler::create();
|
|
|
|
_profile_ar_run = streamfx::util::profiler::create();
|
|
|
|
_profile_ar_calc = streamfx::util::profiler::create();
|
2020-03-25 17:31:22 +00:00
|
|
|
#endif
|
2020-04-10 13:40:12 +00:00
|
|
|
|
2020-05-09 21:40:17 +00:00
|
|
|
{ // Create render target, vertex buffer, and CUDA stream.
|
2021-06-16 10:15:13 +00:00
|
|
|
auto gctx = streamfx::obs::gs::context{};
|
|
|
|
_rt = std::make_shared<streamfx::obs::gs::rendertarget>(GS_RGBA_UNORM, GS_ZS_NONE);
|
|
|
|
_geometry = std::make_shared<streamfx::obs::gs::vertex_buffer>(uint32_t(4), uint8_t(1));
|
|
|
|
auto cctx = _cuda->get_context()->enter();
|
|
|
|
_cuda_stream =
|
|
|
|
std::make_shared<::streamfx::nvidia::cuda::stream>(::streamfx::nvidia::cuda::stream_flags::NON_BLOCKING, 0);
|
2020-05-09 21:40:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
{ // Asynchronously load Face Tracking.
|
|
|
|
async_initialize();
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // Set up initial tracking data.
|
|
|
|
_values.center[0] = _values.center[1] = .5;
|
|
|
|
_values.size[0] = _values.size[1] = 1.;
|
|
|
|
refresh_region_of_interest();
|
|
|
|
}
|
2020-03-25 17:31:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 16:52:06 +00:00
|
|
|
face_tracking_instance::~face_tracking_instance()
|
2020-03-25 17:31:22 +00:00
|
|
|
{
|
2020-05-09 21:40:17 +00:00
|
|
|
// Kill pending tasks.
|
|
|
|
streamfx::threadpool()->pop(_async_initialize);
|
|
|
|
streamfx::threadpool()->pop(_async_track);
|
|
|
|
|
|
|
|
_ar_loaded.store(false);
|
|
|
|
std::unique_lock<std::mutex> alk{_ar_lock};
|
2020-04-10 13:40:12 +00:00
|
|
|
_ar_library->image_dealloc(&_ar_image_temp);
|
|
|
|
_ar_library->image_dealloc(&_ar_image_bgr);
|
2020-03-25 17:31:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 16:52:06 +00:00
|
|
|
void face_tracking_instance::async_initialize(std::shared_ptr<void> ptr)
|
2020-03-25 17:31:22 +00:00
|
|
|
{
|
2020-04-10 13:40:12 +00:00
|
|
|
struct async_data {
|
|
|
|
std::shared_ptr<obs_weak_source_t> source;
|
|
|
|
std::string models_path;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!ptr) {
|
|
|
|
// Spawn the work for the threadpool.
|
|
|
|
std::shared_ptr<async_data> data = std::make_shared<async_data>();
|
|
|
|
data->source =
|
|
|
|
std::shared_ptr<obs_weak_source_t>(obs_source_get_weak_source(_self), obs::obs_weak_source_deleter);
|
|
|
|
|
2020-05-09 21:40:17 +00:00
|
|
|
{
|
|
|
|
std::filesystem::path models_path = _ar_library->get_ar_sdk_path();
|
|
|
|
models_path = models_path.append("models");
|
|
|
|
models_path = std::filesystem::absolute(models_path);
|
|
|
|
models_path.concat("\\");
|
|
|
|
data->models_path = models_path.string();
|
|
|
|
}
|
2020-04-10 13:40:12 +00:00
|
|
|
|
2020-05-09 21:40:17 +00:00
|
|
|
_async_initialize = streamfx::threadpool()->push(
|
|
|
|
std::bind(&face_tracking_instance::async_initialize, this, std::placeholders::_1), data);
|
2020-04-10 13:40:12 +00:00
|
|
|
} else {
|
|
|
|
std::shared_ptr<async_data> data = std::static_pointer_cast<async_data>(ptr);
|
|
|
|
|
|
|
|
// Try and acquire a strong source reference.
|
2020-05-09 21:40:17 +00:00
|
|
|
std::shared_ptr<obs_source_t> remote_work =
|
2020-04-10 13:40:12 +00:00
|
|
|
std::shared_ptr<obs_source_t>(obs_weak_source_get_source(data->source.get()), obs::obs_source_deleter);
|
2020-05-09 21:40:17 +00:00
|
|
|
if (!remote_work) { // If that failed, the source we are working for was deleted - abort now.
|
2020-04-10 13:40:12 +00:00
|
|
|
return;
|
|
|
|
}
|
2020-03-25 17:31:22 +00:00
|
|
|
|
2020-04-10 13:40:12 +00:00
|
|
|
// Update the current CUDA context for working.
|
2021-06-08 02:38:24 +00:00
|
|
|
streamfx::obs::gs::context gctx;
|
|
|
|
auto cctx = _cuda->get_context()->enter();
|
2020-03-25 17:31:22 +00:00
|
|
|
|
2020-04-10 13:40:12 +00:00
|
|
|
// Create Face Detection feature.
|
|
|
|
{
|
|
|
|
NvAR_FeatureHandle fd_inst;
|
|
|
|
if (NvCV_Status res = _ar_library->create(NvAR_Feature_FaceDetection, &fd_inst); res != NVCV_SUCCESS) {
|
|
|
|
throw std::runtime_error("Failed to create Face Detection feature.");
|
|
|
|
}
|
|
|
|
_ar_feature = std::shared_ptr<nvAR_Feature>{fd_inst, ar_feature_deleter};
|
|
|
|
}
|
2020-03-25 17:31:22 +00:00
|
|
|
|
2020-04-10 13:40:12 +00:00
|
|
|
// Set the correct CUDA stream for processing.
|
|
|
|
if (NvCV_Status res = _ar_library->set_cuda_stream(_ar_feature.get(), NvAR_Parameter_Config(CUDAStream),
|
|
|
|
reinterpret_cast<CUstream>(_cuda_stream->get()));
|
|
|
|
res != NVCV_SUCCESS) {
|
|
|
|
throw std::runtime_error("Failed to set CUDA stream.");
|
|
|
|
}
|
2020-03-25 17:31:22 +00:00
|
|
|
|
2020-04-10 13:40:12 +00:00
|
|
|
// Set the correct models path.
|
|
|
|
if (NvCV_Status res =
|
|
|
|
_ar_library->set_string(_ar_feature.get(), NvAR_Parameter_Config(ModelDir), data->models_path.c_str());
|
|
|
|
res != NVCV_SUCCESS) {
|
|
|
|
throw std::runtime_error("Unable to set model path.");
|
|
|
|
}
|
2020-03-25 17:31:22 +00:00
|
|
|
|
2020-04-10 13:40:12 +00:00
|
|
|
// Finally enable Temporal tracking if possible.
|
|
|
|
if (NvCV_Status res = _ar_library->set_uint32(_ar_feature.get(), NvAR_Parameter_Config(Temporal), 1);
|
|
|
|
res != NVCV_SUCCESS) {
|
2020-07-25 14:51:22 +00:00
|
|
|
DLOG_WARNING("<%s> Unable to enable Temporal tracking mode.", obs_source_get_name(remote_work.get()));
|
2020-04-10 13:40:12 +00:00
|
|
|
}
|
2020-03-25 17:31:22 +00:00
|
|
|
|
2020-04-10 13:40:12 +00:00
|
|
|
// Create Bounding Boxes Data
|
|
|
|
_ar_bboxes_data.assign(1, {0., 0., 0., 0.});
|
|
|
|
_ar_bboxes.boxes = _ar_bboxes_data.data();
|
2020-08-10 01:29:05 +00:00
|
|
|
_ar_bboxes.max_boxes = std::clamp<uint8_t>(static_cast<uint8_t>(_ar_bboxes_data.size()), 0, 255);
|
2020-04-10 13:40:12 +00:00
|
|
|
_ar_bboxes.num_boxes = 0;
|
|
|
|
_ar_bboxes_confidence.resize(_ar_bboxes_data.size());
|
|
|
|
if (NvCV_Status res = _ar_library->set_object(_ar_feature.get(), NvAR_Parameter_Output(BoundingBoxes),
|
|
|
|
&_ar_bboxes, sizeof(NvAR_BBoxes));
|
|
|
|
res != NVCV_SUCCESS) {
|
|
|
|
throw std::runtime_error("Failed to set BoundingBoxes for Face Tracking feature.");
|
|
|
|
}
|
|
|
|
if (NvCV_Status res = _ar_library->set_float32_array(
|
|
|
|
_ar_feature.get(), NvAR_Parameter_Output(BoundingBoxesConfidence), _ar_bboxes_confidence.data(),
|
|
|
|
static_cast<int>(_ar_bboxes_confidence.size()));
|
|
|
|
res != NVCV_SUCCESS) {
|
|
|
|
throw std::runtime_error("Failed to set BoundingBoxesConfidence for Face Tracking feature.");
|
|
|
|
}
|
2020-03-25 17:31:22 +00:00
|
|
|
|
2020-04-10 13:40:12 +00:00
|
|
|
// And finally, load the feature (takes long).
|
|
|
|
if (NvCV_Status res = _ar_library->load(_ar_feature.get()); res != NVCV_SUCCESS) {
|
2020-07-25 14:51:22 +00:00
|
|
|
DLOG_ERROR("<%s> Failed to load Face Tracking feature.", obs_source_get_name(_self));
|
2020-04-10 13:40:12 +00:00
|
|
|
_ar_loaded = false;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
_ar_loaded = true;
|
|
|
|
}
|
|
|
|
|
2020-05-09 21:40:17 +00:00
|
|
|
_async_initialize.reset();
|
|
|
|
}
|
2020-03-25 17:31:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 16:52:06 +00:00
|
|
|
void face_tracking_instance::async_track(std::shared_ptr<void> ptr)
|
2020-03-25 17:31:22 +00:00
|
|
|
{
|
2020-04-10 13:40:12 +00:00
|
|
|
struct async_data {
|
|
|
|
std::shared_ptr<obs_weak_source_t> source;
|
|
|
|
};
|
|
|
|
|
2020-05-09 21:40:17 +00:00
|
|
|
if (!_ar_loaded)
|
|
|
|
return;
|
|
|
|
|
2020-04-10 13:40:12 +00:00
|
|
|
if (!ptr) {
|
2020-05-09 21:40:17 +00:00
|
|
|
// Check if we can track.
|
|
|
|
if (_ar_is_tracking)
|
|
|
|
return; // Can't track a new frame right now.
|
|
|
|
|
|
|
|
#ifdef ENABLE_PROFILING
|
2021-06-08 02:38:24 +00:00
|
|
|
streamfx::obs::gs::debug_marker gdm{streamfx::obs::gs::debug_color_convert, "Start Asynchronous Tracking"};
|
2020-05-09 21:40:17 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
// Don't push additional tracking frames while processing one.
|
|
|
|
_ar_is_tracking = true;
|
|
|
|
|
2020-04-10 13:40:12 +00:00
|
|
|
// Spawn the work for the threadpool.
|
|
|
|
std::shared_ptr<async_data> data = std::make_shared<async_data>();
|
|
|
|
data->source =
|
|
|
|
std::shared_ptr<obs_weak_source_t>(obs_source_get_weak_source(_self), obs::obs_weak_source_deleter);
|
|
|
|
|
|
|
|
// Check if things exist as planned.
|
|
|
|
if (!_ar_texture || (_ar_texture->get_width() != _size.first) || (_ar_texture->get_height() != _size.second)) {
|
2020-04-25 23:04:04 +00:00
|
|
|
#ifdef ENABLE_PROFILING
|
2021-06-08 02:38:24 +00:00
|
|
|
auto prof = _profile_capture_realloc->track();
|
|
|
|
streamfx::obs::gs::debug_marker marker{streamfx::obs::gs::debug_color_allocate, "Reallocate GPU Buffer"};
|
2020-04-25 23:04:04 +00:00
|
|
|
#endif
|
2021-06-08 02:38:24 +00:00
|
|
|
_ar_texture =
|
|
|
|
std::make_shared<streamfx::obs::gs::texture>(_size.first, _size.second, GS_RGBA_UNORM, uint32_t(1),
|
|
|
|
nullptr, streamfx::obs::gs::texture::flags::None);
|
2020-04-10 13:40:12 +00:00
|
|
|
_ar_texture_cuda_fresh = false;
|
2020-03-25 17:31:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 13:40:12 +00:00
|
|
|
{ // Copy texture
|
2020-04-25 23:04:04 +00:00
|
|
|
#ifdef ENABLE_PROFILING
|
2021-06-08 02:38:24 +00:00
|
|
|
auto prof = _profile_capture_copy->track();
|
|
|
|
streamfx::obs::gs::debug_marker marker{streamfx::obs::gs::debug_color_copy, "Copy Capture",
|
|
|
|
obs_source_get_name(_self)};
|
2020-04-25 23:04:04 +00:00
|
|
|
#endif
|
2020-04-10 13:40:12 +00:00
|
|
|
gs_copy_texture(_ar_texture->get_object(), _rt->get_texture()->get_object());
|
|
|
|
}
|
2020-03-25 17:31:22 +00:00
|
|
|
|
2020-04-10 13:40:12 +00:00
|
|
|
// Push work
|
2020-05-09 21:40:17 +00:00
|
|
|
_async_track = streamfx::threadpool()->push(
|
|
|
|
std::bind(&face_tracking_instance::async_track, this, std::placeholders::_1), data);
|
2020-04-10 13:40:12 +00:00
|
|
|
} else {
|
2020-05-09 21:40:17 +00:00
|
|
|
// Prevent conflicts.
|
|
|
|
std::unique_lock<std::mutex> alk{_ar_lock};
|
|
|
|
if (!_ar_loaded)
|
|
|
|
return;
|
2020-04-10 13:40:12 +00:00
|
|
|
|
|
|
|
// Try and acquire a strong source reference.
|
2020-05-09 21:40:17 +00:00
|
|
|
std::shared_ptr<async_data> data = std::static_pointer_cast<async_data>(ptr);
|
|
|
|
std::shared_ptr<obs_source_t> remote_work =
|
2020-04-10 13:40:12 +00:00
|
|
|
std::shared_ptr<obs_source_t>(obs_weak_source_get_source(data->source.get()), obs::obs_source_deleter);
|
2020-05-09 21:40:17 +00:00
|
|
|
if (!remote_work) { // If that failed, the source we are working for was deleted - abort now.
|
2020-04-10 13:40:12 +00:00
|
|
|
return;
|
2020-03-25 17:31:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-25 23:04:04 +00:00
|
|
|
// Acquire GS context.
|
2021-06-08 02:38:24 +00:00
|
|
|
streamfx::obs::gs::context gctx{};
|
2020-04-25 23:04:04 +00:00
|
|
|
|
2020-04-10 13:40:12 +00:00
|
|
|
// Update the current CUDA context for working.
|
2021-04-25 23:21:36 +00:00
|
|
|
auto cctx = _cuda->get_context()->enter();
|
2020-03-25 17:31:22 +00:00
|
|
|
|
2020-04-10 13:40:12 +00:00
|
|
|
// Refresh any now broken buffers.
|
|
|
|
if (!_ar_texture_cuda_fresh) {
|
2020-04-25 23:04:04 +00:00
|
|
|
#ifdef ENABLE_PROFILING
|
2021-06-08 02:38:24 +00:00
|
|
|
auto prof = _profile_ar_realloc->track();
|
|
|
|
streamfx::obs::gs::debug_marker marker{streamfx::obs::gs::debug_color_allocate,
|
|
|
|
"%s: Reallocate CUDA Buffers", obs_source_get_name(_self)};
|
2020-04-25 23:04:04 +00:00
|
|
|
#endif
|
2020-04-10 13:40:12 +00:00
|
|
|
// Assign new texture and allocate new memory.
|
2021-06-16 10:15:13 +00:00
|
|
|
std::size_t pitch = _ar_texture->get_width() * 4ul;
|
|
|
|
_ar_texture_cuda = std::make_shared<::streamfx::nvidia::cuda::gstexture>(_ar_texture);
|
|
|
|
_ar_texture_cuda_mem =
|
|
|
|
std::make_shared<::streamfx::nvidia::cuda::memory>(pitch * _ar_texture->get_height());
|
2021-04-27 01:14:13 +00:00
|
|
|
if (auto res = _ar_library->image_init(&_ar_image, static_cast<unsigned int>(_ar_texture->get_width()),
|
|
|
|
static_cast<unsigned int>(_ar_texture->get_height()),
|
|
|
|
static_cast<int>(pitch),
|
|
|
|
reinterpret_cast<void*>(_ar_texture_cuda_mem->get()), NVCV_RGBA,
|
|
|
|
NVCV_U8, NVCV_INTERLEAVED, NVCV_CUDA);
|
|
|
|
res != NVCV_SUCCESS) {
|
|
|
|
DLOG_ERROR("<%s> Failed to allocate image for tracking.", obs_source_get_name(_self));
|
|
|
|
return;
|
|
|
|
}
|
2020-04-10 13:40:12 +00:00
|
|
|
|
|
|
|
// Reallocate transposed buffer.
|
|
|
|
_ar_library->image_dealloc(&_ar_image_temp);
|
2020-05-09 21:40:17 +00:00
|
|
|
_ar_library->image_dealloc(&_ar_image_bgr);
|
2021-04-27 01:14:13 +00:00
|
|
|
if (auto res = _ar_library->image_alloc(&_ar_image_bgr, _ar_image.width, _ar_image.height, NVCV_BGR,
|
|
|
|
NVCV_U8, NVCV_INTERLEAVED, NVCV_CUDA, 0);
|
|
|
|
res != NVCV_SUCCESS) {
|
|
|
|
DLOG_ERROR("<%s> Failed to allocate image for color conversion.", obs_source_get_name(_self));
|
|
|
|
return;
|
|
|
|
}
|
2020-05-09 21:40:17 +00:00
|
|
|
|
|
|
|
// Synchronize Streams.
|
2021-04-25 23:21:36 +00:00
|
|
|
_cuda_stream->synchronize();
|
2020-04-10 13:40:12 +00:00
|
|
|
|
|
|
|
// Finally set the input object.
|
|
|
|
if (NvCV_Status res = _ar_library->set_object(_ar_feature.get(), NvAR_Parameter_Input(Image),
|
|
|
|
&_ar_image_bgr, sizeof(NvCVImage));
|
|
|
|
res != NVCV_SUCCESS) {
|
2020-07-25 14:51:22 +00:00
|
|
|
DLOG_ERROR("<%s> Failed to update input image for tracking.", obs_source_get_name(_self));
|
2020-04-10 13:40:12 +00:00
|
|
|
return;
|
2020-03-25 17:31:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-10 13:40:12 +00:00
|
|
|
// And mark the new texture as fresh.
|
|
|
|
_ar_texture_cuda_fresh = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
{ // Copy from CUDA array to CUDA device memory.
|
2020-04-25 23:04:04 +00:00
|
|
|
#ifdef ENABLE_PROFILING
|
2020-04-10 13:40:12 +00:00
|
|
|
auto prof = _profile_ar_copy->track();
|
2020-03-25 17:31:22 +00:00
|
|
|
#endif
|
2021-06-16 10:15:13 +00:00
|
|
|
::streamfx::nvidia::cuda::memcpy2d_v2_t mc;
|
2020-04-10 13:40:12 +00:00
|
|
|
mc.src_x_in_bytes = 0;
|
|
|
|
mc.src_y = 0;
|
2021-06-16 10:15:13 +00:00
|
|
|
mc.src_memory_type = ::streamfx::nvidia::cuda::memory_type::ARRAY;
|
2020-04-10 13:40:12 +00:00
|
|
|
mc.src_host = nullptr;
|
|
|
|
mc.src_device = 0;
|
|
|
|
mc.src_array = _ar_texture_cuda->map(_cuda_stream);
|
|
|
|
mc.src_pitch = static_cast<size_t>(_ar_image.pitch);
|
|
|
|
mc.dst_x_in_bytes = 0;
|
|
|
|
mc.dst_y = 0;
|
2021-06-16 10:15:13 +00:00
|
|
|
mc.dst_memory_type = ::streamfx::nvidia::cuda::memory_type::DEVICE;
|
2020-04-10 13:40:12 +00:00
|
|
|
mc.dst_host = 0;
|
2021-06-16 10:15:13 +00:00
|
|
|
mc.dst_device = reinterpret_cast<::streamfx::nvidia::cuda::device_ptr_t>(_ar_image.pixels);
|
2020-04-10 13:40:12 +00:00
|
|
|
mc.dst_array = 0;
|
|
|
|
mc.dst_pitch = static_cast<size_t>(_ar_image.pitch);
|
|
|
|
mc.width_in_bytes = static_cast<size_t>(_ar_image.pitch);
|
|
|
|
mc.height = _ar_image.height;
|
|
|
|
|
2021-06-16 10:15:13 +00:00
|
|
|
if (::streamfx::nvidia::cuda::result res = _cuda->get_cuda()->cuMemcpy2DAsync(&mc, _cuda_stream->get());
|
|
|
|
res != ::streamfx::nvidia::cuda::result::SUCCESS) {
|
2020-07-25 14:51:22 +00:00
|
|
|
DLOG_ERROR("<%s> Failed to prepare buffers for tracking.", obs_source_get_name(_self));
|
2020-04-10 13:40:12 +00:00
|
|
|
return;
|
2020-03-25 17:31:22 +00:00
|
|
|
}
|
2020-04-10 13:40:12 +00:00
|
|
|
}
|
2020-03-25 17:31:22 +00:00
|
|
|
|
2020-04-10 13:40:12 +00:00
|
|
|
{ // Convert from RGBA 32-bit to BGR 24-bit.
|
2020-04-25 23:04:04 +00:00
|
|
|
#ifdef ENABLE_PROFILING
|
2020-04-10 13:40:12 +00:00
|
|
|
auto prof = _profile_ar_transfer->track();
|
2020-03-25 17:31:22 +00:00
|
|
|
#endif
|
2020-04-10 13:40:12 +00:00
|
|
|
if (NvCV_Status res =
|
|
|
|
_ar_library->image_transfer(&_ar_image, &_ar_image_bgr, 1.0,
|
|
|
|
reinterpret_cast<CUstream_st*>(_cuda_stream->get()), &_ar_image_temp);
|
|
|
|
res != NVCV_SUCCESS) {
|
2020-07-25 14:51:22 +00:00
|
|
|
DLOG_ERROR("<%s> Failed to convert from RGBX 32-bit to BGR 24-bit.", obs_source_get_name(_self));
|
2020-04-10 13:40:12 +00:00
|
|
|
return;
|
2020-03-25 17:31:22 +00:00
|
|
|
}
|
2020-05-09 21:40:17 +00:00
|
|
|
|
|
|
|
// Synchronize Streams.
|
2021-04-25 23:21:36 +00:00
|
|
|
_cuda_stream->synchronize();
|
|
|
|
_cuda->get_context()->synchronize();
|
2020-04-10 13:40:12 +00:00
|
|
|
}
|
2020-03-25 17:31:22 +00:00
|
|
|
|
2020-04-10 13:40:12 +00:00
|
|
|
{ // Track any faces.
|
2020-04-25 23:04:04 +00:00
|
|
|
#ifdef ENABLE_PROFILING
|
2020-04-10 13:40:12 +00:00
|
|
|
auto prof = _profile_ar_run->track();
|
2020-03-25 17:31:22 +00:00
|
|
|
#endif
|
2020-04-10 13:40:12 +00:00
|
|
|
if (NvCV_Status res = _ar_library->run(_ar_feature.get()); res != NVCV_SUCCESS) {
|
2020-07-25 14:51:22 +00:00
|
|
|
DLOG_ERROR("<%s> Failed to run tracking.", obs_source_get_name(_self));
|
2020-04-10 13:40:12 +00:00
|
|
|
return;
|
2020-03-25 17:31:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-09 21:40:17 +00:00
|
|
|
// Are we tracking anything, and confident enough in the tracking?
|
|
|
|
if ((_ar_bboxes.num_boxes == 0) || (_ar_bboxes_confidence.at(0) < 0.3333)) {
|
|
|
|
// If not, just return to full frame.
|
|
|
|
std::unique_lock<std::mutex> tlk{_values.lock};
|
|
|
|
_values.center[0] = .5;
|
|
|
|
_values.center[1] = .5;
|
|
|
|
_values.size[0] = 1.;
|
|
|
|
_values.size[1] = 1.;
|
|
|
|
_values.velocity[0] = 0;
|
|
|
|
_values.velocity[1] = 0;
|
2020-04-10 13:40:12 +00:00
|
|
|
} else {
|
2020-05-09 21:40:17 +00:00
|
|
|
// If yes, begin tracking.
|
2020-04-25 23:04:04 +00:00
|
|
|
#ifdef ENABLE_PROFILING
|
2020-04-10 13:40:12 +00:00
|
|
|
auto prof = _profile_ar_calc->track();
|
|
|
|
#endif
|
2020-04-25 23:04:04 +00:00
|
|
|
|
2020-05-09 21:40:17 +00:00
|
|
|
double_t sx = static_cast<double_t>(_ar_image_bgr.width);
|
|
|
|
double_t sy = static_cast<double_t>(_ar_image_bgr.height);
|
|
|
|
double_t aspect = double_t(sx) / double_t(sy);
|
|
|
|
double_t fps = 0.;
|
|
|
|
|
|
|
|
{
|
|
|
|
obs_video_info ovi;
|
|
|
|
obs_get_video_info(&ovi);
|
|
|
|
fps = static_cast<double_t>(ovi.fps_num) / static_cast<double_t>(ovi.fps_den);
|
|
|
|
}
|
2020-03-25 17:31:22 +00:00
|
|
|
|
|
|
|
// Store values and center.
|
2020-05-09 21:40:17 +00:00
|
|
|
double_t bsx = _ar_bboxes.boxes[0].width;
|
|
|
|
double_t bsy = _ar_bboxes.boxes[0].height;
|
|
|
|
double_t bcx = _ar_bboxes.boxes[0].x + bsx / 2.0;
|
|
|
|
double_t bcy = _ar_bboxes.boxes[0].y + bsy / 2.0;
|
2020-03-25 17:31:22 +00:00
|
|
|
|
|
|
|
// Zoom, Aspect Ratio, Offset
|
2021-06-08 02:18:02 +00:00
|
|
|
bsy = streamfx::util::math::lerp<double_t>(sy, bsy, _cfg_zoom);
|
2020-05-09 21:40:17 +00:00
|
|
|
bsy = std::clamp(bsy, 10 * aspect, static_cast<double_t>(_size.second));
|
|
|
|
bsx = bsy * aspect;
|
|
|
|
bcx += _ar_bboxes.boxes[0].width * _cfg_offset.first;
|
|
|
|
bcy += _ar_bboxes.boxes[0].height * _cfg_offset.second;
|
2020-03-25 17:31:22 +00:00
|
|
|
|
|
|
|
// Fit back into the frame
|
|
|
|
// - Above code guarantees that height is never bigger than the height of the frame.
|
|
|
|
// - Which also guarantees that width is never bigger than the width of the frame.
|
|
|
|
// Only cx and cy need to be adjusted now to always be in the frame.
|
2020-05-09 21:40:17 +00:00
|
|
|
bcx = std::clamp(bcx, (bsx / 2.), sx - (bsx / 2.));
|
|
|
|
bcy = std::clamp(bcy, (bsy / 2.), sy - (bsy / 2.));
|
|
|
|
|
|
|
|
{ // Update target values.
|
|
|
|
std::unique_lock<std::mutex> tlk{_values.lock};
|
|
|
|
_values.velocity[0] = -_values.center[0];
|
|
|
|
_values.velocity[1] = -_values.center[1];
|
|
|
|
_values.center[0] = bcx / sx;
|
|
|
|
_values.center[1] = bcy / sy;
|
|
|
|
_values.velocity[0] += _values.center[0];
|
|
|
|
_values.velocity[1] += _values.center[1];
|
|
|
|
_values.velocity[0] *= fps;
|
|
|
|
_values.velocity[1] *= fps;
|
|
|
|
_values.size[0] = bsx / sx;
|
|
|
|
_values.size[1] = bsy / sy;
|
2020-03-25 17:31:22 +00:00
|
|
|
}
|
2020-04-10 13:40:12 +00:00
|
|
|
}
|
|
|
|
|
2020-05-09 21:40:17 +00:00
|
|
|
_async_track.reset();
|
|
|
|
|
|
|
|
// Allow new frames to be queued again.
|
|
|
|
_ar_is_tracking = false;
|
2020-04-10 13:40:12 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-09 21:40:17 +00:00
|
|
|
void face_tracking_instance::refresh_geometry()
|
|
|
|
{ // Update Region of Interest Geometry.
|
|
|
|
auto v0 = _geometry->at(0);
|
|
|
|
auto v1 = _geometry->at(1);
|
|
|
|
auto v2 = _geometry->at(2);
|
|
|
|
auto v3 = _geometry->at(3);
|
|
|
|
|
|
|
|
vec3_set(v3.position, static_cast<float_t>(_size.first), static_cast<float_t>(_size.second), 0.);
|
|
|
|
vec3_set(v2.position, v3.position->x, 0., 0.);
|
|
|
|
vec3_set(v1.position, 0., v3.position->y, 0.);
|
|
|
|
vec3_set(v0.position, 0., 0., 0.);
|
2020-04-10 13:40:12 +00:00
|
|
|
|
2020-05-09 21:40:17 +00:00
|
|
|
float_t hsx = static_cast<float_t>(_filters.size[0].get() / 2.);
|
|
|
|
float_t hsy = static_cast<float_t>(_filters.size[1].get() / 2.);
|
|
|
|
vec4_set(v0.uv[0], static_cast<float_t>(_filters.center[0].get() - hsx),
|
|
|
|
static_cast<float_t>(_filters.center[1].get() - hsy), 0., 0.);
|
|
|
|
vec4_set(v1.uv[0], static_cast<float_t>(_filters.center[0].get() - hsx),
|
|
|
|
static_cast<float_t>(_filters.center[1].get() + hsy), 0., 0.);
|
|
|
|
vec4_set(v2.uv[0], static_cast<float_t>(_filters.center[0].get() + hsx),
|
|
|
|
static_cast<float_t>(_filters.center[1].get() - hsy), 0., 0.);
|
|
|
|
vec4_set(v3.uv[0], static_cast<float_t>(_filters.center[0].get() + hsx),
|
|
|
|
static_cast<float_t>(_filters.center[1].get() + hsy), 0., 0.);
|
|
|
|
|
|
|
|
_geometry->update(true);
|
2020-04-10 13:40:12 +00:00
|
|
|
}
|
|
|
|
|
2020-05-09 21:40:17 +00:00
|
|
|
void face_tracking_instance::refresh_region_of_interest()
|
2020-04-10 13:40:12 +00:00
|
|
|
{
|
2020-05-09 21:40:17 +00:00
|
|
|
std::unique_lock<std::mutex> tlk(_values.lock);
|
|
|
|
|
2021-06-08 02:18:02 +00:00
|
|
|
double_t kalman_q = streamfx::util::math::lerp<double_t>(1.0, 1e-6, _cfg_stability);
|
|
|
|
double_t kalman_r =
|
|
|
|
streamfx::util::math::lerp<double_t>(std::numeric_limits<double_t>::epsilon(), 1e+2, _cfg_stability);
|
2020-04-10 13:40:12 +00:00
|
|
|
|
2021-06-08 02:18:02 +00:00
|
|
|
_filters.center[0] = streamfx::util::math::kalman1D<double_t>{kalman_q, kalman_r, 1., _values.center[0]};
|
|
|
|
_filters.center[1] = streamfx::util::math::kalman1D<double_t>{kalman_q, kalman_r, 1., _values.center[1]};
|
|
|
|
_filters.size[0] = streamfx::util::math::kalman1D<double_t>{kalman_q, kalman_r, 1., _values.size[0]};
|
|
|
|
_filters.size[1] = streamfx::util::math::kalman1D<double_t>{kalman_q, kalman_r, 1., _values.size[1]};
|
2020-04-10 13:40:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 16:52:06 +00:00
|
|
|
void face_tracking_instance::load(obs_data_t* data)
|
2020-04-10 13:40:12 +00:00
|
|
|
{
|
|
|
|
update(data);
|
|
|
|
}
|
|
|
|
|
2020-08-10 01:29:05 +00:00
|
|
|
void face_tracking_instance::migrate(obs_data_t* data, uint64_t version) {}
|
2020-04-10 13:40:12 +00:00
|
|
|
|
2020-04-05 16:52:06 +00:00
|
|
|
void face_tracking_instance::update(obs_data_t* data)
|
2020-04-10 13:40:12 +00:00
|
|
|
{
|
2021-06-04 03:00:20 +00:00
|
|
|
_cfg_zoom = obs_data_get_double(data, ST_KEY_ROI_ZOOM) / 100.0;
|
|
|
|
_cfg_offset.first = obs_data_get_double(data, ST_KEY_ROI_OFFSET_X) / 100.0;
|
|
|
|
_cfg_offset.second = obs_data_get_double(data, ST_KEY_ROI_OFFSET_Y) / 100.0;
|
|
|
|
_cfg_stability = obs_data_get_double(data, ST_KEY_ROI_STABILITY) / 100.0;
|
2020-04-10 13:40:12 +00:00
|
|
|
|
|
|
|
// Refresh the Region Of Interest
|
2020-05-09 21:40:17 +00:00
|
|
|
refresh_region_of_interest();
|
2020-04-10 13:40:12 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 16:52:06 +00:00
|
|
|
void face_tracking_instance::video_tick(float_t seconds)
|
2020-04-10 13:40:12 +00:00
|
|
|
{
|
|
|
|
// If we aren't yet ready to do work, abort for now.
|
|
|
|
if (!_ar_loaded) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-05-09 21:40:17 +00:00
|
|
|
// Update the input size.
|
|
|
|
if (obs_source_t* src = obs_filter_get_target(_self); src != nullptr) {
|
|
|
|
_size.first = obs_source_get_base_width(src);
|
|
|
|
_size.second = obs_source_get_base_height(src);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update filters and geometry
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> tlk(_values.lock);
|
|
|
|
_filters.center[0].filter(_values.center[0]);
|
|
|
|
_filters.center[1].filter(_values.center[1]);
|
|
|
|
_filters.size[0].filter(_values.size[0]);
|
|
|
|
_filters.size[1].filter(_values.size[1]);
|
|
|
|
_values.center[0] += _values.velocity[0] * seconds;
|
|
|
|
_values.center[1] += _values.velocity[1] * seconds;
|
2020-04-10 13:40:12 +00:00
|
|
|
}
|
2020-05-09 21:40:17 +00:00
|
|
|
refresh_geometry();
|
2020-04-10 13:40:12 +00:00
|
|
|
|
|
|
|
_rt_is_fresh = false;
|
|
|
|
}
|
|
|
|
|
2021-04-27 01:14:13 +00:00
|
|
|
void face_tracking_instance::video_render(gs_effect_t*)
|
2020-04-10 13:40:12 +00:00
|
|
|
{
|
2020-04-25 23:04:04 +00:00
|
|
|
obs_source_t* filter_parent = obs_filter_get_parent(_self);
|
|
|
|
obs_source_t* filter_target = obs_filter_get_target(_self);
|
|
|
|
gs_effect_t* default_effect = obs_get_base_effect(OBS_EFFECT_DEFAULT);
|
2020-04-10 13:40:12 +00:00
|
|
|
|
|
|
|
if (!filter_parent || !filter_target || !_size.first || !_size.second || !_ar_loaded) {
|
|
|
|
obs_source_skip_video_filter(_self);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2020-04-25 23:04:04 +00:00
|
|
|
#ifdef ENABLE_PROFILING
|
2021-06-08 02:38:24 +00:00
|
|
|
streamfx::obs::gs::debug_marker gdmp{streamfx::obs::gs::debug_color_source, "NVIDIA Face Tracking '%s'...",
|
|
|
|
obs_source_get_name(_self)};
|
|
|
|
streamfx::obs::gs::debug_marker gdmp2{streamfx::obs::gs::debug_color_source, "... on '%s'",
|
|
|
|
obs_source_get_name(obs_filter_get_parent(_self))};
|
2020-04-25 23:04:04 +00:00
|
|
|
#endif
|
|
|
|
|
2020-04-10 13:40:12 +00:00
|
|
|
if (!_rt_is_fresh) { // Capture the filter stack "below" us.
|
2020-04-25 23:04:04 +00:00
|
|
|
#ifdef ENABLE_PROFILING
|
2020-04-10 13:40:12 +00:00
|
|
|
auto prof = _profile_capture->track();
|
|
|
|
#endif
|
|
|
|
|
2020-04-25 23:04:04 +00:00
|
|
|
{
|
|
|
|
#ifdef ENABLE_PROFILING
|
2021-06-08 02:38:24 +00:00
|
|
|
streamfx::obs::gs::debug_marker gdm{streamfx::obs::gs::debug_color_cache, "Cache"};
|
2020-04-25 23:04:04 +00:00
|
|
|
#endif
|
2020-04-10 13:40:12 +00:00
|
|
|
|
2021-04-27 01:14:13 +00:00
|
|
|
if (obs_source_process_filter_begin(_self, GS_RGBA, OBS_NO_DIRECT_RENDERING)) {
|
2020-04-25 23:04:04 +00:00
|
|
|
auto op = _rt->render(_size.first, _size.second);
|
|
|
|
vec4 clr = {0., 0., 0., 0.};
|
|
|
|
|
2020-05-09 21:40:17 +00:00
|
|
|
gs_ortho(0., 1., 0., 1., -1., 1.);
|
|
|
|
gs_clear(GS_CLEAR_COLOR, &clr, 0., 0);
|
|
|
|
gs_enable_color(true, true, true, true);
|
|
|
|
gs_enable_blending(false);
|
2021-04-27 01:14:13 +00:00
|
|
|
auto old_fbsrgb = gs_framebuffer_srgb_enabled();
|
|
|
|
gs_enable_framebuffer_srgb(gs_get_linear_srgb());
|
2020-05-09 21:40:17 +00:00
|
|
|
obs_source_process_filter_tech_end(_self, default_effect, 1, 1, "Draw");
|
2021-04-27 01:14:13 +00:00
|
|
|
gs_enable_framebuffer_srgb(old_fbsrgb);
|
2020-04-25 23:04:04 +00:00
|
|
|
} else {
|
|
|
|
obs_source_skip_video_filter(_self);
|
|
|
|
return;
|
|
|
|
}
|
2020-03-25 17:31:22 +00:00
|
|
|
}
|
|
|
|
|
2020-05-09 21:40:17 +00:00
|
|
|
// Probably spawn new work.
|
|
|
|
async_track(nullptr);
|
2020-03-25 17:31:22 +00:00
|
|
|
|
2020-04-10 13:40:12 +00:00
|
|
|
_rt_is_fresh = true;
|
2020-03-25 17:31:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-25 23:04:04 +00:00
|
|
|
{ // Draw Texture
|
|
|
|
#ifdef ENABLE_PROFILING
|
2021-06-08 02:38:24 +00:00
|
|
|
streamfx::obs::gs::debug_marker gdm{streamfx::obs::gs::debug_color_render, "Render"};
|
2020-04-25 23:04:04 +00:00
|
|
|
#endif
|
|
|
|
|
2021-04-27 01:14:13 +00:00
|
|
|
gs_effect_set_texture(gs_effect_get_param_by_name(default_effect, "image"), _rt->get_texture()->get_object());
|
2020-05-09 21:40:17 +00:00
|
|
|
gs_load_vertexbuffer(_geometry->update(false));
|
2021-04-27 01:14:13 +00:00
|
|
|
while (gs_effect_loop(default_effect, "Draw")) {
|
2020-05-09 21:40:17 +00:00
|
|
|
gs_draw(gs_draw_mode::GS_TRISTRIP, 0, 0);
|
2020-04-25 23:04:04 +00:00
|
|
|
}
|
|
|
|
gs_load_vertexbuffer(nullptr);
|
2020-03-25 17:31:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-25 23:04:04 +00:00
|
|
|
#ifdef ENABLE_PROFILING
|
2020-04-05 16:52:06 +00:00
|
|
|
bool face_tracking_instance::button_profile(obs_properties_t* props, obs_property_t* property)
|
2020-03-25 17:31:22 +00:00
|
|
|
{
|
2020-07-25 14:51:22 +00:00
|
|
|
DLOG_INFO("%-22s: %-10s %-10s %-10s %-10s %-10s", "Task", "Total", "Count", "Average", "99.9%ile", "95.0%ile");
|
2020-04-10 13:40:12 +00:00
|
|
|
|
2021-06-08 02:18:02 +00:00
|
|
|
std::pair<std::string, std::shared_ptr<streamfx::util::profiler>> profilers[]{
|
2020-04-10 13:40:12 +00:00
|
|
|
{"Capture", _profile_capture}, {"Reallocate", _profile_capture_realloc},
|
|
|
|
{"Copy", _profile_capture_copy}, {"AR Reallocate", _profile_ar_realloc},
|
|
|
|
{"AR Copy", _profile_ar_copy}, {"AR Convert", _profile_ar_transfer},
|
|
|
|
{"AR Run", _profile_ar_run}, {"AR Calculate", _profile_ar_calc},
|
|
|
|
};
|
|
|
|
for (auto& kv : profilers) {
|
2020-07-25 14:51:22 +00:00
|
|
|
DLOG_INFO(" %-20s: %8lldµs %10lld %8lldµs %8lldµs %8lldµs", kv.first.c_str(),
|
2020-07-12 16:41:50 +00:00
|
|
|
std::chrono::duration_cast<std::chrono::microseconds>(kv.second->total_duration()).count(),
|
2020-08-10 01:29:05 +00:00
|
|
|
kv.second->count(), static_cast<int64_t>(kv.second->average_duration() / 1000.0),
|
2020-07-12 16:41:50 +00:00
|
|
|
std::chrono::duration_cast<std::chrono::microseconds>(kv.second->percentile(0.999)).count(),
|
|
|
|
std::chrono::duration_cast<std::chrono::microseconds>(kv.second->percentile(0.95)).count());
|
2020-04-10 13:40:12 +00:00
|
|
|
}
|
2020-03-25 17:31:22 +00:00
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-04-05 16:52:06 +00:00
|
|
|
face_tracking_factory::face_tracking_factory()
|
2020-03-25 17:31:22 +00:00
|
|
|
{
|
|
|
|
// Try and load CUDA.
|
2021-06-16 10:15:13 +00:00
|
|
|
_cuda = ::streamfx::nvidia::cuda::obs::get();
|
2020-03-25 17:31:22 +00:00
|
|
|
|
|
|
|
// Try and load AR.
|
2021-06-16 10:15:13 +00:00
|
|
|
_ar = std::make_shared<::streamfx::nvidia::ar::ar>();
|
2020-03-25 17:31:22 +00:00
|
|
|
|
|
|
|
// Info
|
2021-06-08 02:16:33 +00:00
|
|
|
_info.id = S_PREFIX "filter-nvidia-face-tracking";
|
2020-03-25 17:31:22 +00:00
|
|
|
_info.type = OBS_SOURCE_TYPE_FILTER;
|
2021-04-27 01:14:13 +00:00
|
|
|
_info.output_flags = OBS_SOURCE_VIDEO | OBS_SOURCE_CUSTOM_DRAW;
|
2020-03-25 17:31:22 +00:00
|
|
|
|
|
|
|
set_resolution_enabled(false);
|
|
|
|
finish_setup();
|
2020-06-14 02:53:15 +00:00
|
|
|
register_proxy("streamfx-nvidia-face-tracking");
|
2020-03-25 17:31:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 16:52:06 +00:00
|
|
|
face_tracking_factory::~face_tracking_factory() {}
|
2020-03-25 17:31:22 +00:00
|
|
|
|
2020-04-05 16:52:06 +00:00
|
|
|
const char* face_tracking_factory::get_name()
|
2020-03-25 17:31:22 +00:00
|
|
|
{
|
2021-06-04 03:00:20 +00:00
|
|
|
return D_TRANSLATE(ST_I18N);
|
2020-03-25 17:31:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 16:52:06 +00:00
|
|
|
void face_tracking_factory::get_defaults2(obs_data_t* data)
|
2020-03-25 17:31:22 +00:00
|
|
|
{
|
2021-06-04 03:00:20 +00:00
|
|
|
obs_data_set_default_double(data, ST_KEY_ROI_ZOOM, 50.0);
|
|
|
|
obs_data_set_default_double(data, ST_KEY_ROI_OFFSET_X, 0.0);
|
|
|
|
obs_data_set_default_double(data, ST_KEY_ROI_OFFSET_Y, -15.0);
|
|
|
|
obs_data_set_default_double(data, ST_KEY_ROI_STABILITY, 50.0);
|
2020-03-25 17:31:22 +00:00
|
|
|
}
|
|
|
|
|
2020-04-05 16:52:06 +00:00
|
|
|
obs_properties_t* face_tracking_factory::get_properties2(face_tracking_instance* data)
|
2020-03-25 17:31:22 +00:00
|
|
|
{
|
|
|
|
obs_properties_t* pr = obs_properties_create();
|
|
|
|
|
|
|
|
{
|
|
|
|
auto grp = obs_properties_create();
|
2021-06-04 03:00:20 +00:00
|
|
|
obs_properties_add_group(pr, ST_I18N_ROI, D_TRANSLATE(ST_I18N_ROI), OBS_GROUP_NORMAL, grp);
|
2020-03-25 17:31:22 +00:00
|
|
|
{
|
2021-06-04 03:00:20 +00:00
|
|
|
auto p = obs_properties_add_float_slider(grp, ST_KEY_ROI_STABILITY, D_TRANSLATE(ST_I18N_ROI_STABILITY), 0,
|
|
|
|
100.0, 0.01);
|
2020-03-25 17:31:22 +00:00
|
|
|
obs_property_float_set_suffix(p, " %");
|
|
|
|
}
|
|
|
|
{
|
2021-06-04 03:00:20 +00:00
|
|
|
auto p =
|
|
|
|
obs_properties_add_float_slider(grp, ST_KEY_ROI_ZOOM, D_TRANSLATE(ST_I18N_ROI_ZOOM), 0, 200.0, 0.01);
|
2020-03-25 17:31:22 +00:00
|
|
|
obs_property_float_set_suffix(p, " %");
|
|
|
|
}
|
|
|
|
{
|
|
|
|
auto grp2 = obs_properties_create();
|
2021-06-04 03:00:20 +00:00
|
|
|
obs_properties_add_group(grp, ST_I18N_ROI_OFFSET, D_TRANSLATE(ST_I18N_ROI_OFFSET), OBS_GROUP_NORMAL, grp2);
|
2020-03-25 17:31:22 +00:00
|
|
|
|
|
|
|
{
|
2021-06-04 03:00:20 +00:00
|
|
|
auto p = obs_properties_add_float_slider(grp2, ST_KEY_ROI_OFFSET_X, D_TRANSLATE(ST_I18N_ROI_OFFSET_X),
|
|
|
|
-50.0, 50.0, 0.01);
|
2020-03-25 17:31:22 +00:00
|
|
|
obs_property_float_set_suffix(p, " %");
|
|
|
|
}
|
|
|
|
{
|
2021-06-04 03:00:20 +00:00
|
|
|
auto p = obs_properties_add_float_slider(grp2, ST_KEY_ROI_OFFSET_Y, D_TRANSLATE(ST_I18N_ROI_OFFSET_Y),
|
|
|
|
-50.0, 50.0, 0.01);
|
2020-03-25 17:31:22 +00:00
|
|
|
obs_property_float_set_suffix(p, " %");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-04-25 23:04:04 +00:00
|
|
|
#ifdef ENABLE_PROFILING
|
2020-03-25 17:31:22 +00:00
|
|
|
{
|
2020-04-08 21:38:42 +00:00
|
|
|
obs_properties_add_button2(
|
2020-03-25 17:31:22 +00:00
|
|
|
pr, "Profile", "Profile",
|
|
|
|
[](obs_properties_t* props, obs_property_t* property, void* data) {
|
2020-04-05 16:52:06 +00:00
|
|
|
return reinterpret_cast<face_tracking_instance*>(data)->button_profile(props, property);
|
2020-03-25 17:31:22 +00:00
|
|
|
},
|
|
|
|
data);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
return pr;
|
|
|
|
}
|
|
|
|
|
2021-06-16 10:15:13 +00:00
|
|
|
std::shared_ptr<::streamfx::nvidia::ar::ar> face_tracking_factory::get_ar()
|
2020-03-25 17:31:22 +00:00
|
|
|
{
|
|
|
|
return _ar;
|
|
|
|
}
|
2020-04-05 16:52:06 +00:00
|
|
|
|
|
|
|
std::shared_ptr<face_tracking_factory> _filter_nvidia_face_tracking_factory_instance = nullptr;
|
|
|
|
|
|
|
|
void streamfx::filter::nvidia::face_tracking_factory::initialize()
|
2021-09-07 02:22:46 +00:00
|
|
|
try {
|
|
|
|
if (!_filter_nvidia_face_tracking_factory_instance)
|
2020-04-05 16:52:06 +00:00
|
|
|
_filter_nvidia_face_tracking_factory_instance = std::make_shared<filter::nvidia::face_tracking_factory>();
|
2021-09-07 02:22:46 +00:00
|
|
|
} catch (const std::exception& ex) {
|
|
|
|
D_LOG_ERROR("Failed to initialize due to error: %s", ex.what());
|
|
|
|
} catch (...) {
|
|
|
|
D_LOG_ERROR("Failed to initialize due to unknown error.", "");
|
2020-04-05 16:52:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void streamfx::filter::nvidia::face_tracking_factory::finalize()
|
|
|
|
{
|
|
|
|
_filter_nvidia_face_tracking_factory_instance.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::shared_ptr<face_tracking_factory> streamfx::filter::nvidia::face_tracking_factory::get()
|
|
|
|
{
|
|
|
|
return _filter_nvidia_face_tracking_factory_instance;
|
|
|
|
}
|