From 2f24fbf3d587bb902f7ed42fab603e7c58bb4695 Mon Sep 17 00:00:00 2001 From: Nuzhny007 Date: Sat, 7 Oct 2023 00:37:39 +0300 Subject: [PATCH] Fix bug with MouseExample --- example/MouseExample.h | 4 +- example/main.cpp | 127 --------------------------------------- src/Tracker/Ctracker.cpp | 2 +- 3 files changed, 3 insertions(+), 130 deletions(-) diff --git a/example/MouseExample.h b/example/MouseExample.h index 0be86509..14fe49f5 100644 --- a/example/MouseExample.h +++ b/example/MouseExample.h @@ -37,8 +37,8 @@ void MouseTracking(cv::CommandLineParser parser) cv::Mat frame = cv::Mat(800, 800, CV_8UC3); - if (!writer.isOpened()) - writer.open(outFile, cv::VideoWriter::fourcc('P', 'I', 'M', '1'), 20, frame.size(), true); + if (!outFile.empty() && !writer.isOpened()) + writer.open(outFile, cv::VideoWriter::fourcc('h', '2', '6', '4'), 20, frame.size(), true); // Set mouse callback cv::Point2f pointXY; diff --git a/example/main.cpp b/example/main.cpp index 4c385c1d..1fdf343a 100644 --- a/example/main.cpp +++ b/example/main.cpp @@ -43,100 +43,6 @@ const char* keys = // ---------------------------------------------------------------------- -#pragma once - -/// -/// \brief The EmbeddingsCalculator class -/// -class EmbeddingsCalculatorSimple -{ -public: - EmbeddingsCalculatorSimple() = default; - virtual ~EmbeddingsCalculatorSimple() = default; - - /// - bool Initialize(const std::string& cfgName, const std::string& weightsName, const cv::Size& inputLayer) - { - m_inputLayer = inputLayer; - -#if 1 - m_net = cv::dnn::readNet(weightsName); -#else - m_net = cv::dnn::readNetFromTorch(weightsName); -#endif - if (!m_net.empty()) - { - //m_net.setPreferableBackend(cv::dnn::DNN_BACKEND_INFERENCE_ENGINE); - //m_net.setPreferableTarget(cv::dnn::DNN_TARGET_CPU); - - auto outNames = m_net.getUnconnectedOutLayersNames(); - auto outLayers = m_net.getUnconnectedOutLayers(); - auto outLayerType = m_net.getLayer(outLayers[0])->type; - - std::vector outputs; - std::vector internals; - m_net.getLayerShapes(cv::dnn::MatShape(), 0, outputs, internals); - std::cout << "REID: getLayerShapes: outputs (" << outputs.size() << ") = " << (outputs.size() > 0 ? outputs[0].size() : 0) << ", internals (" << internals.size() << ") = " << (internals.size() > 0 ? internals[0].size() : 0) << std::endl; - if (outputs.size() && outputs[0].size() > 3) - std::cout << "outputs = [" << outputs[0][0] << ", " << outputs[0][1] << ", " << outputs[0][2] << ", " << outputs[0][3] << "], internals = [" << internals[0][0] << ", " << internals[0][1] << ", " << internals[0][2] << ", " << internals[0][3] << "]" << std::endl; - } - return !m_net.empty(); - } - - /// - bool IsInitialized() const - { - return !m_net.empty(); - } - - /// - cv::Mat Calc(const cv::Mat& img, cv::Rect rect) - { - auto Clamp = [](int& v, int& size, int hi) -> int - { - int res = 0; - if (v < 0) - { - res = v; - v = 0; - return res; - } - else if (v + size > hi - 1) - { - res = v; - v = hi - 1 - size; - if (v < 0) - { - size += v; - v = 0; - } - res -= v; - return res; - } - return res; - }; - Clamp(rect.x, rect.width, img.cols); - Clamp(rect.y, rect.height, img.rows); - - cv::Mat obj; - cv::resize(img(rect), obj, m_inputLayer, 0., 0., cv::INTER_CUBIC); - cv::Mat blob = cv::dnn::blobFromImage(obj, 1.0 / 255.0, cv::Size(), cv::Scalar(), false, false, CV_32F); - - m_net.setInput(blob); - cv::Mat embedding; - std::cout << "embedding: " << embedding.size() << ", chans = " << embedding.channels() << std::endl; - //std::cout << "orig: " << embedding << std::endl; - cv::normalize(m_net.forward(), embedding); - //std::cout << "normalized: " << embedding << std::endl; - return embedding; - } - -private: - cv::dnn::Net m_net; - cv::Size m_inputLayer{ 128, 256 }; -}; - - int main(int argc, char** argv) { cv::CommandLineParser parser(argc, argv, keys); @@ -148,39 +54,6 @@ int main(int argc, char** argv) cv::ocl::setUseOpenCL(useOCL); std::cout << (cv::ocl::useOpenCL() ? "OpenCL is enabled" : "OpenCL not used") << std::endl; -#if 0 - EmbeddingsCalculatorSimple ec; - ec.Initialize("C:/work/home/mtracker/tmp/reid/models/osnet_x0_25_msmt17.onnx", - "C:/work/home/mtracker/tmp/reid/models/osnet_x0_25_msmt17.onnx", - cv::Size(128, 256)); - std::cout << "ec.IsInitialized(): " << ec.IsInitialized() << std::endl; - - cv::Mat img = cv::imread("C:/work/home/mtracker/Multitarget-tracker/build/Release/vlcsnap-2023-10-06-17h31m54s413.png"); - cv::Rect r1(564, 526, 124, 260); - //cv::Rect r2(860, 180, 48, 160); - cv::Rect r2(560, 522, 132, 264); - - cv::Mat e1 = ec.Calc(img, r1); - cv::Mat e2 = ec.Calc(img, r2); - - //cv::Mat mul = e1 * e2.t(); - std::cout << "e1: " << e1 << std::endl; - std::cout << "e2: " << e2 << std::endl; - cv::Mat diff; - cv::absdiff(e1, e2, diff); - cv::Scalar ss = cv::sum(diff); - cv::Mat mul = e1 * e2.t(); - float res = static_cast(1.f - mul.at(0, 0)); - std::cout << "mul = " << mul << ", sum = " << ss << ", res = " << res << std::endl; - - cv::rectangle(img, r1, cv::Scalar(255, 0, 255)); - cv::rectangle(img, r2, cv::Scalar(255, 0, 0)); - cv::imshow("img", img); - cv::waitKey(0); - - return 0; -#endif - int exampleNum = parser.get("example"); int asyncPipeline = parser.get("async"); diff --git a/src/Tracker/Ctracker.cpp b/src/Tracker/Ctracker.cpp index 0fea8ca5..f93b8d1a 100644 --- a/src/Tracker/Ctracker.cpp +++ b/src/Tracker/Ctracker.cpp @@ -233,7 +233,7 @@ void CTracker::UpdateTrackingState(const regions_t& regions, std::cout << "CTracker::UpdateTrackingState: Distance matrix between all tracks to all regions" << std::endl; #endif distMatrix_t costMatrix(N * M); - const track_t maxPossibleCost = static_cast(currFrame.cols * currFrame.rows); + const track_t maxPossibleCost = std::max(static_cast(1.), static_cast(currFrame.cols * currFrame.rows)); track_t maxCost = 0; CreateDistaceMatrix(regions, regionEmbeddings, costMatrix, maxPossibleCost, maxCost); #if DRAW_DBG_ASSIGNMENT