diff --git a/algorithm/veri/cuda/veri_det_cuda_impl.cpp b/algorithm/veri/cuda/veri_det_cuda_impl.cpp index d5225ec..a35d795 100644 --- a/algorithm/veri/cuda/veri_det_cuda_impl.cpp +++ b/algorithm/veri/cuda/veri_det_cuda_impl.cpp @@ -74,10 +74,10 @@ namespace sv bool VeriDetectorCUDAImpl::cudaSetup() { #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)) { - 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}; size_t trt_model_size{0}; @@ -107,7 +107,7 @@ namespace sv delete[] trt_model_stream; 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->_output_index1 = cu_engine.getBindingIndex("output"); @@ -123,7 +123,6 @@ namespace sv this->_p_data = new float[2 * 3 * 224 * 224]; this->_p_prob1 = new float[2 * 576]; this->_p_prob2 = new float[2 * 1280]; - this->_p_prob3 = new float[2 * 1280]; // Input 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); @@ -139,11 +138,12 @@ namespace sv void VeriDetectorCUDAImpl::cudaRoiCNN( std::vector &input_rois_, - std::vector &output_labels_) + std::vector &output_labels_) { #ifdef WITH_CUDA - for (int i = 0; i < 2; ++i) + + for (int i = 0; i < 2; i++) { for (int row = 0; row < 224; ++row) { @@ -151,14 +151,15 @@ namespace sv for (int col = 0; col < 224; ++col) { // 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[224 * 224 * 3 * i + col + row * 224 + 224 * 224] = ((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 * 3 * i] = ((float)uc_pixel[0] - 136.20f) / 44.77f; + this->_p_data[col + row * 224 + 224 * 224 + 224 * 224 * 3 * i] = ((float)uc_pixel[1] - 141.50f) / 44.20f; + this->_p_data[col + row * 224 + 224 * 224 * 2 + 224 * 224 * 3 * i] = ((float)uc_pixel[2] - 145.41f) / 44.30f; uc_pixel += 3; } } } + // Input 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); @@ -180,10 +181,9 @@ namespace sv } } - // 计算两个数组的余弦相似性 float similarity = cosineSimilarity(_p_prob2, _p_prob2 + 1280, 1280); - std::cout << "余弦相似性: " << similarity << std::endl; - std::cout << "VERI LABEL: " << label << std::endl; + output_labels_.push_back(label); + output_labels_.push_back(similarity); } #endif } diff --git a/algorithm/veri/cuda/veri_det_cuda_impl.h b/algorithm/veri/cuda/veri_det_cuda_impl.h index 559a67f..a77f6b8 100644 --- a/algorithm/veri/cuda/veri_det_cuda_impl.h +++ b/algorithm/veri/cuda/veri_det_cuda_impl.h @@ -29,14 +29,13 @@ public: bool cudaSetup(); void cudaRoiCNN( std::vector& input_rois_, - std::vector& output_labels_ + std::vector& output_labels_ ); #ifdef WITH_CUDA float *_p_data; float *_p_prob1; float *_p_prob2; - float *_p_prob3; nvinfer1::IExecutionContext *_trt_context; int _input_index; int _output_index1; diff --git a/algorithm/veri/sv_veri_det.cpp b/algorithm/veri/sv_veri_det.cpp index a7df6d7..279afe0 100644 --- a/algorithm/veri/sv_veri_det.cpp +++ b/algorithm/veri/sv_veri_det.cpp @@ -7,55 +7,67 @@ #include "veri_det_cuda_impl.h" #endif - -namespace sv { - - -VeriDetector::VeriDetector() +namespace sv { - this->_cuda_impl = new VeriDetectorCUDAImpl; -} -VeriDetector::~VeriDetector() -{ -} -bool VeriDetector::setupImpl() -{ -#ifdef WITH_CUDA - return this->_cuda_impl->cudaSetup(); -#endif - return false; -} - -void VeriDetector::roiCNN( - std::vector& input_rois_, - std::vector& 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) + VeriDetector::VeriDetector() + { + this->_cuda_impl = new VeriDetectorCUDAImpl; + } + VeriDetector::~VeriDetector() { - this->_load(); - this->_loadLabels(); - _params_loaded = true; } - std::vector e_roi = {img1_, img2_}; + bool VeriDetector::setupImpl() + { +#ifdef WITH_CUDA + return this->_cuda_impl->cudaSetup(); +#endif + return false; + } - std::vector output_labels; - roiCNN(e_roi, output_labels); -} + void VeriDetector::roiCNN( + std::vector &input_rois_, + std::vector &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 input_rois_ = {img1_, img2_}; + +#ifdef WITH_CUDA + std::vector 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(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); + } + } } - diff --git a/include/sv_veri_det.h b/include/sv_veri_det.h index d372919..a788694 100644 --- a/include/sv_veri_det.h +++ b/include/sv_veri_det.h @@ -25,7 +25,7 @@ protected: bool setupImpl(); void roiCNN( std::vector& input_rois_, - std::vector& output_labels_ + std::vector& output_labels_ ); VeriDetectorCUDAImpl* _cuda_impl; diff --git a/samples/demo/veri.cpp b/samples/demo/veri.cpp index cbe71b6..e9cff74 100644 --- a/samples/demo/veri.cpp +++ b/samples/demo/veri.cpp @@ -5,40 +5,66 @@ using namespace std; -int main(int argc, char *argv[]) { - // 打开摄像头 +int main(int argc, char *argv[]) +{ + // 实例化 圆形降落标志 检测器类 sv::VeriDetector veri; + // 手动导入相机参数,如果使用Amov的G1等吊舱或相机,则可以忽略该步骤,将自动下载相机参数文件 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"); - // cap.setWH(640, 480); - // cap.setFps(30); - //cap.open(sv::CameraType::WEBCAM, 0); // CameraID 0 + // 打开摄像头 + sv::Camera cap1,cap2; + cap1.setWH(640, 480); + cap1.setFps(30); + 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类,用于内存单帧图像 - - - cv::Mat img1,img2; + cv::Mat img1, img2; int frame_id = 0; + int w = 224; + while (1) { // 实例化SpireCV的 单帧检测结果 接口类 TargetsInFrame sv::TargetsInFrame tgts(frame_id++); - // 读取一帧图像到img cap1.read(img1); cap2.read(img2); - cv::resize(img1, img1, cv::Size(224, 224)); - cv::resize(img2, img2, cv::Size(224, 224)); + cv::resize(img1, img1, cv::Size(w, w)); + cv::resize(img2, img2, cv::Size(w, w)); + // 执行 降落标志 检测 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); - // cv::waitKey(10); + // 打印每个 降落标志 的置信度 + printf(" Similarity Score = %.3f\n", tgts.targets[i].score); + printf(" Category ID = %d\n", tgts.targets[i].category_id); + } + + // 显示检测结果img + cv::imshow("img1", img1); + cv::imshow("img2", img2); + cv::waitKey(10); } return 0;