def detect(self, image: str, thresh: float = .5, hier_thresh: float = .5, nms: float = .45) -> list: if isinstance(image, str): im = dn.load_image(image.encode(), 0, 0) elif image is None: return [] else: arr = image.transpose(2, 0, 1) c, h, w = arr.shape arr = (arr/255.0).flatten() data = dn.c_array(dn.c_float, arr) im = dn.IMAGE(w, h, c, data) num = dn.c_int(0) pnum = dn.pointer(num) dn.predict_image(self.net, im) dets = dn.get_network_boxes( self.net, im.w, im.h, thresh, hier_thresh, None, 0, pnum) num = pnum[0] if (nms): dn.do_nms_obj(dets, num, self.meta.classes, nms) res = [] for j in range(num): if dets[j].prob[PERSON_ID] > 0: bb = dets[j].bbox res.append((dets[j].prob[PERSON_ID], BBOX(bb))) res = sorted(res, key=lambda x: -x[0]) # 0 is prob # dn.free_image(im) # raise double free error dn.free_detections(dets, num) return res
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 array_to_image(array): array = array.transpose(2, 0, 1) c = array.shape[0] h = array.shape[1] w = array.shape[2] array = (array / 255.0).flatten() data = darknet.c_array(darknet.c_float, array) image = darknet.IMAGE(w, h, c, data) return image
def genTestData(name, inpShape, outShape): net = dn.load_net(name + '.cfg', '', 0) inp = np.random.standard_normal(inpShape).astype(np.float32) inpData = dn.c_array(dn.c_float, inp.flatten()) pred = dn.predict(net, inpData) total = np.prod(outShape) out = np.zeros([total], np.float32) for i in range(total): out[i] = pred[i] out = out.reshape(outShape) np.save(name + '_in.npy', inp) np.save(name + '_out.npy', out)
def array_to_image(arr): time1 = time.time() arr = arr.transpose(2, 0, 1) time2 = time.time() print("transpose took: ", time2 - time1) c = arr.shape[0] h = arr.shape[1] w = arr.shape[2] arr = (arr / 255.0).flatten() time3 = time.time() print("flattern took: ", time3 - time2) data = dn.c_array(dn.c_float, arr) time4 = time.time() print("change to c_array took: ", time4 - time3) im = dn.IMAGE(w, h, c, data) time5 = time.time() print("final loading IMAGE from array took: ", time5 - time4) return im