Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Python.h>
- class PyAdapter {
- private:
- PyObject *pModule;
- PyObject *pModel;
- PyObject *pDetectionFunc;
- std::vector< std::pair<char, cv::Rect>> labels;
- int PyAdapter::LoadModel() {
- //---- initialize python
- wchar_t* argv[] = { L"test_application" };
- Py_SetProgramName(argv[0]);
- Py_SetPythonHome(L"D:\\Programs\\Python\\Python36-x64");
- Py_Initialize();
- if (!Py_IsInitialized()){
- fprintf(stderr, "Error initializing Python interpreter");
- return -1;
- }
- PySys_SetArgv(1, argv);
- //---- set path
- PyObject *sys_path, *path;
- sys_path = PySys_GetObject("path");
- path = PyUnicode_FromString("D:\\python\\py-master\\");
- PyList_Append(sys_path, path);
- //---- load module
- PyObject *pName;
- pName = PyUnicode_FromString("detection");
- this->pModule = PyImport_Import(pName);
- Py_DECREF(pName);
- if (this->pModule == NULL) {
- Py_DECREF(this->pModule);
- PyErr_Print();
- fprintf(stderr, "Failed to load module");
- return -1;
- }
- //---- load model
- PyObject *pFunc, *pArgs, *pArg;
- pFunc = PyObject_GetAttrString(this->pModule, "model_init");
- if (!pFunc || !PyCallable_Check(pFunc)) {
- if (PyErr_Occurred())
- PyErr_Print();
- fprintf(stderr, "Cannot run function");
- return -1;
- }
- pArgs = PyTuple_New(1);
- pArg = PyUnicode_FromString("D:\\python\\py-master\\models\\m1.bin");
- PyTuple_SetItem(pArgs, 0, pArg);
- this->pModel = PyObject_CallObject(pFunc, pArgs);
- Py_DECREF(pArgs);
- Py_DECREF(pFunc);
- if (this->pModel == NULL) {
- Py_DECREF(this->pModule);
- PyErr_Print();
- fprintf(stderr, "Model load failed\n");
- return -1;
- }
- //---- get detection function
- this->pDetectionFunc = PyObject_GetAttrString(this->pModule, "detect_me");
- if (!this->pDetectionFunc || !PyCallable_Check(this->pDetectionFunc)) {
- if (PyErr_Occurred())
- PyErr_Print();
- fprintf(stderr, "Cannot run function");
- return -1;
- }
- return 0;
- }
- int PyAdapter::UnloadModel() {
- Py_XDECREF(pDetectionFunc);
- Py_XDECREF(pModel);
- Py_XDECREF(pModule);
- Py_Finalize();
- return 0;
- }
- public:
- PyAdapter::PyAdapter() {
- if (this->LoadModel() < 0)
- throw std::runtime_error("Error on model load.");
- }
- PyAdapter::~PyAdapter() {
- if (this->UnloadModel() < 0)
- throw std::runtime_error("Error on model unload.");
- }
- std::vector< std::pair<char, cv::Rect>> &GetLabels() {
- return labels;
- }
- int PyAdapter::Detect(cv::Mat &img) {
- PyObject *pArgs, *pArg;
- PyObject *pDetection;
- pArgs = PyTuple_New(5);
- PyTuple_SetItem(pArgs, 0, this->pModel);
- pArg = PyByteArray_FromStringAndSize((char*)img.data, img.total() * img.elemSize());
- PyTuple_SetItem(pArgs, 1, pArg);
- PyTuple_SetItem(pArgs, 2, PyLong_FromLong(img.cols));
- PyTuple_SetItem(pArgs, 3, PyLong_FromLong(img.rows));
- PyTuple_SetItem(pArgs, 4, PyLong_FromLong(img.channels()));
- pDetection = PyObject_CallObject(this->pDetectionFunc, pArgs);
- Py_DECREF(pArgs);
- if (pDetection == NULL || !PyByteArray_Check(pDetection)) {
- if (PyErr_Occurred())
- PyErr_Print();
- fprintf(stderr, "Invalid result\n");
- return -1;
- }
- //---- change to opencv mat
- this->labels.clear();
- int NUM_CLASS = 10;
- int SIZE = PyByteArray_Size(pDetection);
- float *data = (float *)PyByteArray_AsString(pDetection);
- Mat det(SIZE / ((NUM_CLASS + 4) * 4), NUM_CLASS + 4, CV_32FC1, data);
- Mat scores = det(cv::Rect(4, 0, det.cols - 4, det.rows));
- vector<pair<char, Rect>> labels;
- double min = 0, max = 0;
- Point minLoc, maxLoc;
- for (int i = 0; i < scores.rows; i++) {
- minMaxLoc(scores.row(i), &min, &max, &minLoc, &maxLoc);
- if (max > 0.5) {
- int x1 = (int)det.at<float>(i, 0),
- y1 = (int)det.at<float>(i, 1),
- x2 = (int)det.at<float>(i, 2),
- y2 = (int)det.at<float>(i, 3);
- this->labels.push_back(pair<char, Rect>(maxLoc.x, Rect(x1, y1, x2 - x1, y2 - y1)));
- }
- }
- Py_DECREF(pDetection);
- return 0;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement