Skip to content

Commit

Permalink
OpenCV 5.0 alpha support
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuzhny007 committed Dec 25, 2024
1 parent 1a2b809 commit 8f279af
Show file tree
Hide file tree
Showing 14 changed files with 111 additions and 22 deletions.
10 changes: 10 additions & 0 deletions src/Detector/BaseDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,22 @@ std::unique_ptr<BaseDetector> BaseDetector::CreateDetector(tracking::Detectors d
break;

case tracking::Face_HAAR:
#if (CV_VERSION_MAJOR < 5)
detector = std::make_unique<FaceDetector>(frame);
#else
std::cerr << "Haar detector was removed from OpenCV 5.0" << std::endl;
CV_Assert(0);
#endif
break;

case tracking::Pedestrian_HOG:
case tracking::Pedestrian_C4:
#if (CV_VERSION_MAJOR < 5)
detector = std::make_unique<PedestrianDetector>(frame);
#else
std::cerr << "HOG detector was removed from OpenCV 5.0" << std::endl;
CV_Assert(0);
#endif
break;

#ifdef USE_OCV_DNN
Expand Down
4 changes: 4 additions & 0 deletions src/Detector/FaceDetector.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "FaceDetector.h"

#if (CV_VERSION_MAJOR < 5)

///
/// \brief FaceDetector::FaceDetector
/// \param gray
Expand Down Expand Up @@ -57,3 +59,5 @@ void FaceDetector::Detect(const cv::UMat& gray)
m_regions.push_back(rect);
}
}

#endif //(CV_VERSION_MAJOR < 5)
2 changes: 2 additions & 0 deletions src/Detector/FaceDetector.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "BaseDetector.h"

#if (CV_VERSION_MAJOR < 5)
///
/// \brief The FaceDetector class
///
Expand All @@ -24,3 +25,4 @@ class FaceDetector final : public BaseDetector
private:
cv::CascadeClassifier m_cascade;
};
#endif //(CV_VERSION_MAJOR < 5)
30 changes: 28 additions & 2 deletions src/Detector/OCVDNNDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,14 +48,23 @@ bool OCVDNNDetector::Init(const config_t& config)
dictTargets[cv::dnn::DNN_TARGET_MYRIAD] = "DNN_TARGET_MYRIAD";
dictTargets[cv::dnn::DNN_TARGET_CUDA] = "DNN_TARGET_CUDA";
dictTargets[cv::dnn::DNN_TARGET_CUDA_FP16] = "DNN_TARGET_CUDA_FP16";
#if (CV_VERSION_MAJOR > 4)
dictTargets[cv::dnn::DNN_TARGET_HDDL] = "DNN_TARGET_HDDL";
dictTargets[cv::dnn::DNN_TARGET_NPU] = "DNN_TARGET_NPU";
dictTargets[cv::dnn::DNN_TARGET_CPU_FP16] = "DNN_TARGET_CPU_FP16";
#endif

std::map<int, std::string> dictBackends;
dictBackends[cv::dnn::DNN_BACKEND_DEFAULT] = "DNN_BACKEND_DEFAULT";
dictBackends[cv::dnn::DNN_BACKEND_HALIDE] = "DNN_BACKEND_HALIDE";
dictBackends[cv::dnn::DNN_BACKEND_INFERENCE_ENGINE] = "DNN_BACKEND_INFERENCE_ENGINE";
dictBackends[cv::dnn::DNN_BACKEND_OPENCV] = "DNN_BACKEND_OPENCV";
dictBackends[cv::dnn::DNN_BACKEND_VKCOM] = "DNN_BACKEND_VKCOM";
dictBackends[cv::dnn::DNN_BACKEND_CUDA] = "DNN_BACKEND_CUDA";
#if (CV_VERSION_MAJOR > 4)
dictBackends[cv::dnn::DNN_BACKEND_WEBNN] = "DNN_BACKEND_WEBNN";
dictBackends[cv::dnn::DNN_BACKEND_TIMVX] = "DNN_BACKEND_TIMVX";
dictBackends[cv::dnn::DNN_BACKEND_CANN] = "DNN_BACKEND_CANN";
#endif
dictBackends[1000000] = "DNN_BACKEND_INFERENCE_ENGINE_NGRAPH";
dictBackends[1000000 + 1] = "DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019";

Expand Down Expand Up @@ -86,6 +95,12 @@ bool OCVDNNDetector::Init(const config_t& config)
targets["DNN_TARGET_CUDA"] = cv::dnn::DNN_TARGET_CUDA;
targets["DNN_TARGET_CUDA_FP16"] = cv::dnn::DNN_TARGET_CUDA_FP16;
#endif
#if (CV_VERSION_MAJOR > 4)
targets["DNN_TARGET_HDDL"] = cv::dnn::DNN_TARGET_HDDL;
targets["DNN_TARGET_NPU"] = cv::dnn::DNN_TARGET_NPU;
targets["DNN_TARGET_CPU_FP16"] = cv::dnn::DNN_TARGET_CPU_FP16;
#endif

std::cout << "Trying to set target " << dnnTarget->second << "... ";
auto target = targets.find(dnnTarget->second);
if (target != std::end(targets))
Expand All @@ -105,13 +120,18 @@ bool OCVDNNDetector::Init(const config_t& config)
{
std::map<std::string, cv::dnn::Backend> backends;
backends["DNN_BACKEND_DEFAULT"] = cv::dnn::DNN_BACKEND_DEFAULT;
backends["DNN_BACKEND_HALIDE"] = cv::dnn::DNN_BACKEND_HALIDE;
backends["DNN_BACKEND_INFERENCE_ENGINE"] = cv::dnn::DNN_BACKEND_INFERENCE_ENGINE;
backends["DNN_BACKEND_OPENCV"] = cv::dnn::DNN_BACKEND_OPENCV;
backends["DNN_BACKEND_VKCOM"] = cv::dnn::DNN_BACKEND_VKCOM;
#if (((CV_VERSION_MAJOR == 4) && (CV_VERSION_MINOR >= 2)) || (CV_VERSION_MAJOR > 4))
backends["DNN_BACKEND_CUDA"] = cv::dnn::DNN_BACKEND_CUDA;
#endif
#if (CV_VERSION_MAJOR > 4)
backends["DNN_BACKEND_WEBNN"] = cv::dnn::DNN_BACKEND_WEBNN;
backends["DNN_BACKEND_TIMVX"] = cv::dnn::DNN_BACKEND_TIMVX;
backends["DNN_BACKEND_CANN"] = cv::dnn::DNN_BACKEND_CANN;
#endif

std::cout << "Trying to set backend " << dnnBackend->second << "... ";
auto backend = backends.find(dnnBackend->second);
if (backend != std::end(backends))
Expand Down Expand Up @@ -209,9 +229,15 @@ bool OCVDNNDetector::Init(const config_t& config)
m_outLayers = m_net.getUnconnectedOutLayers();
m_outLayerType = m_net.getLayer(m_outLayers[0])->type;

#if (CV_VERSION_MAJOR < 5)
std::vector<cv::dnn::MatShape> outputs;
std::vector<cv::dnn::MatShape> internals;
m_net.getLayerShapes(cv::dnn::MatShape(), 0, outputs, internals);
#else
std::vector<cv::MatShape> outputs;
std::vector<cv::MatShape> internals;
m_net.getLayerShapes(cv::MatShape(), CV_32F, 0, outputs, internals);
#endif
std::cout << "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)
{
Expand Down
3 changes: 3 additions & 0 deletions src/Detector/PedestrianDetector.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "PedestrianDetector.h"
#include "nms.h"

#if (CV_VERSION_MAJOR < 5)

///
/// \brief PedestrianDetector::PedestrianDetector
/// \param gray
Expand Down Expand Up @@ -103,3 +105,4 @@ void PedestrianDetector::Detect(const cv::UMat& gray)
m_regions.push_back(rect);
}
}
#endif //(CV_VERSION_MAJOR < 5)
5 changes: 5 additions & 0 deletions src/Detector/PedestrianDetector.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#pragma once

#include "BaseDetector.h"

#if (CV_VERSION_MAJOR < 5)

#include "pedestrians/c4-pedestrian-detector.h"

///
Expand Down Expand Up @@ -47,3 +50,5 @@ class PedestrianDetector final : public BaseDetector
static const int HUMAN_xdiv = 9;
static const int HUMAN_ydiv = 4;
};

#endif //(CV_VERSION_MAJOR < 5)
2 changes: 1 addition & 1 deletion src/Detector/Subsense/DistanceUtils.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <opencv2/core/types_c.h>
//#include <opencv2/core/types_c.h>

//! computes the L1 distance between two integer values
template<typename T> static inline typename std::enable_if<std::is_integral<T>::value,size_t>::type L1dist(T a, T b) {
Expand Down
4 changes: 4 additions & 0 deletions src/Detector/Subsense/LBSP.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
#pragma once

#include <opencv2/core.hpp>
#if (CV_VERSION_MAJOR < 5)
#include <opencv2/features2d/features2d.hpp>
#else
#include <opencv2/features.hpp>
#endif //(CV_VERSION_MAJOR < 5)
#include "DistanceUtils.h"

/*!
Expand Down
17 changes: 16 additions & 1 deletion src/Tracker/EmbeddingsCalculator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,23 @@ class EmbeddingsCalculator
dictTargets[cv::dnn::DNN_TARGET_MYRIAD] = "DNN_TARGET_MYRIAD";
dictTargets[cv::dnn::DNN_TARGET_CUDA] = "DNN_TARGET_CUDA";
dictTargets[cv::dnn::DNN_TARGET_CUDA_FP16] = "DNN_TARGET_CUDA_FP16";
#if (CV_VERSION_MAJOR > 4)
dictTargets[cv::dnn::DNN_TARGET_HDDL] = "DNN_TARGET_HDDL";
dictTargets[cv::dnn::DNN_TARGET_NPU] = "DNN_TARGET_NPU";
dictTargets[cv::dnn::DNN_TARGET_CPU_FP16] = "DNN_TARGET_CPU_FP16";
#endif

std::map<int, std::string> dictBackends;
dictBackends[cv::dnn::DNN_BACKEND_DEFAULT] = "DNN_BACKEND_DEFAULT";
dictBackends[cv::dnn::DNN_BACKEND_HALIDE] = "DNN_BACKEND_HALIDE";
dictBackends[cv::dnn::DNN_BACKEND_INFERENCE_ENGINE] = "DNN_BACKEND_INFERENCE_ENGINE";
dictBackends[cv::dnn::DNN_BACKEND_OPENCV] = "DNN_BACKEND_OPENCV";
dictBackends[cv::dnn::DNN_BACKEND_VKCOM] = "DNN_BACKEND_VKCOM";
dictBackends[cv::dnn::DNN_BACKEND_CUDA] = "DNN_BACKEND_CUDA";
#if (CV_VERSION_MAJOR > 4)
dictBackends[cv::dnn::DNN_BACKEND_WEBNN] = "DNN_BACKEND_WEBNN";
dictBackends[cv::dnn::DNN_BACKEND_TIMVX] = "DNN_BACKEND_TIMVX";
dictBackends[cv::dnn::DNN_BACKEND_CANN] = "DNN_BACKEND_CANN";
#endif
dictBackends[1000000] = "DNN_BACKEND_INFERENCE_ENGINE_NGRAPH";
dictBackends[1000000 + 1] = "DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019";

Expand All @@ -63,9 +72,15 @@ class EmbeddingsCalculator
auto outLayers = m_net.getUnconnectedOutLayers();
auto outLayerType = m_net.getLayer(outLayers[0])->type;

#if (CV_VERSION_MAJOR < 5)
std::vector<cv::dnn::MatShape> outputs;
std::vector<cv::dnn::MatShape> internals;
m_net.getLayerShapes(cv::dnn::MatShape(), 0, outputs, internals);
#else
std::vector<cv::MatShape> outputs;
std::vector<cv::MatShape> internals;
m_net.getLayerShapes(cv::MatShape(), CV_32F, 0, outputs, internals);
#endif
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;
Expand Down
20 changes: 12 additions & 8 deletions src/Tracker/dat/dat_tracker.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
#include <opencv2/opencv.hpp>
#if (CV_VERSION_MAJOR < 5)
#include <opencv2/imgproc/imgproc_c.h>
#endif //(CV_VERSION_MAJOR < 5)

#include "dat_tracker.hpp"

///
Expand Down Expand Up @@ -42,19 +46,19 @@ void DAT_TRACKER::Initialize(const cv::Mat &im, cv::Rect region)
case 1: //1rgb
if (img.channels() == 1)
{
cv::cvtColor(img, img, CV_GRAY2BGR);
cv::cvtColor(img, img, cv::COLOR_GRAY2BGR);
}
break;
case 2: //2lab
cv::cvtColor(img, img, CV_BGR2Lab);
cv::cvtColor(img, img, cv::COLOR_BGR2Lab);
break;
case 3: //3hsv
cv::cvtColor(img, img, CV_BGR2HSV);
cv::cvtColor(img, img, cv::COLOR_BGR2HSV);
break;
case 4: //4gray
if (img.channels() == 3)
{
cv::cvtColor(img, img, CV_BGR2GRAY);
cv::cvtColor(img, img, cv::COLOR_BGR2GRAY);
}
break;
default:
Expand Down Expand Up @@ -95,23 +99,23 @@ cv::RotatedRect DAT_TRACKER::Update(const cv::Mat &im, float& confidence)
case 1://1rgb
if (img_preprocessed.channels() == 1)
{
cv::cvtColor(img_preprocessed, img, CV_GRAY2BGR);
cv::cvtColor(img_preprocessed, img, cv::COLOR_GRAY2BGR);
}
else
{
img_preprocessed.copyTo(img);
}
break;
case 2://2lab
cv::cvtColor(img_preprocessed, img, CV_BGR2Lab);
cv::cvtColor(img_preprocessed, img, cv::COLOR_BGR2Lab);
break;
case 3://3hsv
cv::cvtColor(img_preprocessed, img, CV_BGR2HSV);
cv::cvtColor(img_preprocessed, img, cv::COLOR_BGR2HSV);
break;
case 4://4gray
if (img_preprocessed.channels() == 3)
{
cv::cvtColor(img_preprocessed, img, CV_BGR2GRAY);
cv::cvtColor(img_preprocessed, img, cv::COLOR_BGR2GRAY);
}
break;
default:
Expand Down
2 changes: 2 additions & 0 deletions src/Tracker/dat/dat_tracker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#if (CV_VERSION_MAJOR < 5)
#include <opencv2/features2d/features2d.hpp>
#endif //(CV_VERSION_MAJOR < 5)

#include "../VOTTracker.hpp"

Expand Down
2 changes: 2 additions & 0 deletions src/Tracker/staple/staple_tracker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#if (CV_VERSION_MAJOR < 5)
#include <opencv2/features2d/features2d.hpp>
#endif //(CV_VERSION_MAJOR < 5)

#include "../VOTTracker.hpp"

Expand Down
10 changes: 8 additions & 2 deletions src/Tracker/track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ std::pair<track_t, bool> CTrack::CalcCosine(const RegionEmbedding& embedding) co
//assert(0);
//CV_Assert(!embedding.m_embedding.empty());
//CV_Assert(!m_regionEmbedding.m_embedding.empty());
return { 0, false };
return { (track_t)0, false };
}
}

Expand Down Expand Up @@ -486,7 +486,8 @@ bool CTrack::CheckStatic(int trajLen, cv::UMat currFrame, const CRegion& region,
track_t speed = sqrt(sqr(velocity[0]) + sqr(velocity[1]));

bool inCenter = true;
cv::Rect centerROI(m_trace[m_trace.size() - trajLen].x - region.m_brect.width / 2, m_trace[m_trace.size() - trajLen].y - region.m_brect.height / 2, region.m_brect.width, region.m_brect.height);
cv::Rect centerROI(cvRound(m_trace[m_trace.size() - trajLen].x) - region.m_brect.width / 2,
cvRound(m_trace[m_trace.size() - trajLen].y) - region.m_brect.height / 2, region.m_brect.width, region.m_brect.height);
for (size_t i = m_trace.size() - trajLen; i < m_trace.size() - 1; ++i)
{
if (!centerROI.contains(m_trace[i]))
Expand Down Expand Up @@ -1053,13 +1054,18 @@ void CTrack::CreateExternalTracker(int channels)
#ifdef USE_OCV_KCF
if (!m_tracker || m_tracker.empty())
{
#if (CV_VERSION_MAJOR < 5)
cv::TrackerGOTURN::Params params;

#if (((CV_VERSION_MAJOR == 3) && (CV_VERSION_MINOR >= 3)) || (CV_VERSION_MAJOR > 3))
m_tracker = cv::TrackerGOTURN::create(params);
#else
m_tracker = cv::TrackerGOTURN::createTracker(params);
#endif
#else
std::cerr << "TrackerGOTURN not supported in OpenCV 5.0 and newer!" << std::endl;
CV_Assert(0);
#endif //(CV_VERSION_MAJOR < 5)
}
#endif
if (m_VOTTracker)
Expand Down
22 changes: 14 additions & 8 deletions thirdparty/ruclip/RuCLIPProcessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,20 @@ class RuCLIPProcessor
inline torch::Tensor Relevancy(torch::Tensor embeds, torch::Tensor positives, torch::Tensor negatives)
{
auto embeds2 = torch::cat({ positives, negatives });
auto logits = /*scale * */torch::mm(embeds, embeds2.t()); //[batch_size x phrases]
auto positive_vals = logits.index({ "...", torch::indexing::Slice(0, 1) }); // [batch_size x 1]
auto logits = /*scale * */torch::mm(embeds, embeds2.t()); // [batch_size x phrases]
auto positive_vals = logits.index({ "...", torch::indexing::Slice(0, 1) }); // [batch_size x 1]
auto negative_vals = logits.index({ "...", torch::indexing::Slice(1, torch::indexing::None) }); // [batch_size x negative_phrase_n]
auto repeated_pos = positive_vals.repeat({ 1, negatives.sizes()[0] }); //[batch_size x negative_phrase_n]
auto sims = torch::stack({ repeated_pos, negative_vals }, -1); //[batch_size x negative_phrase_n x 2]
auto smx = torch::softmax(10 * sims, -1); // [batch_size x negative_phrase_n x 2]
auto best_id = smx.index({ "...", 0 }).argmin(1); // [batch_size x 2]
auto result = torch::gather(smx, 1, best_id.index({ "...", torch::indexing::None, torch::indexing::None }).expand({ best_id.sizes()[0], negatives.sizes()[0], 2 })
).index({ torch::indexing::Slice(), 0, torch::indexing::Slice() });// [batch_size x 2]
auto repeated_pos = positive_vals.repeat({ 1, negatives.sizes()[0] }); // [batch_size x negative_phrase_n]
auto sims = torch::stack({ repeated_pos, negative_vals }, -1); // [batch_size x negative_phrase_n x 2]
auto smx = torch::softmax(10 * sims, -1); // [batch_size x negative_phrase_n x 2]
auto best_id = smx.index({ "...", 0 }).argmin(1); // [batch_size x 2]
auto result = torch::gather(smx, 1, best_id.index({
"...", torch::indexing::None, torch::indexing::None
}).expand(
{ best_id.sizes()[0], negatives.sizes()[0], 2
})
).index(
{ torch::indexing::Slice(), 0, torch::indexing::Slice()
});// [batch_size x 2]
return result;
}

0 comments on commit 8f279af

Please sign in to comment.