-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun_python.cpp
More file actions
181 lines (155 loc) · 3.61 KB
/
Copy pathrun_python.cpp
File metadata and controls
181 lines (155 loc) · 3.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <stdio.h>
#include <unistd.h>
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include "Python.h"
#include "numpy/arrayobject.h"
#include "opencv2/opencv.hpp"
void python_init()
{
Py_Initialize();
}
PyObject *python_create_module_from_string(char *module_name, char *code_string)
{
PyObject *module = NULL;
PyObject *compiled_code = Py_CompileString(code_string, "<string>", Py_file_input);
if(compiled_code != nullptr)
{
module = PyImport_ExecCodeModule(module_name, compiled_code);
Py_DECREF(compiled_code);
}
return(module);
}
int python_kludge()
{
import_array1(-1);
return(0);
}
PyObject *python_create_code_module(char *name, char *code)
{
PyObject *module = NULL;
PyObject *sys_path = PySys_GetObject("path");
PyList_Append(sys_path, PyUnicode_FromString("."));
python_kludge();
if(code == NULL)
{
module = PyImport_ImportModule(name);
}
else
{
module = python_create_module_from_string(name, code);
}
return(module);
}
PyObject *python_discover_function(PyObject *module, char *name)
{
// get dictionary of available items in the module
PyObject *dictionary = PyModule_GetDict(module);
// grab the functions we are interested in
PyObject *function = PyDict_GetItemString(dictionary, name);
return(function);
}
PyObject *python_call_function(PyObject *function, PyObject *args)
{
PyObject *r = PyEval_CallObject(function, args);
return(r);
}
PyObject *python_prepare_mat(cv::Mat img)
{
int width = img.cols;
int height = img.rows;
int depth = img.channels();
// total number of elements (here it's an RGB image of size 640x480)
const unsigned int nElem = width * height * depth;
// create an array of apropriate datatype
uchar *m = img.data;
// the dimensions of the matrix
npy_intp mdim[] = { height, width, depth };
// convert the cv::Mat to numpy.array
PyObject *mat = PyArray_SimpleNewFromData(3, mdim, NPY_UINT8, (void *)m);
// create a Python-tuple of arguments for the function call
// "()" means "tuple". "O" means "object"
PyObject *args = Py_BuildValue("(O)", mat);
return(args);
}
void python_xderefrence(void *ptr)
{
if(ptr != NULL)
{
Py_XDECREF(ptr);
}
}
void python_derefrence(void *ptr)
{
if(ptr != NULL)
{
Py_DECREF(ptr);
}
}
void *python_prepare_code(char *module_name, char *function_name, char *code)
{
static int init = 0;
if(init == 0)
{
python_init();
init = 1;
}
PyObject *function = NULL;
PyObject *module = python_create_code_module(module_name, code);
if(module != NULL)
{
function = python_discover_function(module, function_name);
}
return((void *)function);
}
void python_run_frame_filter(void *in_function, cv::Mat mat)
{
void *rr = NULL;
PyObject *function = (PyObject *)in_function;
PyObject *frame = python_prepare_mat(mat);
if(frame != NULL)
{
PyObject *r = python_call_function(function, frame);
if(r != NULL)
{
Py_DECREF(r);
}
Py_DECREF(frame);
}
}
void *python_run(void *in_function)
{
void *rr = NULL;
PyObject *function = (PyObject *)in_function;
PyObject *r = python_call_function(function, NULL);
rr = (void *)r;
return(rr);
}
void python_filter_mat(cv::Mat mat, char *code)
{
static int init = 0;
if(init == 0)
{
python_init();
init = 1;
}
PyObject *module = python_create_code_module("python_frame_filter", code);
if(module != NULL)
{
PyObject *function = python_discover_function(module, "python_filter");
if(function != NULL)
{
PyObject *frame = python_prepare_mat(mat);
if(frame != NULL)
{
PyObject *r = python_call_function(function, frame);
if(r != NULL)
{
Py_DECREF(r);
}
Py_DECREF(frame);
}
Py_XDECREF(function);
}
Py_DECREF(module);
}
}