feat(tracker-native): native Windows app - MF camera + Win32 GUI + shm publish
win_camera: Media Foundation device + native-resolution enumeration and RGB32 capture. win_main: plain Win32 GUI (device/resolution dropdowns, Start/Stop, StretchDIBits live preview with mask+landmark overlay) + CreateFileMappingW on kinect_virtual_frames + a worker thread wiring capture -> PoseEstimator (bootstrap+track) -> FramePipeline -> publish. build_win.sh cross-compiles a static kinect-tracker.exe with MinGW-w64 x64 (ORT header needs -D_stdcall=__stdcall; x64 ignores the calling convention so the win ORT DLL ABI matches). PoseEstimator path made ORTCHAR_T/wchar-portable; M_PI -> local const. Builds clean; exe is PE32+ GUI x86-64.
This commit is contained in:
1 parent
41534b07d1
commit
31178f1675
6 files changed
+643
-6
No files matched your search
@@ -62,12 +62,15 @@ Windows Media Foundation (MinGW-w64 headers available):
|
||||
resolutions for the resolution picker,
|
||||
- convert NV12/YUY2/RGB32 samples to BGR for inference + preview.
|
||||
|
||||
## GUI: Dear ImGui (Win32 + D3D11)
|
||||
## GUI: plain Win32 + GDI
|
||||
|
||||
Standard ImGui `imgui_impl_win32` + `imgui_impl_dx11` backends (MinGW-buildable):
|
||||
device dropdown, resolution dropdown, start/stop, live preview texture with an
|
||||
optional mask/skeleton overlay, and a status line (published seq, fps, player
|
||||
detected).
|
||||
A plain Win32 window (user32/gdi32 only — no ImGui, no Direct3D, no
|
||||
`d3dcompiler` dependency, so it cross-compiles with MinGW first try): two
|
||||
drop-downs (camera device, native resolution), a Start/Stop button, a status
|
||||
line, and a live preview drawn with `StretchDIBits`. The preview shows the
|
||||
captured frame with the segmentation mask tinted and the pose landmarks
|
||||
overlaid. A worker thread runs capture → pose → publish; the UI thread repaints
|
||||
the latest frame on a 33 ms timer.
|
||||
|
||||
## Process / threading
|
||||
|
||||
|
||||
Executable
+32
@@ -0,0 +1,32 @@
|
||||
#!/usr/bin/env bash
|
||||
set -euo pipefail
|
||||
HERE="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
|
||||
CORE="${HERE}/../core"
|
||||
ORTW="${HERE}/third_party/onnxruntime-win-x64"
|
||||
export PATH="${MINGW_BIN:-$HOME/.local/mingw-root/usr/bin}:${PATH}"
|
||||
CXX="${CXX_WIN:-x86_64-w64-mingw32-g++-win32}"
|
||||
|
||||
MODEL="${HERE}/models/pose_landmarks_detector.onnx"
|
||||
if [ ! -f "${MODEL}" ]; then
|
||||
echo "missing ${MODEL} - run scripts/setup_models.sh first" >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
mkdir -p "${HERE}/build/dist"
|
||||
|
||||
"${CXX}" -std=c++17 -O2 -Wall -Wextra -D_stdcall=__stdcall -municode \
|
||||
-static -static-libgcc -static-libstdc++ \
|
||||
-I "${ORTW}/include" -I "${HERE}/src" -I "${CORE}" \
|
||||
"${HERE}/src/win_main.cpp" "${HERE}/src/win_camera.cpp" \
|
||||
"${HERE}/src/pose_estimator.cpp" "${HERE}/src/shm_publisher.cpp" \
|
||||
"${HERE}/src/frame_pipeline.cpp" \
|
||||
"${CORE}/mapping/mapping.cpp" "${CORE}/synth/synth.cpp" \
|
||||
"${CORE}/synth/mask_stabilizer.cpp" "${CORE}/contract/contract.cpp" \
|
||||
"${ORTW}/lib/onnxruntime.lib" \
|
||||
-lmfplat -lmf -lmfreadwrite -lmfuuid -lole32 -loleaut32 -lgdi32 -luser32 \
|
||||
-mwindows -o "${HERE}/build/dist/kinect-tracker.exe"
|
||||
|
||||
cp -f "${ORTW}/lib/onnxruntime.dll" "${HERE}/build/dist/"
|
||||
cp -f "${MODEL}" "${HERE}/build/dist/"
|
||||
echo "dist ready: ${HERE}/build/dist"
|
||||
ls -la "${HERE}/build/dist"
|
||||
@@ -5,10 +5,14 @@
|
||||
#include <algorithm>
|
||||
#include <array>
|
||||
#include <cmath>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
|
||||
namespace kinect_tracker {
|
||||
namespace {
|
||||
|
||||
constexpr float kPi = 3.14159265358979323846f;
|
||||
|
||||
float Sigmoid(float v) { return 1.0f / (1.0f + std::exp(-v)); }
|
||||
|
||||
struct Affine2x3 {
|
||||
@@ -49,7 +53,13 @@ PoseEstimator::PoseEstimator(const char* landmark_model_path) {
|
||||
Ort::SessionOptions so;
|
||||
so.SetIntraOpNumThreads(1);
|
||||
so.SetGraphOptimizationLevel(ORT_ENABLE_ALL);
|
||||
#ifdef _WIN32
|
||||
const std::string p(landmark_model_path);
|
||||
const std::wstring wp(p.begin(), p.end());
|
||||
session_ = std::make_unique<Ort::Session>(*env_, wp.c_str(), so);
|
||||
#else
|
||||
session_ = std::make_unique<Ort::Session>(*env_, landmark_model_path, so);
|
||||
#endif
|
||||
input_.resize(static_cast<size_t>(kLandmarkInput) * kLandmarkInput * 3);
|
||||
}
|
||||
|
||||
@@ -71,7 +81,7 @@ Roi PoseEstimator::roi_from_points(float cx, float cy, float ex, float ey) {
|
||||
r.cx = cx;
|
||||
r.cy = cy;
|
||||
r.size = 2.0f * std::hypot(dx, dy) * 1.25f;
|
||||
r.angle = static_cast<float>(M_PI) * 0.5f - std::atan2(-dy, dx);
|
||||
r.angle = kPi * 0.5f - std::atan2(-dy, dx);
|
||||
return r;
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,195 @@
|
||||
#include "win_camera.h"
|
||||
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#include <mfapi.h>
|
||||
#include <mfidl.h>
|
||||
#include <mfreadwrite.h>
|
||||
#include <mferror.h>
|
||||
|
||||
#include <cstring>
|
||||
|
||||
namespace kinect_tracker {
|
||||
namespace {
|
||||
|
||||
template <class T>
|
||||
void SafeRelease(T** p) {
|
||||
if (*p) {
|
||||
(*p)->Release();
|
||||
*p = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
std::string Narrow(const wchar_t* w) {
|
||||
if (!w) return {};
|
||||
const int n = WideCharToMultiByte(CP_UTF8, 0, w, -1, nullptr, 0, nullptr, nullptr);
|
||||
std::string s(n > 0 ? n - 1 : 0, '\0');
|
||||
if (n > 0) WideCharToMultiByte(CP_UTF8, 0, w, -1, s.data(), n, nullptr, nullptr);
|
||||
return s;
|
||||
}
|
||||
|
||||
IMFMediaSource* CreateSource(const std::wstring& symlink) {
|
||||
IMFAttributes* attr = nullptr;
|
||||
if (FAILED(MFCreateAttributes(&attr, 2))) return nullptr;
|
||||
attr->SetGUID(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE,
|
||||
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID);
|
||||
attr->SetString(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK,
|
||||
symlink.c_str());
|
||||
IMFMediaSource* src = nullptr;
|
||||
MFCreateDeviceSource(attr, &src);
|
||||
SafeRelease(&attr);
|
||||
return src;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
std::vector<CameraDevice> enumerate_cameras() {
|
||||
std::vector<CameraDevice> out;
|
||||
IMFAttributes* attr = nullptr;
|
||||
if (FAILED(MFCreateAttributes(&attr, 1))) return out;
|
||||
attr->SetGUID(MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE,
|
||||
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_GUID);
|
||||
IMFActivate** devices = nullptr;
|
||||
UINT32 count = 0;
|
||||
if (SUCCEEDED(MFEnumDeviceSources(attr, &devices, &count))) {
|
||||
for (UINT32 i = 0; i < count; ++i) {
|
||||
CameraDevice dev;
|
||||
wchar_t* link = nullptr;
|
||||
UINT32 len = 0;
|
||||
if (SUCCEEDED(devices[i]->GetAllocatedString(
|
||||
MF_DEVSOURCE_ATTRIBUTE_SOURCE_TYPE_VIDCAP_SYMBOLIC_LINK, &link, &len))) {
|
||||
dev.symbolic_link = link;
|
||||
CoTaskMemFree(link);
|
||||
}
|
||||
wchar_t* name = nullptr;
|
||||
if (SUCCEEDED(devices[i]->GetAllocatedString(
|
||||
MF_DEVSOURCE_ATTRIBUTE_FRIENDLY_NAME, &name, &len))) {
|
||||
dev.name = Narrow(name);
|
||||
CoTaskMemFree(name);
|
||||
}
|
||||
out.push_back(std::move(dev));
|
||||
SafeRelease(&devices[i]);
|
||||
}
|
||||
CoTaskMemFree(devices);
|
||||
}
|
||||
SafeRelease(&attr);
|
||||
return out;
|
||||
}
|
||||
|
||||
std::vector<CameraResolution> enumerate_resolutions(const std::wstring& symbolic_link) {
|
||||
std::vector<CameraResolution> out;
|
||||
IMFMediaSource* src = CreateSource(symbolic_link);
|
||||
if (!src) return out;
|
||||
IMFSourceReader* reader = nullptr;
|
||||
if (SUCCEEDED(MFCreateSourceReaderFromMediaSource(src, nullptr, &reader))) {
|
||||
for (DWORD i = 0;; ++i) {
|
||||
IMFMediaType* type = nullptr;
|
||||
if (FAILED(reader->GetNativeMediaType(
|
||||
MF_SOURCE_READER_FIRST_VIDEO_STREAM, i, &type))) {
|
||||
break;
|
||||
}
|
||||
UINT32 w = 0, h = 0;
|
||||
if (SUCCEEDED(MFGetAttributeSize(type, MF_MT_FRAME_SIZE, &w, &h))) {
|
||||
bool seen = false;
|
||||
for (const auto& r : out) {
|
||||
if (r.width == static_cast<int>(w) && r.height == static_cast<int>(h)) {
|
||||
seen = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!seen) out.push_back({static_cast<int>(w), static_cast<int>(h)});
|
||||
}
|
||||
SafeRelease(&type);
|
||||
}
|
||||
SafeRelease(&reader);
|
||||
}
|
||||
SafeRelease(&src);
|
||||
return out;
|
||||
}
|
||||
|
||||
Camera::~Camera() { close(); }
|
||||
|
||||
bool Camera::open(const std::wstring& symbolic_link, int width, int height) {
|
||||
close();
|
||||
IMFMediaSource* src = CreateSource(symbolic_link);
|
||||
if (!src) return false;
|
||||
|
||||
IMFAttributes* ra = nullptr;
|
||||
MFCreateAttributes(&ra, 1);
|
||||
ra->SetUINT32(MF_SOURCE_READER_ENABLE_ADVANCED_VIDEO_PROCESSING, TRUE);
|
||||
IMFSourceReader* reader = nullptr;
|
||||
HRESULT hr = MFCreateSourceReaderFromMediaSource(src, ra, &reader);
|
||||
SafeRelease(&ra);
|
||||
SafeRelease(&src);
|
||||
if (FAILED(hr)) return false;
|
||||
|
||||
IMFMediaType* mt = nullptr;
|
||||
MFCreateMediaType(&mt);
|
||||
mt->SetGUID(MF_MT_MAJOR_TYPE, MFMediaType_Video);
|
||||
mt->SetGUID(MF_MT_SUBTYPE, MFVideoFormat_RGB32);
|
||||
MFSetAttributeSize(mt, MF_MT_FRAME_SIZE, width, height);
|
||||
hr = reader->SetCurrentMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM, nullptr, mt);
|
||||
SafeRelease(&mt);
|
||||
if (FAILED(hr)) {
|
||||
SafeRelease(&reader);
|
||||
return false;
|
||||
}
|
||||
|
||||
IMFMediaType* cur = nullptr;
|
||||
if (SUCCEEDED(reader->GetCurrentMediaType(MF_SOURCE_READER_FIRST_VIDEO_STREAM, &cur))) {
|
||||
UINT32 w = 0, h = 0;
|
||||
if (SUCCEEDED(MFGetAttributeSize(cur, MF_MT_FRAME_SIZE, &w, &h))) {
|
||||
width = static_cast<int>(w);
|
||||
height = static_cast<int>(h);
|
||||
}
|
||||
SafeRelease(&cur);
|
||||
}
|
||||
|
||||
reader_ = reader;
|
||||
width_ = width;
|
||||
height_ = height;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool Camera::read_frame(std::vector<uint8_t>& bgra, int& width, int& height) {
|
||||
if (!reader_) return false;
|
||||
auto* reader = static_cast<IMFSourceReader*>(reader_);
|
||||
DWORD stream_flags = 0;
|
||||
LONGLONG timestamp = 0;
|
||||
IMFSample* sample = nullptr;
|
||||
HRESULT hr = reader->ReadSample(MF_SOURCE_READER_FIRST_VIDEO_STREAM, 0, nullptr,
|
||||
&stream_flags, ×tamp, &sample);
|
||||
if (FAILED(hr)) return false;
|
||||
if (!sample) return false;
|
||||
|
||||
IMFMediaBuffer* buffer = nullptr;
|
||||
bool ok = false;
|
||||
if (SUCCEEDED(sample->ConvertToContiguousBuffer(&buffer))) {
|
||||
BYTE* data = nullptr;
|
||||
DWORD max_len = 0, cur_len = 0;
|
||||
if (SUCCEEDED(buffer->Lock(&data, &max_len, &cur_len))) {
|
||||
const size_t need = static_cast<size_t>(width_) * height_ * 4u;
|
||||
if (cur_len >= need) {
|
||||
bgra.resize(need);
|
||||
std::memcpy(bgra.data(), data, need);
|
||||
width = width_;
|
||||
height = height_;
|
||||
ok = true;
|
||||
}
|
||||
buffer->Unlock();
|
||||
}
|
||||
SafeRelease(&buffer);
|
||||
}
|
||||
SafeRelease(&sample);
|
||||
return ok;
|
||||
}
|
||||
|
||||
void Camera::close() {
|
||||
if (reader_) {
|
||||
static_cast<IMFSourceReader*>(reader_)->Release();
|
||||
reader_ = nullptr;
|
||||
}
|
||||
width_ = height_ = 0;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,43 @@
|
||||
#ifndef TRACKER_NATIVE_WIN_CAMERA_H
|
||||
#define TRACKER_NATIVE_WIN_CAMERA_H
|
||||
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace kinect_tracker {
|
||||
|
||||
struct CameraDevice {
|
||||
std::wstring symbolic_link;
|
||||
std::string name;
|
||||
};
|
||||
|
||||
struct CameraResolution {
|
||||
int width;
|
||||
int height;
|
||||
};
|
||||
|
||||
std::vector<CameraDevice> enumerate_cameras();
|
||||
std::vector<CameraResolution> enumerate_resolutions(const std::wstring& symbolic_link);
|
||||
|
||||
class Camera {
|
||||
public:
|
||||
Camera() = default;
|
||||
~Camera();
|
||||
Camera(const Camera&) = delete;
|
||||
Camera& operator=(const Camera&) = delete;
|
||||
|
||||
bool open(const std::wstring& symbolic_link, int width, int height);
|
||||
bool read_frame(std::vector<uint8_t>& bgra, int& width, int& height);
|
||||
void close();
|
||||
bool is_open() const { return reader_ != nullptr; }
|
||||
|
||||
private:
|
||||
void* reader_ = nullptr;
|
||||
int width_ = 0;
|
||||
int height_ = 0;
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif
|
||||
@@ -0,0 +1,354 @@
|
||||
#define WIN32_LEAN_AND_MEAN
|
||||
#include <windows.h>
|
||||
#include <mfapi.h>
|
||||
|
||||
#include <atomic>
|
||||
#include <mutex>
|
||||
#include <string>
|
||||
#include <thread>
|
||||
#include <vector>
|
||||
|
||||
#include "frame_pipeline.h"
|
||||
#include "pose_estimator.h"
|
||||
#include "shm_publisher.h"
|
||||
#include "win_camera.h"
|
||||
|
||||
using namespace kinect_tracker;
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr int kIdDevice = 1001;
|
||||
constexpr int kIdResolution = 1002;
|
||||
constexpr int kIdStart = 1003;
|
||||
constexpr int kIdStatus = 1004;
|
||||
constexpr wchar_t kShmName[] = L"kinect_virtual_frames";
|
||||
constexpr wchar_t kModelName[] = L"pose_landmarks_detector.onnx";
|
||||
|
||||
struct App {
|
||||
HWND main = nullptr;
|
||||
HWND device_combo = nullptr;
|
||||
HWND res_combo = nullptr;
|
||||
HWND start_btn = nullptr;
|
||||
HWND status = nullptr;
|
||||
|
||||
std::vector<CameraDevice> devices;
|
||||
std::vector<CameraResolution> resolutions;
|
||||
|
||||
std::thread worker;
|
||||
std::atomic<bool> running{false};
|
||||
|
||||
HANDLE mapping = nullptr;
|
||||
uint8_t* shm_base = nullptr;
|
||||
|
||||
std::mutex preview_mutex;
|
||||
std::vector<uint8_t> preview_bgra;
|
||||
int preview_w = 0;
|
||||
int preview_h = 0;
|
||||
std::string status_text = "Idle";
|
||||
};
|
||||
|
||||
App g_app;
|
||||
|
||||
std::wstring ExeDir() {
|
||||
wchar_t path[MAX_PATH];
|
||||
GetModuleFileNameW(nullptr, path, MAX_PATH);
|
||||
std::wstring p(path);
|
||||
const size_t slash = p.find_last_of(L"\\/");
|
||||
return slash == std::wstring::npos ? L"." : p.substr(0, slash);
|
||||
}
|
||||
|
||||
std::string ModelPath() {
|
||||
const std::wstring dir = ExeDir();
|
||||
const wchar_t* candidates[] = {L"\\", L"\\models\\"};
|
||||
for (const wchar_t* mid : candidates) {
|
||||
std::wstring w = dir + mid + kModelName;
|
||||
if (GetFileAttributesW(w.c_str()) != INVALID_FILE_ATTRIBUTES) {
|
||||
std::string s(w.begin(), w.end());
|
||||
return s;
|
||||
}
|
||||
}
|
||||
std::wstring w = dir + L"\\" + kModelName;
|
||||
return std::string(w.begin(), w.end());
|
||||
}
|
||||
|
||||
void SetStatus(const std::string& text) {
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_app.preview_mutex);
|
||||
g_app.status_text = text;
|
||||
}
|
||||
SetWindowTextA(g_app.status, text.c_str());
|
||||
}
|
||||
|
||||
void DrawOverlay(std::vector<uint8_t>& bgra, int w, int h, const PoseResult& pose) {
|
||||
auto plot = [&](int x, int y, uint8_t b, uint8_t g, uint8_t r) {
|
||||
for (int dy = -2; dy <= 2; ++dy) {
|
||||
for (int dx = -2; dx <= 2; ++dx) {
|
||||
const int px = x + dx, py = y + dy;
|
||||
if (px < 0 || py < 0 || px >= w || py >= h) continue;
|
||||
uint8_t* p = &bgra[(static_cast<size_t>(py) * w + px) * 4];
|
||||
p[0] = b;
|
||||
p[1] = g;
|
||||
p[2] = r;
|
||||
}
|
||||
}
|
||||
};
|
||||
if (!pose.image_mask.empty() && pose.image_mask_width == w &&
|
||||
pose.image_mask_height == h) {
|
||||
for (size_t i = 0; i < static_cast<size_t>(w) * h; ++i) {
|
||||
if (pose.image_mask[i] > 0.5f) {
|
||||
uint8_t* p = &bgra[i * 4];
|
||||
p[2] = static_cast<uint8_t>((p[2] + 255) / 2);
|
||||
}
|
||||
}
|
||||
}
|
||||
for (int i = 0; i < kBodyLandmarks; ++i) {
|
||||
const int x = static_cast<int>(pose.image_xy[i * 2]);
|
||||
const int y = static_cast<int>(pose.image_xy[i * 2 + 1]);
|
||||
if (pose.visibility[i] > 0.5f) {
|
||||
plot(x, y, 0, 255, 0);
|
||||
} else {
|
||||
plot(x, y, 0, 160, 255);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void WorkerLoop(std::wstring symlink, int req_w, int req_h) {
|
||||
Camera camera;
|
||||
if (!camera.open(symlink, req_w, req_h)) {
|
||||
SetStatus("ERROR: could not open camera");
|
||||
g_app.running = false;
|
||||
return;
|
||||
}
|
||||
|
||||
PoseEstimator estimator(ModelPath().c_str());
|
||||
PipelineConfig cfg;
|
||||
FramePipeline pipeline(g_app.shm_base, cfg);
|
||||
|
||||
Roi roi = PoseEstimator::bootstrap_roi(req_w, req_h);
|
||||
std::vector<uint8_t> bgra;
|
||||
std::vector<uint8_t> rgb;
|
||||
int w = 0, h = 0;
|
||||
uint32_t frame_number = 0;
|
||||
const LARGE_INTEGER t0_now = [] {
|
||||
LARGE_INTEGER x;
|
||||
QueryPerformanceCounter(&x);
|
||||
return x;
|
||||
}();
|
||||
LARGE_INTEGER freq;
|
||||
QueryPerformanceFrequency(&freq);
|
||||
|
||||
while (g_app.running) {
|
||||
if (!camera.read_frame(bgra, w, h)) {
|
||||
continue;
|
||||
}
|
||||
rgb.resize(static_cast<size_t>(w) * h * 3);
|
||||
for (size_t i = 0; i < static_cast<size_t>(w) * h; ++i) {
|
||||
rgb[i * 3 + 0] = bgra[i * 4 + 2];
|
||||
rgb[i * 3 + 1] = bgra[i * 4 + 1];
|
||||
rgb[i * 3 + 2] = bgra[i * 4 + 0];
|
||||
}
|
||||
|
||||
PoseResult pose = estimator.run(rgb.data(), w, h, roi);
|
||||
const bool tracked = pose.valid && pose.presence >= cfg.presence_threshold;
|
||||
roi = tracked ? pose.next_roi : PoseEstimator::bootstrap_roi(w, h);
|
||||
|
||||
LARGE_INTEGER now;
|
||||
QueryPerformanceCounter(&now);
|
||||
const int64_t ts_us = (now.QuadPart - t0_now.QuadPart) * 1000000 / freq.QuadPart;
|
||||
pipeline.publish(++frame_number, ts_us, tracked ? &pose : nullptr, bgra.data(), w, h);
|
||||
|
||||
std::vector<uint8_t> overlay = bgra;
|
||||
if (tracked) DrawOverlay(overlay, w, h, pose);
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_app.preview_mutex);
|
||||
g_app.preview_bgra.swap(overlay);
|
||||
g_app.preview_w = w;
|
||||
g_app.preview_h = h;
|
||||
g_app.status_text = tracked ? "Tracking (player detected)" : "No player detected";
|
||||
}
|
||||
}
|
||||
camera.close();
|
||||
}
|
||||
|
||||
bool StartShm() {
|
||||
PipelineConfig cfg;
|
||||
const size_t region = ShmPublisher::region_size(cfg.depth_width, cfg.depth_height,
|
||||
cfg.color_width, cfg.color_height);
|
||||
g_app.mapping = CreateFileMappingW(INVALID_HANDLE_VALUE, nullptr, PAGE_READWRITE,
|
||||
static_cast<DWORD>((region >> 32) & 0xffffffffu),
|
||||
static_cast<DWORD>(region & 0xffffffffu), kShmName);
|
||||
if (!g_app.mapping) return false;
|
||||
g_app.shm_base = static_cast<uint8_t*>(
|
||||
MapViewOfFile(g_app.mapping, FILE_MAP_ALL_ACCESS, 0, 0, region));
|
||||
return g_app.shm_base != nullptr;
|
||||
}
|
||||
|
||||
void StopShm() {
|
||||
if (g_app.shm_base) {
|
||||
UnmapViewOfFile(g_app.shm_base);
|
||||
g_app.shm_base = nullptr;
|
||||
}
|
||||
if (g_app.mapping) {
|
||||
CloseHandle(g_app.mapping);
|
||||
g_app.mapping = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void PopulateResolutions() {
|
||||
SendMessage(g_app.res_combo, CB_RESETCONTENT, 0, 0);
|
||||
const int sel = static_cast<int>(SendMessage(g_app.device_combo, CB_GETCURSEL, 0, 0));
|
||||
if (sel < 0 || sel >= static_cast<int>(g_app.devices.size())) return;
|
||||
g_app.resolutions = enumerate_resolutions(g_app.devices[sel].symbolic_link);
|
||||
for (const auto& r : g_app.resolutions) {
|
||||
wchar_t buf[64];
|
||||
wsprintfW(buf, L"%d x %d", r.width, r.height);
|
||||
SendMessageW(g_app.res_combo, CB_ADDSTRING, 0, reinterpret_cast<LPARAM>(buf));
|
||||
}
|
||||
if (!g_app.resolutions.empty()) SendMessage(g_app.res_combo, CB_SETCURSEL, 0, 0);
|
||||
}
|
||||
|
||||
void StartCapture() {
|
||||
const int dsel = static_cast<int>(SendMessage(g_app.device_combo, CB_GETCURSEL, 0, 0));
|
||||
const int rsel = static_cast<int>(SendMessage(g_app.res_combo, CB_GETCURSEL, 0, 0));
|
||||
if (dsel < 0 || rsel < 0 || rsel >= static_cast<int>(g_app.resolutions.size())) {
|
||||
SetStatus("Pick a camera and resolution first");
|
||||
return;
|
||||
}
|
||||
if (!StartShm()) {
|
||||
SetStatus("ERROR: shared memory create failed");
|
||||
return;
|
||||
}
|
||||
g_app.running = true;
|
||||
const CameraResolution res = g_app.resolutions[rsel];
|
||||
g_app.worker = std::thread(WorkerLoop, g_app.devices[dsel].symbolic_link, res.width,
|
||||
res.height);
|
||||
SetWindowTextA(g_app.start_btn, "Stop");
|
||||
SetStatus("Starting...");
|
||||
}
|
||||
|
||||
void StopCapture() {
|
||||
g_app.running = false;
|
||||
if (g_app.worker.joinable()) g_app.worker.join();
|
||||
StopShm();
|
||||
SetWindowTextA(g_app.start_btn, "Start");
|
||||
SetStatus("Stopped");
|
||||
}
|
||||
|
||||
void PaintPreview(HWND hwnd, HDC hdc) {
|
||||
std::lock_guard<std::mutex> lock(g_app.preview_mutex);
|
||||
if (g_app.preview_bgra.empty()) return;
|
||||
BITMAPINFO bmi{};
|
||||
bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
|
||||
bmi.bmiHeader.biWidth = g_app.preview_w;
|
||||
bmi.bmiHeader.biHeight = -g_app.preview_h;
|
||||
bmi.bmiHeader.biPlanes = 1;
|
||||
bmi.bmiHeader.biBitCount = 32;
|
||||
bmi.bmiHeader.biCompression = BI_RGB;
|
||||
RECT rc;
|
||||
GetClientRect(hwnd, &rc);
|
||||
const int top = 44;
|
||||
SetStretchBltMode(hdc, HALFTONE);
|
||||
StretchDIBits(hdc, 0, top, rc.right, rc.bottom - top, 0, 0, g_app.preview_w,
|
||||
g_app.preview_h, g_app.preview_bgra.data(), &bmi, DIB_RGB_COLORS,
|
||||
SRCCOPY);
|
||||
}
|
||||
|
||||
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) {
|
||||
switch (msg) {
|
||||
case WM_CREATE: {
|
||||
HINSTANCE inst = reinterpret_cast<LPCREATESTRUCT>(lparam)->hInstance;
|
||||
g_app.device_combo = CreateWindowW(L"COMBOBOX", nullptr,
|
||||
WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST,
|
||||
8, 8, 260, 300, hwnd,
|
||||
reinterpret_cast<HMENU>(kIdDevice), inst, nullptr);
|
||||
g_app.res_combo = CreateWindowW(L"COMBOBOX", nullptr,
|
||||
WS_CHILD | WS_VISIBLE | CBS_DROPDOWNLIST,
|
||||
276, 8, 150, 300, hwnd,
|
||||
reinterpret_cast<HMENU>(kIdResolution), inst, nullptr);
|
||||
g_app.start_btn = CreateWindowA("BUTTON", "Start", WS_CHILD | WS_VISIBLE,
|
||||
434, 8, 70, 26, hwnd,
|
||||
reinterpret_cast<HMENU>(kIdStart), inst, nullptr);
|
||||
g_app.status = CreateWindowA("STATIC", "Idle", WS_CHILD | WS_VISIBLE,
|
||||
512, 12, 300, 20, hwnd,
|
||||
reinterpret_cast<HMENU>(kIdStatus), inst, nullptr);
|
||||
g_app.devices = enumerate_cameras();
|
||||
for (const auto& d : g_app.devices) {
|
||||
SendMessageA(g_app.device_combo, CB_ADDSTRING, 0,
|
||||
reinterpret_cast<LPARAM>(d.name.c_str()));
|
||||
}
|
||||
if (!g_app.devices.empty()) {
|
||||
SendMessage(g_app.device_combo, CB_SETCURSEL, 0, 0);
|
||||
PopulateResolutions();
|
||||
}
|
||||
SetTimer(hwnd, 1, 33, nullptr);
|
||||
return 0;
|
||||
}
|
||||
case WM_COMMAND: {
|
||||
const int id = LOWORD(wparam);
|
||||
const int code = HIWORD(wparam);
|
||||
if (id == kIdDevice && code == CBN_SELCHANGE) {
|
||||
PopulateResolutions();
|
||||
} else if (id == kIdStart && code == BN_CLICKED) {
|
||||
if (g_app.running) {
|
||||
StopCapture();
|
||||
} else {
|
||||
StartCapture();
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
case WM_TIMER: {
|
||||
std::string st;
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(g_app.preview_mutex);
|
||||
st = g_app.status_text;
|
||||
}
|
||||
SetWindowTextA(g_app.status, st.c_str());
|
||||
InvalidateRect(hwnd, nullptr, FALSE);
|
||||
return 0;
|
||||
}
|
||||
case WM_PAINT: {
|
||||
PAINTSTRUCT ps;
|
||||
HDC hdc = BeginPaint(hwnd, &ps);
|
||||
PaintPreview(hwnd, hdc);
|
||||
EndPaint(hwnd, &ps);
|
||||
return 0;
|
||||
}
|
||||
case WM_DESTROY:
|
||||
StopCapture();
|
||||
PostQuitMessage(0);
|
||||
return 0;
|
||||
}
|
||||
return DefWindowProc(hwnd, msg, wparam, lparam);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int WINAPI wWinMain(HINSTANCE inst, HINSTANCE, PWSTR, int show) {
|
||||
CoInitializeEx(nullptr, COINIT_APARTMENTTHREADED);
|
||||
MFStartup(MF_VERSION);
|
||||
|
||||
WNDCLASSW wc{};
|
||||
wc.lpfnWndProc = WndProc;
|
||||
wc.hInstance = inst;
|
||||
wc.lpszClassName = L"KinectTrackerWnd";
|
||||
wc.hCursor = LoadCursor(nullptr, IDC_ARROW);
|
||||
wc.hbrBackground = reinterpret_cast<HBRUSH>(COLOR_WINDOW + 1);
|
||||
RegisterClassW(&wc);
|
||||
|
||||
g_app.main = CreateWindowW(L"KinectTrackerWnd", L"Kinect Tracker (native)",
|
||||
WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 860, 560,
|
||||
nullptr, nullptr, inst, nullptr);
|
||||
ShowWindow(g_app.main, show);
|
||||
UpdateWindow(g_app.main);
|
||||
|
||||
MSG msg;
|
||||
while (GetMessage(&msg, nullptr, 0, 0)) {
|
||||
TranslateMessage(&msg);
|
||||
DispatchMessage(&msg);
|
||||
}
|
||||
|
||||
MFShutdown();
|
||||
CoUninitialize();
|
||||
return 0;
|
||||
}
|
||||
Reference in new issue
Block a user