Filip Kujawa | dc7d47c | 2023-04-08 16:16:51 -0700 | [diff] [blame^] | 1 | #include "yolov5.h" |
| 2 | |
| 3 | #include <opencv2/core.hpp> |
| 4 | |
| 5 | #include "gflags/gflags.h" |
| 6 | #include "glog/logging.h" |
| 7 | |
| 8 | DEFINE_double(conf_threshold, 0.9, |
| 9 | "Threshold value for confidence scores. Detections with a " |
| 10 | "confidence score below this value will be ignored."); |
| 11 | |
| 12 | DEFINE_double( |
| 13 | nms_threshold, 0.5, |
| 14 | "Threshold value for non-maximum suppression. Detections with an " |
| 15 | "intersection-over-union value below this value will be removed."); |
| 16 | |
| 17 | DEFINE_int32(nthreads, 6, "Number of threads to use during inference."); |
| 18 | |
| 19 | namespace y2023 { |
| 20 | namespace vision { |
| 21 | |
| 22 | void YOLOV5::LoadModel(const std::string path) { |
| 23 | model_ = tflite::FlatBufferModel::BuildFromFile(path.c_str()); |
| 24 | CHECK(model_); |
| 25 | size_t num_devices; |
| 26 | std::unique_ptr<edgetpu_device, decltype(&edgetpu_free_devices)> devices( |
| 27 | edgetpu_list_devices(&num_devices), &edgetpu_free_devices); |
| 28 | const auto &device = devices.get()[0]; |
| 29 | CHECK_EQ(num_devices, 1ul); |
| 30 | tflite::ops::builtin::BuiltinOpResolver resolver; |
| 31 | CHECK_EQ(tflite::InterpreterBuilder(*model_, resolver)(&interpreter_), |
| 32 | kTfLiteOk); |
| 33 | |
| 34 | auto *delegate = |
| 35 | edgetpu_create_delegate(device.type, device.path, nullptr, 0); |
| 36 | interpreter_->ModifyGraphWithDelegate(delegate); |
| 37 | |
| 38 | TfLiteStatus status = interpreter_->AllocateTensors(); |
| 39 | CHECK(status == kTfLiteOk); |
| 40 | |
| 41 | input_ = interpreter_->inputs()[0]; |
| 42 | TfLiteIntArray *dims = interpreter_->tensor(input_)->dims; |
| 43 | in_height_ = dims->data[1]; |
| 44 | in_width_ = dims->data[2]; |
| 45 | in_channels_ = dims->data[3]; |
| 46 | in_type_ = interpreter_->tensor(input_)->type; |
| 47 | input_8_ = interpreter_->typed_tensor<uint8_t>(input_); |
| 48 | |
| 49 | interpreter_->SetNumThreads(FLAGS_nthreads); |
| 50 | } |
| 51 | |
| 52 | void YOLOV5::Preprocess(cv::Mat image) { |
| 53 | cv::resize(image, image, cv::Size(in_height_, in_width_), cv::INTER_CUBIC); |
| 54 | cv::cvtColor(image, image, cv::COLOR_BGR2RGB); |
| 55 | image.convertTo(image, CV_8U); |
| 56 | } |
| 57 | |
| 58 | void YOLOV5::ConvertCVMatToTensor(const cv::Mat &src, uint8_t *in) { |
| 59 | CHECK(src.type() == CV_8UC3); |
| 60 | int n = 0, nc = src.channels(), ne = src.elemSize(); |
| 61 | for (int y = 0; y < src.rows; ++y) |
| 62 | for (int x = 0; x < src.cols; ++x) |
| 63 | for (int c = 0; c < nc; ++c) |
| 64 | in[n++] = src.data[y * src.step + x * ne + c]; |
| 65 | } |
| 66 | |
| 67 | std::vector<std::vector<float>> YOLOV5::TensorToVector2D( |
| 68 | TfLiteTensor *src_tensor, const int rows, const int columns) { |
| 69 | auto scale = src_tensor->params.scale; |
| 70 | auto zero_point = src_tensor->params.zero_point; |
| 71 | std::vector<std::vector<float>> result_vec; |
| 72 | for (int32_t i = 0; i < rows; i++) { |
| 73 | std::vector<float> row_values; |
| 74 | for (int32_t j = 0; j < columns; j++) { |
| 75 | float val_float = |
| 76 | ((static_cast<int32_t>(src_tensor->data.uint8[i * columns + j])) - |
| 77 | zero_point) * |
| 78 | scale; |
| 79 | row_values.push_back(val_float); |
| 80 | } |
| 81 | result_vec.push_back(row_values); |
| 82 | } |
| 83 | return result_vec; |
| 84 | } |
| 85 | |
| 86 | void YOLOV5::NonMaximumSupression( |
| 87 | const std::vector<std::vector<float>> &orig_preds, const int rows, |
| 88 | const int columns, std::vector<Detection> *detections, |
| 89 | std::vector<int> *indices) |
| 90 | |
| 91 | { |
| 92 | std::vector<float> scores; |
| 93 | double confidence; |
| 94 | cv::Point class_id; |
| 95 | |
| 96 | for (int i = 0; i < rows; i++) { |
| 97 | if (orig_preds[i][4] > FLAGS_conf_threshold) { |
| 98 | int left = (orig_preds[i][0] - orig_preds[i][2] / 2) * img_width_; |
| 99 | int top = (orig_preds[i][1] - orig_preds[i][3] / 2) * img_height_; |
| 100 | int w = orig_preds[i][2] * img_width_; |
| 101 | int h = orig_preds[i][3] * img_height_; |
| 102 | |
| 103 | for (int j = 5; j < columns; j++) { |
| 104 | scores.push_back(orig_preds[i][j] * orig_preds[i][4]); |
| 105 | } |
| 106 | |
| 107 | cv::minMaxLoc(scores, nullptr, &confidence, nullptr, &class_id); |
| 108 | if (confidence > FLAGS_conf_threshold) { |
| 109 | Detection detection{cv::Rect(left, top, w, h), confidence, |
| 110 | class_id.x - kClassIdOffset}; |
| 111 | detections->push_back(detection); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | std::vector<cv::Rect> boxes; |
| 117 | std::vector<float> confidences; |
| 118 | |
| 119 | for (const Detection &d : *detections) { |
| 120 | boxes.push_back(d.box); |
| 121 | confidences.push_back(d.confidence); |
| 122 | } |
| 123 | |
| 124 | cv::dnn::NMSBoxes(boxes, confidences, FLAGS_conf_threshold, |
| 125 | FLAGS_nms_threshold, *indices); |
| 126 | } |
| 127 | |
| 128 | std::vector<Detection> YOLOV5::ProcessImage(cv::Mat frame) { |
| 129 | img_height_ = frame.rows; |
| 130 | img_width_ = frame.cols; |
| 131 | |
| 132 | Preprocess(frame); |
| 133 | ConvertCVMatToTensor(frame, input_8_); |
| 134 | |
| 135 | // Inference |
| 136 | TfLiteStatus status = interpreter_->Invoke(); |
| 137 | CHECK_EQ(status, kTfLiteOk); |
| 138 | |
| 139 | int output_tensor_index = interpreter_->outputs()[0]; |
| 140 | TfLiteIntArray *out_dims = interpreter_->tensor(output_tensor_index)->dims; |
| 141 | int num_rows = out_dims->data[1]; |
| 142 | int num_columns = out_dims->data[2]; |
| 143 | |
| 144 | TfLiteTensor *src_tensor = interpreter_->tensor(interpreter_->outputs()[0]); |
| 145 | std::vector<std::vector<float>> orig_preds = |
| 146 | TensorToVector2D(src_tensor, num_rows, num_columns); |
| 147 | |
| 148 | std::vector<int> indices; |
| 149 | std::vector<Detection> detections; |
| 150 | |
| 151 | NonMaximumSupression(orig_preds, num_rows, num_columns, &detections, |
| 152 | &indices); |
| 153 | |
| 154 | return detections; |
| 155 | }; |
| 156 | |
| 157 | } // namespace vision |
| 158 | } // namespace y2023 |