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(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(net, meta, im, thresh=.2, hier_thresh=0, nms=.4): # type: (object, METADATA, IMAGE, float, float, float) -> List[YoloData] """ Detect the objects in the given image. free_image function is called inside this function. Therefore the input darkent image is not usable after calling this function. :param net: :param meta: :param im: :param thresh: :param hier_thresh: :param nms: :return: """ num = c_int(0) pnum = pointer(num) predict_image(net, im) dets = get_network_boxes(net, im.w, im.h, thresh, hier_thresh, None, 0, pnum) num = pnum[0] if nms: do_nms_sort(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(YoloData(id=i, name=meta.names[i], bbox=BBox(b.x - b.w/2.0, b.y - b.h/2.0, b.w, b.h, dets[j].prob[i]))) res = sorted(res, key=lambda x: -x.bbox.c) free_image(im) free_detections(dets, num) return res
def detect_from_memory(net, meta, im, thresh=.5, hier_thresh=.5, nms=.45, debug=False): """ Performs the meat of the detection """ num = c_int(0) if debug: print("Assigned num") pnum = pointer(num) if debug: print("Assigned pnum") darknet.predict_image(net, im) if debug: print("did prediction") dets = darknet.get_network_boxes(net, im.w, im.h, thresh, hier_thresh, None, 0, pnum, 1) if debug: print("Got dets") num = pnum[0] if debug: print("got zeroth index of pnum") if nms: darknet.do_nms_sort(dets, num, meta.classes, nms) if debug: print("did sort") res = [] if debug: print("about to range") for j in range(num): if debug: print("Ranging on " + str(j) + " of " + str(num)) if debug: print("Classes: " + str(meta), meta.classes, meta.names) for i in range(meta.classes): if debug: print("Class-ranging on " + str(i) + " of " + str(meta.classes) + "= " + str(dets[j].prob[i])) if dets[j].prob[i] > 0: b = dets[j].bbox if darknet.altNames is None: nameTag = meta.names[i] else: nameTag = darknet.altNames[i] if debug: print("Got bbox", b) print(nameTag) print(dets[j].prob[i]) print((b.x, b.y, b.w, b.h)) res.append((nameTag, dets[j].prob[i], (b.x, b.y, b.w, b.h))) if debug: print("did range") res = sorted(res, key=lambda x: -x[1]) if debug: print("did sort") darknet.free_detections(dets, num) if debug: print("freed detections") 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 detect_np(net, meta, image, thresh=.5, hier_thresh=.5, nms=.45): im = array_to_image(image) 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): 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_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((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
def detect2(net, meta, cvim, thresh=.7, hier_thresh=.5, nms=.45): t1 = time.time() im = nparray_to_image(cvim) t2 = time.time() print "cv2 to IMAGE: ", ((t2 - t1) * 1000) num = c_int(0) pnum = 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): 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(im) dn.free_detections(dets, num) return res