Picamera2 failed to turn on while using python multiprocessing
See original GitHub issuePlease 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:
- Created a year ago
- Comments:19 (1 by maintainers)
Top GitHub Comments
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.
@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.