Skip to content

Commit

Permalink
Small code refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Nuzhny007 committed Nov 7, 2023
1 parent 2f24fbf commit 3fa5c73
Show file tree
Hide file tree
Showing 13 changed files with 68 additions and 39 deletions.
3 changes: 1 addition & 2 deletions example/examples.h
Original file line number Diff line number Diff line change
Expand Up @@ -981,8 +981,7 @@ class YoloTensorRTExample final : public VideoExample
std::string pathToModel = "../data/";
#endif

m_trackerSettings.m_embeddings.emplace_back(pathToModel + "reid/osnet_x0_25_msmt17.onnx",
pathToModel + "reid/osnet_x0_25_msmt17.onnx",
m_trackerSettings.m_embeddings.emplace_back(pathToModel + "reid/osnet_x0_25_msmt17.onnx", pathToModel + "reid/osnet_x0_25_msmt17.onnx",
cv::Size(128, 256),
std::vector<objtype_t>{ TypeConverter::Str2Type("person"), TypeConverter::Str2Type("car"), TypeConverter::Str2Type("bus"), TypeConverter::Str2Type("truck"), TypeConverter::Str2Type("vehicle") });

Expand Down
2 changes: 1 addition & 1 deletion src/Detector/MotionDetector.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
///
/// \brief The MotionDetector class
///
class MotionDetector : public BaseDetector
class MotionDetector final : public BaseDetector
{
public:
MotionDetector(BackgroundSubtract::BGFG_ALGS algType, const cv::UMat& gray);
Expand Down
3 changes: 2 additions & 1 deletion src/Detector/OCVDNNDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,7 @@ bool OCVDNNDetector::Init(const config_t& config)
std::string className;
for (; std::getline(classNamesFile, className); )
{
className.erase(className.find_last_not_of(" \t\n\r\f\v") + 1);
m_classNames.push_back(className);
}
if (!FillTypesMap(m_classNames))
Expand Down Expand Up @@ -315,7 +316,7 @@ void OCVDNNDetector::DetectInCrop(const cv::UMat& colorFrame, const cv::Rect& cr
}
else if (m_outLayerType == "Region")
{
for (size_t i = 0; i < detections.size(); ++i) //-V654 //-V621
for (size_t i = 0; i < detections.size(); ++i)
{
// Network produces output blob with a shape NxC where N is a number of detected objects and C is a number of classes + 4 where the first 4
// numbers are [center_x, center_y, width, height]
Expand Down
2 changes: 1 addition & 1 deletion src/Detector/OCVDNNDetector.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class OCVDNNDetector final : public BaseDetector
float m_inScaleFactor = 0.003921f;
float m_meanVal = 0.f;
float m_confidenceThreshold = 0.24f;
float m_nmsThreshold = 0.4f;
track_t m_nmsThreshold = static_cast<track_t>(0.4);
bool m_swapRB = false;
float m_maxCropRatio = 2.0f;
ModelType m_netType = ModelType::Unknown;
Expand Down
26 changes: 16 additions & 10 deletions src/Detector/YoloTensorRTDetector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ YoloTensorRTDetector::YoloTensorRTDetector(const cv::UMat& colorFrame)
"sheep", "sofa", "train", "tvmonitor" };

m_localConfig.calibration_image_list_file_txt = "";
m_localConfig.inference_precision = tensor_rt::FP32;
m_localConfig.inference_precision = tensor_rt::FP32;
m_localConfig.net_type = tensor_rt::YOLOV4;
m_localConfig.detect_thresh = 0.5f;
m_localConfig.gpu_id = 0;
Expand All @@ -38,7 +38,7 @@ YoloTensorRTDetector::YoloTensorRTDetector(const cv::Mat& colorFrame)
"sheep", "sofa", "train", "tvmonitor" };

m_localConfig.calibration_image_list_file_txt = "";
m_localConfig.inference_precision = tensor_rt::FP32;
m_localConfig.inference_precision = tensor_rt::FP32;
m_localConfig.net_type = tensor_rt::YOLOV4;
m_localConfig.detect_thresh = 0.5f;
m_localConfig.gpu_id = 0;
Expand Down Expand Up @@ -79,15 +79,15 @@ bool YoloTensorRTDetector::Init(const config_t& config)

auto inference_precision = config.find("inference_precision");
if (inference_precision != config.end())
{
std::map<std::string, tensor_rt::Precision> dictprecision;
dictprecision["INT8"] = tensor_rt::INT8;
dictprecision["FP16"] = tensor_rt::FP16;
dictprecision["FP32"] = tensor_rt::FP32;
auto precision = dictprecision.find(inference_precision->second);
if (precision != dictprecision.end())
{
std::map<std::string, tensor_rt::Precision> dictPrecision;
dictPrecision["INT8"] = tensor_rt::INT8;
dictPrecision["FP16"] = tensor_rt::FP16;
dictPrecision["FP32"] = tensor_rt::FP32;
auto precision = dictPrecision.find(inference_precision->second);
if (precision != dictPrecision.end())
m_localConfig.inference_precision = precision->second;
}
}

auto net_type = config.find("net_type");
if (net_type != config.end())
Expand Down Expand Up @@ -117,6 +117,7 @@ bool YoloTensorRTDetector::Init(const config_t& config)
std::string className;
for (; std::getline(classNamesFile, className); )
{
className.erase(className.find_last_not_of(" \t\n\r\f\v") + 1);
m_classNames.push_back(className);
}
if (!FillTypesMap(m_classNames))
Expand All @@ -125,6 +126,11 @@ bool YoloTensorRTDetector::Init(const config_t& config)
assert(0);
}
}
else
{
std::cout << "File with class names can not be opened!" << std::endl;
assert(0);
}
}

m_classesWhiteList.clear();
Expand Down
2 changes: 1 addition & 1 deletion src/Detector/YoloTensorRTDetector.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class YoloTensorRTDetector final : public BaseDetector
{
public:
YoloTensorRTDetector(const cv::UMat& colorFrame);
YoloTensorRTDetector(const cv::Mat& colorFrame);
YoloTensorRTDetector(const cv::Mat& colorFrame);
~YoloTensorRTDetector(void) = default;

bool Init(const config_t& config) override;
Expand Down
8 changes: 4 additions & 4 deletions src/Tracker/Ctracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ class CTracker final : public BaseTracker

void CreateDistaceMatrix(const regions_t& regions, const std::vector<RegionEmbedding>& regionEmbeddings, distMatrix_t& costMatrix, track_t maxPossibleCost, track_t& maxCost);
void UpdateTrackingState(const regions_t& regions, cv::UMat currFrame, float fps);
void CalcEmbeddins(std::vector<RegionEmbedding>& regionEmbeddings, const regions_t& regions, cv::UMat currFrame) const;
void CalcEmbeddins(std::vector<RegionEmbedding>& regionEmbeddings, const regions_t& regions, cv::UMat currFrame) const;

track_t GetEllipseDist(const CTrack& trackRef, const CRegion& reg);
track_t GetEllipseDist(const CTrack& trackRef, const CRegion& reg);
};
// ----------------------------------------------------------------------

Expand Down Expand Up @@ -78,7 +78,7 @@ CTracker::CTracker(const TrackerSettings& settings)
{
for (auto objType : embParam.m_objectTypes)
{
m_embCalculators.try_emplace(objType, embCalc);
m_embCalculators.try_emplace((objtype_t)objType, embCalc);
}
}
}
Expand Down Expand Up @@ -221,7 +221,7 @@ void CTracker::UpdateTrackingState(const regions_t& regions,
#endif
for (const auto& reg : regions)
{
DrawRRect(dbgAssignment, reg.m_rrect, cv::Scalar(0, 255, 255), 2);
DrawRRect(dbgAssignment, reg.m_rrect, cv::Scalar(0, 255, 255), 2);
}
}
#endif
Expand Down
3 changes: 3 additions & 0 deletions src/Tracker/EmbeddingsCalculator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class EmbeddingsCalculator
#else
m_net = cv::dnn::readNetFromTorch(weightsName);
#endif

std::cout << "Re-id model " << weightsName << " loaded: " << (!m_net.empty()) << std::endl;

if (!m_net.empty())
{
#if (((CV_VERSION_MAJOR == 4) && (CV_VERSION_MINOR >= 2)) || (CV_VERSION_MAJOR > 4))
Expand Down
24 changes: 12 additions & 12 deletions src/Tracker/TrackerSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,23 +207,23 @@ struct TrackerSettings
///
TrackerSettings()
{
m_distType[tracking::DistCenters] = 0.0f;
m_distType[tracking::DistRects] = 0.0f;
m_distType[tracking::DistJaccard] = 0.5f;
m_distType[tracking::DistHist] = 0.5f;
m_distType[tracking::DistFeatureCos] = 0.0f;
m_distType[tracking::DistMahalanobis] = 0.0f;
m_distType[tracking::DistCenters] = static_cast<track_t>(0.0);
m_distType[tracking::DistRects] = static_cast<track_t>(0.0);
m_distType[tracking::DistJaccard] = static_cast<track_t>(0.5);
m_distType[tracking::DistHist] = static_cast<track_t>(0.5);
m_distType[tracking::DistFeatureCos] = static_cast<track_t>(0.0);
m_distType[tracking::DistMahalanobis] = static_cast<track_t>(0.0);

assert(CheckDistance());
}

///
bool CheckDistance() const
{
track_t sum = std::accumulate(m_distType.begin(), m_distType.end(), 0.0f);
track_t maxOne = std::max(1.0f, std::fabs(sum));
track_t sum = std::accumulate(m_distType.begin(), m_distType.end(), static_cast<track_t>(0.0));
track_t maxOne = std::max(static_cast<track_t>(1.0), std::fabs(sum));
//std::cout << "CheckDistance: " << sum << " - " << (std::numeric_limits<track_t>::epsilon() * maxOne) << ", " << std::fabs(sum - 1.0f) << std::endl;
return std::fabs(sum - 1.0f) <= std::numeric_limits<track_t>::epsilon() * maxOne;
return std::fabs(sum - static_cast<track_t>(1.0)) <= std::numeric_limits<track_t>::epsilon() * maxOne;
}

///
Expand All @@ -249,7 +249,7 @@ struct TrackerSettings
}

///
void AddNearTypes(objtype_t type1, objtype_t type2, bool sym)
void AddNearTypes(objtype_t type1, objtype_t type2, bool sym)
{
auto AddOne = [&](objtype_t type1, objtype_t type2)
{
Expand All @@ -259,9 +259,9 @@ struct TrackerSettings
else
it->second.insert(type2);
};
AddOne(type1, type2);
AddOne(type1, type2);
if (sym)
AddOne(type2, type1);
AddOne(type2, type1);
}

///
Expand Down
10 changes: 8 additions & 2 deletions src/Tracker/track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,10 @@ CTrack::CTrack(const CRegion& region,
track_t CTrack::CalcDistCenter(const CRegion& reg) const
{
Point_t diff = m_predictionPoint - reg.m_rrect.center;
return sqrtf(sqr(diff.x) + sqr(diff.y));
if constexpr (sizeof(track_t) == sizeof(float))
return sqrtf(sqr(diff.x) + sqr(diff.y));
else
return sqrt(sqr(diff.x) + sqr(diff.y));
}

///
Expand All @@ -137,7 +140,10 @@ track_t CTrack::CalcDistRect(const CRegion& reg) const
{
dist += sqr(diff[i]);
}
return sqrtf(dist);
if constexpr (sizeof(track_t) == sizeof(float))
return sqrtf(dist);
else
return sqrt(dist);
}

///
Expand Down
17 changes: 13 additions & 4 deletions src/Tracker/trajectory.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ struct TrajectoryPoint

Point_t m_prediction;
Point_t m_raw;
bool m_hasRaw = false;
bool m_hasRaw = false;
};

///
Expand All @@ -63,9 +63,9 @@ struct TrajectoryPoint
class Trace
{
public:
///
Trace() = default;
///
///
Trace() = default;
///
Trace(const Trace&) = default;
///
Trace(Trace&&) = default;
Expand Down Expand Up @@ -105,6 +105,15 @@ class Trace
{
return m_trace[i];
}
///
/// \brief at
/// \param i
/// \return
///
TrajectoryPoint& at(size_t i)
{
return m_trace[i];
}

///
/// \brief size
Expand Down
2 changes: 1 addition & 1 deletion src/common/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class CRegion
objtype_t m_type = bad_type;
cv::RotatedRect m_rrect;
cv::Rect m_brect;
float m_confidence = -1;
track_t m_confidence = -1;
cv::Mat m_boxMask;

private:
Expand Down
5 changes: 5 additions & 0 deletions src/common/object_types.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ class TypeConverter
return true;
}

static size_t TypesCount()
{
return m_typeNames.size();
}

private:
static std::vector<std::string> m_typeNames;
static std::string m_badTypeName;
Expand Down

0 comments on commit 3fa5c73

Please sign in to comment.