def run(self):
     while self._running:
         try:
             self.logger.debug('loading inference model ...')
             with CameraInference(
                     image_classification.model()) as inference:
                 self.logger.debug('running inference ...')
                 for result in inference.run():
                     predictions = image_classification.get_classes(
                         result,
                         top_k=self.top_k_predictions(),
                         threshold=0)
                     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()
Exemple #2
0
def main():
    parser = argparse.ArgumentParser(
        description='Example application for displaying a dial indicator for '
        'what object is seen')
    parser.add_argument(
        '--output_overlay',
        default=True,
        type=bool,
        help='Should the visual overlay be generated')
    parser.add_argument(
        '--button_enabled',
        default=True,
        type=bool,
        help='Should the button be monitored')
    parser.add_argument(
        '--button_active',
        default=False,
        type=bool,
        help='Should the button start out active (true) or only be active once '
        'pressed (false)')
    flags = parser.parse_args()
    load_model = time.time()
    category_count = len(category_mapper.get_categories())
    button = AutoButton(flags.button_active, flags.button_enabled)

    for category in category_mapper.get_categories():
        print('Category[%d]: %s' % (category_mapper.get_category_index(category),
                                    category))
    with picamera.PiCamera() as camera:
        camera.resolution = (1640, 1232)
        camera.start_preview()
        overlay = OverlayManager(
            camera) if flags.output_overlay else DummyOverlayManager()
        servo = AngularServo(PIN_A, min_pulse_width=.0005, max_pulse_width=.0019)
        with CameraInference(image_classification.model()) as classifier:
            print('Load Model %f' % (time.time() - load_model))
            for result in classifier.run():
                if not button.on():
                    overlay.clear()
                    servo.angle = -90
                    continue

                classes = image_classification.get_classes(result)

                probs = [0] * (category_count + 1)
                result_categories = []
                for label, score in classes:
                    category = category_mapper.get_category(label) or 'Other'
                    probs[category_mapper.get_category_index(category) + 1] += score
                    result_categories.append(category)
                overlay.update(classes, result_categories)
                max_prob = max(probs)
                best_category = probs.index(max_prob)
                if best_category == 0 and max_prob > .5:
                    servo.angle = -90
                elif best_category != 0:
                    servo.angle = -90 + (180 * best_category) / category_count
                    print('category: %d - %s' %
                          (best_category,
                           category_mapper.get_categories()[best_category - 1]))
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')
    args = parser.parse_args()

    with PiCamera(sensor_mode=4, framerate=30) as camera, \
         CameraPreview(camera, enabled=args.preview), \
         CameraInference(image_classification.model()) as inference:
        for result in inference.run(args.num_frames):
            classes = image_classification.get_classes(result,
                                                       top_k=args.num_objects)
            print(classes_info(classes))
            if classes:
                camera.annotate_text = '%s (%.2f)' % classes[0]
Exemple #4
0
def main():
    parser = argparse.ArgumentParser(
        description='Example application for displaying a dial indicator for '
        'what object is seen')
    parser.add_argument(
        '--output_overlay',
        default=True,
        type=bool,
        help='Should the visual overlay be generated')
    parser.add_argument(
        '--button_enabled',
        default=True,
        type=bool,
        help='Should the button be monitored')
    parser.add_argument(
        '--button_active',
        default=False,
        type=bool,
        help='Should the button start out active (true) or only be active once '
        'pressed (false)')
    flags = parser.parse_args()
    load_model = time.time()
    category_count = len(category_mapper.get_categories())
    button = AutoButton(flags.button_active, flags.button_enabled)

    for category in category_mapper.get_categories():
        print('Category[%d]: %s' % (category_mapper.get_category_index(category),
                                    category))
    with picamera.PiCamera() as camera:
        camera.resolution = (1640, 1232)
        camera.start_preview()
        overlay = OverlayManager(
            camera) if flags.output_overlay else DummyOverlayManager()
        servo = AngularServo(PIN_A, min_pulse_width=.0005, max_pulse_width=.0019)
        with CameraInference(image_classification.model()) as classifier:
            print('Load Model %f' % (time.time() - load_model))
            for result in classifier.run():
                if not button.on():
                    overlay.clear()
                    servo.angle = -90
                    continue

                classes = image_classification.get_classes(result)

                probs = [0] * (category_count + 1)
                result_categories = []
                for label, score in classes:
                    category = category_mapper.get_category(label) or 'Other'
                    probs[category_mapper.get_category_index(category) + 1] += score
                    result_categories.append(category)
                overlay.update(classes, result_categories)
                max_prob = max(probs)
                best_category = probs.index(max_prob)
                if best_category == 0 and max_prob > .5:
                    servo.angle = -90
                elif best_category != 0:
                    servo.angle = -90 + (180 * best_category) / category_count
                    print('category: %d - %s' %
                          (best_category,
                           category_mapper.get_categories()[best_category - 1]))
Exemple #5
0
def main():
    importJsonData()

    ser.write(("START").encode())
    ser.write(str.encode('\n'))

    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')
    args = parser.parse_args()



    with PiCamera(sensor_mode=4, framerate=10) as camera, \
        CameraPreview(camera, enabled=args.preview), \
        CameraInference(image_classification.model()) as inference:
        camera.vflip = True
        for result in inference.run(args.num_frames):
            classes = image_classification.get_classes(result,
                                                       top_k=args.num_objects)

            className = classes[0][0].split('/')[
                0]  #this is because I only want one category at the time
            print(className)

            index = getClassIndex(className)
            print(index)

            smorfia_number = getSmorfiaNumber(index)
            print(smorfia_number)

            smorfia_label = getSmorfiaLabel(smorfia_number)
            print(smorfia_label)

            ser.write((str(smorfia_number)).encode())
            ser.write(str.encode(':'))
            ser.write(smorfia_label.encode())
            ser.write(str.encode('\n'))

            print('\n')
            time.sleep(
                1)  # Delays for 5 seconds. You can also use a float value.

            if classes:
                camera.annotate_text = '%s (%.2f)' % classes[0]
Exemple #6
0
def main():
    parser = argparse.ArgumentParser(
        'Image classification camera inference example.')
    parser.add_argument(
        '--num_frames',
        '-n',
        type=int,
        dest='num_frames',
        default=None,
        help='Sets the number of frames to run for, otherwise runs forever.')
    parser.add_argument('--num_objects',
                        '-c',
                        type=int,
                        dest='num_objects',
                        default=3,
                        help='Sets the number of object interences to print.')

    args = parser.parse_args()

    # Forced sensor mode, 1640x1232, full FoV. See:
    # https://picamera.readthedocs.io/en/release-1.13/fov.html#sensor-modes
    with PiCamera(sensor_mode=4, framerate=30) as camera:
        camera.start_preview()

        with CameraInference(image_classification.model()) as inference:
            for result in inference.run(args.num_frames):
                classes = image_classification.get_classes(result)
                print(classes_info(classes, args.num_objects))

        camera.stop_preview()
Exemple #7
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=('squeezenet', 'mobilenet'),
                        default='mobilenet',
                        help='Model to run.')
    args = parser.parse_args()

    # There are two models available for image classification task:
    # 1) MobileNet based (image_classification.MOBILENET), which has 59.9% top-1
    # accuracy on ImageNet;
    # 2) SqueezeNet based (image_classification.SQUEEZENET), which has 45.3% top-1
    # accuracy on ImageNet;
    model_type = {
        'squeezenet': image_classification.SQUEEZENET,
        'mobilenet': image_classification.MOBILENET
    }[args.model]

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

        if args.sparse:
            configs = image_classification.sparse_configs(
                top_k=args.top_k,
                threshold=args.threshold,
                model_type=model_type)
            result = inference.run(image, sparse_configs=configs)
            classes = image_classification.get_classes_sparse(result)
        else:
            result = inference.run(image)
            classes = image_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))
Exemple #8
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--input', '-i', dest='input', required=True)
    args = parser.parse_args()
    
    with ImageInference(image_classification.model()) as inference:
        image = Image.open(args.input)
        classes = image_classification.get_classes(inference.run(image))
        for i, (label, score) in enumerate(classes):
            print('Result %d: %s (prob=%f)' % (i, label, score))
def main():
    """Image classification camera inference example."""
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--num_frames',
        '-n',
        type=int,
        dest='num_frames',
        default=-1,
        help='Sets the number of frames to run for, otherwise runs forever.')

    parser.add_argument('--num_objects',
                        '-c',
                        type=int,
                        dest='num_objects',
                        default=3,
                        help='Sets the number of object interences to print.')

    args = parser.parse_args()

    def print_classes(classes, object_count):
        s = ''
        for index, (obj, prob) in enumerate(classes):
            if index > object_count - 1:
                break
            s += '%s=%1.2f\t|\t' % (obj, prob)
        print('%s\r' % s)

    with PiCamera() as camera:
        # Forced sensor mode, 1640x1232, full FoV. See:
        # https://picamera.readthedocs.io/en/release-1.13/fov.html#sensor-modes
        # This is the resolution inference run on.
        camera.sensor_mode = 4

        # Scaled and cropped resolution. If different from sensor mode implied
        # resolution, inference results must be adjusted accordingly. This is
        # true in particular when camera.start_recording is used to record an
        # encoded h264 video stream as the Pi encoder can't encode all native
        # sensor resolutions, or a standard one like 1080p may be desired.
        camera.resolution = (1640, 1232)

        # Start the camera stream.
        camera.framerate = 30
        camera.start_preview()

        with CameraInference(image_classification.model()) as inference:
            for i, result in enumerate(inference.run()):
                if i == args.num_frames:
                    break
                classes = image_classification.get_classes(result)
                print_classes(classes, args.num_objects)
                i = i + 1

        camera.stop_preview()
Exemple #10
0
 def process(self, file):
     model_type = image_classification.MOBILENET
     with ImageInference(
             image_classification.model(model_type)) as inference:
         image = file
         classes = image_classification.get_classes(
             inference.run(image),
             max_num_objects=5,
             object_prob_threshold=0.1)
         for i, (label, score) in enumerate(classes):
             print('Result %d: %s (prob=%f)' % (i, label, score))
Exemple #11
0
def main():
    global count
    global lasttime
    global testing

    num_frames = None
    num_objects = 8  # just for debug printing

    # Forced sensor mode, 1640x1232, full FoV. use mode 4, was using frame rate of 10
    # https://picamera.readthedocs.io/en/release-1.13/fov.html#sensor-modes
    # kevinh 3280x2464 is mode 3 (but that is too large for twitter)
    with PiCamera(sensor_mode=4, framerate=3) as camera:
        camera.awb_mode = 'sunlight'
        camera.exposure_mode = 'sports'
        # if out of memory err occurs see https://stackoverflow.com/questions/39251815/python-not-taking-picture-at-highest-resolution-from-raspberry-pi-camera
        camera.resolution = (1920, 1080)

        with CameraInference(
                image_classification.model(
                    image_classification.MOBILENET)) as inference:
            for result in inference.run(num_frames):
                classes = image_classification.get_classes(result)
                newclasses = list(filter(removeold, classes))
                if newclasses:
                    print(classes_info(newclasses, num_objects))
                    name = newclasses[0][0]

                    filename = name.replace("/", "_").replace(" ", ".")
                    hasbird = list(
                        filter(lambda canidate: (canidate in name),
                               interesting))

                    filename += ".jpg"
                    # print('writing', filename)

                    if hasbird:
                        camera.capture(
                            filename
                        )  # the twitter client only reads from disk (for now FIXME)

                        now = datetime.datetime.now()
                        deltat = now - lasttime
                        if deltat > maxdelta:
                            lasttime = now
                            if not testing:
                                tweeter.tweet(
                                    filename,
                                    'I just saw a hummingbird! tweet tweet!')
                                print('tweet a bird')
                            else:
                                print('test a bird')
                        else:
                            print('ignore a bird')
    def test_classification(self):
        with TestImage(self.image_file) as image:
            with ImageInference(ic.model(self.model_type)) as inference:
                if self.sparse:
                    sparse_configs = ic.sparse_configs(top_k=self.TOP_K,
                                                       threshold=self.THRESHOLD,
                                                       model_type=self.model_type)
                    result = inference.run(image, sparse_configs=sparse_configs)
                    classes = ic.get_classes_sparse(result)
                else:
                    result = inference.run(image)
                    classes = ic.get_classes(result, top_k=self.TOP_K, threshold=self.THRESHOLD)

                self.check(classes)
    def testDogMobilenet(self):
        with TestImage('dog.jpg') as image:
            with ImageInference(
                    image_classification.model(
                        image_classification.MOBILENET)) as inference:
                classes = image_classification.get_classes(
                    inference.run(image))
                label, score = classes[0]
                self.assertEqual('boxer', label)
                self.assertAlmostEqual(score, 0.684, delta=0.001)

                label, score = classes[1]
                self.assertEqual('bull mastiff', label)
                self.assertAlmostEqual(score, 0.222, delta=0.001)
    def testDogSqueezenet(self):
        with TestImage('dog.jpg') as image:
            with ImageInference(
                    image_classification.model(
                        image_classification.SQUEEZENET)) as inference:
                classes = image_classification.get_classes(
                    inference.run(image))

                label, score = classes[0]
                self.assertEqual('pug/pug-dog', label)
                self.assertAlmostEqual(score, 0.271, delta=0.001)

                label, score = classes[1]
                self.assertEqual('bull mastiff', label)
                self.assertAlmostEqual(score, 0.141, delta=0.001)
def detect_object(inference,
                  camera,
                  classes,
                  threshold,
                  out_dir,
                  range_x=[0, 1],
                  range_y=[0, 1]):
    """Detects objects belonging to given classes in camera stream."""
    stream = io.BytesIO()
    camera.capture(stream, format='jpeg')
    stream.seek(0)
    image = Image.open(stream)

    # Every so often, we get an image with a decimated green channel
    # Skip these.
    rgb_histogram = np.array(image.histogram()).reshape((3, 256))
    green_peak = np.argmax(rgb_histogram[1, :])
    if green_peak < 3:
        time.sleep(1.0)
        return False, None, None

    debug_data = []
    detection = False
    max_accumulator = 0.
    print('Inferring...')
    for p in crop_parameters(image, range_x, range_y):
        im_crop = image.crop(p)
        accumulator = 0.
        infer_classes = image_classification.get_classes(
            inference.run(im_crop),
            max_num_objects=5,
            object_prob_threshold=0.05)
        corner = [p[0], p[1]]
        print(corner)
        for idx, (label, score) in enumerate(infer_classes):
            debug_data.append((corner, im_crop.size, idx, label, score))
            if label in classes:
                accumulator += score
        if accumulator > max_accumulator:
            max_accumulator = accumulator
        if accumulator >= threshold:
            detection = True
            break
    if out_dir:
        debug_output(image, debug_data, out_dir)
    print('Accumulator: %f' % (max_accumulator))
    print('Detection!' if detection else 'Non Detection')
    return detection, image, debug_data
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')
    args = parser.parse_args()

    with PiCamera(sensor_mode=4, framerate=30) as camera, \
         CameraPreview(camera, enabled=args.preview), \
         CameraInference(image_classification.model()) as inference:
        for result in inference.run(args.num_frames):
            classes = image_classification.get_classes(result, top_k=args.num_objects)
            print(classes_info(classes))
            if classes:
                camera.annotate_text = '%s (%.2f)' % classes[0]
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--input', '-i', dest='input', required=True)
    args = parser.parse_args()

    # There are two models available for image classification task:
    # 1) MobileNet based (image_classification.MOBILENET), which has 59.9% top-1
    # accuracy on ImageNet;
    # 2) SqueezeNet based (image_classification.SQUEEZENET), which has 45.3% top-1
    # accuracy on ImageNet;
    model_type = image_classification.MOBILENET
    with ImageInference(image_classification.model(model_type)) as inference:
        image = Image.open(
            io.BytesIO(sys.stdin.buffer.read()) if args.input ==
            '-' else args.input)
        classes = image_classification.get_classes(inference.run(image),
                                                   max_num_objects=5,
                                                   object_prob_threshold=0.1)
        for i, (label, score) in enumerate(classes):
            print('Result %d: %s (prob=%f)' % (i, label, score))
Exemple #18
0
def detect_object(inference, camera, classes, threshold, out_dir, range_x=[0, 1], range_y=[0, 1]):
    """Detects objects belonging to given classes in camera stream."""
    stream = io.BytesIO()
    camera.capture(stream, format='jpeg')
    stream.seek(0)
    image = Image.open(stream)

    # Every so often, we get an image with a decimated green channel
    # Skip these.
    rgb_histogram = np.array(image.histogram()).reshape((3, 256))
    green_peak = np.argmax(rgb_histogram[1, :])
    if green_peak < 3:
        time.sleep(1.0)
        return False, None, None

    debug_data = []
    detection = False
    max_accumulator = 0.
    print('Inferring...')
    for p in crop_parameters(image, range_x, range_y):
        im_crop = image.crop(p)
        accumulator = 0.
        infer_classes = image_classification.get_classes(
            inference.run(im_crop), top_k=5, threshold=0.05)
        corner = [p[0], p[1]]
        print(corner)
        for idx, (label, score) in enumerate(infer_classes):
            debug_data.append((corner, im_crop.size, idx, label, score))
            if label in classes:
                accumulator += score
        if accumulator > max_accumulator:
            max_accumulator = accumulator
        if accumulator >= threshold:
            detection = True
            break
    if out_dir:
        debug_output(image, debug_data, out_dir)
    print('Accumulator: %f' % (max_accumulator))
    print('Detection!' if detection else 'Non Detection')
    return detection, image, debug_data
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=('squeezenet', 'mobilenet'), default='mobilenet',
                        help='Model to run.')
    args = parser.parse_args()

    # There are two models available for image classification task:
    # 1) MobileNet based (image_classification.MOBILENET), which has 59.9% top-1
    # accuracy on ImageNet;
    # 2) SqueezeNet based (image_classification.SQUEEZENET), which has 45.3% top-1
    # accuracy on ImageNet;
    model_type = {'squeezenet': image_classification.SQUEEZENET,
                  'mobilenet': image_classification.MOBILENET}[args.model]

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

        if args.sparse:
            configs = image_classification.sparse_configs(top_k=args.top_k,
                                                          threshold=args.threshold,
                                                          model_type=model_type)
            result = inference.run(image, sparse_configs=configs)
            classes = image_classification.get_classes_sparse(result)
        else:
            result = inference.run(image)
            classes = image_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))
Exemple #20
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=2,
                        help='Sets the number of object interences to print.')
    parser.add_argument('--nopreview',
                        dest='preview',
                        action='store_false',
                        default=True,
                        help='Enable camera preview')
    args = parser.parse_args()

    with PiCamera(sensor_mode=4, framerate=30) as camera, \
         CameraPreview(camera, enabled=args.preview), \
         CameraInference(image_classification.model()) as inference, \
         Leds() as leds:

        leds.update(Leds.privacy_on())

        for result in inference.run(args.num_frames):
            classes = image_classification.get_classes(result,
                                                       top_k=args.num_objects,
                                                       threshold=.3)
            print(classes_info(classes))
            if classes:
                #annotator.clear()
                camera.annotate_text = '%s (%.2f)' % classes[0]
                if 'chicken' in classes[0]:
                    camera.capture('chickens.jpg')
                    print('Chicken captured')
Exemple #21
0
def process_inference(model, result, params):

    output = ApiObject()

    # handler for the AIY Vision object detection model
    if model == "object":
        output.threshold = 0.3
        objects = object_detection.get_objects(result, output.threshold)

        for obj in objects:
            # print(object)
            item = {
                'name': 'object',
                'class_name': obj._LABELS[obj.kind],
                'score': obj.score,
                'x': obj.bounding_box[0] / params['width'],
                'y': obj.bounding_box[1] / params['height'],
                'width': obj.bounding_box[2] / params['width'],
                'height': obj.bounding_box[3] / params['height']
            }

            output.numObjects += 1
            output.objects.append(item)

    # handler for the AIY Vision face detection model
    elif model == "face":
        faces = face_detection.get_faces(result)

        for face in faces:
            # print(face)
            item = {
                'name': 'face',
                'score': face.face_score,
                'joy': face.joy_score,
                'x': face.bounding_box[0] / params['width'],
                'y': face.bounding_box[1] / params['height'],
                'width': face.bounding_box[2] / params['width'],
                'height': face.bounding_box[3] / params['height']
            }

            output.numObjects += 1
            output.objects.append(item)

    elif model == "class":
        output.threshold = 0.3
        classes = image_classification.get_classes(result)

        s = ""

        for (obj, prob) in classes:
            if prob > output.threshold:
                s += '%s=%1.2f\t|\t' % (obj, prob)

                item = {'name': 'class', 'class_name': obj, 'score': prob}

                output.numObjects += 1
                output.objects.append(item)

        # print('%s\r' % s)

    return output
def run_inference(run_event,
                  model="face",
                  framerate=15,
                  cammode=5,
                  hres=1640,
                  vres=922,
                  stats=True):
    # See the Raspicam documentation for mode and framerate limits:
    # https://picamera.readthedocs.io/en/release-1.13/fov.html#sensor-modes
    # Default to the highest resolution possible at 16:9 aspect ratio

    global socket_connected, time_log

    leds = Leds()

    with PiCamera() as camera, PrivacyLed(leds):
        camera.sensor_mode = cammode
        camera.resolution = (hres, vres)
        camera.framerate = framerate
        camera.video_stabilization = True
        camera.start_preview()  # fullscreen=True)

        def model_selector(argument):
            options = {
                "object": object_detection.model(),
                "face": face_detection.model(),
                "class": image_classification.model()
            }
            return options.get(argument, "nothing")

        tf_model = model_selector(model)

        # this is not needed because the function defaults to "face"
        if tf_model == "nothing":
            print("No tensorflow model or invalid model specified - exiting..")
            camera.stop_preview()
            os._exit(0)
            return

        with CameraInference(tf_model) as inference:
            print("%s model loaded" % model)

            last_time = time()  # measure inference time

            for result in inference.run():

                # exit on shutdown
                if not run_event.is_set():
                    camera.stop_preview()
                    return

                output = ApiObject()

                # handler for the AIY Vision object detection model
                if model == "object":
                    output.threshold = 0.3
                    objects = object_detection.get_objects(
                        result, output.threshold)

                    for obj in objects:
                        # print(object)
                        item = {
                            'name': 'object',
                            'class_name': obj._LABELS[obj.kind],
                            'score': obj.score,
                            'x': obj.bounding_box[0] / capture_width,
                            'y': obj.bounding_box[1] / capture_height,
                            'width': obj.bounding_box[2] / capture_width,
                            'height': obj.bounding_box[3] / capture_height
                        }

                        output.numObjects += 1
                        output.objects.append(item)

                # handler for the AIY Vision face detection model
                elif model == "face":
                    faces = face_detection.get_faces(result)

                    for face in faces:
                        # print(face)
                        item = {
                            'name': 'face',
                            'score': face.face_score,
                            'joy': face.joy_score,
                            'x': face.bounding_box[0] / capture_width,
                            'y': face.bounding_box[1] / capture_height,
                            'width': face.bounding_box[2] / capture_width,
                            'height': face.bounding_box[3] / capture_height,
                        }

                        output.numObjects += 1
                        output.objects.append(item)

                elif model == "class":
                    output.threshold = 0.3
                    classes = image_classification.get_classes(result)

                    s = ""

                    for (obj, prob) in classes:
                        if prob > output.threshold:
                            s += '%s=%1.2f\t|\t' % (obj, prob)

                            item = {
                                'name': 'class',
                                'class_name': obj,
                                'score': prob
                            }

                            output.numObjects += 1
                            output.objects.append(item)

                    # print('%s\r' % s)

                now = time()
                output.timeStamp = now
                output.inferenceTime = (now - last_time)
                last_time = now

                # No need to do anything else if there are no objects
                if output.numObjects > 0:
                    output_json = output.to_json()
                    print(output_json)

                    # Send the json object if there is a socket connection
                    if socket_connected is True:
                        q.put(output_json)

                # Additional data to measure inference time
                if stats is True:
                    time_log.append(output.inferenceTime)
                    time_log = time_log[-10:]  # just keep the last 10 times
                    print("Avg inference time: %s" %
                          (sum(time_log) / len(time_log)))
def main():
    """Image classification camera inference example."""
    parser = argparse.ArgumentParser()
    #button = Button(23)
    pin = 24

    parser.add_argument(
        '--num_frames',
        '-n',
        type=int,
        dest='num_frames',
        default=-1,
        help='Sets the number of frames to run for, otherwise runs forever.')

    parser.add_argument('--num_objects',
                        '-c',
                        type=int,
                        dest='num_objects',
                        default=1,
                        help='Sets the number of object interences to print.')

    args = parser.parse_args()

    def print_classes(classes, object_count):
        s = ''
        for index, (obj, prob) in enumerate(classes):
            if index > object_count - 1:
                break
            s += '%s=%1.2f\t|\t' % (obj, prob)
        f = open('./object_recognition.txt', 'w')
        f.write(s)
        f.close()
        print('%s\r' % s)

    def firebase_upload(counter):
        camera.capture('/home/pi/training_images/firebase_image%s.jpg' %
                       counter)
        print('Image captured: firebase_image%s.jpg' % counter)

    with PiCamera() as camera:
        print('Script Started')
        # Forced sensor mode, 1640x1232, full FoV. See:
        # https://picamera.readthedocs.io/en/release-1.13/fov.html#sensor-modes
        # This is the resolution inference run on.
        camera.sensor_mode = 4

        # Scaled and cropped resolution. If different from sensor mode implied
        # resolution, inference results must be adjusted accordingly. This is
        # true in particular when camera.start_recording is used to record an
        # encoded h264 video stream as the Pi encoder can't encode all native
        # sensor resolutions, or a standard one like 1080p may be desired.
        camera.resolution = (1640, 1232)

        # Start the camera stream.
        camera.framerate = 30
        #		camera.start_preview()

        GPIO.setmode(GPIO.BCM)
        GPIO.setup(pin, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
        state = GPIO.input(pin)
        counter = 0
        model_type = image_classification.MOBILENET

        with CameraInference(
                image_classification.model(model_type)) as inference:
            for i, result in enumerate(inference.run()):
                state = GPIO.input(pin)
                if i == args.num_frames:
                    break
                classes = image_classification.get_classes(result)
                #if button.is_pressed or
                if state == 1:
                    counter += 1
                    print_classes(classes, args.num_objects)
                    #firebase_upload(counter)

                    time.sleep(.5)
Exemple #24
0
#!/usr/bin/env python
# This file is a modified version of image_classification.py from Google AIY Vision Kit.

import argparse
from PIL import Image
import subprocess
from aiy.vision.inference import ImageInference
from aiy.vision.models import image_classification

devnull = open('os.devnull', 'w')
ipaddr = subprocess.check_output(["hostname", "-I"]).decode("utf-8").strip()
commd = "http://" + ipaddr + ":9000/?action=snapshot"

while True:
    subprocess.run(["wget", "-O", "photo.jpg", commd],
                   stdout=devnull,
                   stderr=subprocess.STDOUT)

    with ImageInference(image_classification.model()) as inference:
        image = Image.open("./photo.jpg")
        classes = image_classification.get_classes(inference.run(image),
                                                   max_num_objects=1)
        for i, (label, score) in enumerate(classes):
            print('Result %d: %s (prob=%f)' % (i, label, score))
Exemple #25
0
def main():
	"""Image classification camera inference example."""
	parser = argparse.ArgumentParser()
	button = Button(23)
	parser.add_argument(
		'--num_frames',
		'-n',
		type=int,
		dest='num_frames',
		default=-1,
		help='Sets the number of frames to run for, otherwise runs forever.')

	parser.add_argument(
		'--num_objects',
		'-c',
		type=int,
		dest='num_objects',
		default=1,
		help='Sets the number of object interences to print.')

	args = parser.parse_args()

	def print_classes(classes, object_count):
		s = ''
		for index, (obj, prob) in enumerate(classes):
			if index > object_count - 1:
				break
			s += '%s=%1.2f\t|\t' % (obj, prob)
		f = open('object_recognition.txt','w')
		f.write(s)
		f.close()
		print('%s\r' % s)
		sys.stdout.flush()

	def firebase_upload(fileName):
		camera.capture('/home/pi/NotPushedToWifi/%s.jpg' % fileName)
		# print('Image captured')
		# print('%s' % fileName)

	with PiCamera() as camera:
		print('Script Started')
		# Forced sensor mode, 1640x1232, full FoV. See:
		# https://picamera.readthedocs.io/en/release-1.13/fov.html#sensor-modes
		# This is the resolution inference run on.
		camera.sensor_mode = 4

		# Scaled and cropped resolution. If different from sensor mode implied
		# resolution, inference results must be adjusted accordingly. This is
		# true in particular when camera.start_recording is used to record an
		# encoded h264 video stream as the Pi encoder can't encode all native
		# sensor resolutions, or a standard one like 1080p may be desired.
		camera.resolution = (1640, 1232)

		# Start the camera stream.
		camera.framerate = 30
		#camera.start_preview()
		model_type = image_classification.MOBILENET
		counter = 0
		with CameraInference(image_classification.model(model_type)) as inference:
			for i, result in enumerate(inference.run()):
				if i == args.num_frames:
					break
				classes = image_classification.get_classes(result)
				if button.is_pressed:
					now = datetime.now()
					local_time = now.strftime("%I-%M-%S_%Y-%d-%B")
					firebase_upload(local_time)
					print_classes(classes, args.num_objects)
					time.sleep(.5)