[BUG] Capture file in loop Picamera2

Open
#711 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Needs clarification
Activity status
Stale
Tech stack
python
Domain
embedded-iot

Research direction

Reproduce the hang on the reported RPi 3B setup, starting with the loop, take_rpi_cam_image, and thread_main entry points in image.py. Trace the 30-second timeout around picam2.capture_file and the stop behavior, then verify that repeated captures return and the camera thread exits cleanly.

Written by the indexing model from the issue text.

Description

When calling capture_file in a long loop sometimes thread gets stuck on call.

`
class Camera:
_running = False
last_image_time = None
def init(self):
self.path = img_dir
self.missing_images_counter = 0
self.image_cadence = 10
self.image_resolution = {"size": (1000, 754)}
self.stop_flag = threading.Event()

def setup(self, picam2):
    logging.info("Starting camera setup")
    try:
        picam2.set_logging(Picamera2.WARNING)
        camera_config = picam2.create_still_configuration(main=self.image_resolution)
        picam2.configure(camera_config)
        picam2.start()
        time.sleep(1)
        Camera._running = True
    except Exception as e:
        logging.error(f"Camera setup was unsuccessful: {e}")
        Camera._running = False
 def loop(self, picam2):

    image_time = int(time.time())
    self.take_rpi_cam_image(image_time, picam2)
    time_since_last_img = time.time() - image_time
    sleep_interval = self.image_cadence - time_since_last_img
    if self.image_cadence - sleep_interval > 0:
        logging.info(f"sleep interval before next image {sleep_interval}")
        time.sleep(sleep_interval)        
 
 def take_rpi_cam_image(self, image_time, picam2):
    timeout_timer = threading.Timer(30, self.handle_timeout)
    timeout_timer.start()
    try:
        picam2.capture_file(image_location)
        timeout_timer.cancel()
        logging.info(f"image {image_location} taken successfully ")
        Camera.last_image_time = image_time
    except Exception:
        logging.exception(f"failed to take image")
        
def thread_main():
    logging.info("Camera thread starting...")
    try:
        with Picamera2() as picam2:
            camera = Camera()

            camera.setup(picam2)
            while True:

                if camera.stop_flag.is_set():
                    picam2.stop()
                    break
                if camera._running:
                    logging.info("before image loop")
                    camera.loop(picam2)
                    logging.info("after image loop")

                else:
                    time.sleep(10)
    finally:
        picam2.stop()`

This loops takes pictures in exactly a 10 second interval and writes to a dir. i know it is getting stuck on the capture_file call because i am getting a time out error

  | 2023-06-08T19:29:03.092+03:00 | 2023-06-08:16:29:03,001 INFO [image.py:96] after image loop   | 2023-06-08T19:29:03.342+03:00 | 2023-06-08:16:29:03,002 INFO [image.py:94] before image loop |2023-06-08T19:29:03.342+03:00 | 2023-06-08:16:29:03,093 INFO [image.py:60] sleep interval before next image 9.906635522842407 |2023-06-08T19:29:13.117+03:00 | 2023-06-08:16:29:13,009 INFO [image.py:96] after image loop |2023-06-08T19:29:13.618+03:00 | 2023-06-08:16:29:13,010 INFO [image.py:94] before image loop   | 2023-06-08T19:29:43.201+03:00 | 2023-06-08:16:29:43,011 WARNING [image.py:65] Camera has been stuck taking an image for more than 30 seconds restarting the thread

Hardware :
Rpi 3b

Additional context
usually the loop has a 7-8 second sleep wierd that it took less than a second to take the picture the loop before it got stuck.
also the stop function was not enough to break the with block so the thread never died.

Dominant language
Python
Stars
1.2k
Forks
258
PR merge metrics
No merged PRs in 30d

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from raspberrypi/picamera2

All issues in raspberrypi/picamera2

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.