Esempio n. 1
0
def main():
    parser = argparse.ArgumentParser(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('--input', '-i', required=True,
                        help='Input image file.')
    parser.add_argument('--threshold', '-t', type=float, default=0.1,
                        help='Classification probability threshold.')
    parser.add_argument('--top_k', '-n', type=int, default=5,
                        help='Max number of returned classes.')
    parser.add_argument('--sparse', '-s', action='store_true', default=False,
                        help='Use sparse tensors.')
    parser.add_argument('--model', '-m', choices=('plants', 'insects', 'birds'), required=True,
                        help='Model to run.')
    args = parser.parse_args()

    model_type = {'plants':  inaturalist_classification.PLANTS,
                  'insects': inaturalist_classification.INSECTS,
                  'birds':   inaturalist_classification.BIRDS}[args.model]

    with ImageInference(inaturalist_classification.model(model_type)) as inference:
        image = Image.open(args.input)

        if args.sparse:
            configs = inaturalist_classification.sparse_configs(top_k=args.top_k,
                                                                threshold=args.threshold,
                                                                model_type=model_type)
            result = inference.run(image, sparse_configs=configs)
            classes = inaturalist_classification.get_classes_sparse(result)
        else:
            result = inference.run(image)
            classes = inaturalist_classification.get_classes(result,
                                                             top_k=args.top_k,
                                                             threshold=args.threshold)

        for i, (label, score) in enumerate(classes):
            print('Result %d: %s (prob=%f)' % (i, label, score))
Esempio n. 2
0
def main():
    parser = argparse.ArgumentParser('Image classification camera inference example.')
    parser.add_argument('--num_frames', '-n', type=int, default=None,
        help='Sets the number of frames to run for, otherwise runs forever.')
    parser.add_argument('--num_objects', '-c', type=int, default=3,
        help='Sets the number of object interences to print.')
    parser.add_argument('--nopreview', dest='preview', action='store_false', default=True,
        help='Enable camera preview')
    parser.add_argument('--threshold', '-t', type=float, default=0.1,
                        help='Classification probability threshold.')
    parser.add_argument('--top_k', '-k', type=int, default=3,
                        help='Max number of returned classes.')
    parser.add_argument('--sparse', '-s', action='store_true', default=False,
                        help='Use sparse tensors')
    parser.add_argument('--model', '-m', choices=('plants', 'insects', 'birds'), required=True,
                        help='Model to run')
    args = parser.parse_args()

    model_type = {'plants':  inaturalist_classification.PLANTS,
                  'insects': inaturalist_classification.INSECTS,
                  'birds':   inaturalist_classification.BIRDS}[args.model]

    with PiCamera(sensor_mode=4, framerate=10) as camera, \
         CameraPreview(camera, enabled=args.preview), \
         CameraInference(inaturalist_classification.model(model_type)) as inference:
        for result in inference.run(args.num_frames):
            classes = inaturalist_classification.get_classes(result, top_k=args.top_k, threshold=args.threshold)
            print(classes_info(classes))
            if classes:
                camera.annotate_text = '%s (%.2f)' % classes[0]
 def run(self):
     while self._running:
         try:
             self.logger.debug(
                 'loading {} model ...'.format(self.model().name))
             model = inaturalist_classification.model(self.model().value)
             with CameraInference(model) as inference:
                 self.logger.debug('running inference ...')
                 for result in inference.run():
                     predictions = inaturalist_classification.get_classes(
                         result,
                         top_k=self.top_k_predictions())
                     outgoing_signals = []
                     for label, score in predictions:
                         signal_dict = {
                             'label': label,
                             'score': score,
                         }
                         outgoing_signal = Signal(signal_dict)
                         outgoing_signals.append(outgoing_signal)
                     if not self._running:
                         break
                     self.notify_signals(outgoing_signals)
         except:
             self.logger.exception('failed to get inference result!')
             self.reset_camera()
     self.release_camera()
Esempio n. 4
0
 def run(self, image):
     self.result = self.inference.run(image)
     self.bird_class = inaturalist_classification.get_classes(self.result,
                                                              top_k=1,
                                                              threshold=0.8)
     if len(self.bird_class) == 0:  # if nothing is found, return none
         return None
     elif self.bird_class[0][
             0] == 'background':  # if background is found, return none
         return None
     else:  # If a bird clas is returned, flip the image, run again and ensure same class is returned
         self.result_2 = self.inference.run(ImageOps.mirror(image))
         self.bird_class_2 = inaturalist_classification.get_classes(
             self.result_2, top_k=1, threshold=0.8)
         if len(self.bird_class_2) == 0:
             return None
         else:
             if self.bird_class_2[0][0] == self.bird_class[0][0]:
                 return self.bird_class[0]
             else:
                 return None
    def test_classification(self):
        with TestImage(self.image_file) as image:
            with ImageInference(m.model(self.model_type)) as inference:
                if self.sparse:
                    configs = m.sparse_configs(top_k=self.TOP_K,
                                               threshold=self.THRESHOLD,
                                               model_type=self.model_type)
                    result = inference.run(image, sparse_configs=configs)
                    classes = m.get_classes_sparse(result)
                else:
                    result = inference.run(image)
                    classes = m.get_classes(result, top_k=self.TOP_K, threshold=self.THRESHOLD)

                self.check(classes)
Esempio n. 6
0
    def test_classification(self):
        with TestImage(self.image_file) as image:
            with ImageInference(m.model(self.model_type)) as inference:
                if self.sparse:
                    configs = m.sparse_configs(top_k=self.TOP_K,
                                               threshold=self.THRESHOLD,
                                               model_type=self.model_type)
                    result = inference.run(image, sparse_configs=configs)
                    classes = m.get_classes_sparse(result)
                else:
                    result = inference.run(image)
                    classes = m.get_classes(result, top_k=self.TOP_K, threshold=self.THRESHOLD)

                self.check(classes)
Esempio n. 7
0
#Test of varying resolution images#

scene_number = 3

from PIL import Image
import os

from aiy.vision.inference import ImageInference
from aiy.vision.models import inaturalist_classification

images_to_test = os.listdir("scene" + str(scene_number))

result_file = open('scene_' + str(scene_number) + '_res_test.txt', 'w+')

for image_name in images_to_test:
    with ImageInference(
            inaturalist_classification.model(
                inaturalist_classification.BIRDS)) as inference:
        image = Image.open('scene' + str(scene_number) + '/' + image_name)
        print(image_name)
        result = inference.run(image)
        classes = inaturalist_classification.get_classes(result,
                                                         top_k=5,
                                                         threshold=0.0)

        for i, (label, score) in enumerate(classes):
            print('Test: %s Result %d: %s (prob=%f)' %
                  (image_name, i, label, score))
            result_file.write('Test: %s Result %d: %s (prob=%f)\n' %
                              (image_name, i, label, score))
def main():
    parser = argparse.ArgumentParser(
        'Image classification camera inference example.')
    parser.add_argument(
        '--num_frames',
        '-n',
        type=int,
        default=None,
        help='Sets the number of frames to run for, otherwise runs forever.')
    parser.add_argument('--num_objects',
                        '-c',
                        type=int,
                        default=3,
                        help='Sets the number of object interences to print.')
    parser.add_argument('--nopreview',
                        dest='preview',
                        action='store_false',
                        default=True,
                        help='Enable camera preview')
    parser.add_argument('--threshold',
                        '-t',
                        type=float,
                        default=0.1,
                        help='Classification probability threshold.')
    parser.add_argument('--top_k',
                        '-k',
                        type=int,
                        default=3,
                        help='Max number of returned classes.')
    parser.add_argument('--sparse',
                        '-s',
                        action='store_true',
                        default=False,
                        help='Use sparse tensors')
    parser.add_argument('--model',
                        '-m',
                        choices=('plants', 'insects', 'birds'),
                        required=True,
                        help='Model to run')
    args = parser.parse_args()

    model_type = {
        'plants': inaturalist_classification.PLANTS,
        'insects': inaturalist_classification.INSECTS,
        'birds': inaturalist_classification.BIRDS
    }[args.model]

    collector = collections.deque([], maxlen=7)
    with PiCamera(sensor_mode=4, framerate=10) as camera, \
         CameraPreview(camera, enabled=args.preview), \
         CameraInference(inaturalist_classification.model(model_type)) as inference:
        for result in inference.run(args.num_frames):
            classes = inaturalist_classification.get_classes(
                result, top_k=args.top_k, threshold=args.threshold)
            if classes:
                collector.appendleft(list(itertools.chain(*classes))[0])
            if len(collector) > 5:
                if collector[0] != 'background' and collector.count(
                        collector[0]) > 4:
                    log.info(classes_info(classes))
                    collector.clear()
                    if get_size(log_path) < max_log and flip(0.1):
                        camera.capture(log_path + ts() + '.jpg')
            if classes:
                camera.annotate_text = '%s (%.2f)' % classes[0]
Esempio n. 9
0
def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument(
        '--num_frames',
        '-n',
        type=int,
        default=None,
        help='Sets the number of frames to run for, otherwise runs forever.')
    parser.add_argument('--threshold',
                        '-t',
                        type=float,
                        default=0.1,
                        help='Classification probability threshold.')
    parser.add_argument('--top_k',
                        '-k',
                        type=int,
                        default=5,
                        help='Max number of returned classes.')
    parser.add_argument('--sparse',
                        '-s',
                        action='store_true',
                        default=False,
                        help='Use sparse tensors.')
    parser.add_argument('--model',
                        '-m',
                        choices=('plants', 'insects', 'birds'),
                        required=True,
                        help='Model to run.')
    parser.add_argument('--nopreview',
                        dest='preview',
                        action='store_false',
                        default=True,
                        help='Enable camera preview')
    args = parser.parse_args()

    model_type = {
        'plants': inaturalist_classification.PLANTS,
        'insects': inaturalist_classification.INSECTS,
        'birds': inaturalist_classification.BIRDS
    }[args.model]

    with PiCamera(sensor_mode=4, framerate=10) as camera, \
         CameraPreview(camera, enabled=args.preview), \
         CameraInference(inaturalist_classification.model(model_type)) as inference:

        if args.sparse:
            configs = inaturalist_classification.sparse_configs(
                top_k=args.top_k,
                threshold=args.threshold,
                model_type=model_type)
            for result in inference.run(args.num_frames,
                                        sparse_configs=configs):
                classes = inaturalist_classification.get_classes_sparse(result)
                for i, (label, score) in enumerate(classes):
                    print('Result %d: %s (prob=%f)' % (i, label, score))
                if classes:
                    camera.annotate_text = '%s (%.2f)' % classes[0]

        else:
            for result in inference.run(args.num_frames):
                classes = inaturalist_classification.get_classes(
                    result, top_k=args.top_k, threshold=args.threshold)
                for i, (label, score) in enumerate(classes):
                    print('Result %d: %s (prob=%f)' % (i, label, score))
                if classes:
                    camera.annotate_text = '%s (%.2f)' % classes[0]