7 probs = [a/s
for a
in probs]
8 r = random.uniform(0, 1)
9 for i
in range(len(probs)):
16 arr = (ctype*len(values))()
21 _fields_ = [(
"x", c_float),
27 _fields_ = [(
"bbox", BOX),
29 (
"prob", POINTER(c_float)),
30 (
"mask", POINTER(c_float)),
31 (
"objectness", c_float),
32 (
"sort_class", c_int)]
36 _fields_ = [(
"w", c_int),
39 (
"data", POINTER(c_float))]
42 _fields_ = [(
"classes", c_int),
43 (
"names", POINTER(c_char_p))]
48 lib = CDLL(
"libdarknet.so", RTLD_GLOBAL)
49 lib.network_width.argtypes = [c_void_p]
50 lib.network_width.restype = c_int
51 lib.network_height.argtypes = [c_void_p]
52 lib.network_height.restype = c_int
54 predict = lib.network_predict
55 predict.argtypes = [c_void_p, POINTER(c_float)]
56 predict.restype = POINTER(c_float)
58 set_gpu = lib.cuda_set_device
59 set_gpu.argtypes = [c_int]
61 make_image = lib.make_image
62 make_image.argtypes = [c_int, c_int, c_int]
63 make_image.restype = IMAGE
65 get_network_boxes = lib.get_network_boxes
66 get_network_boxes.argtypes = [c_void_p, c_int, c_int, c_float, c_float, POINTER(c_int), c_int, POINTER(c_int)]
67 get_network_boxes.restype = POINTER(DETECTION)
69 make_network_boxes = lib.make_network_boxes
70 make_network_boxes.argtypes = [c_void_p]
71 make_network_boxes.restype = POINTER(DETECTION)
73 free_detections = lib.free_detections
74 free_detections.argtypes = [POINTER(DETECTION), c_int]
76 free_ptrs = lib.free_ptrs
77 free_ptrs.argtypes = [POINTER(c_void_p), c_int]
79 network_predict = lib.network_predict
80 network_predict.argtypes = [c_void_p, POINTER(c_float)]
82 reset_rnn = lib.reset_rnn
83 reset_rnn.argtypes = [c_void_p]
85 load_net = lib.load_network
86 load_net.argtypes = [c_char_p, c_char_p, c_int]
87 load_net.restype = c_void_p
89 do_nms_obj = lib.do_nms_obj
90 do_nms_obj.argtypes = [POINTER(DETECTION), c_int, c_int, c_float]
92 do_nms_sort = lib.do_nms_sort
93 do_nms_sort.argtypes = [POINTER(DETECTION), c_int, c_int, c_float]
95 free_image = lib.free_image
96 free_image.argtypes = [IMAGE]
98 letterbox_image = lib.letterbox_image
99 letterbox_image.argtypes = [IMAGE, c_int, c_int]
100 letterbox_image.restype = IMAGE
102 load_meta = lib.get_metadata
103 lib.get_metadata.argtypes = [c_char_p]
104 lib.get_metadata.restype = METADATA
106 load_image = lib.load_image_color
107 load_image.argtypes = [c_char_p, c_int, c_int]
108 load_image.restype = IMAGE
110 rgbgr_image = lib.rgbgr_image
111 rgbgr_image.argtypes = [IMAGE]
113 predict_image = lib.network_predict_image
114 predict_image.argtypes = [c_void_p, IMAGE]
115 predict_image.restype = POINTER(c_float)
120 for i
in range(meta.classes):
121 res.append((meta.names[i], out[i]))
122 res = sorted(res, key=
lambda x: -x[1])
125 def detect(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45):
132 if (nms):
do_nms_obj(dets, num, meta.classes, nms);
136 for i
in range(meta.classes):
137 if dets[j].prob[i] > 0:
139 res.append((meta.names[i], dets[j].prob[i], (b.x, b.y, b.w, b.h)))
140 res = sorted(res, key=
lambda x: -x[1])
145 if __name__ ==
"__main__":
151 net =
load_net(
"cfg/tiny-yolo.cfg",
"tiny-yolo.weights", 0)
def detect(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45)
def c_array(ctype, values)
def classify(net, meta, im)