2017-11-08 03:10:33 +03:00
|
|
|
# Stupid python path shit.
|
|
|
|
# Instead just add darknet.py to somewhere in your python path
|
|
|
|
# OK actually that might not be a great idea, idk, work in progress
|
|
|
|
# Use at your own risk. or don't, i don't care
|
|
|
|
|
|
|
|
from scipy.misc import imread
|
|
|
|
import cv2
|
|
|
|
|
|
|
|
def array_to_image(arr):
|
|
|
|
arr = arr.transpose(2,0,1)
|
|
|
|
c = arr.shape[0]
|
|
|
|
h = arr.shape[1]
|
|
|
|
w = arr.shape[2]
|
|
|
|
arr = (arr/255.0).flatten()
|
|
|
|
data = dn.c_array(dn.c_float, arr)
|
|
|
|
im = dn.IMAGE(w,h,c,data)
|
|
|
|
return im
|
|
|
|
|
|
|
|
def detect2(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45):
|
|
|
|
boxes = dn.make_boxes(net)
|
|
|
|
probs = dn.make_probs(net)
|
|
|
|
num = dn.num_boxes(net)
|
|
|
|
dn.network_detect(net, image, thresh, hier_thresh, nms, boxes, probs)
|
|
|
|
res = []
|
|
|
|
for j in range(num):
|
|
|
|
for i in range(meta.classes):
|
|
|
|
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])
|
|
|
|
dn.free_ptrs(dn.cast(probs, dn.POINTER(dn.c_void_p)), num)
|
|
|
|
return res
|
|
|
|
|
|
|
|
import sys, os
|
|
|
|
sys.path.append(os.path.join(os.getcwd(),'python/'))
|
|
|
|
|
|
|
|
import darknet as dn
|
|
|
|
|
|
|
|
# Darknet
|
|
|
|
net = dn.load_net("cfg/tiny-yolo.cfg", "tiny-yolo.weights", 0)
|
|
|
|
meta = dn.load_meta("cfg/coco.data")
|
2018-03-16 04:21:21 +03:00
|
|
|
r = dn.detect(net, meta, "data/dog.jpg")
|
|
|
|
print(r)
|
2017-11-08 03:10:33 +03:00
|
|
|
|
|
|
|
# scipy
|
|
|
|
arr= imread('data/dog.jpg')
|
2018-03-16 04:21:21 +03:00
|
|
|
im = array_to_image(arr)
|
|
|
|
r = detect2(net, meta, im)
|
|
|
|
print(r)
|
2017-11-08 03:10:33 +03:00
|
|
|
|
|
|
|
# OpenCV
|
|
|
|
arr = cv2.imread('data/dog.jpg')
|
|
|
|
im = array_to_image(arr)
|
|
|
|
dn.rgbgr_image(im)
|
|
|
|
r = detect2(net, meta, im)
|
2018-03-16 04:21:21 +03:00
|
|
|
print(r)
|