wheel-detector.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "wheel-detector.h"
  2. TensorrtWheelDetector::TensorrtWheelDetector(const std::string &model_file, const std::string &class_file){
  3. yolov8_ = new Inference(model_file, cv::Size{640, 640}, class_file, false);
  4. }
  5. TensorrtWheelDetector::~TensorrtWheelDetector(){
  6. }
  7. bool TensorrtWheelDetector::detect(cv::Mat& img,std::vector<Object>& objs){
  8. std::vector<Detection> rets = yolov8_->runInference(img);
  9. for (auto &ret: rets) {
  10. Object obj;
  11. obj.rect = ret.box;
  12. obj.label = ret.class_id;
  13. obj.prob = ret.confidence;
  14. objs.push_back(obj);
  15. }
  16. return true;
  17. }
  18. bool TensorrtWheelDetector::detect(cv::Mat& img,std::vector<Object>& objs,cv::Mat& res){
  19. std::vector<Detection> rets = yolov8_->runInference(img);
  20. for (auto &ret: rets) {
  21. Object obj;
  22. obj.rect = ret.box;
  23. obj.label = ret.class_id;
  24. obj.prob = ret.confidence;
  25. obj.boxMask = img;
  26. objs.push_back(obj);
  27. cv::Rect box = ret.box;
  28. cv::Scalar color = ret.color;
  29. // Detection box
  30. cv::rectangle(res, box, color, 2);
  31. // Detection box text
  32. std::string classString = ret.className + ' ' + std::to_string(ret.confidence).substr(0, 4);
  33. cv::Size textSize = cv::getTextSize(classString, cv::FONT_HERSHEY_DUPLEX, 1, 2, 0);
  34. cv::Rect textBox(box.x, box.y - 40, textSize.width + 10, textSize.height + 20);
  35. cv::rectangle(res, textBox, color, cv::FILLED);
  36. cv::putText(res, classString, cv::Point(box.x + 5, box.y - 10), cv::FONT_HERSHEY_DUPLEX, 1, cv::Scalar(0, 0, 0), 2, 0);
  37. }
  38. return true;
  39. }
  40. std::vector<cv::Point> TensorrtWheelDetector::getPointsFromObj(const Object &obj){
  41. std::vector<cv::Point> ret;
  42. int x=int(obj.rect.x+0.5);
  43. int y=int(obj.rect.y+0.5);
  44. int width=int(obj.rect.width);
  45. int height=int(obj.rect.height);
  46. for(int i=0;i<height;++i){
  47. for(int j=0;j<width;++j){
  48. ret.emplace_back(x+j,y+i);
  49. }
  50. }
  51. return ret;
  52. }