I am using a python script to access my NoIr Pi camera module via the "picamera" package. Yesterday everything was working fine.

Today everything is still working fine, however whenever I close my python scrpit, the red LED stays on, and it appears that the pi is still trying to use the camera.

When run my script again, it is unable to access the camera due to lack of resources (I.e the previous closed script still has access to the camera)

After a quick reboot I can run my script again, but its getting a little tedious having to reboot every time I make a change when trying to debug my script.

Any idea's?

Update - Code as requested

fwidth  = (351) // 32 * 32
fheight = (255) // 16 * 16

with picamera.PiCamera() as camera:
    with picamera.array.PiYUVArray(camera) as stream:
        stream = open('image.data', 'w+b')
        camera.resolution = (320, 240)

        fcntl.fcntl(comm, fcntl.F_SETFL, os.O_NONBLOCK)

        while True:                
            camera.capture(stream, format='yuv', use_video_port = True)

            image = np.fromfile(stream, dtype=np.uint8, count=fwidth*fheight).reshape((fheight, fwidth))
  • can we see the code?
    – rob
    Feb 11, 2015 at 11:09
  • Sure, hang on i'll post the relevant part here.
    – Aphire
    Feb 11, 2015 at 11:10
  • BTW... 32*32 != 351 and 16*16 != 255
    – Bex
    Feb 11, 2015 at 12:10
  • I know ;) "//" is a floor division symbol (not comments) to make sure only the luminescence value was pulled from the YUV input (wanted it to be as fast as possible)
    – Aphire
    Feb 11, 2015 at 12:24

1 Answer 1


You need to close the stream. The object you named stream is a ByteIO, that in turn is-a IOBase. It should be closed to ensure proper release and clean-up. You need a condition in your loop, and when that turns to false, you need to do a graceful exit involving stream.close()

  • I actually truncate the stream instead at the bottom of my loop as its constantly in use. (I didn't post the whole loop as its massive) The stream should be closing even without that line if I close the terminal that the script is running in. I don't think the error is with the code, as it was working fine yesterday and I have made no changes, I think it might be something to do with power perhaps?
    – Aphire
    Feb 11, 2015 at 12:23
  • 1
    I tried adding the stream.close() as you suggested, it threw an exception when I ran it, so I removed it again. And now strangely my bug has gone! I guessed a force close from the exception must have made a change to something that persists through a reboot. So looks like your suggestion fixed it even if it wasn't in the way you thought :) Cheers.
    – Aphire
    Feb 11, 2015 at 12:33
  • I should point out that if you're using PiYUVArray, you can access stream.array to get a numpy array of the image without messing around with things like np.fromfile and reshaping; the PiYUVArray class does all that for you.
    – Dave Jones
    Feb 12, 2015 at 14:54
  • Cheers Dave, I will take a look at it. Great lib thanks for making!
    – Aphire
    Mar 5, 2015 at 16:26

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.