Ejemplo n.º 1
0
def main():
    """Face detection 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.')
    args = parser.parse_args()

    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()

        # Annotator renders in software so use a smaller size and scale results
        # for increased performace.
        annotator = Annotator(camera, dimensions=(320, 240))
        scale_x = 320 / 1640
        scale_y = 240 / 1232

        # Incoming boxes are of the form (x, y, width, height). Scale and
        # transform to the form (x1, y1, x2, y2).
        def transform(bounding_box):
            x, y, width, height = bounding_box
            return (scale_x * x, scale_y * y, scale_x * (x + width),
                    scale_y * (y + height))

        with CameraInference(face_detection.model()) as inference:
            print("Running face detection...")
            for i, result in enumerate(inference.run()):
                if i == args.num_frames:
                    break
                faces = face_detection.get_faces(result)
                annotator.clear()
                for face in faces:
                    annotator.bounding_box(transform(face.bounding_box),
                                           fill=0)
                    annotator.text((5, 5), "Welcome to the family room!")
                annotator.update()
                #print('Iteration #%d: num_faces=%d' % (i, len(faces)))

        camera.stop_preview()
Ejemplo n.º 2
0
def main():
    """Face detection camera inference example."""
    parser = argparse.ArgumentParser()
    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.')
    args = parser.parse_args()

    # 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.
    with PiCamera(sensor_mode=4, resolution=(1640, 1232), framerate=10) as camera:
        camera.start_preview()

        # Annotator renders in software so use a smaller size and scale results
        # for increased performace.
        annotator = Annotator(camera, dimensions=(320, 240))
        scale_x = 320 / 1640
        scale_y = 240 / 1232

        # Incoming boxes are of the form (x, y, width, height). Scale and
        # transform to the form (x1, y1, x2, y2).
        def transform(bounding_box):
            x, y, width, height = bounding_box
            return (scale_x * x, scale_y * y, scale_x * (x + width),
                    scale_y * (y + height))

        def textXYTransform(bounding_box):
            x, y, width, height = bounding_box
            return (scale_x * x, scale_y * y)

        with CameraInference(object_detection.model()) as inference:
            for result in inference.run():
                objs = object_detection.get_objects(result, 0.3);
                annotator.clear()
                for obj in objs:
                    # blue for person, green for cat, purple for dog, red for anything else
                    outlineColor = "blue" if obj.kind == 1 else "green" if obj.kind == 2 else "purple" if obj.kind == 3 else "red"
                    print(obj.kind)
                    annotator.bounding_box(transform(obj.bounding_box), fill=0 , outline=outlineColor)
                    annotator.text(textXYTransform(obj.bounding_box), "person" if obj.kind == 1 else "cat" if obj.kind == 2 else "dog" if obj.kind == 3 else "other", color=outlineColor)

                annotator.update()

        camera.stop_preview()
Ejemplo n.º 3
0
class OverlayManager(object):
    """Overlay utility for managing state and drawing of overlay."""
    LINE_HEIGHT = 12
    ROW_HEIGHT = 50

    def __init__(self, camera):
        self._clear_needed = False
        self._annotator = Annotator(camera,
                                    default_color=(0xFF, 0xFF, 0xFF, 0xFF),
                                    dimensions=(320, 240))

    def _draw_annotation(self, result, category, index):
        self._annotator.text(
            (5, index * self.ROW_HEIGHT + 5 + 0 * self.LINE_HEIGHT),
            '{:.2%}'.format(result[1]))
        self._annotator.text(
            (5, index * self.ROW_HEIGHT + 5 + 1 * self.LINE_HEIGHT),
            '{:25.25}'.format(result[0]))
        self._annotator.text(
            (5, index * self.ROW_HEIGHT + 5 + 2 * self.LINE_HEIGHT),
            'category: {:20.20}'.format(category))

    def clear(self):
        if self._clear_needed:
            self._annotator.stop()
            self._clear_needed = False

    def update(self, classes, categories):
        self._annotator.clear()
        self._clear_needed = True
        for i, result in enumerate(classes):
            self._draw_annotation(result, categories[i], i)
        self._annotator.update()
def main():
    """object detection camera inference example."""
    parser = argparse.ArgumentParser()
    countdown = 20
    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.')
    args = parser.parse_args()

    servoX = AngularServo(PIN_B)
    servoY = AngularServo(PIN_A)
    relay = DigitalOutputDevice(PIN_C, active_high=True, initial_value=True)
    #relay.blink(n=1)
    relay.blink(on_time=0.05, n=1)

    # 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.
    with PiCamera(sensor_mode=4, resolution=(1640, 1232),
                  framerate=10) as camera:
        camera.start_preview()

        # Annotator renders in software so use a smaller size and scale results
        # for increased performace.
        annotator = Annotator(camera, dimensions=(320, 240))
        scale_x = 320 / 1640
        scale_y = 240 / 1232

        # Incoming boxes are of the form (x, y, width, height). Scale and
        # transform to the form (x1, y1, x2, y2).
        def transform(bounding_box):
            x, y, width, height = bounding_box
            return (scale_x * x, scale_y * y, scale_x * (x + width),
                    scale_y * (y + height))

        def textXYTransform(bounding_box):
            x, y, width, height = bounding_box
            return (scale_x * x, scale_y * y)

        with CameraInference(object_detection.model()) as inference:
            for result in inference.run():
                objs = object_detection.get_objects(result, 0.3)
                annotator.clear()
                for obj in objs:
                    # blue for person, green for cat, purple for dog, red for anything else
                    outlineColor = "blue" if obj.kind == 1 else "green" if obj.kind == 2 else "purple" if obj.kind == 3 else "red"
                    print(obj.kind)
                    tBoundingBox = transform(obj.bounding_box)
                    annotator.bounding_box(tBoundingBox,
                                           fill=0,
                                           outline=outlineColor)
                    annotator.text(
                        textXYTransform(obj.bounding_box),
                        "person" if obj.kind == 1 else "cat" if obj.kind == 2
                        else "dog" if obj.kind == 3 else "other",
                        color=outlineColor)

                    if len(objs) == 1:
                        x1, y1, x2, y2 = transform(obj.bounding_box)
                        midX = ((x2 - x1) / 2) + x1
                        midY = ((y2 - y1) / 2) + y1
                        servoPosX = remap(midX, 0, 320, 75, -75)
                        servoPosY = remap(midY, 0, 240, -90,
                                          80)  # 90 is low, -90 is high
                        servoPosX = min(90, servoPosX)
                        servoPosX = max(-90, servoPosX)
                        servoPosY = min(90, servoPosY)
                        servoPosY = max(-90, servoPosY)
                        print("x", midX, servoPosX)
                        print("y", midY, servoPosY)
                        servoX.angle = servoPosX
                        servoY.angle = servoPosY

                        countdown -= 1

                        if countdown == -1:
                            # squirt
                            annotator.text((midX, midY),
                                           "Squirt!!",
                                           color=outlineColor)
                            relay.blink(on_time=0.5, n=1)
                            countdown = 20
                        else:
                            annotator.text((midX, midY),
                                           str(countdown),
                                           color=outlineColor)
                if len(objs) == 0:
                    countdown = 20
                annotator.update()

        camera.stop_preview()
Ejemplo n.º 5
0
    def run(self, num_frames, preview_alpha, image_format, image_folder):
        logger.info('Starting...')
        leds = Leds()
        player = Player(gpio=22, bpm=10)
        photographer = Photographer(image_format, image_folder)
        animator = Animator(leds, self._done)

        try:
            # 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.
            with PiCamera(sensor_mode=4, resolution=(1640, 1232)) as camera, PrivacyLed(leds):
                def take_photo():
                    logger.info('Button pressed.')
                    player.play(BEEP_SOUND)
                    photographer.shoot(camera)

                def sound_alarm():
                    logger.info('Button pressed.')
                    player.play(SIREN_SOUND)
                    animator.danger()

                scale_x = 320 / 1640
                scale_y = 240 / 1232
                def transform(bounding_box):
                    x, y, width, height = bounding_box
                    return (scale_x * x, scale_y * y, scale_x * (x + width), scale_y * (y + height))

                # Blend the preview layer with the alpha value from the flags.
                #camera.start_preview(alpha=preview_alpha)
                camera.start_preview()

                annotator = Annotator(camera, dimensions=(320, 240))


                button = Button(23)
                button.when_pressed = sound_alarm

                joy_score_moving_average = MovingAverage(10)
                prev_joy_score = 0.0
                with CameraInference(face_detection.model()) as inference:
                    logger.info('Model loaded.')
                    #player.play(MODEL_LOAD_SOUND)
                    for i, result in enumerate(inference.run()):
                        annotator.clear()
                        faces = face_detection.get_faces(result)
                        joy_score = joy_score_moving_average.next(average_joy_score(faces))
                        animator.update_joy_score(joy_score)
                        photographer.update_faces(faces)
                        for face in faces:
                            annotator.bounding_box(transform(face.bounding_box), fill=0)
                            
                        if joy_score >= JOY_SCORE_PEAK:
                            annotator.text((5,5), "I detect a happy human")
                        elif joy_score > 0 and joy_score <= JOY_SCORE_MIN:
                            annotator.text((5,5), "I detect a sad human")
                        elif joy_score > 0:
                            annotator.text((5,5), "I detect a human")
                        #if joy_score > JOY_SCORE_PEAK > prev_joy_score:
                        #    player.play(JOY_SOUND)
                        #elif joy_score < JOY_SCORE_MIN < prev_joy_score:
                        #    player.play(SAD_SOUND)

                        prev_joy_score = joy_score
                        annotator.update()

                        if self._done.is_set() or i == num_frames:
                            break
        finally:
            player.stop()
            photographer.stop()

            player.join()
            photographer.join()
            animator.join()