Esempio n. 1
0
def main(model_number):
    model = models[model_number]

    cfg = get_config(model['model_cfg'])

    detector = ObjectDetector(
        cfg,
        os.path.join(
            'resource',
            model['checkpoint']
        ),
        device_id=0
    )

    image = cv2.imread('image.jpg')

    for _ in trange(NUM_WARM, desc='Warm-up'):
        detector.predict(image, threshold=0.3)

    start = time.time()
    for _ in trange(NUM_RUNS, desc='Benches'):
        detector.predict(image, threshold=0.3)
    torch.cuda.synchronize()
    total = time.time() - start

    print(f'Performance: {NUM_RUNS / total:0.2f} rps')
Esempio n. 2
0
    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 main(args):
    # 1) get value from sample calibration image
    files_to_check = HT.getFilesInDirectory(args['directory'], '.jpg')
    logger.info(f"Looking at {len(files_to_check)} jpg images")
    results = []
    # option to use calibration
    if args['use_calibration']:
        calib = CI.Image.open(args['calibration_image'])
        calib_result = SD.calibratePlaque(calib)
        area = calib_result.get('contour_area')
        # ratio = calib_result.get('ratio')
        bl, tl, tr, br = calib_result.get('bl_tl_tr_br')
        # 3) loop over images in a directory, outputting the names of positive and negative images
        # TODO: below steps can be optimized with text recognition happpening over the returned list
        # we get a dict of file names, with each filename having a list of metadata
        for f in tqdm(files_to_check,
                      desc=f"finding plaques with area {area}"):
            results.extend(
                SD.get_plaques_matching_ratio(
                    f,
                    good_area=area,
                    save_directory=args['save_directory'],
                    _debug_mode=args['debug'],
                    cutoff_ratio=float(args['cutoff_ratio'])))
    # option to use object detection
    elif args['use_hog']:
        detector = ObjectDetector(loadPath=args["detector"])
        for f in tqdm(files_to_check, desc="finding plaques with HOG"):
            plaque_details_list = SD.get_plaques_with_hog(
                f,
                hog=detector,
                save_directory=args['save_directory'],
                _debug_mode=args['debug'])
            logger.debug(
                f"How many results for {f}: {len(plaque_details_list)}")
            results.extend(plaque_details_list)

    # 4) after successfull plaque grabbing, use open and image stuff to get a bounding box around just the words, and send that to ocr
    # for idx, meta in tqdm(enumerate(results), desc="reading text from cropped images"):
    #     results[idx].text, results[idx].thresheld_image = TR.tess_from_image(results[idx].image)

    # 5) plot the results
    # plot_multiple_images(results)
    # with open('/home/johnny/Documents/performance_metrics_3-6/hog_results.pkl', 'wb') as p:
    #     pickle.dump(results, p)
    # 6) evaluate performance for images with matching calb image and those with mismatched calibrtation image
    evaluate_performance(results)
def main():
    """
    Full Jetson Detector Program
    """

    parser = argparse.ArgumentParser(description="Inference Program")
    parser.add_argument(
        '--config_file',
        help=
        f'json configuration file containint params to initialize the jetson',
        type=str)
    args = parser.parse_args()

    try:
        global objectDetector
        if not os.path.exists(args.config_file):
            raise ValueError(
                f'Cannot find configuration file "{args.config_file}"')
        with open(args.config_file, 'r') as f:
            robotJetsonConfiguration = json.load(f)

        log.warning(LOGGER_OBJECT_DETECTOR_STARTUP,
                    msg='Launching object detector now.')
        loop = asyncio.get_event_loop()
        # loop.set_debug(enabled=True)
        loop.set_exception_handler(handle_exception)
        log.warning(LOGGER_OBJECT_DETECTOR_STARTUP, msg='Launching runner.')
        signals = (signal.SIGINT, signal.SIGTERM)
        for s in signals:
            loop.add_signal_handler(s,
                                    lambda s=s: loop.create_task(
                                        objectDetector.graceful_shutdown(s)))
        objectDetector = ObjectDetector(robotJetsonConfiguration, loop)
        loop.create_task(objectDetector.run())
        loop.run_forever()

    except SystemExit:
        log.info(LOGGER_OBJECT_DETECTOR_STARTUP, 'Caught SystemExit...')
    except Exception:
        log.critical(LOGGER_OBJECT_DETECTOR_STARTUP,
                     'Crash in startup : {}'.format(traceback.print_exc()))
    finally:
        # loop.stop()
        loop.close()
        logging.shutdown()
Esempio n. 5
0
parse.add_argument("-i",
                   "--images",
                   help="Path to saved images",
                   default="train_images.npy")
parse.add_argument("-d",
                   "--detector",
                   help="Path to save model",
                   default="svm_model.svm")
parse.add_argument("-ta",
                   "--test_annotate",
                   help="Path to saved test boxes annotations",
                   default="test_annot.npy")
parse.add_argument("-tim",
                   "--test_images",
                   help="Path to test images",
                   default="test_images.npy")
args = vars(parse.parse_args())

annots = np.load(args["annotations"])
imagePaths = np.load(args["images"])
trainAnnot = np.load(args["test_annotate"])
trainImages = np.load(args["test_images"])

detector = ObjectDetector()
detector.fit(imagePaths,
             annots,
             trainAnnot,
             trainImages,
             visualize=True,
             savePath=args["detector"])
from detector import ObjectDetector
import numpy as np
import cv2
import argparse

ap = argparse.ArgumentParser()
ap.add_argument("-d",
                "--detector",
                required=True,
                help="path to trained detector to load...")
#ap.add_argument("-i","--image",required=True,help="path to an image for object detection...")
ap.add_argument("-a", "--annotate", default=None, help="text to annotate...")
args = vars(ap.parse_args())

detector = ObjectDetector(loadPath=args["detector"])

cap = cv2.VideoCapture("naspati.webm")
grabbed, image = cap.read()
while True:

    grabbed, image = cap.read()

    x, y, xb, yb = detector.detect(image, annotate=args["annotate"])

    #detector.detect(image,annotate=args["annotate"])
    center = [[(x / 2 + xb / 2) / 2], [(y + yb) / 3]]

    key = cv2.waitKey(1) & 0xFF

    # if the 'q' key is pressed, stop the loop
    if key == ord("q"):
Esempio n. 7
0
    def __init__(self):
        global test_mode
        """
        コンストラクタ。引数をパースする。また、カメラ、画像判定モデルを初期化する。
        """

        # (1)引数をパースする
        arg_parser = argparse.ArgumentParser(description='Detecting Camera')
        arg_parser.add_argument('target_name',
                                help='Target name to detecting. Split by commas.')
        arg_parser.add_argument('-x', '--exclude', default=None,
                                help='Target excluded from detection. Split by commas.')
        arg_parser.add_argument('-i', '--interval', type=int, default=60,
                                help='Camera shooting interval[sec] (default:60)')
        arg_parser.add_argument('-p', '--period', type=int, default=0,
                                help='Shooting period[min]. Infinite if 0(default).')
        arg_parser.add_argument('-d', '--directory', default='./',
                                help='Output directory (default:current dir)')
        arg_parser.add_argument('-l', '--list', action='store_true', default=False,
                                help='List target names.')
        arg_parser.add_argument('-s', '--saveimage', action='store_true', default=False,
                                help='Save image mode.(default:Off)')
        arg_parser.add_argument('-t', '--tweet', action='store_true', default=False,
                                help='Tweet detected.(default:Off)')
        arg_parser.add_argument('-e', '--test', action='store_true', default=False,
                                help='Test mode.(default:Off)')
        args = arg_parser.parse_args()

        # パースした引数の内容を変数で覚えておく。
        self._target_names = args.target_name.split(',')
        self._exclude_names = args.exclude.split(',') if args.exclude is not None else []
        self._output_directory = args.directory
        self._period_sec = args.period * 60
        self._interval_sec = args.interval
        self._list_option = args.list
        self._save_image_mode = args.saveimage
        self._tweet_mode = args.tweet
        self._test_mode = args.test

        # 検知器を用意し、利用可能なターゲット名称をそこから取得する。
        self._detector = ObjectDetector()
        available_names = self._detector.list_names()

        # 引数"-l"が指定されていたら、利用可能なターゲット名称をリストアップして終了
        if self._list_option:
            print('Available names:')
            print(', '.join(available_names))
            exit(0)

        # (2)ターゲット名が、利用可能なターゲット名称に含まれるかチェック。
        # なければプログラム終了。
        for name in self._target_names + self._exclude_names:
            if name not in available_names:
                print('"{0}" is not available word. Retry input.'.format(name))
                print('(Search available words for -l options.)')
                exit(0)

        # カメラの無い環境であればテストモードを強制的にONにする。
        if without_camera:
            self._test_mode = True

        # (3)引数の内容を表示。
        print(('Camera started.\n'
               '  - Target name     : {0}\n'
               '  - Exclude name    : {1}\n'
               '  - Interval        : {2}\n'
               '  - Shooting period : {3}\n'
               '  - Output directory: {4}\n'
               '  - Save image mode : {5}\n'
               '  - Tweet mode      : {6}\n'
               '  - Test mode       : {7}\n'
               ).format(','.join(self._target_names),
                        ','.join(self._exclude_names),
                        str(self._interval_sec) + '[sec]',
                        str(args.period) + '[min]' if self._period_sec != 0 else 'infinite',
                        self._output_directory,
                        self._save_image_mode,
                        self._tweet_mode,
                        self._test_mode)
              )

        # (4)画像判定モデルを初期化。
        print('Start model loading...')
        self._detector.open()
        print('Finish.')

        # (5)Twitter接続を初期化。
        if self._tweet_mode:
            self._twitter = Twitter()
Esempio n. 8
0
from detector import ObjectDetector
import numpy as np
import cv2
import argparse

detector_path = r'd:\Users\boykni\Desktop\object_detection\Object-Detector-master\detector.svm'
#annotations_path = r"d:\Users\boykni\Desktop\object_detection\Object-Detector-master\annot.npy"
images_path = r"\\Mos-srv1\Petroview\NAWAT\Completion Data\LA_COMPLETION_REPORTS\imageclassifier\training_dataset\titlepage\3_242294_2_1.jpg"

detector = ObjectDetector(loadPath=detector_path)

imagePath = images_path
image = cv2.imread(imagePath)
detector.detect(image,annotate="LOGO")
Esempio n. 9
0
import cv2
from centroidtracker import CentroidTracker
from detector import ObjectDetector
import numpy as np
import tensorflow as tf
from stream import ThreadedCam
from trackedobjects import TrackableObject
from sort import Sort
from time import time
from numba import jit
from utils import roiselector


det = ObjectDetector('cardetector')
ct = CentroidTracker()
trackers = []
totalframes = 0
skipframes = 5
trackedobjects = {}
roiwindow = 'Pls Select ROI'

H = None
W = None

def check_and_correct_boundaries(x,y,xmin,ymin,xmax,ymax):
    if x < xmin:
        x = xmin
    elif x > xmax:
        x = xmax-1
    if y < ymin:
        y = ymin
Esempio n. 10
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)
from detector import ObjectDetector
from centroidtracker import CentroidTracker
import cv2
import numpy as np
import tensorflow as tf
import stream

det = ObjectDetector('facedetector')
# tracker = CentroidTracker()

# cap = stream.ThreadedCam('samples/traffic.mp4',fps=30).start()
cap = stream.ThreadedCam(0).start()

with tf.Session(graph=det.graph) as sess:
    while 2:
        frame = cap.get_frame()
        frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        boxes = det.detect_boxes_and_scores(frame, sess, sort=True)
        print(boxes)
        # print(objects)
        # for objid,centroid in objects.items():
        #     y,x = centroid
        #     text = "ID {}".format(objid)
        #     cv2.putText(frame, text, (int(x) - 10, int(y) - 10),cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
        #     cv2.circle(frame,(int(x),int(y)),radius=5,color=[0,255,255],thickness=-1)
        cv2.imshow("Test", cv2.cvtColor(frame, cv2.COLOR_RGB2BGR))
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break

cap.stop()
cv2.destroyAllWindows()
Esempio n. 12
0
    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:
            im_name = im_name.strip()
            img = Image.open(im_name)
Esempio n. 13
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)
Esempio n. 14
0
    img_path = os.path.join(os.path.dirname(__file__), "world", "render_files")
    file_list = sorted(os.listdir(img_path))

    for f in file_list:
        img = cv.imread(os.path.join(img_path, f))
        vid.write(img)

    cv.destroyAllWindows()
    vid.release()


if __name__ == "__main__":
    img_difficulty = Difficulty.MEDIUM_FAST
    original, new, binary, color_matrix = load_image(img_difficulty)

    detector = ObjectDetector(new, binary, color_matrix)
    detector.scan_image()

    # All we need to supply to the world
    objects = detector.get_objects_array()
    objects_dict = detector.get_objects()
    label_plane = detector.get_label_plane()

    # detector.print_label_plane()

    # call world
    World.get_instance().create_world(objects=objects,
                                      objects_dict=objects_dict,
                                      label_plane=label_plane)

    # Discover properties of objects
Esempio n. 15
0
 def __init__(self):
     self.detector = ObjectDetector()
     self.classifier = Classifier()
Esempio n. 16
0
from detector import ObjectDetector
import sys
from PIL import Image
import numpy as np

img_path = sys.argv[1]
img = np.asarray(Image.open(img_path), dtype=np.uint8)

# Provide the .pb model file path
graph_path = "./faster_rcnn_resnet50_coco_2018_01_28/frozen_inference_graph.pb"

model = ObjectDetector(graph_path)
out = model.run(img)

print(out)