From 0158fb23b2e25436620ab8a452d6201bebf6b8f7 Mon Sep 17 00:00:00 2001 From: Daniel Gordon Date: Tue, 13 Mar 2018 11:25:44 -0700 Subject: [PATCH] python stuff --- examples/detector-scipy-opencv.py | 14 +++++++++----- examples/detector.py | 9 --------- python/darknet.py | 32 +++++++++++++++++++++++++------ 3 files changed, 35 insertions(+), 20 deletions(-) diff --git a/examples/detector-scipy-opencv.py b/examples/detector-scipy-opencv.py index 3bfc5913..88efaa24 100644 --- a/examples/detector-scipy-opencv.py +++ b/examples/detector-scipy-opencv.py @@ -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') diff --git a/examples/detector.py b/examples/detector.py index a9b19f29..b95ab71a 100644 --- a/examples/detector.py +++ b/examples/detector.py @@ -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") diff --git a/python/darknet.py b/python/darknet.py index a745eef5..d0c6e961 100644 --- a/python/darknet.py +++ b/python/darknet.py @@ -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) - +