Example #1
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 = torch.cuda.is_available()
    if use_cuda:
        m.cuda()

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

    start = time.time()
    boxes = do_detect(m, sized, 0.5, 0.1, use_cuda)
    correct_yolo_boxes(boxes, img.width, img.height, m.width, m.height)

    finish = time.time()
    print('%s: Predicted in %f seconds.' % (imgfile, (finish - start)))

    class_names = load_class_names(namesfile)
    plot_boxes(img, boxes, 'predictions.jpg', class_names)
def detect(m, img, img_name, use_cuda):
    sized = letterbox_image(img, m.width, m.height)
    start = time.time()
    boxes = do_detect(m, sized, 0.5, 0.4, use_cuda)
    correct_yolo_boxes(boxes, img.width, img.height, m.width, m.height)
    finish = time.time()
    print('%s: Predicted in %f seconds.' % (img_name, (finish - start)))
    return boxes
Example #3
0
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
Example #4
0
def dnn_table_predict(img, prob=0.5):

    imgResize, fx, fy, dx, dy = letterbox_image(img, SIZE)

    imgResize = np.array(imgResize)
    imgW, imgH = SIZE
    image = cv2.dnn.blobFromImage(imgResize,
                                  1,
                                  size=(imgW, imgH),
                                  swapRB=False)
    image = np.array(image) / 255
    tableNet.setInput(image)
    out = tableNet.forward()
    out = exp(out[0])
    out = out[:, dy:, dx:]
    return out, fx, fy
Example #5
0
def camera_detect(cfgfile, weightfile):
    """
    - camera detect
    :cfgfile    use tiny config file
    :weightfile use tiny weight file 
    """
    model = DarkNet(cfgfile)
    model.print_net()
    model.load_weights(weightfile)
    print('load weights done!')

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

    cap = cv2.VideoCapture(0)
    if not cap.isOpened():
        print("Unable to open camera")
        exit(-1)
    while True:
        res, img = cap.read()
        if res:
            img = Image.fromarray(img, mode='RGB')  # numpy.array -> PIL.Image
            sized = letterbox_image(img, model.width, model.height)

            boxes = do_detect(model, sized, 0.5, 0.4, False)
            correct_yolo_boxes(boxes, img.width, img.height, model.width,
                               model.height)
            class_names = load_class_names(namesfile)
            image_draw = plot_boxes(img, boxes, None, class_names)

            np_img = np.asarray(image_draw)  # PIL.Image -> numpy.array
            cv2.imshow(cfgfile, np_img)
            cv2.waitKey(1)
        else:
            print("Unable to read image")
            exit(-1)
Example #6
0
from utils import *
from image import letterbox_image, correct_yolo_boxes
from darknet import Darknet

globals()["namesfile"] = "coco.names"

def detect(m, cfgfile, weightfile, imgfile, destfile):
	m = Darknet(cfgfile)
	
    m.print_network()
    m.load_weights(weightfile)
    print('Loading weights from %s... Done!' % (weightfile))
    
    use_cuda = torch.cuda.is_available()
    if use_cuda:
        m.cuda()

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

    start = time.time()
    boxes = do_detect(m, sized, 0.5, 0.4, use_cuda)
    correct_yolo_boxes(boxes, img.width, img.height, m.width, m.height)

    finish = time.time()
    print('%s: Predicted in %f seconds.' % (imgfile, (finish-start)))

    class_names = load_class_names(namesfile)
    plot_boxes(img, boxes, destfile, class_names)
Example #7
0
def valid(datacfg, cfgfile, weightfile, outfile):
    cudnn.enabled = True
    cudnn.benchmark = True

    options = read_data_cfg(datacfg)
    valid_images = options['valid']
    name_list = options['names']
    prefix = 'results'
    names = load_class_names(name_list)

    with open(valid_images) as fp:
        tmp_files = fp.readlines()
        valid_files = [item.rstrip() for item in tmp_files]

    m = Darknet(cfgfile)
    m.print_network()
    m.load_weights(weightfile)
    m.cuda()
    m.eval()
    print('shape:', m.width, 'x', m.height)

    fps = []
    if not os.path.exists('results'):
        os.mkdir('results')
    for i in range(m.num_classes):
        buf = '%s/%s%s.txt' % (prefix, outfile, names[i])
        fps.append(open(buf, 'w'))

    conf_thresh = 0.005
    nms_thresh = 0.45
    for batch_idx, valid_file in enumerate(valid_files):
        image = cv2.imread(valid_file)
        assert image is not None
        image2 = letterbox_image(image, m.width, m.height)
        if batch_idx == 0:
            cv2.imwrite('letterbox_image.jpg', image2.astype(np.uint8))
        image_tensor = image_to_tensor(image2)

        data = image_tensor.cuda()
        with torch.no_grad():
            output = m(data)
        # if batch_idx == 0:
        #     outputs[-1] = data
        #     save_outputs('./outputs.npz', outputs)
        batch_boxes = get_region_boxes2(output, image.shape[1], image.shape[0],
                                        m.width, m.height, conf_thresh,
                                        m.num_classes, m.anchors,
                                        m.num_anchors, 1)

        fileId = os.path.basename(valid_file).split('.')[0]
        height, width = image.shape[:2]
        print('[{}/{}]: '.format(batch_idx, len(valid_files)), valid_file, ' ',
              len(batch_boxes[0]))
        boxes = batch_boxes[0]
        boxes = nms_class(boxes, nms_thresh, m.num_classes)
        for box in boxes:
            x1 = (box[0] - box[2] / 2.0) * width
            y1 = (box[1] - box[3] / 2.0) * height
            x2 = (box[0] + box[2] / 2.0) * width
            y2 = (box[1] + box[3] / 2.0) * height

            if x1 < 0:
                x1 = 0
            if y1 < 0:
                y1 = 0
            if x2 >= width:
                x2 = width - 1
            if y2 >= height:
                y2 = height - 1

            for j in range(m.num_classes):
                prob = box[5 + j]
                if prob >= conf_thresh:
                    fps[j].write('%s %f %f %f %f %f\n' %
                                 (fileId, prob, x1, y1, x2, y2))

    for i in range(m.num_classes):
        fps[i].close()