4

I am trying to use a Pi Camera to take various pictures, but only if the overall picture is not pitch black or really dark (as I will mount it in my office and do not want it going while I'm away). I've got a Raspberry Pi Zero with no internet and do not have access to the graphical interface while it is mounted. The camera runs on a for loop within a sentinel while loop (to keep tabs on a counter variable) that takes a picture at constant intervals and names the images based on the actual loop iteration to create a time lapse-like stream of photos.

How can I access the raw camera data, preferably in a 1D array, and use it to determine if a picture should be taken before it actually is. I don't want the picture to be taken then compared for storage reasons. I'm using the standard from picamera import PiCamera to use the package for the camera.

I can use this algorithm (https://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color) to determine particular pixel's grey colour value and compare it to a threshold to determine if it is considered "dark". I will probably find the average colour of the entire picture and then compare that to a threshold.

I am looking for a simple implementation in Python that will help solve this problem. If you've got anything please let me know. I am sort of new to Python but I have 3 years experience with vanilla Java self-taught.

Thanks.

2

It's a while since I wrote any code for the picam, but here's what I'd do:

  • Set a low resolution, I think you can also set black and white (using color_effects, it may not be worth it). You may need to fix an exposure.
  • Capture an image to a RAM disk as suggested in another answer, using a fixed filename (i.e. don't delete, overwrite next time).
  • Open that image (using an appropriate library) then use python's sum function to get a light level across the whole image. If you use the rgb output format this should be really simple, not needing a library.
  • If the light level is above a threshold, change the resolution (and select colour), take your real image and save in you real location.
  • Wait an appropriate amount of time.

The camera shouldn't get hot especially if it's not doing video, but if you write:

while True
    with PiCamera() as camera:
        camera.capture()
    sleep(30)

the camera will be shut down in between images (which in this example are taken every 30s).

1

I'm afraid I couldn't find any way to access the data before it is being saved - at least with standard drivers.

Method 1

However, I assume your 'storage reasons' are wearing down the SD card? If so, create a Ram disk on your Pi (alter the size to fit your desired image resolution/size), let the python script capture them to that place and analyse right there.

If it is bright enough, move to SD card with proper naming, if not just overwrite with the next capture (nothing is written to persistent storage).

Method 2

Another approach would be to use an additional device to sense light levels. The easiest of which would be an Light-Dependent Resistor.

To use these, you either need an external ADC or a simple RC Charging Circuit because the Pi by itself cannot convert analogue values. The latter is explained here in great detail with polished python code as well.

This method requires more tinkering and hardware but is a great task to start your 'Internet of Things'-Journey and learn some stuff along the lines.

  • Thanks for your quick response! The reason why I prefer to process the photo before saving it is because the camera setup I have already gets quite warm and has already melted part of a custom case I made. I figured that it will be more intensive to take a photo, process it, then delete it, and I do not want to damage the case any more. – red_kb Apr 17 '17 at 22:56
  • ... Your ldr was very creative and I hadn't thought about a physical approach like that. Since the camera and pi are mounted in a case and pointed in a specific direction, I can't use the sensor the way I have it currently set up. I don't want the camera and light sensor be picking up different values, as that could be problematic down the road. Is there any way to do it as you mentioned by saving the photo then comparing it? – red_kb Apr 17 '17 at 22:58
  • You could always shield off the sensor with some tape or any other light blocking material, to make it 'face' the same direction as your cam. I don't really understand your last question? – mystery Apr 18 '17 at 0:05
  • Sorry. I initially asked if the photo could be processed without saving it, but it seems that cannot be the case. However, is there any way to access the raw colour data after saving it? Perhaps by saving it as a different format with capture()? I would have to take it, process it, then delete it if necessary, which is quite intensive. That's why I opted for the other way. Thanks! – red_kb Apr 20 '17 at 2:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.