Skip to content

Commit

Permalink
* Prepare image data by converting to float32 and transposing instead…
Browse files Browse the repository at this point in the history
… of iterating.

Speed is much faster, as expected.

Part of JdeRobot#38
  • Loading branch information
pcuenca committed Oct 1, 2018
1 parent 9f71dc4 commit f006b8e
Showing 1 changed file with 8 additions and 24 deletions.
32 changes: 8 additions & 24 deletions Net/Darknet/darknet/darknet.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,29 +149,14 @@ def detect(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45):

# Detect from numpy image
def detect_from_image(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45):
# Make an empty image and copy the values
# I tried to pass a pointer to the floats but didn't know how
# If we really need to do this it should be in C
h = image.shape[0]
w = image.shape[1]
c = image.shape[2]
im = make_image(w, h, c)
for x in np.arange(w):
for y in np.arange(h):
for l in np.arange(c):
pixel = float(image[y, x, l]) / 255.
#set_pixel(im, x, y, l, pixel)
im.data[ l * im.h * im.w + y * im.w + x ] = pixel

# im = IMAGE()
# im.h = image.shape[0]
# im.w = image.shape[1]
# im.c = image.shape[2]
# f_image = image.astype(float) / 255.
# #im.data = LP_c_float(f_image.ctypes.data)
# #im.data = byref(f_image.ctypes)
# LP_c_float = POINTER(c_float)
# im.data = byref(c_float(f_image[0, 0, 0]))
im = IMAGE()
im.h = image.shape[0]
im.w = image.shape[1]
im.c = image.shape[2]

f_image = image.astype(np.float32) / 255.
f_image = f_image.transpose(2, 0, 1).flatten()
im.data = f_image.ctypes.data_as(POINTER(c_float))

num = c_int(0)
pnum = pointer(num)
Expand All @@ -187,7 +172,6 @@ def detect_from_image(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45):
b = dets[j].bbox
res.append((meta.names[i], dets[j].prob[i], (b.x, b.y, b.w, b.h)))
res = sorted(res, key=lambda x: -x[1])
free_image(im)
free_detections(dets, num)
return res

Expand Down

0 comments on commit f006b8e

Please sign in to comment.