1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-29 23:33:05 +04:00

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
This commit is contained in:
Gursimar Singh
2024-10-28 12:19:33 +05:30
committed by GitHub
parent 3b01a4d4e9
commit 331d327760
2 changed files with 17 additions and 6 deletions
+12 -4
View File
@@ -107,10 +107,11 @@ static void extractFeatures(vector<Mat> &imglist, Net &net, vector<Mat> &feature
Mat out=net.forward();
vector<int> 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<Mat>& 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."<<endl;
return -1;
}
Net net = readNetFromONNX(yoloPath);
Net net = readNetFromONNX(yoloPath, engine);
FontFace fontFace("sans");
@@ -342,6 +348,8 @@ int main(int argc, char **argv)
fontSize = min(fontSize, (stdSize*imgWidth)/stdImgSize);
fontWeight = min(fontWeight, (stdWeight*imgWidth)/stdImgSize);
}
detectedImages.clear();
galleryFeatures.clear();
yoloDetector(frame, net, detectedImages);
extractFeatures(detectedImages, reidNet, galleryFeatures);
+5 -2
View File
@@ -183,8 +183,11 @@ def main():
else:
args.yolo_model = findModel(args.yolo_model, args.yolo_sha1)
yolo_net = cv.dnn.readNet(args.yolo_model)
reid_net = cv.dnn.readNet(args.model)
engine = cv.dnn.ENGINE_AUTO
if args.backend != "default" or args.backend != "cpu":
engine = cv.dnn.ENGINE_CLASSIC
yolo_net = cv.dnn.readNetFromONNX(args.yolo_model, engine)
reid_net = cv.dnn.readNetFromONNX(args.model, engine)
reid_net.setPreferableBackend(get_backend_id(args.backend))
reid_net.setPreferableTarget(get_target_id(args.target))
cap = cv.VideoCapture(cv.samples.findFile(args.input) if args.input else 0)