darknet  v3
detector-scipy-opencv.py
Go to the documentation of this file.
1 # Stupid python path shit.
2 # Instead just add darknet.py to somewhere in your python path
3 # OK actually that might not be a great idea, idk, work in progress
4 # Use at your own risk. or don't, i don't care
5 
6 from scipy.misc import imread
7 import cv2
8 
9 def array_to_image(arr):
10  arr = arr.transpose(2,0,1)
11  c = arr.shape[0]
12  h = arr.shape[1]
13  w = arr.shape[2]
14  arr = (arr/255.0).flatten()
15  data = dn.c_array(dn.c_float, arr)
16  im = dn.IMAGE(w,h,c,data)
17  return im
18 
19 def detect2(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45):
20  boxes = dn.make_boxes(net)
21  probs = dn.make_probs(net)
22  num = dn.num_boxes(net)
23  dn.network_detect(net, image, thresh, hier_thresh, nms, boxes, probs)
24  res = []
25  for j in range(num):
26  for i in range(meta.classes):
27  if probs[j][i] > 0:
28  res.append((meta.names[i], probs[j][i], (boxes[j].x, boxes[j].y, boxes[j].w, boxes[j].h)))
29  res = sorted(res, key=lambda x: -x[1])
30  dn.free_ptrs(dn.cast(probs, dn.POINTER(dn.c_void_p)), num)
31  return res
32 
33 import sys, os
34 sys.path.append(os.path.join(os.getcwd(),'python/'))
35 
36 import darknet as dn
37 
38 # Darknet
39 net = dn.load_net("cfg/tiny-yolo.cfg", "tiny-yolo.weights", 0)
40 meta = dn.load_meta("cfg/coco.data")
41 r = dn.detect(net, meta, "data/dog.jpg")
42 print r
43 
44 # scipy
45 arr= imread('data/dog.jpg')
46 im = array_to_image(arr)
47 r = detect2(net, meta, im)
48 print r
49 
50 # OpenCV
51 arr = cv2.imread('data/dog.jpg')
52 im = array_to_image(arr)
53 dn.rgbgr_image(im)
54 r = detect2(net, meta, im)
55 print r
56 
def detect2(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45)
void flatten(float *x, int size, int layers, int batch, int forward)
Definition: blas.c:32