Beispiel #1
0
 def test_image_classification_mobilenet(self):
     avg_end_to_end, avg_bonnet = self.benchmarkModel(
         image_classification.model(image_classification.MOBILENET),
         partial(image_classification.get_classes,
                 threshold=CLASSIFICATION_THRESHOLD))
     self.assertLatency(avg_bonnet, 42.0)
     self.assertLatency(avg_end_to_end, 80.0, 0.3)
Beispiel #2
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()
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]
Beispiel #4
0
 def test_image_classification_squeezenet(self):
     avg_end_to_end, avg_bonnet = self.benchmarkModel(
         image_classification.model(image_classification.SQUEEZENET),
         partial(image_classification.get_classes,
                 threshold=CLASSIFICATION_THRESHOLD))
     self.assertLatency(avg_bonnet, 183.0)
     self.assertLatency(avg_end_to_end, 202.0)
 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()
 def model_selector(argument):
     options = {
         "object": object_detection.model(),
         "face": face_detection.model(),
         "class": image_classification.model()
     }
     return options.get(argument, "nothing")
Beispiel #7
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]
Beispiel #8
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]))
Beispiel #9
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]))
Beispiel #10
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))
Beispiel #11
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))
Beispiel #12
0
 def test_image_classification_squeezenet_sparse(self):
     sparse_configs = image_classification.sparse_configs(
         threshold=CLASSIFICATION_THRESHOLD,
         model_type=image_classification.SQUEEZENET)
     avg_end_to_end, avg_bonnet = self.benchmarkModel(
         image_classification.model(image_classification.SQUEEZENET),
         partial(image_classification.get_classes_sparse),
         sparse_configs=sparse_configs)
     self.assertLatency(avg_bonnet, 183.0)
     self.assertLatency(avg_end_to_end, 202.0)
 def test_image_classification_mobilenet_sparse(self):
     sparse_configs = image_classification.sparse_configs(
         threshold=CLASSIFICATION_THRESHOLD,
         model_type=image_classification.MOBILENET)
     avg_end_to_end, avg_bonnet = self.benchmarkModel(
         image_classification.model(image_classification.MOBILENET),
         image_classification.get_classes_sparse,
         sparse_configs=sparse_configs)
     self.assertLatency(avg_bonnet, 42.0)
     self.assertLatency(avg_end_to_end, 56.0)
Beispiel #14
0
 def test_image_classification_mobilenet_sparse(self):
     sparse_configs = image_classification.sparse_configs(
         threshold=CLASSIFICATION_THRESHOLD,
         model_type=image_classification.MOBILENET)
     avg_end_to_end, avg_bonnet = self.benchmarkModel(
         image_classification.model(image_classification.MOBILENET),
         image_classification.get_classes_sparse,
         sparse_configs=sparse_configs)
     self.assertLatency(avg_bonnet, 42.0)
     self.assertLatency(avg_end_to_end, 56.0)
 def test_image_classification_squeezenet_sparse(self):
     sparse_configs = image_classification.sparse_configs(
         threshold=CLASSIFICATION_THRESHOLD,
         model_type=image_classification.SQUEEZENET)
     avg_end_to_end, avg_bonnet = self.benchmarkModel(
         image_classification.model(image_classification.SQUEEZENET),
         partial(image_classification.get_classes_sparse),
         sparse_configs=sparse_configs)
     self.assertLatency(avg_bonnet, 183.0)
     self.assertLatency(avg_end_to_end, 202.0)
Beispiel #16
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))
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()
Beispiel #18
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 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 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 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)
Beispiel #22
0
def main():
  parser = argparse.ArgumentParser()
  parser.add_argument('--input', '-i', dest='input', required=True)
  parser.add_argument('--output', '-o', dest='output')
  args = parser.parse_args()

  with ImageInference(image_classification.model()) as inference:
    image = Image.open(args.input)
    image_center, offset = _crop_center(image)
    draw = ImageDraw.Draw(image)
    result = inference.run(image_center)
    for i, obj in enumerate(image_classification.get_objects(result, 0.3, offset)):
      print('Object #%d: %s' % (i, str(obj)))
      x, y, width, height = obj.bounding_box
      draw.rectangle((x, y, x + width, y + height), outline='red')
    if args.output:
      image.save(args.output)
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))
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))
Beispiel #26
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')
Beispiel #27
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)
 def testImageClassificationMobilenetLatency(self):
     avg_end_to_end, avg_bonnet = self.benchmarkModel(
         image_classification.model(image_classification.MOBILENET),
         image_classification.get_classes)
     self.assertLatency(avg_bonnet, 42.0)
     self.assertLatency(avg_end_to_end, 80.0, 0.3)
 def testImageClassificationSqueezenetLatency(self):
     avg_end_to_end, avg_bonnet = self.benchmarkModel(
         image_classification.model(image_classification.SQUEEZENET),
         image_classification.get_classes)
     self.assertLatency(avg_bonnet, 183.0)
     self.assertLatency(avg_end_to_end, 202.0)
 def test_image_classification_squeezenet(self):
     avg_end_to_end, avg_bonnet = self.benchmarkModel(
         image_classification.model(image_classification.SQUEEZENET),
         partial(image_classification.get_classes, threshold=CLASSIFICATION_THRESHOLD))
     self.assertLatency(avg_bonnet, 183.0)
     self.assertLatency(avg_end_to_end, 202.0)
 def test_image_classification_mobilenet(self):
     avg_end_to_end, avg_bonnet = self.benchmarkModel(
         image_classification.model(image_classification.MOBILENET),
         partial(image_classification.get_classes, threshold=CLASSIFICATION_THRESHOLD))
     self.assertLatency(avg_bonnet, 42.0)
     self.assertLatency(avg_end_to_end, 80.0, 0.3)
Beispiel #32
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--classfile', '-c', dest='classfile', required=True)
    parser.add_argument(
        '--threshold',
        '-t',
        dest='threshold',
        required=False,
        type=float,
        default=0.5)
    parser.add_argument('--out_dir', '-o', dest='out_dir', required=False, type=str, default='./')
    parser.add_argument(
        '--capture_delay',
        dest='capture_delay',
        required=False,
        type=float,
        default=5.0)
    parser.add_argument(
        '--capture_length',
        dest='capture_length',
        required=False,
        type=int,
        default=20)
    parser.add_argument('--debug', '-d', dest='debug', required=False, action='store_true')
    # Crop box in fraction of the image width. By default full camera image is processed.
    parser.add_argument(
        '--cropbox_left',
        dest='cropbox_left',
        required=False,
        type=float,
        default=0.0)
    parser.add_argument(
        '--cropbox_right',
        dest='cropbox_right',
        required=False,
        type=float,
        default=1.0)
    parser.add_argument(
        '--cropbox_top',
        dest='cropbox_top',
        required=False,
        type=float,
        default=0.0)
    parser.add_argument(
        '--cropbox_bottom',
        dest='cropbox_bottom',
        required=False,
        type=float,
        default=1.0)
    parser.set_defaults(debug=False)
    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

    # Read the class list from a text file
    with open(args.classfile) as f:
        classes = [line.strip() for line in f]

    print('Starting camera detection, using the following classes:')
    for label in classes:
        print('  ', label)
    print('Threshold:', args.threshold)
    print('Debug mode:', args.debug)
    print('Capture Delay:', args.capture_delay)

    debug_out = args.out_dir if args.debug else ''

    with ImageInference(image_classification.model(model_type)) as inference:
        with picamera.PiCamera(resolution=(1920, 1080)) as camera:
            stream = picamera.PiCameraCircularIO(camera, seconds=args.capture_length)
            camera.start_recording(stream, format='h264')
            while True:
                detection, image, inference_data = detect_object(
                    inference, camera, classes, args.threshold, debug_out,
                    (args.cropbox_left, args.cropbox_right),
                    (args.cropbox_top, args.cropbox_bottom))
                if detection:
                    detect_time = int(time.time())
                    camera.wait_recording(args.capture_delay)
                    video_file = 'capture_%d.mpeg' % detect_time
                    image_file = 'capture_%d.jpg' % detect_time
                    stream.copy_to(os.path.join(args.out_dir, video_file))
                    stream.flush()
                    debug_output(image, inference_data, args.out_dir, image_file)
                    print('Wrote video file to', os.path.join(args.out_dir, video_file))
                    camera.wait_recording(max(args.capture_length - args.capture_delay, 0))
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)
Beispiel #34
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--classfile', '-c', dest='classfile', required=True)
    parser.add_argument(
        '--threshold',
        '-t',
        dest='threshold',
        required=False,
        type=float,
        default=0.5)
    parser.add_argument('--out_dir', '-o', dest='out_dir', required=False, type=str, default='./')
    parser.add_argument(
        '--capture_delay',
        dest='capture_delay',
        required=False,
        type=float,
        default=5.0)
    parser.add_argument(
        '--capture_length',
        dest='capture_length',
        required=False,
        type=int,
        default=20)
    parser.add_argument('--debug', '-d', dest='debug', required=False, action='store_true')
    # Crop box in fraction of the image width. By default full camera image is processed.
    parser.add_argument(
        '--cropbox_left',
        dest='cropbox_left',
        required=False,
        type=float,
        default=0.0)
    parser.add_argument(
        '--cropbox_right',
        dest='cropbox_right',
        required=False,
        type=float,
        default=1.0)
    parser.add_argument(
        '--cropbox_top',
        dest='cropbox_top',
        required=False,
        type=float,
        default=0.0)
    parser.add_argument(
        '--cropbox_bottom',
        dest='cropbox_bottom',
        required=False,
        type=float,
        default=1.0)
    parser.set_defaults(debug=False)
    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

    # Read the class list from a text file
    with open(args.classfile) as f:
        classes = [line.strip() for line in f]

    print('Starting camera detection, using the following classes:')
    for label in classes:
        print('  ', label)
    print('Threshold:', args.threshold)
    print('Debug mode:', args.debug)
    print('Capture Delay:', args.capture_delay)

    debug_out = args.out_dir if args.debug else ''

    with ImageInference(image_classification.model(model_type)) as inference:
        with picamera.PiCamera(resolution=(1920, 1080)) as camera:
            stream = picamera.PiCameraCircularIO(camera, seconds=args.capture_length)
            camera.start_recording(stream, format='h264')
            while True:
                detection, image, inference_data = detect_object(
                    inference, camera, classes, args.threshold, debug_out,
                    (args.cropbox_left, args.cropbox_right),
                    (args.cropbox_top, args.cropbox_bottom))
                if detection:
                    detect_time = int(time.time())
                    camera.wait_recording(args.capture_delay)
                    video_file = 'capture_%d.mpeg' % detect_time
                    image_file = 'capture_%d.jpg' % detect_time
                    stream.copy_to(os.path.join(args.out_dir, video_file))
                    stream.flush()
                    debug_output(image, inference_data, args.out_dir, image_file)
                    print('Wrote video file to', os.path.join(args.out_dir, video_file))
                    camera.wait_recording(max(args.capture_length - args.capture_delay, 0))
Beispiel #35
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))