inference.h 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #ifndef YOLO_INFERENCE_H_
  2. #define YOLO_INFERENCE_H_
  3. #include <string>
  4. #include <vector>
  5. #include <opencv2/imgproc.hpp>
  6. #include <openvino/openvino.hpp>
  7. namespace yolo {
  8. struct Detection {
  9. short class_id;
  10. float confidence;
  11. cv::Rect box;
  12. };
  13. class Inference {
  14. public:
  15. Inference() {}
  16. // Constructor to initialize the model with default input shape
  17. Inference(const std::string &model_path, const float &model_confidence_threshold, const float &model_NMS_threshold);
  18. // Constructor to initialize the model with specified input shape
  19. Inference(const std::string &model_path, const cv::Size model_input_shape, const float &model_confidence_threshold, const float &model_NMS_threshold);
  20. void RunInference(cv::Mat &frame);
  21. private:
  22. void InitializeModel(const std::string &model_path);
  23. void Preprocessing(const cv::Mat &frame);
  24. void PostProcessing(cv::Mat &frame);
  25. cv::Rect GetBoundingBox(const cv::Rect &src) const;
  26. void DrawDetectedObject(cv::Mat &frame, const Detection &detections) const;
  27. cv::Point2f scale_factor_; // Scaling factor for the input frame
  28. cv::Size2f model_input_shape_; // Input shape of the model
  29. cv::Size model_output_shape_; // Output shape of the model
  30. ov::InferRequest inference_request_; // OpenVINO inference request
  31. ov::CompiledModel compiled_model_; // OpenVINO compiled model
  32. float model_confidence_threshold_; // Confidence threshold for detections
  33. float model_NMS_threshold_; // Non-Maximum Suppression threshold
  34. std::vector<std::string> classes_ {
  35. "person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck", "boat", "traffic light",
  36. "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow",
  37. "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee",
  38. "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard",
  39. "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple",
  40. "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch",
  41. "potted plant", "bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard",
  42. "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase",
  43. "scissors", "teddy bear", "hair drier", "toothbrush"
  44. };
  45. };
  46. } // namespace yolo
  47. #endif // YOLO_INFERENCE_H_