Skip to content

Commit

Permalink
Fix bug with MouseExample
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuzhny007 committed Oct 6, 2023
1 parent 7dea766 commit 2f24fbf
Show file tree
Hide file tree
Showing 3 changed files with 3 additions and 130 deletions.
4 changes: 2 additions & 2 deletions example/MouseExample.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
127 changes: 0 additions & 127 deletions example/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<cv::dnn::MatShape> outputs;
std::vector<cv::dnn::MatShape> 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);
Expand All @@ -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<float>(1.f - mul.at<float>(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<int>("example");
int asyncPipeline = parser.get<int>("async");

Expand Down
2 changes: 1 addition & 1 deletion src/Tracker/Ctracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<track_t>(currFrame.cols * currFrame.rows);
const track_t maxPossibleCost = std::max(static_cast<track_t>(1.), static_cast<track_t>(currFrame.cols * currFrame.rows));
track_t maxCost = 0;
CreateDistaceMatrix(regions, regionEmbeddings, costMatrix, maxPossibleCost, maxCost);
#if DRAW_DBG_ASSIGNMENT
Expand Down

0 comments on commit 2f24fbf

Please sign in to comment.