48#include <opencv2/opencv.hpp>
49#include <onnxruntime_c_api.h>
55namespace xrt::tracking::hand::mercury {
57using namespace xrt::auxiliary::util;
59using namespace xrt::auxiliary::onnx;
61#define HG_TRACE(hgt, ...) U_LOG_IFL_T(hgt->log_level, __VA_ARGS__)
62#define HG_DEBUG(hgt, ...) U_LOG_IFL_D(hgt->log_level, __VA_ARGS__)
63#define HG_INFO(hgt, ...) U_LOG_IFL_I(hgt->log_level, __VA_ARGS__)
64#define HG_WARN(hgt, ...) U_LOG_IFL_W(hgt->log_level, __VA_ARGS__)
65#define HG_ERROR(hgt, ...) U_LOG_IFL_E(hgt->log_level, __VA_ARGS__)
67static constexpr uint16_t kDetectionInputSize = 160;
68static constexpr uint16_t kKeypointInputSize = 128;
70static constexpr uint16_t kKeypointOutputHeatmapSize = 22;
71static constexpr uint16_t kVisSpacerSize = 8;
73static const cv::Scalar RED(255, 30, 30);
74static const cv::Scalar YELLOW(255, 255, 0);
75static const cv::Scalar PINK(255, 0, 255);
76static const cv::Scalar GREEN(0, 255, 0);
78static const cv::Scalar colors[2] = {YELLOW, RED};
83 XRT_HAND_JOINT_THUMB_METACARPAL,
84 XRT_HAND_JOINT_THUMB_PROXIMAL,
85 XRT_HAND_JOINT_THUMB_DISTAL,
86 XRT_HAND_JOINT_THUMB_TIP,
89 XRT_HAND_JOINT_INDEX_METACARPAL,
90 XRT_HAND_JOINT_INDEX_PROXIMAL,
91 XRT_HAND_JOINT_INDEX_INTERMEDIATE,
92 XRT_HAND_JOINT_INDEX_DISTAL,
93 XRT_HAND_JOINT_INDEX_TIP,
96 XRT_HAND_JOINT_MIDDLE_METACARPAL,
97 XRT_HAND_JOINT_MIDDLE_PROXIMAL,
98 XRT_HAND_JOINT_MIDDLE_INTERMEDIATE,
99 XRT_HAND_JOINT_MIDDLE_DISTAL,
100 XRT_HAND_JOINT_MIDDLE_TIP,
103 XRT_HAND_JOINT_RING_METACARPAL,
104 XRT_HAND_JOINT_RING_PROXIMAL,
105 XRT_HAND_JOINT_RING_INTERMEDIATE,
106 XRT_HAND_JOINT_RING_DISTAL,
107 XRT_HAND_JOINT_RING_TIP,
110 XRT_HAND_JOINT_LITTLE_METACARPAL,
111 XRT_HAND_JOINT_LITTLE_PROXIMAL,
112 XRT_HAND_JOINT_LITTLE_INTERMEDIATE,
113 XRT_HAND_JOINT_LITTLE_DISTAL,
114 XRT_HAND_JOINT_LITTLE_TIP,
118namespace ROIProvenance {
137using hand21_2d = std::array<vec2_5, 21>;
141 Eigen::Quaternionf rot_quat = Eigen::Quaternionf::Identity();
142 float stereographic_radius = 0;
151 float *data =
nullptr;
152 int64_t dimensions[4];
153 size_t num_dimensions = 0;
155 OrtValue *tensor =
nullptr;
161 std::unique_ptr<OnnxWrapper> wrap = {};
163 std::vector<model_input_wrap> wraps = {};
172 ROIProvenance::ROIProvenance provenance;
179 float hand_detection_confidence;
214 cv::Mat run_model_on_this;
215 cv::Mat debug_out_to_this;
248 float multiply_px_coord_for_undistort;
253 struct xrt_size calibration_one_view_size_px = {};
256 struct xrt_size last_frame_one_view_size_px = {};
268 xrt_pose hand_pose_camera_offset = {};
270 uint64_t current_frame_timestamp = {};
272 bool debug_scribble =
false;
274 char models_folder[1024];
286 bool hand_seen_before[2] = {
false,
false};
292 bool last_frame_hand_detected[2] = {
false,
false};
295 bool this_frame_hand_detected[2] = {
false,
false};
305 uint64_t hand_tracked_for_num_frames[2] = {0, 0};
309 Eigen::Array<float, 3, 21> pose_predicted_keypoints[2];
311 int detection_counter = 0;
321 void (*keypoint_estimation_run_func)(
void *);
331 HandSizeRefinement hand_size_refinement{};
334 explicit HandTracking();
337 static inline HandTracking &
340 return *
reinterpret_cast<HandTracking *
>(ht_sync);
349 int64_t *out_timestamp_ns);
357init_hand_detection(HandTracking *hgt, onnx_state *wrap);
360init_keypoint_estimation(HandTracking *hgt, onnx_state *wrap);
366run_hand_detection(
void *ptr);
370run_keypoint_estimation(
void *ptr);
374release_onnx_state(onnx_state *wrap);
382 Eigen::Array<float, 3, 21> &joints,
383 projection_instructions &out_instructions,
384 hand21_2d &out_hand);
388make_projection_instructions_angular(
xrt_vec3 direction_3d,
390 float angular_radius,
393 projection_instructions &out_instructions);
397 const projection_instructions &instructions,
398 cv::Mat &input_image,
399 cv::Mat *debug_image,
400 const cv::Scalar &boundary_color,
Stores some number of values in a ring buffer, overwriting the earliest-pushed-remaining element if o...
Definition u_template_historybuf.hpp:38
u_logging_level
Logging level enum.
Definition u_logging.h:45
@ U_LOGGING_INFO
Info messages: not very verbose, not indicating a problem.
Definition u_logging.h:48
xrt_hand_joint
Number of joints in a hand.
Definition xrt_defines.h:1452
Debug instrumentation for mercury_train or others to control hand tracking.
Mercury hand size optimization heuristics.
void make_projection_instructions(t_camera_model_params &dist, bool flip_after, float expand_val, float twist, Eigen::Array< float, 3, 21 > &joints, projection_instructions &out_instructions, hand21_2d &out_hand)
Definition hg_image_distorter.cpp:494
Public interface of Mercury hand tracking.
Random common stuff for Mercury kinematic optimizers.
Interface for Levenberg-Marquardt kinematic optimizer.
C interface to math library.
Interoperability helpers connecting internal math types and Eigen.
Wrapper header for <math.h> to ensure pi-related math constants are defined.
C++-only functionality in the Math helper library.
Definition m_documentation.hpp:15
onnxruntime wrapper objects and functions.
Definition hg_debug_instrumentation.hpp:23
Floating point calibration data for a single calibrated camera.
Definition t_camera_models.h:47
Synchronously processes frames and returns two hands.
Definition t_hand_tracking.h:120
Stereo camera calibration data to be given to trackers.
Definition t_tracking.h:261
Allows more safely to debug sink inputs and outputs.
Definition u_sink.h:214
A worker group where you submit tasks to.
Definition u_worker.h:102
A worker pool, can shared between multiple groups worker pool.
Definition u_worker.h:33
Definition hg_sync.hpp:133
Main class of Mercury hand tracking.
Definition hg_sync.hpp:239
float target_hand_size
Regularly updated from hand_size_refinement.
Definition hg_sync.hpp:314
Definition hg_sync.hpp:185
Definition hg_sync.hpp:171
Definition hg_sync.hpp:201
Definition hg_sync.hpp:195
Definition lm_defines.hpp:528
Definition hg_sync.hpp:223
Definition hg_sync.hpp:160
Definition hg_sync.hpp:140
Basic frame data structure - holds a pointer to buffer.
Definition xrt_frame.h:25
Joint set type used for hand tracking.
Definition xrt_defines.h:1531
An object to push xrt_hand_masks_sample to.
Definition xrt_tracking.h:204
A pose composed of a position and orientation.
Definition xrt_defines.h:502
Image size.
Definition xrt_defines.h:446
A 2 element vector with single floats.
Definition xrt_defines.h:268
A 3 element vector with single floats.
Definition xrt_defines.h:299
Camera (un)projection C API for various camera models.
Hand tracking interfaces.
Basic logging functionality.
xrt_frame_sink converters and other helpers.
Ringbuffer implementation for keeping track of the past state of things.
Tracing support code, see Tracing support.
Worker and threading pool.
Common defines and enums for XRT.
Header defining the tracking system integration in Monado.