From cdc3c469327d770552b1dcc4b297a1b444b7ef5d Mon Sep 17 00:00:00 2001 From: tian <11429339@qq.com> Date: Sat, 14 Mar 2026 14:41:21 +0800 Subject: [PATCH] Keep high-res frames for two-stage shoe ROI --- configs/sample_person_shoe_two_stage.json | 4 +- plugins/ai_yolo/ai_yolo_node.cpp | 66 +++++++++++++++++++---- 2 files changed, 57 insertions(+), 13 deletions(-) diff --git a/configs/sample_person_shoe_two_stage.json b/configs/sample_person_shoe_two_stage.json index 08554a7..773b4df 100644 --- a/configs/sample_person_shoe_two_stage.json +++ b/configs/sample_person_shoe_two_stage.json @@ -24,8 +24,8 @@ "type": "preprocess", "role": "filter", "enable": true, - "dst_w": 640, - "dst_h": 640, + "dst_w": 1920, + "dst_h": 1080, "dst_format": "rgb", "dst_packed": true, "resize_mode": "stretch", diff --git a/plugins/ai_yolo/ai_yolo_node.cpp b/plugins/ai_yolo/ai_yolo_node.cpp index 881504c..8b5ed0e 100644 --- a/plugins/ai_yolo/ai_yolo_node.cpp +++ b/plugins/ai_yolo/ai_yolo_node.cpp @@ -817,6 +817,38 @@ private: } #if defined(RK3588_ENABLE_RKNN) + void ResizeRgbBilinear(const uint8_t* src, int src_w, int src_h, int src_stride, + uint8_t* dst, int dst_w, int dst_h, int dst_stride) { + const float scale_x = static_cast(src_w) / static_cast(dst_w); + const float scale_y = static_cast(src_h) / static_cast(dst_h); + + for (int y = 0; y < dst_h; ++y) { + const float fy = static_cast(y) * scale_y; + const int y0 = static_cast(fy); + const int y1 = std::min(y0 + 1, src_h - 1); + const float dy = fy - static_cast(y0); + + for (int x = 0; x < dst_w; ++x) { + const float fx = static_cast(x) * scale_x; + const int x0 = static_cast(fx); + const int x1 = std::min(x0 + 1, src_w - 1); + const float dx = fx - static_cast(x0); + + for (int c = 0; c < 3; ++c) { + const float v00 = src[y0 * src_stride + x0 * 3 + c]; + const float v01 = src[y0 * src_stride + x1 * 3 + c]; + const float v10 = src[y1 * src_stride + x0 * 3 + c]; + const float v11 = src[y1 * src_stride + x1 * 3 + c]; + const float v = v00 * (1.0f - dx) * (1.0f - dy) + + v01 * dx * (1.0f - dy) + + v10 * (1.0f - dx) * dy + + v11 * dx * dy; + dst[y * dst_stride + x * 3 + c] = static_cast(v); + } + } + } + } + void RunInference(FramePtr frame) { if (!frame->data || frame->data_size == 0) return; @@ -837,9 +869,12 @@ private: if (!src || src_stride <= 0) return; InferInput input; - if (static_cast(src_stride) == packed_row && frame->data_size >= packed_size) { + const bool exact_model_input = (w == model_input_w_ && h == model_input_h_); + if (exact_model_input && static_cast(src_stride) == packed_row && frame->data_size >= packed_size) { input.data = src; input.size = packed_size; + input.width = w; + input.height = h; // Best-effort RKNN DMA-BUF zero-copy path. if (frame->DmaFd() >= 0 && frame->data) { @@ -858,18 +893,27 @@ private: } if (frame->DmaFd() >= 0) frame->SyncStart(); - rgb_tmp_.resize(packed_size); - for (int y = 0; y < h; ++y) { - memcpy(rgb_tmp_.data() + static_cast(y) * packed_row, - src + static_cast(y) * static_cast(src_stride), - packed_row); + const size_t input_row = static_cast(model_input_w_) * 3; + const size_t input_size = input_row * static_cast(model_input_h_); + resized_input_.resize(input_size); + + if (exact_model_input) { + for (int y = 0; y < h; ++y) { + memcpy(resized_input_.data() + static_cast(y) * input_row, + src + static_cast(y) * static_cast(src_stride), + input_row); + } + } else { + ResizeRgbBilinear(src, w, h, src_stride, + resized_input_.data(), model_input_w_, model_input_h_, + static_cast(input_row)); } if (frame->DmaFd() >= 0) frame->SyncEnd(); - input.data = rgb_tmp_.data(); - input.size = packed_size; + input.data = resized_input_.data(); + input.size = input_size; + input.width = model_input_w_; + input.height = model_input_h_; } - input.width = w; - input.height = h; input.is_nhwc = true; auto result = infer_backend_->InferBorrowed(model_handle_, input); @@ -1219,7 +1263,7 @@ private: #if defined(RK3588_ENABLE_RKNN) ModelHandle model_handle_ = kInvalidModelHandle; uint32_t n_output_ = 0; - std::vector rgb_tmp_; + std::vector resized_input_; std::vector fp32_buffer_; // For FP16 to FP32 conversion #endif };