|
| 1 | +// Copyright (c) OpenMMLab. All rights reserved. |
| 2 | + |
| 3 | +#include "rotated_detector.h" |
| 4 | + |
| 5 | +#include <numeric> |
| 6 | + |
| 7 | +#include "codebase/mmrotate/mmrotate.h" |
| 8 | +#include "core/device.h" |
| 9 | +#include "core/graph.h" |
| 10 | +#include "core/mat.h" |
| 11 | +#include "core/utils/formatter.h" |
| 12 | +#include "handle.h" |
| 13 | + |
| 14 | +using namespace std; |
| 15 | +using namespace mmdeploy; |
| 16 | + |
| 17 | +namespace { |
| 18 | + |
| 19 | +Value& config_template() { |
| 20 | + // clang-format off |
| 21 | + static Value v{ |
| 22 | + { |
| 23 | + "pipeline", { |
| 24 | + {"input", {"image"}}, |
| 25 | + {"output", {"det"}}, |
| 26 | + { |
| 27 | + "tasks",{ |
| 28 | + { |
| 29 | + {"name", "mmrotate"}, |
| 30 | + {"type", "Inference"}, |
| 31 | + {"params", {{"model", "TBD"}}}, |
| 32 | + {"input", {"image"}}, |
| 33 | + {"output", {"det"}} |
| 34 | + } |
| 35 | + } |
| 36 | + } |
| 37 | + } |
| 38 | + } |
| 39 | + }; |
| 40 | + // clang-format on |
| 41 | + return v; |
| 42 | +} |
| 43 | + |
| 44 | +template <class ModelType> |
| 45 | +int mmdeploy_rotated_detector_create_impl(ModelType&& m, const char* device_name, int device_id, |
| 46 | + mm_handle_t* handle) { |
| 47 | + try { |
| 48 | + auto value = config_template(); |
| 49 | + value["pipeline"]["tasks"][0]["params"]["model"] = std::forward<ModelType>(m); |
| 50 | + |
| 51 | + auto pose_estimator = std::make_unique<Handle>(device_name, device_id, std::move(value)); |
| 52 | + |
| 53 | + *handle = pose_estimator.release(); |
| 54 | + return MM_SUCCESS; |
| 55 | + |
| 56 | + } catch (const std::exception& e) { |
| 57 | + MMDEPLOY_ERROR("exception caught: {}", e.what()); |
| 58 | + } catch (...) { |
| 59 | + MMDEPLOY_ERROR("unknown exception caught"); |
| 60 | + } |
| 61 | + return MM_E_FAIL; |
| 62 | +} |
| 63 | + |
| 64 | +} // namespace |
| 65 | + |
| 66 | +int mmdeploy_rotated_detector_create(mm_model_t model, const char* device_name, int device_id, |
| 67 | + mm_handle_t* handle) { |
| 68 | + return mmdeploy_rotated_detector_create_impl(*static_cast<Model*>(model), device_name, device_id, |
| 69 | + handle); |
| 70 | +} |
| 71 | + |
| 72 | +int mmdeploy_rotated_detector_create_by_path(const char* model_path, const char* device_name, |
| 73 | + int device_id, mm_handle_t* handle) { |
| 74 | + return mmdeploy_rotated_detector_create_impl(model_path, device_name, device_id, handle); |
| 75 | +} |
| 76 | + |
| 77 | +int mmdeploy_rotated_detector_apply(mm_handle_t handle, const mm_mat_t* mats, int mat_count, |
| 78 | + mm_rotated_detect_t** results, int** result_count) { |
| 79 | + if (handle == nullptr || mats == nullptr || mat_count == 0 || results == nullptr || |
| 80 | + result_count == nullptr) { |
| 81 | + return MM_E_INVALID_ARG; |
| 82 | + } |
| 83 | + |
| 84 | + try { |
| 85 | + auto detector = static_cast<Handle*>(handle); |
| 86 | + |
| 87 | + Value input{Value::kArray}; |
| 88 | + for (int i = 0; i < mat_count; ++i) { |
| 89 | + mmdeploy::Mat _mat{mats[i].height, mats[i].width, PixelFormat(mats[i].format), |
| 90 | + DataType(mats[i].type), mats[i].data, Device{"cpu"}}; |
| 91 | + input.front().push_back({{"ori_img", _mat}}); |
| 92 | + } |
| 93 | + |
| 94 | + auto output = detector->Run(std::move(input)).value().front(); |
| 95 | + auto detector_outputs = from_value<vector<mmrotate::RotatedDetectorOutput>>(output); |
| 96 | + |
| 97 | + vector<int> _result_count; |
| 98 | + _result_count.reserve(mat_count); |
| 99 | + for (const auto& det_output : detector_outputs) { |
| 100 | + _result_count.push_back((int)det_output.detections.size()); |
| 101 | + } |
| 102 | + |
| 103 | + auto total = std::accumulate(_result_count.begin(), _result_count.end(), 0); |
| 104 | + |
| 105 | + std::unique_ptr<int[]> result_count_data(new int[_result_count.size()]{}); |
| 106 | + std::copy(_result_count.begin(), _result_count.end(), result_count_data.get()); |
| 107 | + |
| 108 | + std::unique_ptr<mm_rotated_detect_t[]> result_data(new mm_rotated_detect_t[total]{}); |
| 109 | + auto result_ptr = result_data.get(); |
| 110 | + |
| 111 | + for (const auto& det_output : detector_outputs) { |
| 112 | + for (const auto& detection : det_output.detections) { |
| 113 | + result_ptr->label_id = detection.label_id; |
| 114 | + result_ptr->score = detection.score; |
| 115 | + const auto& rbbox = detection.rbbox; |
| 116 | + for (int i = 0; i < 5; i++) { |
| 117 | + result_ptr->rbbox[i] = rbbox[i]; |
| 118 | + } |
| 119 | + ++result_ptr; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + *result_count = result_count_data.release(); |
| 124 | + *results = result_data.release(); |
| 125 | + |
| 126 | + return MM_SUCCESS; |
| 127 | + |
| 128 | + } catch (const std::exception& e) { |
| 129 | + MMDEPLOY_ERROR("exception caught: {}", e.what()); |
| 130 | + } catch (...) { |
| 131 | + MMDEPLOY_ERROR("unknown exception caught"); |
| 132 | + } |
| 133 | + return MM_E_FAIL; |
| 134 | +} |
| 135 | + |
| 136 | +void mmdeploy_rotated_detector_release_result(mm_rotated_detect_t* results, |
| 137 | + const int* result_count) { |
| 138 | + delete[] results; |
| 139 | + delete[] result_count; |
| 140 | +} |
| 141 | + |
| 142 | +void mmdeploy_rotated_detector_destroy(mm_handle_t handle) { delete static_cast<Handle*>(handle); } |
0 commit comments