0

I've been using the raspi cam for some time now. I was using it inside a machine, to take photos from what's going on there. As far as I can tell, the cables are all attached as they should be - but all of a sudden, the images look like this.

Can anybody help? What's going on here?

enter image description here

In the preview window, the photo looks normal. After a restart, the output is completely black instead.

This is my test code which makes the same output as the production code. First, I measure the lighting and fix the camera setting. Then I take a series of photos while the machine works.

I take the photo in the original cam's format (rgb), crop it with numpy slicing and save it afterwards. That had been the fastest image taking process I was able to come up with.

Around 12 000 photos later, the camera does this. Why? What can I do about it?

from time import sleep
from picamera import PiCamera
import numpy as np
from PIL import Image

CROPBOX = (317, 464, 317+370, 464+251) #x0,y0,x1,y1
RESOLUTION = (1024, 768)

cam = PiCamera(camera_num=0, 
      resolution=RESOLUTION, 
      framerate=40, 
      sensor_mode=0, 
      clock_mode='reset', 
      framerate_range=None)

cam.meter_mode = 'average'
#cam.zoom = 0.3, 0.3, 0.65, 0.65
cam.iso = 50
sleep(0.5)
cam.shutter_speed = cam.exposure_speed
cam.exposure_mode = 'off'
sleep(0.5)

# fix white bracket
gains = cam.awb_gains
cam.awb_mode = 'off'
cam.awb_gains = gains

x0, y0, x1, y1 = CROPBOX
output = np.empty((RESOLUTION[0], RESOLUTION[1], 3), dtype=np.uint8)
cam.capture(output, "rgb")
Image.fromarray(output[y0:y1,x0:x1,:]).save("test_image.png")
cam.close()
3
  • If this suddenly happens after 12000 successful pictures, without any code change, I would assume that something broke. Try getting a new camera.
    – PMF
    Commented Feb 28, 2021 at 7:28
  • Thanks PMF! I am constantly editing my code. This edit, I totally forgot between the last run and this one. It was not yet tested, last time I had no time left. I found the error (see below).
    – Anderas
    Commented Feb 28, 2021 at 16:25
  • That's what git (or any other versioning software) is for, actually ;-)
    – PMF
    Commented Feb 28, 2021 at 17:56

1 Answer 1

0

Found the error.

I define RESOLUTION here and use it correctly

RESOLUTION = (1024, 768)
cam = PiCamera(camera_num=0, 
    resolution=RESOLUTION, 
    framerate=40, 
    sensor_mode=0, 
    clock_mode='reset', 
    framerate_range=None)

But then in the capture area I use it in the same order, and there it is wrong:

output = np.empty((RESOLUTION[0], RESOLUTION[1], 3), dtype=np.uint8)

Because that's the old numpy trap - images have it the other way round than numpy. I should have used this instead:

output = np.empty((RESOLUTION[1], RESOLUTION[0], 3), dtype=np.uint8)

Because, effectively, numpy slicing uses what is y in images as first argument, and what is x in images as second argument. That's the way numpy is organized.

Your Answer

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

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