camera.get_image() sometimes returns an image from the past

Issue #325 new
Tom Dobbelaere
created an issue

Also it's pygame 1.9.3 which isn't even listed, not 1.9.2.

Running pygame 1.9.3 on a Raspberry PI 3 with a Logitech C270.

I wanted to set up a camera that takes a picture every hour. To my surprise in the morning, it took pictures hours apart but with the light still on while I was asleep. When I opened the curtains at 12.30, they were still closed in the picture of 12.45.

The images have different noise and filesizes so they are unique. Eventually I found that get_image() wasn't always returning the last up-to-date image of the webcam.

When I tested by calling the get_image() and upload methods myself, I could reproduce it for a while, until it suddenly didn't do it anymore. Strange behavior.

Enclosed below is a picture of proof http://imgur.com/gallery/TBSfu

And the code that is running in question, maybe I'm the one doing something stupid:

import threading
import time
import requests
import pygame
import pygame.camera

pygame.camera.init()
camlist = pygame.camera.list_cameras()
cam = pygame.camera.Camera(camlist[0],(640,480))
cam.start()

def capAndSend():
        img = cam.get_image()
        timestr = time.strftime('%d_%m_%Y_%H_%M_%S')
        pygame.image.save(img, 'temp.jpg')

        r = requests.post('<private url that processes a fileupload>', files={timestr: open('temp.jpg', 'rb')})

        print r
        print r.text
        threading.Timer(60.0 * 60.0, capAndSend).start()

capAndSend()

Comments (1)

  1. René Dudfield

    I don't have that particular web cam to test with. (not sure I have any usb ones anymore). Luckily I have some raspberrypi 3, and cam modules version 2 to test with.

    How about you try removing the threading...

    import time
    import requests
    import pygame
    import pygame.camera
    
    pygame.camera.init()
    camlist = pygame.camera.list_cameras()
    cam = pygame.camera.Camera(camlist[0],(640,480))
    cam.start()
    
    def capAndSend():
            img = cam.get_image()
            timestr = time.strftime('%d_%m_%Y_%H_%M_%S')
            pygame.image.save(img, 'temp.jpg')
    
            r = requests.post('<private url that processes a fileupload>', files={timestr: open('temp.jpg', 'rb')})
    
            print r
            print r.text
    
    while 1:
        capAndSend()
        time.sleep(60*60)
    
  2. Log in to comment