python stuff

This commit is contained in:
Daniel Gordon 2018-03-13 11:25:44 -07:00
parent d7088fdec1
commit 0158fb23b2
3 changed files with 35 additions and 20 deletions

View File

@ -38,14 +38,18 @@ import darknet as dn
# Darknet
net = dn.load_net("cfg/tiny-yolo.cfg", "tiny-yolo.weights", 0)
meta = dn.load_meta("cfg/coco.data")
r = dn.detect(net, meta, "data/dog.jpg")
print r
import time
tStart = time.time()
for i in range(10):
r = dn.detect(net, meta, "data/dog.jpg")
print(time.time() - tStart)
# scipy
arr= imread('data/dog.jpg')
im = array_to_image(arr)
r = detect2(net, meta, im)
print r
tStart = time.time()
for i in range(10):
r = dn.detect(net, meta, arr)
print(time.time() - tStart)
# OpenCV
arr = cv2.imread('data/dog.jpg')

View File

@ -9,20 +9,11 @@ sys.path.append(os.path.join(os.getcwd(),'python/'))
import darknet as dn
import pdb
<<<<<<< HEAD
net = dn.load_net("cfg/yolo-tag.cfg", "yolo-tag_final.weights", 0)
meta = dn.load_meta("cfg/openimages.data")
pdb.set_trace()
rr = dn.detect(net, meta, 'data/dog.jpg')
print rr
pdb.set_trace()
=======
dn.set_gpu(0)
net = dn.load_net("cfg/tiny-yolo.cfg", "tiny-yolo.weights", 0)
meta = dn.load_meta("cfg/coco.data")
r = dn.detect(net, meta, "data/dog.jpg")
print r
>>>>>>> 16686cec576580489ab3c7c78183e6efeafae780
# And then down here you could detect a lot more images like:
rr = dn.detect(net, meta, "data/eagle.jpg")

View File

@ -13,7 +13,10 @@ def sample(probs):
return len(probs)-1
def c_array(ctype, values):
return (ctype * len(values))(*values)
arr = (ctype * len(values))()
arr[:] = values
return arr
class BOX(Structure):
_fields_ = [("x", c_float),
@ -31,7 +34,7 @@ class METADATA(Structure):
_fields_ = [("classes", c_int),
("names", POINTER(c_char_p))]
#lib = CDLL("/home/pjreddie/documents/darknet/libdarknet.so", RTLD_GLOBAL)
lib = CDLL("libdarknet.so", RTLD_GLOBAL)
@ -101,6 +104,18 @@ predict_image.restype = POINTER(c_float)
network_detect = lib.network_detect
network_detect.argtypes = [c_void_p, IMAGE, c_float, c_float, c_float, POINTER(BOX), POINTER(POINTER(c_float))]
import numpy
def array_to_image(arr):
arr = arr.copy()
arr = arr.transpose(2,0,1)
c = arr.shape[0]
h = arr.shape[1]
w = arr.shape[2]
arr = (arr.astype(numpy.float32)/255.0).flatten()
data = c_array(c_float, arr)
im = IMAGE(w,h,c,data)
return im
def classify(net, meta, im):
out = predict_image(net, im)
res = []
@ -110,7 +125,10 @@ def classify(net, meta, im):
return res
def detect(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45):
im = load_image(image, 0, 0)
if type(image) == numpy.ndarray:
im = array_to_image(image)
else:
im = load_image(image, 0, 0)
boxes = make_boxes(net)
probs = make_probs(net)
num = num_boxes(net)
@ -121,10 +139,12 @@ def detect(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45):
if probs[j][i] > 0:
res.append((meta.names[i], probs[j][i], (boxes[j].x, boxes[j].y, boxes[j].w, boxes[j].h)))
res = sorted(res, key=lambda x: -x[1])
free_image(im)
if type(image) != numpy.ndarray:
free_image(im)
free_ptrs(cast(probs, POINTER(c_void_p)), num)
return res
if __name__ == "__main__":
#net = load_net("cfg/densenet201.cfg", "/home/pjreddie/trained/densenet201.weights", 0)
#im = load_image("data/wolf.jpg", 0, 0)
@ -135,5 +155,5 @@ if __name__ == "__main__":
meta = load_meta("cfg/coco.data")
r = detect(net, meta, "data/dog.jpg")
print(r)