// Copyright (c) OpenMMLab. All rights reserved. #include "common.h" namespace mmdeploy { std::map& gPythonBindings() { static std::map v; return v; } mm_mat_t GetMat(const PyImage& img) { auto info = img.request(); if (info.ndim != 3) { fprintf(stderr, "info.ndim = %d\n", (int)info.ndim); throw std::runtime_error("continuous uint8 HWC array expected"); } auto channels = (int)info.shape[2]; mm_mat_t mat{}; if (channels == 1) { mat.format = MM_GRAYSCALE; } else if (channels == 3) { mat.format = MM_BGR; } else { throw std::runtime_error("images of 1 or 3 channels are supported"); } mat.height = (int)info.shape[0]; mat.width = (int)info.shape[1]; mat.channel = channels; mat.type = MM_INT8; mat.data = (uint8_t*)info.ptr; return mat; } #if 0 py::object ConvertToPyObject(const Value& value) { switch (value.type()) { case ValueType::kNull: return py::none(); case ValueType::kBool: return py::bool_(value.get()); case ValueType::kInt: return py::int_(value.get()); case ValueType::kUInt: return py::int_(value.get()); case ValueType::kFloat: return py::float_(value.get()); case ValueType::kString: return py::str(value.get()); case ValueType::kArray: { py::list list; for (const auto& x : value) { list.append(ConvertToPyObject(x)); } return list; } case ValueType::kObject: { py::dict dict; for (auto it = value.begin(); it != value.end(); ++it) { dict[it.key().c_str()] = ConvertToPyObject(*it); } return dict; } case ValueType::kAny: return py::str(""); default: return py::str(""); } } Value ConvertToValue(const py::object& obj) { if (py::isinstance(obj)) { return nullptr; } else if (py::isinstance(obj)) { return obj.cast(); } else if (py::isinstance(obj)) { return obj.cast(); } else if (py::isinstance(obj)) { return obj.cast(); } else if (py::isinstance(obj)) { return obj.cast(); } else if (py::isinstance(obj)) { py::list src(obj); Value::Array dst; dst.reserve(src.size()); for (const auto& item : src) { dst.push_back(ConvertToValue(py::reinterpret_borrow(item))); } return dst; } else if (py::isinstance(obj)) { py::dict src(obj); Value::Object dst; for (const auto& item : src) { dst.insert({item.first.cast(), ConvertToValue(py::reinterpret_borrow(item.second))}); } return dst; } else { MMDEPLOY_ERROR("unsupported Python object type: {}", obj.get_type().cast()); return nullptr; } } #endif } // namespace mmdeploy PYBIND11_MODULE(mmdeploy_python, m) { for (const auto& [_, f] : mmdeploy::gPythonBindings()) { f(m); } }