class apiModel():
    def __init__(self, model_path, model_name):
        self.model_name = model_name

        nms_thresh = 0.3
        conf_thresh = 0.5
        cfg_path = glob(os.path.join(model_path, '*cfg'))[0]
        weight_path = glob(os.path.join(model_path, '*weights'))[0]
        self.det = ObjectDetector(cfg_path, weight_path, conf_thresh,
                                  nms_thresh, 0)

        name_path = glob(os.path.join(model_path, '*names'))[0]
        self.class_names = open(name_path).read().splitlines()

    def _predict_api(self, url):
        image = url_to_image(url)
        if not np.any(image):
            return []

        image = self.det.format_image(image)
        detections, _, _ = self.det.detect_object(*image)

        results = []
        for bbox in detections:
            class_name = self.class_names[bbox.cls]
            results.append({
                "model":
                self.model_name,
                "url":
                url,
                "label":
                class_name,
                "score":
                round(float(bbox.confidence), 2),
                "bbox": [
                    float(bbox.top),
                    float(bbox.left),
                    float(bbox.bottom),
                    float(bbox.right)
                ]
            })

        return results

    def predict_api(self, urls):
        urls = urls or []
        urls = filter(None, urls)
        if len(urls) == 0:
            return []

        results = [self._predict_api(url) for url in urls]
        return [obj for image in results for obj in image]
    def __init__(self, model_path, model_name):
        self.model_name = model_name

        nms_thresh = 0.3
        conf_thresh = 0.5
        cfg_path = glob(os.path.join(model_path, '*cfg'))[0]
        weight_path = glob(os.path.join(model_path, '*weights'))[0]
        self.det = ObjectDetector(cfg_path, weight_path, conf_thresh,
                                  nms_thresh, 0)

        name_path = glob(os.path.join(model_path, '*names'))[0]
        self.class_names = open(name_path).read().splitlines()
Exemple #3
0
        "bird",
        "boat",
        "bottle",
        "bus",
        "car",
        "cat",
        "chair",
        "cow",
        "diningtable",
        "dog",
        "horse",
        "motorbike",
        "person",
        "pottedplant",
        "sheep",
        "sofa",
        "train",
        "tvmonitor",
    ]
    det = ObjectDetector("../cfg/yolo.cfg", "../yolo.weights")
    url = "http://farm9.staticflickr.com/8323/8141398311_2fd0af60f7.jpg"
    # for i in xrange(4):
    rst, run_time = det.detect_object(_get_image(url))

    print "got {} objects in {} seconds".format(len(rst), run_time)

    for bbox in rst:
        print "{} {} {} {} {} {}".format(
            voc_names[bbox.cls], bbox.top, bbox.left, bbox.bottom, bbox.right, bbox.confidence
        )
Exemple #4
0
from detector import Darknet_ObjectDetector as ObjectDetector
from detector import DetBBox

if __name__ == '__main__':
    from PIL import Image
    det = ObjectDetector('../cfg/yolo.cfg','/media/ssd/models/yolo/yolo.weights')
    for i in xrange(4):
        rst, run_time = det.detect_object(Image.open('../data/dog.jpg'))

    print 'got {} objects in {} seconds'.format(len(rst), run_time)

    for bbox in rst:
        print '{} {} {} {} {} {}'.format(bbox.top, bbox.left, bbox.bottom, bbox.right, bbox.cls, bbox.confidence)
Exemple #5
0
from detector import Darknet_ObjectDetector as ObjectDetector
from detector import DetBBox

import requests
from PIL import Image
from PIL import ImageFilter
from StringIO import StringIO

def _get_image(url):
    return Image.open(StringIO(requests.get(url).content))

if __name__ == '__main__':
    from PIL import Image
    voc_names = ["aeroplane", "bicycle", "bird", "boat", "bottle","bus", "car", "cat", "chair", "cow", "diningtable","dog", "horse", "motorbike", "person", "pottedplant","sheep", "sofa", "train", "tvmonitor"]
    det = ObjectDetector('../cfg/yolo.cfg','../yolo.weights')
    url = 'http://farm9.staticflickr.com/8323/8141398311_2fd0af60f7.jpg'
    #for i in xrange(4):
    rst, run_time = det.detect_object(_get_image(url))

    print 'got {} objects in {} seconds'.format(len(rst), run_time)

    for bbox in rst:
        print '{} {} {} {} {} {}'.format(voc_names[bbox.cls], bbox.top, bbox.left, bbox.bottom, bbox.right, bbox.confidence)
Exemple #6
0
from detector import Darknet_ObjectDetector as ObjectDetector
from detector import DetBBox

import requests
from PIL import Image
from PIL import ImageFilter
from StringIO import StringIO
import cv2


def _get_image(url):
    return Image.open(StringIO(requests.get(url).content))


if __name__ == '__main__':
    ObjectDetector.set_device(0)

    from PIL import Image
    names = open('../data/coco.names').read().splitlines()
    det = ObjectDetector('../cfg/yolo.cfg', '../yolo.weights')

    img = Image.open("../data/dog.jpg")

    rst, run_time = det.detect_object(img)
    print 'got {} objects in {} seconds'.format(len(rst), run_time)

    for bbox in rst:
        print '{} {} {} {} {} {}'.format(names[bbox.cls], bbox.top, bbox.left,
                                         bbox.bottom, bbox.right,
                                         bbox.confidence)
Exemple #7
0
    parser = argparse.ArgumentParser()
    parser.add_argument('--name-path', type=str, default=defaults['name_path'])
    parser.add_argument('--cfg-path', type=str, default=defaults['cfg_path'])
    parser.add_argument('--weight-path',
                        type=str,
                        default=defaults['weight_path'])
    parser.add_argument('--thresh', type=float, default=0.5)
    parser.add_argument('--nms', type=float, default=0.4)
    parser.add_argument('--draw', action='store_true')
    return parser.parse_args()


if __name__ == "__main__":
    args = parse_args()

    ObjectDetector.set_device(0)
    det = ObjectDetector(args.cfg_path, args.weight_path, args.thresh,
                         args.nms, int(args.draw))

    class_names = open(args.name_path).read().splitlines()
    print class_names

    start = time()
    c_load_time, c_pred_time = 0, 0
    for i, im_name in enumerate(sys.stdin):
        py_all_time = time() - start
        py_load_time = py_all_time - c_load_time - c_pred_time
        print >> sys.stderr, "%d | pyall %f | pyload %f | cload %f | cpred %f" % (
            i, py_all_time, py_load_time, c_load_time, c_pred_time)

        try:
Exemple #8
0
import requests
from PIL import Image
from PIL import ImageFilter
from StringIO import StringIO
import cv2


def _get_image(url):
    return Image.open(StringIO(requests.get(url).content))


if __name__ == '__main__':
    from PIL import Image
    voc_names = [
        "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat",
        "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person",
        "pottedplant", "sheep", "sofa", "train", "tvmonitor"
    ]
    det = ObjectDetector('../cfg/yolo.cfg', '../yolo.weights')
    #url = 'http://farm9.staticflickr.com/8323/8141398311_2fd0af60f7.jpg'
    #for i in xrange(4):
    img = Image.open("deng.jpg")
    rst, run_time = det.detect_object(img)

    print 'got {} objects in {} seconds'.format(len(rst), run_time)

    for bbox in rst:
        print '{} {} {} {} {} {}'.format(voc_names[bbox.cls], bbox.top,
                                         bbox.left, bbox.bottom, bbox.right,
                                         bbox.confidence)
Exemple #9
0
from PIL import Image
from PIL import ImageFilter
from StringIO import StringIO


def _get_image(path):
    #return Image.open(StringIO(requests.get(url).content))
    return Image.open(path)


if __name__ == '__main__':
    from PIL import Image
    voc_names = [
        "aeroplane", "bicycle", "bird", "boat", "bottle", "bus", "car", "cat",
        "chair", "cow", "diningtable", "dog", "horse", "motorbike", "person",
        "pottedplant", "sheep", "sofa", "train", "tvmonitor", "plate_license"
    ]
    det = ObjectDetector('../cfg/yolo-plate.cfg',
                         '/home/himon/code/yolo/yolo-plate_final.weights')
    #url = 'http://farm9.staticflickr.com/8323/8141398311_2fd0af60f7.jpg'
    path = '/home/himon/code/yolo/cars.jpg'
    #for i in xrange(4):
    rst, run_time = det.detect_object(_get_image(path))

    print 'got {} objects in {} seconds'.format(len(rst), run_time)

    for bbox in rst:
        print '{} {} {} {} {} {}'.format(voc_names[bbox.cls], bbox.top,
                                         bbox.left, bbox.bottom, bbox.right,
                                         bbox.confidence)
from detector import Darknet_ObjectDetector as ObjectDetector
from detector import DetBBox

import requests
from PIL import Image
from PIL import ImageFilter
from StringIO import StringIO
import cv2
def _get_image(url):
    return Image.open(StringIO(requests.get(url).content))

if __name__ == '__main__':
    from PIL import Image
    voc_names = ["aeroplane", "bicycle", "bird", "boat", "bottle","bus", "car", "cat", "chair", "cow", "diningtable","dog", "horse", "motorbike", "person", "pottedplant","sheep", "sofa", "train", "tvmonitor"]
    det = ObjectDetector('../cfg/yolo.cfg','../yolo.weights')
    #url = 'http://farm9.staticflickr.com/8323/8141398311_2fd0af60f7.jpg'
    #for i in xrange(4):
    img = Image.open("deng.jpg")
    rst, run_time = det.detect_object(img)

    print 'got {} objects in {} seconds'.format(len(rst), run_time)

    for bbox in rst:
        print '{} {} {} {} {} {}'.format(voc_names[bbox.cls], bbox.top, bbox.left, bbox.bottom, bbox.right, bbox.confidence)