// Copyright 2018 Slightech Co., Ltd. All rights reserved. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // #include #include #include #include #include #include #include #include #include #include #include "mynteye/api.h" #include "mynteye/glog_init.h" #include "mynteye/utils.h" #include "array_indexing_suite.hpp" #include "array_ref.hpp" // #define PY_ARRAY_UNIQUE_SYMBOL pbcvt_ARRAY_API // #include "pyboostcvconverter/pyboostcvconverter.hpp" #include "np_opencv_converter.hpp" #define ENUM_EXPORT_VALUES namespace bp = boost::python; namespace { template inline void std_vector_assign( std::vector &l, const bp::object &o) { // NOLINT l.assign(bp::stl_input_iterator(o), bp::stl_input_iterator()); } template inline std::vector py_list_to_std_vector(const bp::object &o) { return std::vector( bp::stl_input_iterator(o), bp::stl_input_iterator()); } template inline bp::list std_vector_to_py_list(const std::vector &v) { bp::list l; for (auto &&val : v) { l.append(val); } return l; } template char **new_cstrings(const Container &strings, std::size_t n) { char **cstrings = new char *[n]; for (std::size_t i = 0; i < n; i++) { cstrings[i] = new char[strings[i].size() + 1]; std::strcpy(cstrings[i], strings[i].c_str()); // NOLINT } return cstrings; } void del_cstrings(char **cstrings, std::size_t n) { for (std::size_t i = 0; i < n; i++) { delete[] cstrings[i]; } delete[] cstrings; } } // namespace MYNTEYE_BEGIN_NAMESPACE namespace python { // api wrapper struct MYNTEYE_API StreamData { /** ImgData. */ ImgData img; /** Frame. */ PyObject *frame; bool operator==(const StreamData &other) const { return img.frame_id == other.img.frame_id && img.timestamp == other.img.timestamp; } }; struct MYNTEYE_API MotionData { /** ImuData. */ ImuData imu; bool operator==(const MotionData &other) const { return imu.frame_id == other.imu.frame_id && imu.timestamp == other.imu.timestamp; } }; class MYNTEYE_API APIWrap : public API { public: explicit APIWrap(std::shared_ptr device) : API(device) {} ~APIWrap() {} static std::shared_ptr Create() { auto &&device = device::select(); if (!device) return nullptr; return std::make_shared(device); } static std::shared_ptr Create(int argc, char *argv[]) { static glog_init _(argc, argv); auto &&device = device::select(); if (!device) return nullptr; return std::make_shared(device); } python::StreamData GetStreamData(const Stream &stream) { auto &&data = API::GetStreamData(stream); // return {*data.img, pbcvt::fromMatToNDArray(data.frame)}; return {*data.img, fs::python::Mat_to_PyObject::convert(data.frame)}; } std::vector GetStreamDatas(const Stream &stream) { std::vector datas; for (auto &&data : API::GetStreamDatas(stream)) { // datas.push_back({*data.img, pbcvt::fromMatToNDArray(data.frame)}); datas.push_back( {*data.img, fs::python::Mat_to_PyObject::convert(data.frame)}); } return datas; } std::vector GetMotionDatas() { std::vector datas; for (auto &&data : API::GetMotionDatas()) { datas.push_back({*data.imu}); } return datas; } }; // api create static methods std::shared_ptr (*api_create_1)() = &APIWrap::Create; std::shared_ptr api_create_2(bp::list argv) { auto &&args = py_list_to_std_vector(argv); auto &&n = args.size(); if (n == 0) { return APIWrap::Create(); } char **cstrings = new_cstrings(args, n); auto &&api = APIWrap::Create(args.size(), cstrings); del_cstrings(cstrings, n); return api; } // glog_init create static methods std::shared_ptr glog_init_create(bp::list argv) { auto &&args = py_list_to_std_vector(argv); auto &&n = args.size(); assert(n > 0); char **cstrings = new_cstrings(args, n); auto &&ret = std::make_shared(args.size(), cstrings); del_cstrings(cstrings, n); return ret; } // BOOST_PYTHON_MODULE BOOST_PYTHON_MODULE(mynteye_py) { /* Py_Initialize(); import_array(); bp::to_python_converter(); pbcvt::matFromNDArrayBoostConverter(); */ fs::python::init_and_export_converters(); py::scope scope = py::scope(); bp::class_>("DoubleArray") .def(array_indexing_suite>()); bp::class_>>("Double2DArray") .def(array_indexing_suite>>{}); // types.h - enums bp::enum_("Model") .value("STANDARD", Model::STANDARD) #ifdef ENUM_EXPORT_VALUES .export_values() #endif ; // NOLINT bp::enum_("Stream") .value("LEFT", Stream::LEFT) .value("RIGHT", Stream::RIGHT) .value("LEFT_RECTIFIED", Stream::LEFT_RECTIFIED) .value("RIGHT_RECTIFIED", Stream::RIGHT_RECTIFIED) .value("DISPARITY", Stream::DISPARITY) .value("DISPARITY_NORMALIZED", Stream::DISPARITY_NORMALIZED) .value("DEPTH", Stream::DEPTH) .value("POINTS", Stream::POINTS) #ifdef ENUM_EXPORT_VALUES .export_values() #endif ; // NOLINT bp::enum_("Capabilities") .value("STEREO", Capabilities::STEREO) .value("COLOR", Capabilities::COLOR) .value("DEPTH", Capabilities::DEPTH) .value("POINTS", Capabilities::POINTS) .value("FISHEYE", Capabilities::FISHEYE) .value("INFRARED", Capabilities::INFRARED) .value("INFRARED2", Capabilities::INFRARED2) .value("IMU", Capabilities::IMU) #ifdef ENUM_EXPORT_VALUES .export_values() #endif ; // NOLINT bp::enum_("Info") .value("DEVICE_NAME", Info::DEVICE_NAME) .value("SERIAL_NUMBER", Info::SERIAL_NUMBER) .value("FIRMWARE_VERSION", Info::FIRMWARE_VERSION) .value("HARDWARE_VERSION", Info::HARDWARE_VERSION) .value("SPEC_VERSION", Info::SPEC_VERSION) .value("LENS_TYPE", Info::LENS_TYPE) .value("IMU_TYPE", Info::IMU_TYPE) .value("NOMINAL_BASELINE", Info::NOMINAL_BASELINE) #ifdef ENUM_EXPORT_VALUES .export_values() #endif ; // NOLINT bp::enum_