Ejemplo n.º 1
0
def main():
    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()

    with PiCamera(sensor_mode=4, resolution=(1640, 1232),
                  framerate=30) 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))

        with CameraInference(object_detection.model()) as inference:
            for result in inference.run(args.num_frames):
                objects = object_detection.get_objects(result)
                annotator.clear()
                for obj in objects:
                    rect = transform(obj.bounding_box)
                    annotator.bounding_box(rect, fill=0)
                    loc = (rect[0] + 4, rect[1])
                    annotator.text(loc, objectLabel(obj.kind))
                annotator.update()
                #print('#%05d (%5.2f fps): num_objects=%d, objects=%s' %
                #       (inference.count, inference.rate, len(objects), objects))
                if len(objects) > 0:
                    print(
                        f"num_objects={len(objects)}, objects={[objectLabel(obj.kind) for obj in objects]}"
                    )

        camera.stop_preview()
Ejemplo n.º 2
0
class OverlayManager:
    """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()
Ejemplo n.º 3
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument(
        '--num_frames',
        '-f',
        type=int,
        dest='num_frames',
        default=-1,
        help='Sets the number of frames to run for, otherwise runs forever.')

    parser.add_argument(
        '--num_pics',
        '-p',
        type=int,
        dest='num_pics',
        default=-1,
        help='Sets the max number of pictures to take, otherwise runs forever.'
    )

    args = parser.parse_args()

    with PiCamera() as camera, PrivacyLed(Leds()):
        # See the Raspicam documentation for mode and framerate limits:
        # https://picamera.readthedocs.io/en/release-1.13/fov.html#sensor-modes
        # Set to the highest resolution possible at 16:9 aspect ratio
        camera.sensor_mode = 4
        camera.resolution = (1640, 1232)
        camera.start_preview(fullscreen=True)

        with CameraInference(pikachu_object_detection.model()) as inference:
            print("Camera inference started")
            player.play(*MODEL_LOAD_SOUND)

            last_time = time()
            pics = 0
            save_pic = False
            enable_label = True

            # 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 leftCorner(bounding_box):
                x, y, width, height = bounding_box
                return (scale_x * x, scale_y * y)

            def truncateFloat(value):
                return '%.3f' % (value)

            for f, result in enumerate(inference.run()):
                print("sono dentro al ciclo..")
                print(os.getcwd() + '/pikachu_detector.binaryproto')
                annotator.clear()
                detections = enumerate(
                    pikachu_object_detection.get_objects(result, 0.3))
                for i, obj in detections:
                    print("sono dentro al secondo ciclo..")
                    print('%s', obj.label)
                    annotator.bounding_box(transform(obj.bounding_box), fill=0)
                    if enable_label:
                        annotator.text(
                            leftCorner(obj.bounding_box),
                            obj.label + " - " + str(truncateFloat(obj.score)))
                    print('%s Object #%d: %s' %
                          (strftime("%Y-%m-%d-%H:%M:%S"), i, str(obj)))
                    x, y, width, height = obj.bounding_box
                    if obj.label == 'PIKACHU':
                        save_pic = True
                        #player.play(*BEEP_SOUND)

                # save the image if there was 1 or more cats detected

                if save_pic:
                    # save the clean image
                    #camera.capture("images/image_%s.jpg" % strftime("%Y%m%d-%H%M%S"))
                    pics += 1
                    save_pic = False

                #if f == args.num_frames or pics == args.num_pics:
                #    break

                now = time()
                duration = (now - last_time)
                annotator.update()

                # The Movidius chip runs at 35 ms per image.
                # Then there is some additional overhead for the object detector to
                # interpret the result and to save the image. If total process time is
                # running slower than 50 ms it could be a sign the CPU is geting overrun
                #if duration > 0.50:
                #    print("Total process time: %s seconds. Bonnet inference time: %s ms " %
                #          (duration, result.duration_ms))

                last_time = now

        camera.stop_preview()
Ejemplo n.º 4
0
        def objdet():
            with CameraInference(ObjectDetection.model()) as inference:
                print("Camera inference started")
                player.play(*MODEL_LOAD_SOUND)

                last_time = time()
                pics = 0
                save_pic = False

                enable_label = True
                # 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 leftCorner(bounding_box):
                    x, y, width, height = bounding_box
                    return (scale_x * x, scale_y * y)

                def truncateFloat(value):
                    return '%.3f' % (value)

                for f, result in enumerate(inference.run()):

                    annotator.clear()
                    detections = enumerate(
                        ObjectDetection.get_objects(result, 0.3))

                    for i, obj in detections:
                        print('%s', obj.label)
                        annotator.bounding_box(transform(obj.bounding_box),
                                               fill=0)
                        if enable_label:
                            annotator.text(
                                leftCorner(obj.bounding_box), obj.label +
                                " - " + str(truncateFloat(obj.score)))

                        print('%s Object #%d: %s' %
                              (strftime("%Y-%m-%d-%H:%M:%S"), i, str(obj)))
                        x, y, width, height = obj.bounding_box

                        if obj.label == 'chair':
                            #dt = datetime.datetime.now()
                            #os.system("ffplay -nodisp -autoexit  /home/pi/AIY-projects-python/src/LorecObjectSoundFiles/insan.mp3")
                            #query = ("INSERT INTO Log (Time, Location, GlassNameDbid, ModulDbid, Screenshot, Tag, Distance) VALUES ('"+ dt+"', 'Ankara', '1', '2', 'No  Screenshot', 'Insan', '150')")
                            #save_pic = True
                            player.play(*BEEP_SOUND)
                        #elif obj.label == 'tvmonitor':
                        #os.system("ffplay -nodisp -autoexit  /home/pi/AIY-projects-python/src/LorecObjectSoundFiles/Ekran.mp3")

                    # save the image
                    if save_pic:
                        # save the clean image
                        camera.capture("images/image_%s.jpg" %
                                       strftime("%Y%m%d-%H%M%S"))
                        pics += 1
                        save_pic = False

                    if f == args.num_frames or pics == args.num_pics:
                        break

                    annotator.update()
                    now = time()
                    duration = (now - last_time)

                    # The Movidius chip runs at 35 ms per image.
                    # Then there is some additional overhead for the object detector to
                    # interpret the result and to save the image. If total process time is
                    # running slower than 50 ms it could be a sign the CPU is geting overrun
                    if duration > 0.50:
                        print(
                            "Total process time: %s seconds. Bonnet inference time: %s ms "
                            % (duration, result.duration_ms))

                    last_time = now