question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Picamera2 failed to turn on while using python multiprocessing

See original GitHub issue

Please only include one item/question/problem per issue! This is the follow up for my topic on Raspberry pi forum " Picamera2 initialized successfully but failed to configure".

This morning, my picamera2 was able to initialize successfully and configure successfully but failed to turn on (could be problems with the rest of my code). The program did not throw any error. It just stopped. However, the second time I tried to run it. It give me the same error I experienced as mentioned in my pi forum topic, which are " failure to acquire camera" and " __init__sequence did not complete"

Two errors generated in the console, attached here. error.txt

Using Raspberry pi 4 8GB Arducam 5MP Camera 1080P HD OV5647 Camera Module V1 PRETTY_NAME=“Raspbian GNU/Linux 11 (bullseye)” NAME=“Raspbian GNU/Linux” VERSION_ID=“11” VERSION=“11 (bullseye)” VERSION_CODENAME=bullseye ID=raspbian ID_LIKE=debian HOME_URL=“http://www.raspbian.org/” SUPPORT_URL=“http://www.raspbian.org/RaspbianForums” BUG_REPORT_URL=“http://www.raspbian.org/RaspbianBugs

pi is connected to a screen. But I am operating it headless via VNC viewer.

I think it could be the multiprocessing module because I changed my camera.py, see below.

Current camera.py `import multiprocessing as mp from multiprocessing import JoinableQueue, Event from queue import Empty from time import sleep from picamera2 import Picamera2

import logging

logger = logging.getLogger(name)

class Camera(object): “”“Control the camera using the Picamera module (threaded)”“”

def __init__(self):
    
    self.file_queue = JoinableQueue(1)

    self.quit_event = Event()
    

def _worker(self):

    logger.info("Initialising camera...")
    
    camera = Picamera2()
    camera_config = camera.create_still_configuration(main={"size": (240, 240)})
    camera.configure(camera_config)
    
    while True:
        try:  # Timeout raises queue.Empty
            file_path = self.file_queue.get(block=True, timeout=0.1)

            logger.debug("Capturing image")
            camera.capture_file(file_path)

            self.file_queue.task_done()

        except Empty:
            if self.quit_event.is_set():
                logger.debug("Quitting camera thread...")
                break

def start(self, file_path):
    logger.debug("Calling start")
    self.file_queue.put(file_path, block=True)

def join(self):
    logger.debug("Calling join")
    self.file_queue.join()

def launch(self):
    logger.debug("Initialising worker")
    self.p = mp.Process(target=self._worker, daemon=True)
    self.p.start()

def quit(self):
    self.quit_event.set()
    self.p.join()

Old camera.py from time import sleep from picamera2 import Picamera2

class Camera(object):

def __init__(self):

    camera = Picamera2()
    camera_config = camera.create_still_configuration(main={"size": (240, 240)})
    camera.configure(camera_config)
  
    self.camera=camera
    self.camera.start()
    sleep(2)

def capture(self, file_path):

    
    self.camera.capture_file(file_path)

    return file_path

`

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:19 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
davidplowmancommented, Sep 29, 2022

The camera will certainly get released when you reboot your Pi. In fact, the camera will also be released when the process that has been using the camera terminates. I guess it might possible to end up with “zombie” processes that have got stuck holding the camera, in which case you would either have to kill that process or reboot if that doesn’t work.

0reactions
davidplowmancommented, Oct 21, 2022

@kodonnell Thanks for the confirmation!

I think I’m going to close this issue now as the thread has got fairly long I’m not quite sure what else there is to look. But if folks do have issues in this area then please continue to file new issues for them. As always, when there’s a simple script or instructions to reproduce a problem then that really helps us to resolve problems. Thanks to everyone for contributing on this one.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Picamera2 initialized successfully but failed to configure
It's giving me a runtime error at the camera configuration. But it was initialized and opened. Then at requesting configuration, it failed. Code ......
Read more >
picamera2 how to capture multiple images (3) with different ...
Now I want to migrate my picamera(1) project to picamera2. But I cant find a way to capture 3 images in a row,...
Read more >
Increasing Raspberry Pi FPS with Python and OpenCV
Learn the one little track that can dramatically increase the FPS processing rate of your Raspberry Pi and Pi camera module using Python...
Read more >
5. Frequently Asked Questions (FAQ) - Picamera
Attempting to use the camera from multiple processes simultaneously will fail in a variety of ways (from simple errors to the process locking...
Read more >
Picamera 1.13 Documentation - Read the Docs
by starting Python and trying to import picamera: ... Warning: Make sure your Pi is off while installing the camera module.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found