Filip Kujawa | dc7d47c | 2023-04-08 16:16:51 -0700 | [diff] [blame^] | 1 | #ifndef Y2023_VISION_YOLOV5_H_ |
| 2 | #define Y2023_VISION_YOLOV5_H_ |
| 3 | |
| 4 | #include <tensorflow/lite/interpreter.h> |
| 5 | #include <tensorflow/lite/kernels/register.h> |
| 6 | #include <tensorflow/lite/model.h> |
| 7 | #include <tflite/public/edgetpu_c.h> |
| 8 | |
| 9 | #include <chrono> |
| 10 | #include <cmath> |
| 11 | #include <cstdint> |
| 12 | #include <fstream> |
| 13 | #include <iostream> |
| 14 | #include <opencv2/core.hpp> |
| 15 | #include <opencv2/dnn.hpp> |
| 16 | #include <opencv2/highgui/highgui.hpp> |
| 17 | #include <opencv2/imgcodecs.hpp> |
| 18 | #include <opencv2/imgproc.hpp> |
| 19 | #include <vector> |
| 20 | |
| 21 | namespace y2023 { |
| 22 | namespace vision { |
| 23 | |
| 24 | struct Detection { |
| 25 | cv::Rect box; |
| 26 | double confidence; |
| 27 | int class_id; |
| 28 | }; |
| 29 | |
| 30 | class YOLOV5 { |
| 31 | public: |
| 32 | // Takes a model path as string and loads a pre-trained |
| 33 | // YOLOv5 model from the specified path. |
| 34 | void LoadModel(const std::string path); |
| 35 | |
| 36 | // Takes an image and returns a Detection. |
| 37 | std::vector<Detection> ProcessImage(cv::Mat image); |
| 38 | |
| 39 | private: |
| 40 | // Convert an OpenCV Mat object to a tensor input |
| 41 | // that can be fed to the TensorFlow Lite model. |
| 42 | void ConvertCVMatToTensor(const cv::Mat &src, uint8_t *in); |
| 43 | |
| 44 | // Resizes, converts color space, and converts |
| 45 | // image data type before inference. |
| 46 | void Preprocess(cv::Mat image); |
| 47 | |
| 48 | // Converts a TensorFlow Lite tensor to a 2D vector. |
| 49 | std::vector<std::vector<float>> TensorToVector2D(TfLiteTensor *src_tensor, |
| 50 | const int rows, |
| 51 | const int columns); |
| 52 | |
| 53 | // Performs non-maximum suppression to remove overlapping bounding boxes. |
| 54 | void NonMaximumSupression(const std::vector<std::vector<float>> &orig_preds, |
| 55 | const int rows, const int columns, |
| 56 | std::vector<Detection> *detections, |
| 57 | std::vector<int> *indices); |
| 58 | // Models |
| 59 | std::unique_ptr<tflite::FlatBufferModel> model_; |
| 60 | std::unique_ptr<tflite::Interpreter> interpreter_; |
| 61 | tflite::StderrReporter error_reporter_; |
| 62 | |
| 63 | // Parameters of interpreter's input |
| 64 | int input_; |
| 65 | int in_height_; |
| 66 | int in_width_; |
| 67 | int in_channels_; |
| 68 | int in_type_; |
| 69 | |
| 70 | // Parameters of original image |
| 71 | int img_height_; |
| 72 | int img_width_; |
| 73 | |
| 74 | // Input of the interpreter |
| 75 | uint8_t *input_8_; |
| 76 | |
| 77 | // Subtract this offset from class labels to get the actual label. |
| 78 | static constexpr int kClassIdOffset = 5; |
| 79 | }; |
| 80 | |
| 81 | } // namespace vision |
| 82 | } // namespace y2023 |
| 83 | |
| 84 | #endif // Y2023_VISION_YOLOV5_H_ |