2

My goal is to set up a webcam with Raspberry Pi to be active during a certain time (let's say 2 minutes). During this time I should be able to capture a still image at any time and save that image to a folder. I chose to work with pygame. My project is about capturing an image as soon as a sensor has been triggered, so it needs to be very responsive.

According to the documentation for Pygame camera here it says:

open()

Opens the camera device, attempts to initialize it, and begins recording images to a buffer. The camera must be started before any of the below functions can be used.

get_image()

Pulls an image off of the buffer as an RGB Surface. It can optionally reuse an existing Surface to save time. The bit-depth of the surface is either 24 bits or the same as the optionally supplied Surface.

So for my case, the get_image() simply seems to pull out the first image captured after start() has been called. My question is, how can I reach the buffer with all captured images or how does the capturing actually work? I can't find a solution for capturing and saving a still image (at any time) after in between I call start() and stop() on the pygame camera. Since the start() function initiates during a few seconds, it is simply too slow to just call start(), get_image() and stop() after one another. Any help or suggestions would be appreciated.

See my python code below:

def activateCapturing:
    pygame.init()
    pygame.camera.init()
    cam = pygame.camera.Camera("/dev/video0",(320,180))
    cam.start()

    pngdata = None
    imageAsbytes = []
    activated = True

    while activated:
        if readSensor():
            img = cam.get_image()
            pygame.image.save(img, "/tmp/image.png")
            activated = False

     with open("/tmp/image.png", 'rb') as f:
            pngdata = f.read()
            imageAsbytes = bytearray(pngdata)

    cam.stop()
    return imageAsbytes

Thanks in advance!

Fatmajk
  • 1,770
  • 1
  • 13
  • 22

1 Answers1

1

You simpy do not stop the camera after capturing one image.

See https://www.pygame.org/docs/tut/CameraIntro.html.

get_image() gets the image the camera currently sees from the buffer - the buffer NOT being "all pictures since start()" but simply the currently viewed image.

You use stop() after your 3s of "capturing window" to stop aquiering more images.

If you are after performance, you might want to scroll down that page and review the section about Capturing a Live Stream - if you did the same (w/o displaying the stream) and just saved 1 image to disk when needed you should get a decent framerate.

Api: get_image()

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • But this is not the case when I try it? I mean I call start() and after get_image() but it seems it is always returning the first image after start() is called. I don't get why? For example if I start() the camera, and I 10 seconds later try to capture an image, I still get an image that was taken 10 seconds ago. This is not what I am trying to achieve. – Fatmajk May 27 '18 at 20:37