diff --git a/tracker-native/CMakeLists.txt b/tracker-native/CMakeLists.txt index 0044f62..56fe59c 100644 --- a/tracker-native/CMakeLists.txt +++ b/tracker-native/CMakeLists.txt @@ -33,7 +33,7 @@ target_compile_definitions(kinect-tracker PRIVATE UNICODE _UNICODE WIN32_LEAN_AN target_link_libraries(kinect-tracker PRIVATE "${ORT_DIR}/lib/onnxruntime.lib" - mfplat mf mfreadwrite mfuuid ole32 oleaut32 gdi32 user32 + mfplat mf mfreadwrite mfuuid ole32 oleaut32 gdi32 user32 windowscodecs comdlg32 uuid ) add_custom_command(TARGET kinect-tracker POST_BUILD diff --git a/tracker-native/build_win.sh b/tracker-native/build_win.sh index 570cd4a..2b07ab5 100755 --- a/tracker-native/build_win.sh +++ b/tracker-native/build_win.sh @@ -25,6 +25,7 @@ mkdir -p "${HERE}/build/dist" "${CORE}/synth/mask_stabilizer.cpp" "${CORE}/contract/contract.cpp" \ "${ORTW}/lib/onnxruntime.lib" \ -lmfplat -lmf -lmfreadwrite -lmfuuid -lole32 -loleaut32 -lgdi32 -luser32 \ + -lwindowscodecs -lcomdlg32 -luuid \ -mwindows -o "${HERE}/build/dist/kinect-tracker.exe" cp -f "${ORTW}/lib/onnxruntime.dll" "${HERE}/build/dist/" diff --git a/tracker-native/src/win_main.cpp b/tracker-native/src/win_main.cpp index 413ed4f..2ea42d8 100644 --- a/tracker-native/src/win_main.cpp +++ b/tracker-native/src/win_main.cpp @@ -1,6 +1,8 @@ #define WIN32_LEAN_AND_MEAN #include #include +#include +#include #include #include @@ -45,6 +47,9 @@ struct App { std::vector preview_bgra; int preview_w = 0; int preview_h = 0; + std::vector test_bgra; // decoded test image (BGRA), used in image mode + int test_w = 0; + int test_h = 0; std::string status_text = "Idle"; }; @@ -156,10 +161,59 @@ void DrawOverlay(std::vector& bgra, int w, int h, const PoseResult& pos } } -void WorkerLoop(std::wstring symlink, int req_w, int req_h) { +// Decode an image file (jpg/png/bmp) to top-down BGRA via WIC. +bool LoadImageBGRA(const wchar_t* path, std::vector& bgra, int& width, + int& height) { + IWICImagingFactory* factory = nullptr; + if (FAILED(CoCreateInstance(CLSID_WICImagingFactory, nullptr, + CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, + reinterpret_cast(&factory)))) { + return false; + } + bool ok = false; + IWICBitmapDecoder* decoder = nullptr; + if (SUCCEEDED(factory->CreateDecoderFromFilename( + path, nullptr, GENERIC_READ, WICDecodeMetadataCacheOnDemand, + &decoder))) { + IWICBitmapFrameDecode* frame = nullptr; + if (SUCCEEDED(decoder->GetFrame(0, &frame))) { + IWICFormatConverter* converter = nullptr; + if (SUCCEEDED(factory->CreateFormatConverter(&converter))) { + if (SUCCEEDED(converter->Initialize( + frame, GUID_WICPixelFormat32bppBGRA, WICBitmapDitherTypeNone, + nullptr, 0.0, WICBitmapPaletteTypeCustom))) { + UINT uw = 0, uh = 0; + converter->GetSize(&uw, &uh); + width = static_cast(uw); + height = static_cast(uh); + bgra.resize(static_cast(width) * height * 4); + if (SUCCEEDED(converter->CopyPixels(nullptr, width * 4, + static_cast(bgra.size()), + bgra.data()))) { + ok = true; + } + } + converter->Release(); + } + frame->Release(); + } + decoder->Release(); + } + factory->Release(); + return ok; +} + +void WorkerLoop(bool image_mode, std::wstring symlink, int req_w, int req_h) { CoInitializeEx(nullptr, COINIT_MULTITHREADED); Camera camera; - if (!camera.open(symlink, req_w, req_h)) { + std::vector source_bgra; + int src_w = 0, src_h = 0; + if (image_mode) { + source_bgra = g_app.test_bgra; + src_w = g_app.test_w; + src_h = g_app.test_h; + TraceLog("WorkerLoop: test-image mode %dx%d", src_w, src_h); + } else if (!camera.open(symlink, req_w, req_h)) { TraceLog("WorkerLoop: camera.open(%dx%d) FAILED", req_w, req_h); SetStatus("ERROR: could not open camera"); g_app.running = false; @@ -188,7 +242,12 @@ void WorkerLoop(std::wstring symlink, int req_w, int req_h) { QueryPerformanceFrequency(&freq); while (g_app.running) { - if (!camera.read_frame(bgra, w, h)) { + if (image_mode) { + bgra = source_bgra; + w = src_w; + h = src_h; + Sleep(33); + } else if (!camera.read_frame(bgra, w, h)) { continue; } // Mirror horizontally at the source so the preview and the published @@ -236,7 +295,7 @@ void WorkerLoop(std::wstring symlink, int req_w, int req_h) { g_app.status_text = tracked ? "Tracking (player detected)" : "No player detected"; } } - camera.close(); + if (!image_mode) camera.close(); CoUninitialize(); } @@ -295,8 +354,41 @@ void PopulateResolutions() { void StartCapture() { const int dsel = static_cast(SendMessage(g_app.device_combo, CB_GETCURSEL, 0, 0)); + if (dsel < 0) { + SetStatus("Pick a camera or test image first"); + return; + } + // The entry after the real cameras is the test-image option. + if (dsel == static_cast(g_app.devices.size())) { + wchar_t path[MAX_PATH] = L""; + OPENFILENAMEW ofn{}; + ofn.lStructSize = sizeof(ofn); + ofn.hwndOwner = g_app.main; + ofn.lpstrFilter = L"Images\0*.jpg;*.jpeg;*.png;*.bmp\0All files\0*.*\0"; + ofn.lpstrFile = path; + ofn.nMaxFile = MAX_PATH; + ofn.Flags = OFN_FILEMUSTEXIST | OFN_PATHMUSTEXIST; + if (!GetOpenFileNameW(&ofn)) { + SetStatus("No test image selected"); + return; + } + if (!LoadImageBGRA(path, g_app.test_bgra, g_app.test_w, g_app.test_h)) { + SetStatus("ERROR: could not load image"); + return; + } + if (!StartShm()) { + SetStatus("ERROR: shared memory create failed"); + return; + } + g_app.running = true; + TraceLog("Start: test image %dx%d", g_app.test_w, g_app.test_h); + g_app.worker = std::thread(WorkerLoop, true, std::wstring(), 0, 0); + SetWindowTextA(g_app.start_btn, "Stop"); + SetStatus("Starting (test image)..."); + return; + } const int rsel = static_cast(SendMessage(g_app.res_combo, CB_GETCURSEL, 0, 0)); - if (dsel < 0 || rsel < 0 || rsel >= static_cast(g_app.resolutions.size())) { + if (rsel < 0 || rsel >= static_cast(g_app.resolutions.size())) { SetStatus("Pick a camera and resolution first"); return; } @@ -308,8 +400,8 @@ void StartCapture() { const CameraResolution res = g_app.resolutions[rsel]; TraceLog("Start: device[%d]='%s' %dx%d", dsel, g_app.devices[dsel].name.c_str(), res.width, res.height); - g_app.worker = std::thread(WorkerLoop, g_app.devices[dsel].symbolic_link, res.width, - res.height); + g_app.worker = std::thread(WorkerLoop, false, g_app.devices[dsel].symbolic_link, + res.width, res.height); SetWindowTextA(g_app.start_btn, "Stop"); SetStatus("Starting..."); } @@ -406,6 +498,8 @@ LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam) { SendMessageA(g_app.device_combo, CB_ADDSTRING, 0, reinterpret_cast(d.name.c_str())); } + SendMessageA(g_app.device_combo, CB_ADDSTRING, 0, + reinterpret_cast("[ Test image file... ]")); if (!g_app.devices.empty()) { SendMessage(g_app.device_combo, CB_SETCURSEL, 0, 0); PopulateResolutions();