1
0
mirror of https://github.com/opencv/opencv.git synced 2026-07-30 15:53:03 +04:00

Merge pull request #25771 from fengyuentau:vittrack_black_input

video: fix vittrack in the case where crop size grows until out-of-memory when the input is black #25771

Fixes https://github.com/opencv/opencv/issues/25760

### 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:
Yuantao Feng
2024-06-18 17:48:28 +08:00
committed by GitHub
parent 0fac5d52bc
commit e3884a9ea8
4 changed files with 57 additions and 50 deletions
+21 -14
View File
@@ -16,6 +16,7 @@ const char *keys =
"{ help h | | Print help message }"
"{ input i | | Full path to input video folder, the specific camera index. (empty for camera 0) }"
"{ net | vitTracker.onnx | Path to onnx model of vitTracker.onnx}"
"{ tracking_score_threshold t | 0.3 | Tracking score threshold. If a bbox of score >= 0.3, it is considered as found }"
"{ backend | 0 | Choose one of computation backends: "
"0: automatically (by default), "
"1: Halide language (http://halide-lang.org/), "
@@ -49,6 +50,7 @@ int run(int argc, char** argv)
std::string net = parser.get<String>("net");
int backend = parser.get<int>("backend");
int target = parser.get<int>("target");
float tracking_score_threshold = parser.get<float>("tracking_score_threshold");
Ptr<TrackerVit> tracker;
try
@@ -57,6 +59,7 @@ int run(int argc, char** argv)
params.net = samples::findFile(net);
params.backend = backend;
params.target = target;
params.tracking_score_threshold = tracking_score_threshold;
tracker = TrackerVit::create(params);
}
catch (const cv::Exception& ee)
@@ -108,6 +111,11 @@ int run(int argc, char** argv)
Rect selectRect = selectROI(winName, image_select);
std::cout << "ROI=" << selectRect << std::endl;
if (selectRect.empty())
{
std::cerr << "Invalid ROI!" << std::endl;
return 2;
}
tracker->init(image, selectRect);
@@ -130,30 +138,29 @@ int run(int argc, char** argv)
float score = tracker->getTrackingScore();
std::cout << "frame " << count <<
": predicted score=" << score <<
" rect=" << rect <<
" time=" << tickMeter.getTimeMilli() << "ms" <<
std::endl;
std::cout << "frame " << count;
if (ok) {
std::cout << ": predicted score=" << score <<
"\trect=" << rect <<
"\ttime=" << tickMeter.getTimeMilli() << "ms" << std::endl;
Mat render_image = image.clone();
if (ok)
{
rectangle(render_image, rect, Scalar(0, 255, 0), 2);
rectangle(image, rect, Scalar(0, 255, 0), 2);
std::string timeLabel = format("Inference time: %.2f ms", tickMeter.getTimeMilli());
std::string scoreLabel = format("Score: %f", score);
putText(render_image, timeLabel, Point(0, 15), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 255, 0));
putText(render_image, scoreLabel, Point(0, 35), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 255, 0));
putText(image, timeLabel, Point(0, 15), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 255, 0));
putText(image, scoreLabel, Point(0, 35), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 255, 0));
} else {
std::cout << ": target lost" << std::endl;
putText(image, "Target lost", Point(0, 15), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 255));
}
imshow(winName, render_image);
imshow(winName, image);
tickMeter.reset();
int c = waitKey(1);
if (c == 27 /*ESC*/)
if (c == 27 /*ESC*/ || c == 'q' || c == 'Q')
break;
}