-
Notifications
You must be signed in to change notification settings - Fork 64
/
Copy pathmodule.c
180 lines (150 loc) · 4.04 KB
/
module.c
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
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
#include <Python.h>
#include <numpy/arrayobject.h>
#include <stdio.h>
#include "libyolo.h"
#include "./darknet/src/image.h"
static PyObject *PyyoloError;
static yolo_handle g_handle = NULL;
static PyObject *pyyolo_init(PyObject *self, PyObject *args)
{
char *darknet_path;
char *datacfg;
char *cfgfile;
char *weightfile;
if (!PyArg_ParseTuple(args, "ssss", &darknet_path, &datacfg, &cfgfile, &weightfile))
return NULL;
g_handle = yolo_init(darknet_path, datacfg, cfgfile, weightfile);
if (!g_handle) {
PyErr_SetString(PyyoloError, "Initialzing YOLO failed");
return Py_None;
}
return Py_None;
}
static PyObject *pyyolo_cleanup(PyObject *self, PyObject *args)
{
yolo_cleanup(g_handle);
g_handle = NULL;
return Py_None;
}
static PyObject *pyyolo_detect(PyObject *self, PyObject *args)
{
int w;
int h;
int c;
PyArrayObject *array;
float thresh;
float hier_thresh;
if (!PyArg_ParseTuple(args, "iiiOff", &w, &h, &c, &array, &thresh, &hier_thresh))
return NULL;
// convert (copy) PyArrayObject(float32) to float []
float *data;
data = (float*) PyArray_GETPTR1(array, 0); // data is a ptr to a c array
image img = float_to_image(w, h, c, data);
rgbgr_image(img);
int num = 0;
detection_info **info = yolo_detect(g_handle, img, thresh, hier_thresh, &num);
if (info == NULL) {
PyErr_SetString(PyyoloError, "Testing YOLO failed");
return Py_None;
}
PyObject *dict = NULL;
PyObject *list = PyList_New(num);
int i;
for (i = 0; i < num; i++) {
dict = Py_BuildValue("{s:s,s:i,s:i,s:i,s:i,s:f}",
"class", info[i]->name,
"left", info[i]->left,
"right", info[i]->right,
"top", info[i]->top,
"bottom", info[i]->bottom,
"prob", info[i]->prob);
PyList_SetItem(list, i, dict);
free(info[i]);
}
free(info);
return list;
}
static PyObject *pyyolo_test(PyObject *self, PyObject *args)
{
char *filename;
float thresh;
float hier_thresh;
float *feature_map = NULL;
int map_size = 0;
int return_map = 0;
if (!PyArg_ParseTuple(args, "sffi", &filename, &thresh, &hier_thresh, &return_map))
return NULL;
int num = 0;
detection_info **info = yolo_test(g_handle, filename, thresh, hier_thresh, &num, &feature_map, &map_size);
if (info == NULL) {
PyErr_SetString(PyyoloError, "Testing YOLO failed");
return Py_None;
}
PyObject *dict = NULL;
PyObject *list = NULL;
if (return_map) {
list = PyList_New(num + 1);
} else {
list = PyList_New(num);
}
int i;
for (i = 0; i < num; i++) {
dict = Py_BuildValue("{s:s,s:i,s:i,s:i,s:i,s:f}",
"class", info[i]->name,
"left", info[i]->left,
"right", info[i]->right,
"top", info[i]->top,
"bottom", info[i]->bottom,
"prob", info[i]->prob);
PyList_SetItem(list, i, dict);
free(info[i]);
}
free(info);
/* if (return_map) {
import_array();
npy_intp dims[] = {map_size};
PyObject *data = PyArray_SimpleNewFromData(1, dims, NPY_FLOAT, feature_map);
Py_INCREF(data);
dict = Py_BuildValue("{s:O}", "feature_map", data);
PyList_SetItem(list, i, dict);
} */
return list;
}
static PyMethodDef pyyolo_methods[] = {
{"init", pyyolo_init, METH_VARARGS, "Initialize YOLO."},
{"cleanup", pyyolo_cleanup, METH_VARARGS, "Cleanup YOLO."},
{"detect", pyyolo_detect, METH_VARARGS, "Test image (image array)."},
{"test", pyyolo_test, METH_VARARGS, "Test image (image file)."},
{NULL, NULL, 0, NULL} /* Sentinel */
};
#if PY_MAJOR_VERSION >= 3
static struct PyModuleDef pyyolo_module =
{
PyModuleDef_HEAD_INIT,
"pyyolo",
"",
-1,
pyyolo_methods
};
PyMODINIT_FUNC PyInit_pyyolo(void)
{
PyObject *m;
m = PyModule_Create(&pyyolo_module);
if (m == NULL) return;
PyyoloError = PyErr_NewException("pyyolo.error", NULL, NULL);
Py_INCREF(PyyoloError);
PyModule_AddObject(m, "error", PyyoloError);
return m;
}
#else
PyMODINIT_FUNC initpyyolo(void)
{
PyObject *m;
m = Py_InitModule("pyyolo", pyyolo_methods);
if (m == NULL) return;
PyyoloError = PyErr_NewException("pyyolo.error", NULL, NULL);
Py_INCREF(PyyoloError);
PyModule_AddObject(m, "error", PyyoloError);
}
#endif