raspberrypi / picamera2

New libcamera based python library
BSD 2-Clause "Simplified" License
845 stars 180 forks source link

[BUG] Camera does not start (genuine issue, not a setup or hardware issue) #668

Open kiranvholla opened 1 year ago

kiranvholla commented 1 year ago

libcamera works perfectly, however picamera2 throws an error upon calling start()

code: As simple as it can get:

import time from picamera2 import Picamera2 picam2 = Picamera2() time.sleep(2) imgname = 'CapturedImage' + str(time.time()) + '.jpeg' print('About to take pic. Name will be:', img_name) picam2.start() <<<<<point of failure

program logs:

[0:33:32.221313336] [3200] INFO Camera camera.cpp:1028 configuring streams: (0) 640x480-XBGR8888 [0:33:32.221646579] [3201] INFO RPI raspberrypi.cpp:851 Sensor: /base/soc/i2c0mux/i2c@1/ov5647@36 - Selected sensor format: 640x480-SGBRG10_1X10 - Selected unicam format: 640x480-pGAA [0:34:47.007090670] [3201] ERROR V4L2 v4l2_videodevice.cpp:1906 /dev/video0[15:cap]: Failed to start streaming: Connection timed out Camera did not start properly. Traceback (most recent call last): File "/home/kiran/1testcamera.py", line 22, in picam2.start() File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 1031, in start self.start() File "/usr/lib/python3/dist-packages/picamera2/picamera2.py", line 1003, in start_ raise RuntimeError("Camera did not start properly.") RuntimeError: Camera did not start properly.

dmesg logs:

[Sun Apr 16 12:30:53 2023] i2c-bcm2835 fe205000.i2c: i2c transfer timed out [Sun Apr 16 12:30:54 2023] i2c-bcm2835 fe205000.i2c: i2c transfer timed out ...few more of the above... [Sun Apr 16 12:30:56 2023] IPv6: MLD: clamping QRV from 1 to 2! [Sun Apr 16 12:30:56 2023] i2c-bcm2835 fe205000.i2c: i2c transfer timed out [Sun Apr 16 12:30:56 2023] ov5647 10-0036: Failed to program sensor mode: -110 [Sun Apr 16 12:30:56 2023] ov5647 10-0036: stream start failed: -110 [Sun Apr 16 12:30:56 2023] unicam fe801000.csi: stream on failed in subdev [Sun Apr 16 12:30:57 2023] i2c-bcm2835 fe205000.i2c: i2c transfer timed out

Additional information:

Versions:

uname -a reports: Linux raspberrypi 5.15.84-v8+ #1613 SMP PREEMPT Thu Jan 5 12:03:08 GMT 2023 aarch64 GNU/Linux

libcamera-hello --version reports: libcamera build: v0.0.4+22-923f5d70

Absolutely no changes to /boot/config.txt, or custom installations etc. Basic OS installation followed by sudo apt update and sudo apt upgrade with reboot

cat /etc/os-release gives output as: PRETTY_NAME="Debian GNU/Linux 11 (bullseye)" NAME="Debian GNU/Linux" VERSION_ID="11" VERSION="11 (bullseye)" VERSION_CODENAME=bullseye ID=debian

sudo apt install -y python3-picamera2 gives me: python3-picamera2 is already the newest version (0.3.9-1).

kiranvholla commented 1 year ago

ok, I think the problem was the preview Window. In headless mode, there is none. This is causing some issue. I was able to get the code working by simply doing:

config = picam2.create_still_configuration() picam2.configure(config)

which as per the documentation - generates a configuration suitable for capturing a high-resolution still image. But what it probably ALSO does is to tell the interpreter that there is no need for a preview screen and hence subsequently error is not thrown when .start() is called.

Headless mode is probably a important aspect but is not dealt with in the official documentation. I am closing the bug, but will refrain from deleting this issue (at the risk of looking foolish) as I believe there could be others who could run along this path given the lack of adequate documentation for the headless mode in the user-guide.

PS: Another workaround was to simply call the shell commands from python directly: import os os.system('libcamera-jpeg -o testyay.jpg') which one can try this approach in case of any other error.

kiranvholla commented 1 year ago

On second thoughts, I will leave it as open for 2 reasons. 1, Perhaps this can be treated as a documentation bug and the userguide PDF can be updated for headless configurations

  1. I also noticed additional odd behaviour with headless. For e.g. with the above configuration:

picam2.start_and_capture_file(img_name) throws same "camera does not start" error>>>>Fails

One needs to do a camera start with the right config as indicated above followed by a: picam2.capture_file(img_name). >>>>This works without error.

Basically start_and_capture_file fails in headless even with the below code: config = picam2.create_still_configuration() picam2.configure(config)

davidplowman commented 1 year ago

Hi, thank you for the report.

One thing to note is that picam2.start() doesn't create a preview window, and will run fine in a headless environment. So I'm inclined to think the problem is actually something else. It certainly seems like you are suffering from some strange intermittent behaviour, whereas my own ov5647 runs without any trouble at all. I'm afraid that doesn't help you very much, but it does seem to me that the problem lies somewhere else. Do you have other camera modules or cables to try?