예제 #1
0
    def detect2(self, net, meta, im, thresh=.5, hier_thresh=.5, nms=.45):
        darknet2Montreal = {
            'orange': ['orange', 'cup', 'bowl', 'knife', 'fork', 'spoon'],
            'cake': ['scrubby'],
            'bottle': [
                'noodles', 'coke', 'sprite', 'sausages', 'pringles',
                'chocolate drink'
            ],
            'book': ['crackers', 'chips'],
            'cell phone': ['grape juice', 'orange juice']
        }

        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
                    darknetName = meta.names[i]
                    if darknetName in darknet2Montreal.keys():
                        for montrealName in darknet2Montreal[darknetName]:
                            res.append((montrealName, dets[j].prob[i],
                                        (b.x, b.y, b.w, b.h)))
                    else:
                        res.append((darknetName, dets[j].prob[i], (b.x, b.y,
                                                                   b.w, b.h)))
        res = sorted(res, key=lambda x: -x[1])
        return res
예제 #2
0
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 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
예제 #4
0
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
예제 #5
0
    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
예제 #6
0
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
예제 #7
0
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")
예제 #8
0
def classify(net, meta, im):
    # type: (object, METADATA, IMAGE) -> Tuple[str, float]
    out = predict_image(net, im)
    res = []
    for i in range(meta.classes):
        res.append((meta.names[i], out[i]))
    res = sorted(res, key=lambda x: -x[1])
    return res
예제 #9
0
def detect2(net, meta, im, thresh=.5, hier_thresh=.5, nms=.45):
    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])
    return res
예제 #10
0
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, drawBoxes = False, alphabet=None):
    num = c_int(0)
    pnum = pointer(num)
    dn.predict_image(net, im)
    dets = dn.get_network_boxes(net, image.w, image.h, thresh, hier_thresh, None, 1, 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])
    if drawBoxes == True:
        dn.draw_detections(image, dets, num, thresh, meta.names, alphabet, meta.classes)
    #dn.free_image(image)
    #dn.free_detections(dets, num)
    return res
예제 #12
0
def image_classification(image, network, class_names):
    width = darknet.network_width(network)
    height = darknet.network_height(network)
    image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
    image_resized = cv2.resize(image_rgb, (width, height), interpolation=cv2.INTER_LINEAR)
    darknet_image = darknet.make_image(width, height, 3)
    darknet.copy_image_from_bytes(darknet_image, image_resized.tobytes())
    detections = darknet.predict_image(network, darknet_image)
    predictions = [(name, detections[idx]) for idx, name in enumerate(class_names)]
    darknet.free_image(darknet_image)
    return sorted(predictions, key=lambda x: -x[1])
예제 #13
0
파일: table.py 프로젝트: mppsk0/table-ocr-1
def darknet_GPU_predict(img, prob=0.5):
    imgW, imgH = SIZE
    imgResize, fx, fy, dx, dy = letterbox_image(img, SIZE)
    im = array_to_image(imgResize)
    out = predict_image(tableNet, im)
    values = []
    for i in range(2 * imgW * imgH):
        values.append(out[i])
    out = np.array(values).reshape((2, imgH, imgW))
    #out = exp(out)
    out = out[:, dy:, dx:]
    return out, fx, fy
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