From 331d327760e641f32f9813ddc9708addaf5d065e Mon Sep 17 00:00:00 2001 From: Gursimar Singh Date: Mon, 28 Oct 2024 12:19:33 +0530 Subject: [PATCH] Merge pull request #26336 from gursimarsingh:person_reid_bug_fix [BUG FIX] Fix issues in Person ReID C++ sample #26336 This PR fixes multiple issues in the Person ReID C++ sample that were causing incorrect outputs. It addresses improper matrix initialization, adds a missing return statement, and ensures that vectors are properly cleared before reuse. These changes correct the output of the sample. ### Pull Request Readiness Checklist See details at https://github.com/opencv/opencv/wiki/How_to_contribute#making-a-good-pull-request - [x] I agree to contribute to the project under Apache 2 License. - [x] To the best of my knowledge, the proposed patch is not based on a code under GPL or another license that is incompatible with OpenCV - [x] The PR is proposed to the proper branch - [x] There is a reference to the original bug report and related work - [x] There is accuracy test, performance test and test data in opencv_extra repository, if applicable Patch to opencv_extra has the same branch name. - [x] The feature is well documented and sample code can be built with the project CMake --- samples/dnn/person_reid.cpp | 16 ++++++++++++---- samples/dnn/person_reid.py | 7 +++++-- 2 files changed, 17 insertions(+), 6 deletions(-) diff --git a/samples/dnn/person_reid.cpp b/samples/dnn/person_reid.cpp index 506e7446f5..3f128df9f0 100644 --- a/samples/dnn/person_reid.cpp +++ b/samples/dnn/person_reid.cpp @@ -107,10 +107,11 @@ static void extractFeatures(vector &imglist, Net &net, vector &feature Mat out=net.forward(); vector s {out.size[0], out.size[1]}; out = out.reshape(1, s); - features.resize(out.rows); for (int i = 0; i < out.rows; i++) { - normalize(out.row(i), features[i], 1.0, 0.0, NORM_L2); + Mat norm_features; + normalize(out.row(i), norm_features, 1.0, 0.0, NORM_L2); + features.push_back(norm_features); } } return; @@ -212,6 +213,7 @@ static void yoloDetector(Mat &frame, Net &net, vector& images) images[i] = frame(roi); // Crop the region from the frame imgDict[images[i]] = roi; } + return; } int main(int argc, char **argv) @@ -256,7 +258,11 @@ int main(int argc, char **argv) int fontSize = 50; int fontWeight = 500; - Net reidNet = readNetFromONNX(modelPath); + EngineType engine = ENGINE_AUTO; + if (backend != "default" || target != "cpu"){ + engine = ENGINE_CLASSIC; + } + Net reidNet = readNetFromONNX(modelPath, engine); reidNet.setPreferableBackend(getBackendID(backend)); reidNet.setPreferableTarget(getTargetID(target)); @@ -264,7 +270,7 @@ int main(int argc, char **argv) cout<<"[ERROR] Please pass path to yolov8.onnx model file using --yolo_model."<