I'm trying to detect the darkness using picamera.

I guess I have to capture an image and elaborate it pixel by pixel getting the brightness or color value and if it is beyond a threshold that I consider "darkness" increase a counter, and finally see if at least e.g. 90% of pixels are "dark".

I found this https://picamera.readthedocs.io/en/release-1.13/api_array.html#piyuvarray but I don't understand how to read the array.

how can I do that?

  • 1
    You may wish to have a look at this thread, though specifically this answer. My use case was to auto-remove night time shots from my timelapses and found the filesize of the compressed shots was a suitable way to sense this.
    – v25
    Commented Jun 25, 2019 at 22:03
  • My use case is similar, is an onboard timelapse camera and I'd use the darkness detection to trigger a clean shutdown of the Pi when I cover the sensor with a hand. the problem is that in tunnels I could get very low light images and undesidered shutdowns.
    – Miky
    Commented Jun 25, 2019 at 22:26
  • If you're trying to find a viable threshold between "covered with hand" and "in a completely dark place", you're going to be out of luck. You could try something time based, i.e., distinguish going into a tunnel from covering with hand based on how the light diminishes, or instead of just being covered with hand, make it cover and release on the equivalent of three taps in five seconds (cover, uncover, cover, uncover). But in the end you will probably be happier with a legit switch.
    – goldilocks
    Commented Jun 25, 2019 at 22:36
  • My hope is that I can distinguish partial darkness (tunnels) and total darkness (hand)
    – Miky
    Commented Jun 25, 2019 at 22:41
  • Presuming this involves a moving vehicle, another possibility involving time would be how much the image changes from frame to frame. Covered with a hand, it should be much less than moving through a dark tunnel. You may want to edit some more of the use case context into the question.
    – goldilocks
    Commented Jun 25, 2019 at 22:46


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.