wheel-detector.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. #include "wheel-detector.h"
  2. TensorrtWheelDetector::TensorrtWheelDetector(const std::string &enginfile){
  3. yolov8_=new YOLOv8_seg(enginfile);
  4. yolov8_->make_pipe(false);
  5. imgsz_=cv::Size{640, 480};
  6. seg_h_ = 120;
  7. seg_w_ = 160;
  8. seg_channels_ = 32;
  9. }
  10. TensorrtWheelDetector::~TensorrtWheelDetector(){
  11. if(yolov8_!=nullptr){
  12. delete yolov8_;
  13. yolov8_=nullptr;
  14. }
  15. }
  16. bool TensorrtWheelDetector::detect(cv::Mat& img,std::vector<Object>& objs){
  17. if(yolov8_==nullptr){
  18. return false;
  19. }
  20. if(img.size()!=imgsz_){
  21. printf("imgsz required [%d,%d],but input is [%d,%d]\n",imgsz_.height,imgsz_.width,img.rows,img.cols);
  22. return false;
  23. }
  24. yolov8_->copy_from_Mat(img, imgsz_);
  25. yolov8_->infer();
  26. float score_thres=0.9;
  27. float iou_thres=0.65;
  28. int topk=10;
  29. yolov8_->postprocess(objs, score_thres, iou_thres, topk, seg_channels_, seg_h_, seg_w_);
  30. return true;
  31. }
  32. bool TensorrtWheelDetector::detect(cv::Mat& img,std::vector<Object>& objs,cv::Mat& res){
  33. if(detect(img,objs))
  34. {
  35. const std::vector<std::string> classes={"none","wheel"};
  36. const std::vector<std::vector<unsigned int>> colors = {{0, 114, 189}, {0, 255, 0}};
  37. const std::vector<std::vector<unsigned int>> mask_colors = {{255, 56, 56}, {255, 0, 0}};
  38. yolov8_->draw_objects(img, res, objs, classes, colors, mask_colors);
  39. return true;
  40. }else{
  41. return false;
  42. }
  43. }
  44. std::vector<cv::Point> TensorrtWheelDetector::getPointsFromObj(Object obj){
  45. std::vector<cv::Point> ret;
  46. int x=int(obj.rect.x+0.5);
  47. int y=int(obj.rect.y+0.5);
  48. int width=int(obj.rect.width);
  49. int height=int(obj.rect.height);
  50. printf("mask type:%d\n",obj.boxMask.type());
  51. for(int i=0;i<height;++i){
  52. for(int j=0;j<width;++j){
  53. //printf("%d ",obj.boxMask.at<uchar>(i,j));
  54. if(obj.boxMask.at<uchar>(i,j)!=0){
  55. ret.push_back(cv::Point(x+j,y+i));
  56. }
  57. }
  58. }
  59. //printf("\n");
  60. return ret;
  61. }