mirror of
https://github.com/pjreddie/darknet.git
synced 2023-08-10 21:13:14 +03:00
python stuff
This commit is contained in:
parent
d7088fdec1
commit
0158fb23b2
@ -38,14 +38,18 @@ import darknet as dn
|
|||||||
# Darknet
|
# Darknet
|
||||||
net = dn.load_net("cfg/tiny-yolo.cfg", "tiny-yolo.weights", 0)
|
net = dn.load_net("cfg/tiny-yolo.cfg", "tiny-yolo.weights", 0)
|
||||||
meta = dn.load_meta("cfg/coco.data")
|
meta = dn.load_meta("cfg/coco.data")
|
||||||
r = dn.detect(net, meta, "data/dog.jpg")
|
import time
|
||||||
print r
|
tStart = time.time()
|
||||||
|
for i in range(10):
|
||||||
|
r = dn.detect(net, meta, "data/dog.jpg")
|
||||||
|
print(time.time() - tStart)
|
||||||
|
|
||||||
# scipy
|
# scipy
|
||||||
arr= imread('data/dog.jpg')
|
arr= imread('data/dog.jpg')
|
||||||
im = array_to_image(arr)
|
tStart = time.time()
|
||||||
r = detect2(net, meta, im)
|
for i in range(10):
|
||||||
print r
|
r = dn.detect(net, meta, arr)
|
||||||
|
print(time.time() - tStart)
|
||||||
|
|
||||||
# OpenCV
|
# OpenCV
|
||||||
arr = cv2.imread('data/dog.jpg')
|
arr = cv2.imread('data/dog.jpg')
|
||||||
|
@ -9,20 +9,11 @@ sys.path.append(os.path.join(os.getcwd(),'python/'))
|
|||||||
import darknet as dn
|
import darknet as dn
|
||||||
import pdb
|
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)
|
dn.set_gpu(0)
|
||||||
net = dn.load_net("cfg/tiny-yolo.cfg", "tiny-yolo.weights", 0)
|
net = dn.load_net("cfg/tiny-yolo.cfg", "tiny-yolo.weights", 0)
|
||||||
meta = dn.load_meta("cfg/coco.data")
|
meta = dn.load_meta("cfg/coco.data")
|
||||||
r = dn.detect(net, meta, "data/dog.jpg")
|
r = dn.detect(net, meta, "data/dog.jpg")
|
||||||
print r
|
print r
|
||||||
>>>>>>> 16686cec576580489ab3c7c78183e6efeafae780
|
|
||||||
|
|
||||||
# And then down here you could detect a lot more images like:
|
# And then down here you could detect a lot more images like:
|
||||||
rr = dn.detect(net, meta, "data/eagle.jpg")
|
rr = dn.detect(net, meta, "data/eagle.jpg")
|
||||||
|
@ -13,7 +13,10 @@ def sample(probs):
|
|||||||
return len(probs)-1
|
return len(probs)-1
|
||||||
|
|
||||||
def c_array(ctype, values):
|
def c_array(ctype, values):
|
||||||
return (ctype * len(values))(*values)
|
arr = (ctype * len(values))()
|
||||||
|
arr[:] = values
|
||||||
|
return arr
|
||||||
|
|
||||||
|
|
||||||
class BOX(Structure):
|
class BOX(Structure):
|
||||||
_fields_ = [("x", c_float),
|
_fields_ = [("x", c_float),
|
||||||
@ -31,7 +34,7 @@ class METADATA(Structure):
|
|||||||
_fields_ = [("classes", c_int),
|
_fields_ = [("classes", c_int),
|
||||||
("names", POINTER(c_char_p))]
|
("names", POINTER(c_char_p))]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#lib = CDLL("/home/pjreddie/documents/darknet/libdarknet.so", RTLD_GLOBAL)
|
#lib = CDLL("/home/pjreddie/documents/darknet/libdarknet.so", RTLD_GLOBAL)
|
||||||
lib = CDLL("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 = lib.network_detect
|
||||||
network_detect.argtypes = [c_void_p, IMAGE, c_float, c_float, c_float, POINTER(BOX), POINTER(POINTER(c_float))]
|
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):
|
def classify(net, meta, im):
|
||||||
out = predict_image(net, im)
|
out = predict_image(net, im)
|
||||||
res = []
|
res = []
|
||||||
@ -110,7 +125,10 @@ def classify(net, meta, im):
|
|||||||
return res
|
return res
|
||||||
|
|
||||||
def detect(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45):
|
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)
|
boxes = make_boxes(net)
|
||||||
probs = make_probs(net)
|
probs = make_probs(net)
|
||||||
num = num_boxes(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:
|
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.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])
|
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)
|
free_ptrs(cast(probs, POINTER(c_void_p)), num)
|
||||||
return res
|
return res
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
#net = load_net("cfg/densenet201.cfg", "/home/pjreddie/trained/densenet201.weights", 0)
|
#net = load_net("cfg/densenet201.cfg", "/home/pjreddie/trained/densenet201.weights", 0)
|
||||||
#im = load_image("data/wolf.jpg", 0, 0)
|
#im = load_image("data/wolf.jpg", 0, 0)
|
||||||
@ -135,5 +155,5 @@ if __name__ == "__main__":
|
|||||||
meta = load_meta("cfg/coco.data")
|
meta = load_meta("cfg/coco.data")
|
||||||
r = detect(net, meta, "data/dog.jpg")
|
r = detect(net, meta, "data/dog.jpg")
|
||||||
print(r)
|
print(r)
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user