Skip to content

Commit dd330a8

Browse files
committed
Add network_depth (net->c)
1 parent 1d3ffc4 commit dd330a8

File tree

2 files changed

+56
-5
lines changed

2 files changed

+56
-5
lines changed

src/darknet/py/network.pyx

Lines changed: 48 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,10 @@ cdef class Network:
6868
def shape(self):
6969
return dn.network_width(self._c_network), dn.network_height(self._c_network)
7070

71+
@property
72+
def depth(self):
73+
return dn.network_depth(self._c_network)
74+
7175
def input_size(self):
7276
return dn.network_input_size(self._c_network)
7377

@@ -126,4 +130,47 @@ cdef class Network:
126130
letterbox)
127131
rv = convert_detections_to_tuples(detections, num_dets, nms_type, nms_threshold)
128132
dn.free_detections(detections, num_dets)
129-
return sorted(rv, key=lambda x: x[1], reverse=True)
133+
134+
return rv
135+
136+
def detect_batch(self,
137+
int batch_size,
138+
np.ndarray[dtype=np.float32_t, ndim=1, mode="c"] frames,
139+
frame_size=None,
140+
float threshold=.5,
141+
float hierarchical_threshold=.5,
142+
int relative=0,
143+
int letterbox=1,
144+
str nms_type="sort",
145+
float nms_threshold=.45
146+
):
147+
pred_width, pred_height = self.shape if frame_size is None else frame_size
148+
149+
cdef dn.image imr
150+
# This looks awkward, but the batch predict *does not* use c, w, h.
151+
imr.c = 0
152+
imr.w = 0
153+
imr.h = 0
154+
imr.data = <float *> frames.data
155+
156+
cdef dn.det_num_pair* batch_detections
157+
batch_detections = dn.network_predict_batch(
158+
self._c_network,
159+
imr,
160+
batch_size,
161+
pred_width,
162+
pred_height,
163+
threshold,
164+
hierarchical_threshold,
165+
<int*>0,
166+
relative,
167+
letterbox
168+
)
169+
rv = [
170+
convert_detections_to_tuples(batch_detections[b].dets, batch_detections[b].num, nms_type, nms_threshold)
171+
for b in range(batch_size)
172+
]
173+
dn.free_batch_detections(batch_detections, batch_size)
174+
return rv
175+
176+

src/libdarknet/__init__.pxd

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,16 @@ cdef extern from "darknet.h":
55
/*
66
* darknet.h forgot to extern some useful network functions
77
*/
8+
static int network_depth(network* net) {
9+
return net->c;
10+
}
811
static int network_input_size(network* net) {
9-
return net->layers[0].inputs;
12+
return net->layers[0].inputs;
1013
}
1114
static int network_output_size(network* net) {
12-
int i;
13-
for(i = net->n-1; i > 0; --i) if(net->layers[i].type != COST) break;
14-
return net->layers[i].outputs;
15+
int i;
16+
for(i = net->n-1; i > 0; --i) if(net->layers[i].type != COST) break;
17+
return net->layers[i].outputs;
1518
}
1619
"""
1720

@@ -71,6 +74,7 @@ cdef extern from "darknet.h":
7174

7275
int network_width(network *self);
7376
int network_height(network *self);
77+
int network_depth(network *self);
7478
int network_input_size(network* self);
7579
int network_output_size(network* self);
7680
float* network_predict(network, float* input)

0 commit comments

Comments
 (0)