def detect3(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45): if isinstance(image, bytes): # image is a filename # i.e. image = b'/darknet/data/dog.jpg' im = load_image(image, 0, 0) else: # image is an nparray # i.e. image = cv2.imread('/darknet/data/dog.jpg') im, image = array_to_image(image) dn.rgbgr_image(im) num = dn.c_int(0) pnum = dn.pointer(num) dn.predict_image(net, im) dets = dn.get_network_boxes(net, im.w, im.h, thresh, hier_thresh, None, 0, pnum) num = pnum[0] if nms: dn.do_nms_obj(dets, num, meta.classes, nms) res = [] for j in range(num): a = dets[j].prob[0:meta.classes] if any(a): ai = np.array(a).nonzero()[0] for i in ai: b = dets[j].bbox res.append( (meta.names[i], dets[j].prob[i], (b.x, b.y, b.w, b.h))) res = sorted(res, key=lambda x: -x[1]) if isinstance(image, bytes): free_image(im) dn.free_detections(dets, num) return res
def detect(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45): # im = load_image(image, 0, 0) t = time.time() im = array_to_image(image) print('array_to_image time: {}').format(time.time() - t) dn.rgbgr_image(im) num = dn.c_int(0) pnum = dn.pointer(num) dn.predict_image(net, im) dets = dn.get_network_boxes(net, im.w, im.h, thresh, hier_thresh, None, 0, pnum) num = pnum[0] if (nms): dn.do_nms_obj(dets, num, meta.classes, nms) t = time.time() res = [] for j in range(num): for i in range(meta.classes): if dets[j].prob[i] > 0: b = dets[j].bbox res.append( (meta.names[i], dets[j].prob[i], (b.x, b.y, b.w, b.h))) res = sorted(res, key=lambda x: -x[1]) # free_image(im) # free_detections(dets, num) return res
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 detect(): print('Loading image') # Use tmp.jpg for demo purposes im = dn.load_image(bytes('tmp.jpg', encoding='utf-8'), 0, 0) num = dn.c_int(0) pnum = dn.pointer(num) print('Predicting image') dn.predict_image(net, im) print('Getting boxes') dets = dn.get_network_boxes(net, im.w, im.h, 0.5, 0.5, None, 1, pnum) print('Marking boxes') res = [] classes = 1 for j in range(num.value): for i in range(classes): if dets[j].prob[i] > 0.75: b = dets[j].bbox res.append((b.x, b.y, b.w, b.h)) dn.free_image(im) dn.free_detections(dets, num) print('Saving image') source_img = Image.open('tmp.jpg').convert("RGB") size = source_img.size w = size[0] h = size[1] draw = ImageDraw.Draw(source_img) for b in res: x1 = (b[0] - b[2] / 2.) * w x2 = (b[0] + b[2] / 2.) * w y1 = (b[1] - b[3] / 2.) * h y2 = (b[1] + b[3] / 2.) * h draw.rectangle(((x1, y1), (x2, y2)), outline="red") print(b) source_img.save('tmp.jpg', "JPEG")
def detect2(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45): # num = dn.c_int(0) # pnum = dn.pointer(num) # dn.predict_image(net, image) # dets = dn.get_network_boxes(net, image.w, image.h, thresh, hier_thresh, None, 0, pnum) # num = pnum[0] # if (nms): dn.do_nms_obj(dets, num, meta.classes, nms); # res = [] # for j in range(num): # for i in range(meta.classes): # if dets[j].prob[i] > 0: # b = dets[j].bbox # res.append((meta.names[i], dets[j].prob[i], (b.x, b.y, b.w, b.h))) # res = sorted(res, key=lambda x: -x[1]) # dn.free_image(image) # dn.free_detections(dets, num) # return res num = dn.c_int(0) pnum = dn.pointer(num) dn.predict_image(net, image) dets = dn.get_network_boxes(net, image.w, image.h, thresh, hier_thresh, None, 0, pnum) num = pnum[0] if nms: dn.do_nms_obj(dets, num, meta.classes, nms) res = [] for j in range(num): a = dets[j].prob[0:meta.classes] if any(a): ai = np.array(a).nonzero()[0] for i in ai: b = dets[j].bbox res.append( (meta.names[i], dets[j].prob[i], (b.x, b.y, b.w, b.h))) res = sorted(res, key=lambda x: -x[1]) if isinstance(image, bytes): dn.free_image(image) #这步什么情况下执行?多次无法执行的原因是未对这句做判断 print("free image") # dn.free_image(image) dn.free_detections(dets, num) return res
def detect2(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45): num = dn.c_int(0) pnum = dn.pointer(num) dn.predict_image(net, image) dets = dn.get_network_boxes(net, image.w, image.h, thresh, hier_thresh, None, 0, pnum) num = pnum[0] if (nms): dn.do_nms_obj(dets, num, meta.classes, nms) res = [] for j in range(num): for i in range(meta.classes): if dets[j].prob[i] > 0: b = dets[j].bbox res.append({ "name": meta.names[i].decode("utf-8"), "conf": dets[j].prob[i], "box": (b.x, b.y, b.w, b.h) }) res = sorted(res, key=lambda x: x["name"]) #dn.free_image(im) dn.free_detections(dets, num) return res