diff --git a/README.md b/README.md index b99a0cc4..88adfac7 100644 --- a/README.md +++ b/README.md @@ -6,6 +6,7 @@ ## Latest Features +- Add YOLOE detection support [THU-MIG/yoloe](https://github.com/THU-MIG/yoloe) - Add new SOTA: YOLOv26, YOLOv26-obb and YOLOv26-seg models from [ultralytics/ultralytics](https://github.com/ultralytics/ultralytics) - Add RT-DETRv4 (API similar D-FINE) detection model [RT-DETRs/RT-DETRv4](https://github.com/RT-DETRs/RT-DETRv4) - Add D-FINE seg detection model [ArgoHA/D-FINE-seg](https://github.com/ArgoHA/D-FINE-seg) diff --git a/data/settings_yoloe_seg.ini b/data/settings_yoloe_seg.ini new file mode 100644 index 00000000..0fce832e --- /dev/null +++ b/data/settings_yoloe_seg.ini @@ -0,0 +1,141 @@ +[detection] + +#----------------------------- +# opencv_dnn = 6 +# tensorrt = 5 +detector_backend = 5 + +#----------------------------- +# Target and backend for opencv_dnn detector +# DNN_TARGET_CPU +# DNN_TARGET_OPENCL +# DNN_TARGET_OPENCL_FP16 +# DNN_TARGET_MYRIAD +# DNN_TARGET_CUDA +# DNN_TARGET_CUDA_FP16 +ocv_dnn_target = DNN_TARGET_CPU + +# DNN_BACKEND_DEFAULT +# DNN_BACKEND_HALIDE +# DNN_BACKEND_INFERENCE_ENGINE +# DNN_BACKEND_OPENCV +# DNN_BACKEND_VKCOM +# DNN_BACKEND_CUDA +# DNN_BACKEND_INFERENCE_ENGINE_NGRAPH +# DNN_BACKEND_INFERENCE_ENGINE_NN_BUILDER_2019 +ocv_dnn_backend = DNN_BACKEND_OPENCV + +#----------------------------- +nn_weights = C:/work/home/mtracker/Multitarget-tracker/data/yoloe-11m-seg.onnx +nn_config = C:/work/home/mtracker/Multitarget-tracker/data/yoloe-11m-seg.onnx +class_names = C:/work/home/mtracker/Multitarget-tracker/data/coco/coco.names + +#----------------------------- +confidence_threshold = 0.3 + +max_crop_ratio = 0 +max_batch = 1 +gpu_id = 0 + +#----------------------------- +# YOLOV3 +# YOLOV4 +# YOLOV5 +net_type = YOLOEMask + +#----------------------------- +# INT8 +# FP16 +# FP32 +inference_precision = FP16 + + +[tracking] + +#----------------------------- +# DistCenters = 0 // Euclidean distance between centers, pixels +# DistRects = 1 // Euclidean distance between bounding rectangles, pixels +# DistJaccard = 2 // Intersection over Union, IoU, [0, 1] +# DistHist = 3 // Bhatacharia distance between histograms, [0, 1] + +distance_type = 0 + +#----------------------------- +# KalmanLinear = 0 +# KalmanUnscented = 1 + +kalman_type = 0 + +#----------------------------- +# FilterCenter = 0 +# FilterRect = 1 +# FilterRRect = 2 + +filter_goal = 0 + +#----------------------------- +# TrackNone = 0 +# TrackKCF = 1 +# TrackMIL = 2 +# TrackMedianFlow = 3 +# TrackGOTURN = 4 +# TrackMOSSE = 5 +# TrackCSRT = 6 +# TrackDAT = 7 +# TrackSTAPLE = 8 +# TrackLDES = 9 +# TrackDaSiamRPN = 10 +# Used if filter_goal == FilterRect + +lost_track_type = 0 + +#----------------------------- +# MatchHungrian = 0 +# MatchBipart = 1 + +match_type = 0 + +#----------------------------- +# Use constant acceleration motion model: +# 0 - unused (stable) +# 1 - use acceleration in Kalman filter (experimental) +use_aceleration = 0 + +#----------------------------- +# Delta time for Kalman filter +delta_time = 0.4 + +#----------------------------- +# Accel noise magnitude for Kalman filter +accel_noise = 0.2 + +#----------------------------- +# Distance threshold between region and object on two frames +dist_thresh = 0.8 + +#----------------------------- +# If this value > 0 than will be used circle with this radius +# If this value <= 0 than will be used ellipse with size (3*vx, 3*vy), vx and vy - horizontal and vertical speed in pixelsa +min_area_radius_pix = -1 + +#----------------------------- +# Minimal area radius in ration for object size. Used if min_area_radius_pix < 0 +min_area_radius_k = 0.8 + +#----------------------------- +# If the object do not assignment more than this seconds then it will be removed +max_lost_time = 2 + +#----------------------------- +# The maximum trajectory length +max_trace_len = 2 + +#----------------------------- +# Detection abandoned objects +detect_abandoned = 0 +# After this time (in seconds) the object is considered abandoned +min_static_time = 5 +# After this time (in seconds) the abandoned object will be removed +max_static_time = 25 +# Speed in pixels. If speed of object is more that this value than object is non static +max_speed_for_static = 10 diff --git a/example/VideoExample.cpp b/example/VideoExample.cpp index 4c41e003..632f7db2 100644 --- a/example/VideoExample.cpp +++ b/example/VideoExample.cpp @@ -532,6 +532,9 @@ void VideoExample::DrawTrack(cv::Mat frame, const std::string& userLabel) { cv::Scalar color = track.m_isStatic ? cv::Scalar(255, 0, 255) : cv::Scalar(0, 255, 0); + cv::Rect brect = track.m_rrect.boundingRect(); + +#if 0 cv::Point2f rectPoints[4]; track.m_rrect.points(rectPoints); //std::cout << "track: rrect [" << track.m_rrect.size << " from " << track.m_rrect.center << ", " << track.m_rrect.angle << "]" << std::endl; @@ -539,6 +542,9 @@ void VideoExample::DrawTrack(cv::Mat frame, { cv::line(frame, rectPoints[i], rectPoints[(i+1) % 4], color); } +#else + cv::rectangle(frame, brect, color); +#endif #if 0 #if 0 @@ -607,7 +613,6 @@ void VideoExample::DrawTrack(cv::Mat frame, } } - cv::Rect brect = track.m_rrect.boundingRect(); std::stringstream label; label << track.m_ID.ID2Str(); if (track.m_type != bad_type) diff --git a/example/examples.h b/example/examples.h index bc6db8a1..c58584e7 100644 --- a/example/examples.h +++ b/example/examples.h @@ -543,7 +543,7 @@ class ONNXTensorRTExample final : public VideoExample } } - //m_detector->CalcMotionMap(frame); + m_detector->CalcMotionMap(frame, true); } }; diff --git a/src/Detector/BaseDetector.h b/src/Detector/BaseDetector.h index bf8ae882..0625017c 100644 --- a/src/Detector/BaseDetector.h +++ b/src/Detector/BaseDetector.h @@ -159,7 +159,7 @@ class BaseDetector /// \brief CalcMotionMap /// \param frame /// - virtual void CalcMotionMap(cv::Mat& frame) + virtual void CalcMotionMap(cv::Mat& frame, bool drawOnlyMasks = true) { if (m_motionMap.size() != frame.size()) m_motionMap = cv::Mat(frame.size(), CV_32FC1, cv::Scalar(0, 0, 0)); @@ -169,7 +169,8 @@ class BaseDetector { if (region.m_boxMask.empty()) { - cv::ellipse(foreground, region.m_rrect, cv::Scalar(255, 255, 255), cv::FILLED); + if (!drawOnlyMasks) + cv::ellipse(foreground, region.m_rrect, cv::Scalar(255, 255, 255), cv::FILLED); } else { diff --git a/src/Detector/MotionDetector.cpp b/src/Detector/MotionDetector.cpp index a97ad063..972db5f4 100644 --- a/src/Detector/MotionDetector.cpp +++ b/src/Detector/MotionDetector.cpp @@ -100,7 +100,7 @@ void MotionDetector::ResetModel(const cv::UMat& img, const cv::Rect& roiRect) /// \brief MotionDetector::CalcMotionMap /// \param frame /// -void MotionDetector::CalcMotionMap(cv::Mat& frame) +void MotionDetector::CalcMotionMap(cv::Mat& frame, bool /*drawOnlyMasks*/) { if (m_motionMap.size() != frame.size()) m_motionMap = cv::Mat(frame.size(), CV_32FC1, cv::Scalar(0, 0, 0)); diff --git a/src/Detector/MotionDetector.h b/src/Detector/MotionDetector.h index f61d4f0f..ad3d1921 100644 --- a/src/Detector/MotionDetector.h +++ b/src/Detector/MotionDetector.h @@ -22,7 +22,7 @@ class MotionDetector : public BaseDetector return true; } - void CalcMotionMap(cv::Mat& frame) override; + void CalcMotionMap(cv::Mat& frame, bool drawOnlyMasks) override; void ResetModel(const cv::UMat& img, const cv::Rect& roiRect) override; diff --git a/src/Detector/OCVDNNDetector.cpp b/src/Detector/OCVDNNDetector.cpp index 8151c6be..08d681db 100644 --- a/src/Detector/OCVDNNDetector.cpp +++ b/src/Detector/OCVDNNDetector.cpp @@ -176,6 +176,8 @@ bool OCVDNNDetector::Init(const config_t& config) dictNetType["YOLOV26"] = ModelType::YOLOV26; dictNetType["YOLOV26_OBB"] = ModelType::YOLOV26_OBB; dictNetType["YOLOV26Mask"] = ModelType::YOLOV26Mask; + dictNetType["YOLOE"] = ModelType::YOLOE; + dictNetType["YOLOEMask"] = ModelType::YOLOEMask; auto netType = dictNetType.find(net_type->second); if (netType != dictNetType.end()) @@ -383,6 +385,14 @@ void OCVDNNDetector::DetectInCrop(const cv::UMat& colorFrame, const cv::Rect& cr std::vector detections; m_net.forward(detections, m_outNames); //compute output +#if 0 + std::cout << "Out layers:\n"; + for (size_t i = 0; i < detections.size(); ++i) + { + std::cout << i << ": " << detections[i].size() << "\n"; + } +#endif + switch (m_netType) { case ModelType::YOLOV5: @@ -448,6 +458,14 @@ void OCVDNNDetector::DetectInCrop(const cv::UMat& colorFrame, const cv::Rect& cr ParseYOLOv26_seg(crop, detections, tmpRegions); break; + case ModelType::YOLOE: + ParseYOLOE(crop, detections, tmpRegions); + break; + + case ModelType::YOLOEMask: + ParseYOLOEMask(crop, detections, tmpRegions); + break; + default: ParseOldYOLO(crop, detections, tmpRegions); break; @@ -1225,3 +1243,97 @@ void OCVDNNDetector::ParseYOLOv26_seg(const cv::Rect& crop, std::vector } } } + +/// +/// \brief OCVDNNDetector::ParseYOLOE +/// \param crop +/// \param detections +/// \param tmpRegions +/// +void OCVDNNDetector::ParseYOLOE(const cv::Rect& crop, std::vector& detections, regions_t& tmpRegions) +{ + float x_factor = crop.width / static_cast(m_inWidth); + float y_factor = crop.height / static_cast(m_inHeight); + + //0: name: images, size: 1x3x640x640 + //1: name: output0, size: 1x54x8400 + //2: name: output1, size: 1x32x160x160 + + int rows = detections[0].size[2]; + int dimensions = detections[0].size[1]; + + detections[0] = detections[0].reshape(1, dimensions); + cv::transpose(detections[0], detections[0]); + + float* data = (float*)detections[0].data; + + for (int i = 0; i < rows; ++i) + { + float* classes_scores = data + 4; + + cv::Mat scores(1, static_cast(m_classNames.size()), CV_32FC1, classes_scores); + cv::Point class_id; + double maxClassScore = 0; + cv::minMaxLoc(scores, 0, &maxClassScore, 0, &class_id); + + if (maxClassScore > m_confidenceThreshold) + { + float x = data[0] * x_factor + crop.x; + float y = data[1] * y_factor + crop.y; + float w = data[2] * x_factor; + float h = data[3] * y_factor; + //float angle = 180.f * data[4 + scores.cols] / M_PI; + + if (m_classesWhiteList.empty() || m_classesWhiteList.find(T2T(class_id.x)) != std::end(m_classesWhiteList)) + tmpRegions.emplace_back(cv::RotatedRect(cv::Point2f(x, y), cv::Size2f(w, h), 0), T2T(class_id.x), static_cast(maxClassScore)); + } + data += dimensions; + } +} + +/// +/// \brief OCVDNNDetector::ParseYOLOEMask +/// \param crop +/// \param detections +/// \param tmpRegions +/// +void OCVDNNDetector::ParseYOLOEMask(const cv::Rect& crop, std::vector& detections, regions_t& tmpRegions) +{ + float x_factor = crop.width / static_cast(m_inWidth); + float y_factor = crop.height / static_cast(m_inHeight); + + //0: name: images, size: 1x3x640x640 + //1: name: output0, size: 1x54x8400 + //2: name: output1, size: 1x32x160x160 + + int rows = detections[0].size[2]; + int dimensions = detections[0].size[1]; + + detections[0] = detections[0].reshape(1, dimensions); + cv::transpose(detections[0], detections[0]); + + float* data = (float*)detections[0].data; + + for (int i = 0; i < rows; ++i) + { + float* classes_scores = data + 4; + + cv::Mat scores(1, static_cast(m_classNames.size()), CV_32FC1, classes_scores); + cv::Point class_id; + double maxClassScore = 0; + cv::minMaxLoc(scores, 0, &maxClassScore, 0, &class_id); + + if (maxClassScore > m_confidenceThreshold) + { + float x = data[0] * x_factor + crop.x; + float y = data[1] * y_factor + crop.y; + float w = data[2] * x_factor; + float h = data[3] * y_factor; + //float angle = 180.f * data[4 + scores.cols] / M_PI; + + if (m_classesWhiteList.empty() || m_classesWhiteList.find(T2T(class_id.x)) != std::end(m_classesWhiteList)) + tmpRegions.emplace_back(cv::RotatedRect(cv::Point2f(x, y), cv::Size2f(w, h), 0), T2T(class_id.x), static_cast(maxClassScore)); + } + data += dimensions; + } +} diff --git a/src/Detector/OCVDNNDetector.h b/src/Detector/OCVDNNDetector.h index 3a55dd67..a53bea11 100644 --- a/src/Detector/OCVDNNDetector.h +++ b/src/Detector/OCVDNNDetector.h @@ -56,7 +56,9 @@ class OCVDNNDetector final : public BaseDetector DFINE_IS, YOLOV26, YOLOV26_OBB, - YOLOV26Mask + YOLOV26Mask, + YOLOE, + YOLOEMask }; cv::dnn::Net m_net; @@ -97,6 +99,8 @@ class OCVDNNDetector final : public BaseDetector void ParseYOLOv26(const cv::Rect& crop, std::vector& detections, regions_t& tmpRegions); void ParseYOLOv26_obb(const cv::Rect& crop, std::vector& detections, regions_t& tmpRegions); void ParseYOLOv26_seg(const cv::Rect& crop, std::vector& detections, regions_t& tmpRegions); + void ParseYOLOE(const cv::Rect& crop, std::vector& detections, regions_t& tmpRegions); + void ParseYOLOEMask(const cv::Rect& crop, std::vector& detections, regions_t& tmpRegions); }; #endif diff --git a/src/Detector/ONNXTensorRTDetector.cpp b/src/Detector/ONNXTensorRTDetector.cpp index b0a734a7..593bc29c 100644 --- a/src/Detector/ONNXTensorRTDetector.cpp +++ b/src/Detector/ONNXTensorRTDetector.cpp @@ -106,6 +106,8 @@ bool ONNXTensorRTDetector::Init(const config_t& config) dictNetType["YOLOV26"] = tensor_rt::YOLOV26; dictNetType["YOLOV26_OBB"] = tensor_rt::YOLOV26_OBB; dictNetType["YOLOV26Mask"] = tensor_rt::YOLOV26Mask; + dictNetType["YOLOE"] = tensor_rt::YOLOE; + dictNetType["YOLOEMask"] = tensor_rt::YOLOEMask; auto netType = dictNetType.find(net_type->second); if (netType != dictNetType.end()) diff --git a/src/Detector/tensorrt_onnx/YoloEONNX.hpp b/src/Detector/tensorrt_onnx/YoloEONNX.hpp new file mode 100644 index 00000000..950ea152 --- /dev/null +++ b/src/Detector/tensorrt_onnx/YoloEONNX.hpp @@ -0,0 +1,64 @@ +#pragma once + +#include "YoloONNX.hpp" + +/// +/// \brief The YOLOE_onnx class +/// +class YOLOE_onnx : public YoloONNX +{ +public: + YOLOE_onnx(std::vector& inputTensorNames, std::vector& outputTensorNames) + { + inputTensorNames.push_back("images"); + outputTensorNames.push_back("output0"); + } + +protected: + /// + /// \brief GetResult + /// \param output + /// \return + /// + std::vector GetResult(size_t imgIdx, int /*keep_topk*/, const std::vector& outputs, cv::Size frameSize) + { + std::vector resBoxes; + + //0: name: images, size: 1x3x640x640 + //1: name: output0, size: 1x300x6 + + const float fw = static_cast(frameSize.width) / static_cast(m_resizedROI.width); + const float fh = static_cast(frameSize.height) / static_cast(m_resizedROI.height); + + auto output = outputs[0]; + + size_t lenInd = 1; + size_t len = static_cast(m_outpuDims[0].d[lenInd]); + auto volume = len * m_outpuDims[0].d[2]; + output += volume * imgIdx; + //std::cout << "len = " << len << ", confThreshold = " << m_params.m_confThreshold << ", volume = " << volume << std::endl; + + for (size_t i = 0; i < len; ++i) + { + auto ind = i * m_outpuDims[0].d[2]; + + float classConf = output[ind + 4]; + int64_t classId = output[ind + 5]; + + if (classConf >= m_params.m_confThreshold) + { + float x = fw * (output[ind + 0] - m_resizedROI.x); + float y = fh * (output[ind + 1] - m_resizedROI.y); + float width = fw * (output[ind + 2] - output[ind + 0]); + float height = fh * (output[ind + 3] - output[ind + 1]); + + //std::cout << "ind = " << ind << ", output[0] = " << output[ind + 0] << ", output[1] = " << output[ind + 1] << ", output[2] = " << output[ind + 2] << ", output[3] = " << output[ind + 3] << std::endl; + //std::cout << "ind = " << ind << ", classConf = " << classConf << ", classId = " << classId << ", x = " << x << ", y = " << y << ", width = " << width << ", height = " << height << std::endl; + + resBoxes.emplace_back(classId, classConf, cv::Rect(cvRound(x), cvRound(y), cvRound(width), cvRound(height))); + } + } + + return resBoxes; + } +}; diff --git a/src/Detector/tensorrt_onnx/YoloEONNX_instance.hpp b/src/Detector/tensorrt_onnx/YoloEONNX_instance.hpp new file mode 100644 index 00000000..d65820de --- /dev/null +++ b/src/Detector/tensorrt_onnx/YoloEONNX_instance.hpp @@ -0,0 +1,318 @@ +#pragma once + +#include "YoloONNX.hpp" + +/// +/// \brief The YOLOE_onnx class +/// +class YOLOE_instance_onnx : public YoloONNX +{ +public: + YOLOE_instance_onnx(std::vector& inputTensorNames, std::vector& outputTensorNames) + { + inputTensorNames.push_back("images"); + outputTensorNames.push_back("output0"); + outputTensorNames.push_back("output1"); + } + +protected: + /// + /// \brief GetResult + /// \param output + /// \return + /// + std::vector GetResult(size_t imgIdx, int /*keep_topk*/, const std::vector& outputs, cv::Size frameSize) + { + //0: name: images, size: 1x3x640x640 + //1: name: output0, size: 1x54x8400 + //2: name: output1, size: 1x32x160x160 + + std::vector resBoxes; + + const float fw = static_cast(frameSize.width) / static_cast(m_resizedROI.width); + const float fh = static_cast(frameSize.height) / static_cast(m_resizedROI.height); + + size_t outInd = 0; + size_t segInd = 1; + + auto output = outputs[0]; + + int INPUT_W = static_cast(m_inputDims[0].d[3]); + int INPUT_H = static_cast(m_inputDims[0].d[2]); + cv::Rect frameRect(0, 0, INPUT_W, INPUT_H); + + //std::cout << "output[1] mem:\n"; + //auto output1 = outputs[1]; + //for (size_t ii = 0; ii < 100; ++ii) + //{ + // std::cout << ii << ": "; + // for (size_t jj = 0; jj < 20; ++jj) + // { + // std::cout << output1[ii * 20 + jj] << " "; + // } + // std::cout << ";" << std::endl; + //} + //std::cout << ";" << std::endl; + + //0: name: images, size: 1x3x640x640 + //1: name: output0, size: 1x116x8400 + //2: name: output1, size: 1x32x160x160 + // 25200 = 3x80x80 + 3x40x40 + 3x20x20 + // 116 = x, y, w, h, 80 classes, 32 seg ancors + // 80 * 8 = 640, 40 * 16 = 640, 20 * 32 = 640 + + size_t ncInd = 1; + size_t lenInd = 2; + int nc = static_cast(m_outpuDims[outInd].d[ncInd] - 4 - 32); + int dimensions = nc + 32 + 4; + size_t len = static_cast(m_outpuDims[outInd].d[lenInd]);// / m_params.explicitBatchSize; + //auto Volume = [](const nvinfer1::Dims& d) + //{ + // return std::accumulate(d.d, d.d + d.nbDims, 1, std::multiplies()); + //}; + auto volume = len * m_outpuDims[outInd].d[ncInd]; // Volume(m_outpuDims[0]); + output += volume * imgIdx; + //std::cout << "len = " << len << ", nc = " << nc << ", m_params.confThreshold = " << m_params.confThreshold << ", volume = " << volume << std::endl; + + cv::Mat rawMemory(1, dimensions * static_cast(len), CV_32FC1, output); + rawMemory = rawMemory.reshape(1, dimensions); + cv::transpose(rawMemory, rawMemory); + output = (float*)rawMemory.data; + + //std::cout << "output[0] mem:\n"; + //for (size_t ii = 0; ii < 100; ++ii) + //{ + // std::cout << ii << ": "; + // for (size_t jj = 0; jj < 20; ++jj) + // { + // std::cout << output[ii * 20 + jj] << " "; + // } + // std::cout << ";" << std::endl; + //} + //std::cout << ";" << std::endl; + +#if 1 + int segWidth = 160; + int segHeight = 160; + int segChannels = 32; + + if (outputs.size() > 1) + { + //std::cout << "output1 nbDims: " << m_outpuDims[segInd].nbDims << ", "; + //for (size_t i = 0; i < m_outpuDims[segInd].nbDims; ++i) + //{ + // std::cout << m_outpuDims[segInd].d[i]; + // if (i + 1 != m_outpuDims[segInd].nbDims) + // std::cout << "x"; + //} + //std::cout << std::endl; + //std::cout << "output nbDims: " << m_outpuDims[outInd].nbDims << ", "; + //for (size_t i = 0; i < m_outpuDims[outInd].nbDims; ++i) + //{ + // std::cout << m_outpuDims[outInd].d[i]; + // if (i + 1 != m_outpuDims[outInd].nbDims) + // std::cout << "x"; + //} + //std::cout << std::endl; + + segChannels = static_cast(m_outpuDims[segInd].d[1]); + segWidth = static_cast(m_outpuDims[segInd].d[2]); + segHeight = static_cast(m_outpuDims[segInd].d[3]); + } + cv::Mat maskProposals; + std::vector> picked_proposals; + int net_width = nc + 4 + segChannels; +#endif + + std::vector classIds; + std::vector confidences; + std::vector rectBoxes; + classIds.reserve(len); + confidences.reserve(len); + rectBoxes.reserve(len); + + for (size_t i = 0; i < len; ++i) + { + // Box + size_t k = i * (nc + 4 + 32); + + int classId = -1; + float objectConf = 0.f; + for (int j = 0; j < nc; ++j) + { + const float classConf = output[k + 4 + j]; + if (classConf > objectConf) + { + classId = j; + objectConf = classConf; + } + } + + //if (objectConf > 0.1) + // std::cout << i << ": objectConf = " << objectConf << ", classId = " << classId << std::endl; + + //if (i == 0) + //{ + // std::cout << "without nms: mem" << i << ": "; + // for (size_t ii = 0; ii < 4; ++ii) + // { + // std::cout << output[k + ii] << " "; + // } + // std::cout << ";" << std::endl; + // for (size_t ii = 4; ii < nc + 4; ++ii) + // { + // std::cout << output[k + ii] << " "; + // } + // std::cout << ";" << std::endl; + // for (size_t ii = nc + 4; ii < nc + 4 + 32; ++ii) + // { + // std::cout << output[k + ii] << " "; + // } + // std::cout << ";" << std::endl; + //} + + if (objectConf >= m_params.m_confThreshold) + { + // (center x, center y, width, height) to (x, y, w, h) + float x = output[k] - output[k + 2] / 2; + float y = output[k + 1] - output[k + 3] / 2; + float width = output[k + 2]; + float height = output[k + 3]; + + //auto ClampToFrame = [](float& v, float& size, int hi) -> int + //{ + // int res = 0; +// + // if (size < 1) + // size = 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; + //}; + //ClampToFrame(x, width, frameSize.width); + //ClampToFrame(y, height, frameSize.height); + + if (width > 4 && height > 4) + { + classIds.push_back(classId); + confidences.push_back(objectConf); + rectBoxes.emplace_back(cvRound(x), cvRound(y), cvRound(width), cvRound(height)); + rectBoxes.back() = rectBoxes.back() & frameRect; + + std::vector temp_proto(output + k + 4 + nc, output + k + net_width); + picked_proposals.push_back(temp_proto); + } + } + } + + //std::cout << "rectBoxes.size = " << rectBoxes.size() << std::endl; + + // Non-maximum suppression to eliminate redudant overlapping boxes + std::vector indices; + cv::dnn::NMSBoxes(rectBoxes, confidences, m_params.m_confThreshold, m_params.m_nmsThreshold, indices); + resBoxes.reserve(indices.size()); + + for (size_t bi = 0; bi < indices.size(); ++bi) + { + resBoxes.emplace_back(classIds[indices[bi]], confidences[indices[bi]], Clamp(rectBoxes[indices[bi]], frameSize)); + maskProposals.push_back(cv::Mat(picked_proposals[indices[bi]]).t()); + } + + if (!maskProposals.empty()) + { + // Mask processing + const float* pdata = outputs[1]; + std::vector maskFloat(pdata, pdata + segChannels * segWidth * segHeight); + + static constexpr float MASK_THRESHOLD = 0.5; + + cv::Mat mask_protos = cv::Mat(maskFloat); + cv::Mat protos = mask_protos.reshape(0, { segChannels, segWidth * segHeight }); + + cv::Mat matmulRes = (maskProposals * protos).t();//n*32 32*25600 + cv::Mat masks = matmulRes.reshape(static_cast(resBoxes.size()), { segWidth, segHeight }); + std::vector maskChannels; + split(masks, maskChannels); + for (size_t i = 0; i < resBoxes.size(); ++i) + { + cv::Mat dest; + cv::Mat mask; + //sigmoid + cv::exp(-maskChannels[i], dest); + dest = 1.0 / (1.0 + dest);//160*160 + + int padw = 0; + int padh = 0; + cv::Rect roi(int((float)padw / INPUT_W * segWidth), int((float)padh / INPUT_H * segHeight), int(segWidth - padw / 2), int(segHeight - padh / 2)); + dest = dest(roi); + + cv::resize(dest, mask, cv::Size(INPUT_W, INPUT_H), cv::INTER_NEAREST); + + resBoxes[i].m_boxMask = mask(resBoxes[i].m_brect) > MASK_THRESHOLD; + +#if 0 + static int globalObjInd = 0; + SaveMat(resBoxes[i].m_boxMask, std::to_string(globalObjInd++), ".png", "tmp", true); +#endif + +#if 1 + std::vector> contours; +#if ((CV_VERSION_MAJOR > 4) || ((CV_VERSION_MAJOR == 4) && (CV_VERSION_MINOR > 9))) + cv::findContoursLinkRuns(resBoxes[i].m_boxMask, contours); +#else + cv::findContours(resBoxes[i].m_boxMask, contours, cv::RETR_EXTERNAL, cv::CHAIN_APPROX_SIMPLE, cv::Point()); +#endif + for (const auto& contour : contours) + { + cv::Rect br = cv::boundingRect(contour); + + //std::cout << "contour br: " << br << std::endl; + + if (br.width >= 4 && + br.height >= 4) + { + int dx = resBoxes[i].m_brect.x; + int dy = resBoxes[i].m_brect.y; + + cv::RotatedRect rr = (contour.size() < 5) ? cv::minAreaRect(contour) : cv::fitEllipse(contour); + rr.center.x = (rr.center.x + dx - m_resizedROI.x) * fw; + rr.center.y = (rr.center.y + dy - m_resizedROI.y) * fw; + rr.size.width *= fw; + rr.size.height *= fh; + + br.x = cvRound((dx + br.x - m_resizedROI.x) * fw); + br.y = cvRound((dy + br.y - m_resizedROI.y) * fh); + br.width = cvRound(br.width * fw); + br.height = cvRound(br.height * fh); + + resBoxes[i].m_brect = br; + resBoxes[i].m_rrect = rr; + + //std::cout << "resBoxes[" << i << "] br: " << br << ", rr: (" << rr.size << " from " << rr.center << ", " << rr.angle << ")" << std::endl; + + break; + } + } +#endif + } + } + return resBoxes; + } +}; diff --git a/src/Detector/tensorrt_onnx/class_detector.cpp b/src/Detector/tensorrt_onnx/class_detector.cpp index f5f4fb66..cc98ce3c 100644 --- a/src/Detector/tensorrt_onnx/class_detector.cpp +++ b/src/Detector/tensorrt_onnx/class_detector.cpp @@ -22,6 +22,8 @@ #include "YoloONNXv26_bb.hpp" #include "YoloONNXv26_obb.hpp" #include "YoloONNXv26_instance.hpp" +#include "YoloEONNX.hpp" +#include "YoloEONNX_instance.hpp" namespace tensor_rt { @@ -115,6 +117,12 @@ namespace tensor_rt case ModelType::DFINE_IS: m_detector = std::make_unique(m_params.m_inputTensorNames, m_params.m_outputTensorNames); break; + case ModelType::YOLOE: + m_detector = std::make_unique(m_params.m_inputTensorNames, m_params.m_outputTensorNames); + break; + case ModelType::YOLOEMask: + m_detector = std::make_unique(m_params.m_inputTensorNames, m_params.m_outputTensorNames); + break; } // Threshold values diff --git a/src/Detector/tensorrt_onnx/class_detector.h b/src/Detector/tensorrt_onnx/class_detector.h index 7ea989bc..454abc36 100644 --- a/src/Detector/tensorrt_onnx/class_detector.h +++ b/src/Detector/tensorrt_onnx/class_detector.h @@ -66,7 +66,9 @@ namespace tensor_rt DFINE_IS, YOLOV26, YOLOV26_OBB, - YOLOV26Mask + YOLOV26Mask, + YOLOE, + YOLOEMask }; ///