Pre Merge pull request !23 from Daniel/lxm

This commit is contained in:
Daniel 2023-10-29 11:03:09 +00:00 committed by Gitee
commit 7d93fa3787
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
5 changed files with 113 additions and 76 deletions

View File

@ -74,10 +74,10 @@ namespace sv
bool VeriDetectorCUDAImpl::cudaSetup() bool VeriDetectorCUDAImpl::cudaSetup()
{ {
#ifdef WITH_CUDA #ifdef WITH_CUDA
std::string trt_model_fn = get_home() + SV_MODEL_DIR + "model.engine"; std::string trt_model_fn = get_home() + SV_MODEL_DIR + "veri.engine";
if (!is_file_exist(trt_model_fn)) if (!is_file_exist(trt_model_fn))
{ {
throw std::runtime_error("SpireCV (104) Error loading the VERI TensorRT model (File Not Exist)"); throw std::runtime_error("SpireCV (104) Error loading the LandingMarker TensorRT model (File Not Exist)");
} }
char *trt_model_stream{nullptr}; char *trt_model_stream{nullptr};
size_t trt_model_size{0}; size_t trt_model_size{0};
@ -107,7 +107,7 @@ namespace sv
delete[] trt_model_stream; delete[] trt_model_stream;
const ICudaEngine &cu_engine = this->_trt_context->getEngine(); const ICudaEngine &cu_engine = this->_trt_context->getEngine();
assert(cu_engine.getNbBindings() == 2); assert(cu_engine.getNbBindings() == 3);
this->_input_index = cu_engine.getBindingIndex("input"); this->_input_index = cu_engine.getBindingIndex("input");
this->_output_index1 = cu_engine.getBindingIndex("output"); this->_output_index1 = cu_engine.getBindingIndex("output");
@ -123,7 +123,6 @@ namespace sv
this->_p_data = new float[2 * 3 * 224 * 224]; this->_p_data = new float[2 * 3 * 224 * 224];
this->_p_prob1 = new float[2 * 576]; this->_p_prob1 = new float[2 * 576];
this->_p_prob2 = new float[2 * 1280]; this->_p_prob2 = new float[2 * 1280];
this->_p_prob3 = new float[2 * 1280];
// Input // Input
TRTCHECK(cudaMemcpyAsync(_p_buffers[this->_input_index], this->_p_data, 2 * 3 * 224 * 224 * sizeof(float), cudaMemcpyHostToDevice, this->_cu_stream)); TRTCHECK(cudaMemcpyAsync(_p_buffers[this->_input_index], this->_p_data, 2 * 3 * 224 * 224 * sizeof(float), cudaMemcpyHostToDevice, this->_cu_stream));
// this->_trt_context->enqueue(1, _p_buffers, this->_cu_stream, nullptr); // this->_trt_context->enqueue(1, _p_buffers, this->_cu_stream, nullptr);
@ -139,11 +138,12 @@ namespace sv
void VeriDetectorCUDAImpl::cudaRoiCNN( void VeriDetectorCUDAImpl::cudaRoiCNN(
std::vector<cv::Mat> &input_rois_, std::vector<cv::Mat> &input_rois_,
std::vector<int> &output_labels_) std::vector<float> &output_labels_)
{ {
#ifdef WITH_CUDA #ifdef WITH_CUDA
for (int i = 0; i < 2; ++i)
for (int i = 0; i < 2; i++)
{ {
for (int row = 0; row < 224; ++row) for (int row = 0; row < 224; ++row)
{ {
@ -151,14 +151,15 @@ namespace sv
for (int col = 0; col < 224; ++col) for (int col = 0; col < 224; ++col)
{ {
// mean=[136.20, 141.50, 145.41], std=[44.77, 44.20, 44.30] // mean=[136.20, 141.50, 145.41], std=[44.77, 44.20, 44.30]
this->_p_data[224 * 224 * 3 * i + col + row * 224] = ((float)uc_pixel[0] - 136.20f) / 44.77f; this->_p_data[col + row * 224 + 224 * 224 * 3 * i] = ((float)uc_pixel[0] - 136.20f) / 44.77f;
this->_p_data[224 * 224 * 3 * i + col + row * 224 + 224 * 224] = ((float)uc_pixel[1] - 141.50f) / 44.20f; this->_p_data[col + row * 224 + 224 * 224 + 224 * 224 * 3 * i] = ((float)uc_pixel[1] - 141.50f) / 44.20f;
this->_p_data[224 * 224 * 3 * i + col + row * 224 + 224 * 224 * 2] = ((float)uc_pixel[2] - 145.41f) / 44.30f; this->_p_data[col + row * 224 + 224 * 224 * 2 + 224 * 224 * 3 * i] = ((float)uc_pixel[2] - 145.41f) / 44.30f;
uc_pixel += 3; uc_pixel += 3;
} }
} }
} }
// Input // Input
TRTCHECK(cudaMemcpyAsync(_p_buffers[this->_input_index], this->_p_data, 2 * 3 * 224 * 224 * sizeof(float), cudaMemcpyHostToDevice, this->_cu_stream)); TRTCHECK(cudaMemcpyAsync(_p_buffers[this->_input_index], this->_p_data, 2 * 3 * 224 * 224 * sizeof(float), cudaMemcpyHostToDevice, this->_cu_stream));
// this->_trt_context->enqueue(1, _p_buffers, this->_cu_stream, nullptr); // this->_trt_context->enqueue(1, _p_buffers, this->_cu_stream, nullptr);
@ -180,10 +181,9 @@ namespace sv
} }
} }
// 计算两个数组的余弦相似性
float similarity = cosineSimilarity(_p_prob2, _p_prob2 + 1280, 1280); float similarity = cosineSimilarity(_p_prob2, _p_prob2 + 1280, 1280);
std::cout << "余弦相似性: " << similarity << std::endl; output_labels_.push_back(label);
std::cout << "VERI LABEL: " << label << std::endl; output_labels_.push_back(similarity);
} }
#endif #endif
} }

View File

@ -29,14 +29,13 @@ public:
bool cudaSetup(); bool cudaSetup();
void cudaRoiCNN( void cudaRoiCNN(
std::vector<cv::Mat>& input_rois_, std::vector<cv::Mat>& input_rois_,
std::vector<int>& output_labels_ std::vector<float>& output_labels_
); );
#ifdef WITH_CUDA #ifdef WITH_CUDA
float *_p_data; float *_p_data;
float *_p_prob1; float *_p_prob1;
float *_p_prob2; float *_p_prob2;
float *_p_prob3;
nvinfer1::IExecutionContext *_trt_context; nvinfer1::IExecutionContext *_trt_context;
int _input_index; int _input_index;
int _output_index1; int _output_index1;

View File

@ -7,55 +7,67 @@
#include "veri_det_cuda_impl.h" #include "veri_det_cuda_impl.h"
#endif #endif
namespace sv
namespace sv {
VeriDetector::VeriDetector()
{ {
this->_cuda_impl = new VeriDetectorCUDAImpl;
}
VeriDetector::~VeriDetector()
{
}
bool VeriDetector::setupImpl() VeriDetector::VeriDetector()
{ {
#ifdef WITH_CUDA this->_cuda_impl = new VeriDetectorCUDAImpl;
return this->_cuda_impl->cudaSetup(); }
#endif VeriDetector::~VeriDetector()
return false;
}
void VeriDetector::roiCNN(
std::vector<cv::Mat>& input_rois_,
std::vector<int>& output_labels_
)
{
#ifdef WITH_CUDA
this->_cuda_impl->cudaRoiCNN(
input_rois_,
output_labels_
);
#endif
}
void VeriDetector::detect(cv::Mat img1_, cv::Mat img2_, TargetsInFrame& tgts_)
{
if (!_params_loaded)
{ {
this->_load();
this->_loadLabels();
_params_loaded = true;
} }
std::vector<cv::Mat> e_roi = {img1_, img2_}; bool VeriDetector::setupImpl()
{
#ifdef WITH_CUDA
return this->_cuda_impl->cudaSetup();
#endif
return false;
}
std::vector<int> output_labels; void VeriDetector::roiCNN(
roiCNN(e_roi, output_labels); std::vector<cv::Mat> &input_rois_,
} std::vector<float> &output_labels_)
{
#ifdef WITH_CUDA
this->_cuda_impl->cudaRoiCNN(
input_rois_,
output_labels_);
#endif
}
void VeriDetector::detect(cv::Mat img1_, cv::Mat img2_, TargetsInFrame &tgts_)
{
if (!_params_loaded)
{
this->_load();
this->_loadLabels();
_params_loaded = true;
}
std::vector<cv::Mat> input_rois_ = {img1_, img2_};
#ifdef WITH_CUDA
std::vector<float> output_labels;
roiCNN(input_rois_, output_labels);
#endif
tgts_.setSize(img1_.cols, img1_.rows);
tgts_.setFOV(this->fov_x, this->fov_y);
auto t1 = std::chrono::system_clock::now();
tgts_.setFPS(1000.0 / std::chrono::duration_cast<std::chrono::milliseconds>(t1 - this->_t0).count());
this->_t0 = std::chrono::system_clock::now();
tgts_.setTimeNow();
if (output_labels.size() > 0)
{
Target tgt;
tgt.category_id = output_labels[0];
tgt.score = output_labels[1];
tgts_.targets.push_back(tgt);
}
}
} }

View File

@ -25,7 +25,7 @@ protected:
bool setupImpl(); bool setupImpl();
void roiCNN( void roiCNN(
std::vector<cv::Mat>& input_rois_, std::vector<cv::Mat>& input_rois_,
std::vector<int>& output_labels_ std::vector<float>& output_labels_
); );
VeriDetectorCUDAImpl* _cuda_impl; VeriDetectorCUDAImpl* _cuda_impl;

View File

@ -5,40 +5,66 @@
using namespace std; using namespace std;
int main(int argc, char *argv[]) { int main(int argc, char *argv[])
// 打开摄像头 {
// 实例化 圆形降落标志 检测器类
sv::VeriDetector veri; sv::VeriDetector veri;
// 手动导入相机参数如果使用Amov的G1等吊舱或相机则可以忽略该步骤将自动下载相机参数文件
veri.loadCameraParams(sv::get_home() + "/SpireCV/calib_webcam_640x480.yaml"); veri.loadCameraParams(sv::get_home() + "/SpireCV/calib_webcam_640x480.yaml");
cv::VideoCapture cap1("/home/amov/Videos/com/FlyVideo_2023-09-02_11-36-00.avi"); // 打开摄像头
cv::VideoCapture cap2("/home/amov/Videos/com/FlyVideo_2023-09-02_11-41-55.avi"); sv::Camera cap1,cap2;
// cap.setWH(640, 480); cap1.setWH(640, 480);
// cap.setFps(30); cap1.setFps(30);
//cap.open(sv::CameraType::WEBCAM, 0); // CameraID 0 cap1.open(sv::CameraType::WEBCAM, 4); // CameraID 0
cap2.setWH(640, 480);
cap2.setFps(30);
cap2.open(sv::CameraType::WEBCAM, 10); // CameraID 0
// 实例化OpenCV的Mat类用于内存单帧图像 // 实例化OpenCV的Mat类用于内存单帧图像
cv::Mat img1, img2;
cv::Mat img1,img2;
int frame_id = 0; int frame_id = 0;
int w = 224;
while (1) while (1)
{ {
// 实例化SpireCV的 单帧检测结果 接口类 TargetsInFrame // 实例化SpireCV的 单帧检测结果 接口类 TargetsInFrame
sv::TargetsInFrame tgts(frame_id++); sv::TargetsInFrame tgts(frame_id++);
// 读取一帧图像到img // 读取一帧图像到img
cap1.read(img1); cap1.read(img1);
cap2.read(img2); cap2.read(img2);
cv::resize(img1, img1, cv::Size(224, 224)); cv::resize(img1, img1, cv::Size(w, w));
cv::resize(img2, img2, cv::Size(224, 224)); cv::resize(img2, img2, cv::Size(w, w));
// 执行 降落标志 检测
veri.detect(img1, img2, tgts); veri.detect(img1, img2, tgts);
// 可视化检测结果叠加到img上
// sv::drawTargetsInFrame(img, tgts);
// // 控制台打印 降落标志 检测结果
printf("Frame-[%d]\n", frame_id);
// 打印当前检测的FPS
printf(" FPS = %.2f\n", tgts.fps);
// 打印当前相机的视场角degree
printf(" FOV (fx, fy) = (%.2f, %.2f)\n", tgts.fov_x, tgts.fov_y);
// 打印当前输入图像的像素宽度和高度
printf(" Frame Size (width, height) = (%d, %d)\n", tgts.width, tgts.height);
for (int i = 0; i < tgts.targets.size(); i++)
{
// 显示img // 打印每个 降落标志 的置信度
// cv::imshow("img", img); printf(" Similarity Score = %.3f\n", tgts.targets[i].score);
// cv::waitKey(10); printf(" Category ID = %d\n", tgts.targets[i].category_id);
}
// 显示检测结果img
cv::imshow("img1", img1);
cv::imshow("img2", img2);
cv::waitKey(10);
} }
return 0; return 0;