Example #1
0
def detect(cfgfile, weightfile, imgfile):
    if cfgfile.find('.prototxt') >= 0:
        from caffenet import CaffeNet
        m = CaffeNet(cfgfile)
    else:
        m = Darknet(cfgfile)

    m.print_network()
    m.load_weights(weightfile)
    print('Loading weights from %s... Done!' % (weightfile))

    if m.num_classes == 20:
        namesfile = 'data/voc.names'
    elif m.num_classes == 80:
        namesfile = 'data/coco.names'
    else:
        namesfile = 'data/names'
    
    use_cuda = 1
    if use_cuda:
        m.cuda()

    img = Image.open(imgfile).convert('RGB')
    sized = img.resize((m.width, m.height))
    
    for i in range(2):
        start = time.time()
        boxes = do_detect(m, sized, 0.5, 0.4, use_cuda)
        finish = time.time()
        if i == 1:
            print('%s: Predicted in %f seconds.' % (imgfile, (finish-start)))

    class_names = load_class_names(namesfile)
    plot_boxes(img, boxes, 'predictions.jpg', class_names)
Example #2
0
def detect(cfgfile, weightfile, imgfile):
    m = Darknet(cfgfile)

    m.print_network()
    m.load_weights(weightfile)
    print('Loading weights from %s... Done!' % (weightfile))

    if m.num_classes == 20:
        namesfile = 'data/voc.names'
    elif m.num_classes == 80:
        namesfile = 'data/coco.names'
    else:
        namesfile = 'data/names'
    
    use_cuda = 1
    if use_cuda:
        m.cuda()

    img = Image.open(imgfile).convert('RGB')
    sized = img.resize((m.width, m.height))
    
    for i in range(2):
        start = time.time()
        boxes = do_detect(m, sized, 0.5, 0.4, use_cuda)
        finish = time.time()
        if i == 1:
            print('%s: Predicted in %f seconds.' % (imgfile, (finish-start)))

    class_names = load_class_names(namesfile)
    plot_boxes(img, boxes, 'predictions.jpg', class_names)
Example #3
0
def detect(m, imgfile, savepath=None):
    use_cuda = 1
    m.cuda()

    img = Image.open(imgfile).convert('RGB')
    sized = img.resize((m.width, m.height))

    boxes, layers = do_detect(m, sized, 0.5, 0.4, use_cuda)

    act = layers[29].cpu().data
    bounds = find_bounds(act, boxes)

    if len(bounds) == 0:
        return

    x1, x2, y1, y2 = max(bounds, key=lambda x: (x[1] - x[0]) * (x[3] - x[2]))
    sect = act[:, :,
               int(round(x1)):int(round(x2)),
               int(round(y1)):int(round(y2))]

    print(sect.size())
    if savepath:
        torch.save(sect, savepath)
    else:
        return sect
def yolo_detect(imgfile, cfgfile="cfg/yolo.cfg", weightfile="yolo.weights"):
    m = Darknet(cfgfile)
    m.load_weights(weightfile)
    #print('Loading weights from %s... Done!' % (weightfile))

    if m.num_classes == 20:
        namesfile = 'data/voc.names'
    elif m.num_classes == 80:
        namesfile = 'data/coco.names'
    else:
        namesfile = 'data/names'

    use_cuda = 1
    if use_cuda:
        m.cuda()

    img = Image.open(imgfile).convert('RGB')
    sized = img.resize((m.width, m.height))

    for i in range(2):
        start = time.time()
        boxes = do_detect(m, sized, 0.5, 0.4, use_cuda)
        finish = time.time()
    class_names = load_class_names(namesfile)
    width = img.width
    height = img.height
    draw = ImageDraw.Draw(img)
    for i in range(len(boxes)):
        box = boxes[i]
        if len(box) >= 7 and class_names:
            cls_conf = box[5]
            cls_id = box[6]
            if class_names[cls_id] == "person" and cls_conf >= 0.3:
                return True
    return False
Example #5
0
def IDS_worker(input_q, output_q, cfgfile, weightfile, useGPU):

    sharing.detect_min = 3
    sharing.colorframe = 'nothing'
    sharing.saveimage = False
    sharing.counterimage = 0

    if useGPU:
        sharing.usegpu = True
    else:
        sharing.usegpu = False

    ### initialization of neural network based upon the specified config and weights files
    m = Darknet(cfgfile)
    m.print_network()
    m.load_weights(weightfile)

    ### if GPU optimizations are enabled, do some initialization
    if sharing.usegpu:
        m.cuda()
    print('Loading weights from %s... Done!' % (weightfile))

    while True:

        image = input_q.get()
        sized = cv2.resize(image, (m.width, m.height))
        #third value in this call sets the confidence threshold for object detection
        output_q.put((image, do_detect(m, sized, 0.4, 0.4, useGPU)))
Example #6
0
def detect(cfgfile, weightfile, imgfile):
    m = Darknet(cfgfile)
    m.load_weights(weightfile)
    print('Loading weights from %s... Done!' % (weightfile))

    img = Image.open(imgfile).convert('RGB')
    sized = img.resize((m.width, m.height))
    
    start = time.time()
    boxes = utils.do_detect(m, sized, 0.5, 0.4)
    finish = time.time()
    print('%s: Predicted in %f seconds.' % (imgfile, (finish-start)))

    class_names = utils.load_class_names(namesfile)
    utils.plot_boxes(img, boxes, 'predictions.jpg', class_names)
Example #7
0
def handle_run_inference(req):
    global model, class_names, use_cuda, bridge
    try:
        cv_img = bridge.imgmsg_to_cv2(req.img_req, desired_encoding="passthrough")
    except CvBridgeError as e:
        print(e)
    bboxes = do_detect(model, cv_img, 0.5, 0.4, use_cuda)
    print(len(bboxes))
    print('------')
    draw_img = plot_boxes_cv2(cv_img, bboxes, None, class_names)
    try:
        img = bridge.cv2_to_imgmsg(draw_img, "rgb8")
    except CvBridgeError as e:
        print(e)

    return Yolo2Response(img)
def eval_list(cfgfile, namefile, weightfile, testfile):
    m = Darknet(cfgfile)
    m.load_weights(weightfile)
    use_cuda = 1
    if use_cuda:
        m.cuda()

    class_names = load_class_names(namefile)

    file_list = []
    with open(testfile, "r") as fin:
        for f in fin:
            file_list.append(f.strip())

    for imgfile in file_list:
        img = Image.open(imgfile).convert('RGB')
        sized = img.resize((m.width, m.height))
        filename = os.path.basename(imgfile)
        filename = os.path.splitext(filename)[0]
        #print(filename, img.width, img.height, sized_width, sized_height)

        if m.width * m.height > 1024 * 2560:
            print('omit %s' % filename)
            continue

        if False:
            boxes = do_detect(m, sized, conf_thresh, nms_thresh, use_cuda)
        else:
            m.eval()
            sized = image2torch(sized).cuda();
            #output = m(Variable(sized, volatile=True)).data
            output = m(sized)
            #boxes = get_region_boxes(output, conf_thresh, m.num_classes, m.anchors, m.num_anchors, 0, 1)[0]
            boxes = get_all_boxes(output, conf_thresh, m.num_classes)[0]
            boxes = np.array(nms(boxes, nms_thresh))

        if False:
            savename = get_det_image_name(imgfile)
            print('img: save to %s' % savename)
            plot_boxes(img, boxes, savename, class_names)

        if False:
            savename = get_det_result_name(imgfile)
            print('det: save to %s' % savename)
            save_boxes(imgfile, img, boxes, savename)
 def _detect_objects_handler(self, req):
     """ Handler for object detection service.
     Args:
         req - request in the form of ROS image type.
     Returns:
         res = ROS response, image with detected objects.
     """
     try:
         cv_img = self._bridge.imgmsg_to_cv2(req.img_req,
                                             desired_encoding="passthrough")
     except CvBridgeError as e:
         rospy.logerr(e)
     bboxes = do_detect(self._model, cv_img, 0.5, 0.4, self._use_cuda)
     draw_img = plot_boxes_cv2(cv_img, bboxes, None, self._class_names)
     try:
         img = self._bridge.cv2_to_imgmsg(draw_img, "rgb8")
     except CvBridgeError as e:
         rospy.logerr(e)
     return Yolo2Response(img)
def eval_widerface(cfgfile, weightfile, valdir, savedir):
    m = Darknet(cfgfile)
    m.load_weights(weightfile)
    use_cuda = 1
    if use_cuda:
        m.cuda()

    scale_size = 16
    class_names = load_class_names('data/names')
    for parent, dirnames, filenames in os.walk(valdir):
        if parent != valdir:
            targetdir = os.path.join(savedir, os.path.basename(parent))
            if not os.path.isdir(targetdir):
                os.mkdir(targetdir)
            for filename in filenames:
                imgfile = os.path.join(parent, filename)
                img = Image.open(imgfile).convert('RGB')
                sized_width = int(round(img.width * 1.0 / scale_size) * 16)
                sized_height = int(round(img.height * 1.0 / scale_size) * 16)
                sized = img.resize((sized_width, sized_height))
                print(filename, img.width, img.height, sized_width,
                      sized_height)
                if sized_width * sized_height > 1024 * 2560:
                    print('omit %s' % filename)
                    continue
                boxes = do_detect(m, sized, 0.05, 0.4, use_cuda)
                if True:
                    savename = os.path.join(targetdir, filename)
                    print('save to %s' % savename)
                    plot_boxes(img, boxes, savename, class_names)
                if True:
                    savename = os.path.join(
                        targetdir,
                        os.path.splitext(filename)[0] + ".txt")
                    print('save to %s' % savename)
                    save_boxes(img, boxes, savename)
                blobs[k] = v
        output_name = blobs.keys()[-1]
        print 'output_name', output_name
        return blobs[output_name]


if __name__ == '__main__':
    prototxt = 'tiny_yolo_nbn_reluface.prototxt'
    caffemodel = '/nfs/xiaohang/for_chenchao/tiny_yolo_nbn_reluface.caffemodel'
    imgfile = 'data/face.jpg'

    m = CaffeNet(prototxt, caffemodel)
    use_cuda = 1
    if use_cuda:
        m.cuda()

    img = Image.open(imgfile).convert('RGB')
    sized = img.resize((m.width, m.height))
    #if m.num_classes == 20:
    #    namesfile = '../data/voc.names'
    #class_names = load_class_names(namesfile)
    class_names = ['face']
    for i in range(1):
        start = time.time()
        boxes = do_detect(m, sized, 0.5, 0.4, use_cuda)
        finish = time.time()
        if i == 1:
            print('%s: Predicted in %f seconds.' % (imgfile, (finish - start)))

    plot_boxes(img, boxes, 'predictions.jpg', class_names)