I want to store the input given by my pi camera directly into a variable, rather than storing it in a file. I want to do this so that it takes less processing power of the pi, as I am working on a autonomus car project and it takes alot of processing. When I try to store the image to a variable it gives me the following error -
AttributeError: 'int' object has no attribute 'name'
During handling of the above exception, another exception occurred:
'Format must be specified when output has no filename')
picamera.exc.PiCameraValueError: Format must be specified when output has no filename
My code -
img = 1
camera = picamera.PiCamera()
camera.capture(img)
time.sleep(0.0001)
img = cv2.imread(img)
cv2.imshow('img', img)
cv2.waitKey(1)
I have made img as a variable to store the captured image, but it is not working. If there is any library that can do this for me please do let me know.
Thanks in advance for your kind response.
Came across the same issue but the answers proposed around the web were not clear enough for my issue. Also, this is the first result that came up during my research, and yet there are no answers to it. Hopefully my summary helps answer this problem properly. Here is how I figured it out:
camera.capture(output, 'jpeg', use_video_port=True)
this statement takes an output, an optional output type (must be specified if the format cannot be figured out from the output name), and whether or not you will use the video port for the output.
output must be a bytes-like object. So this means that we should feed it a BytesIO object. By doing this, we get a stream of the desired output.
However, if you try to process this stream immediately, you will notice that there seems to be no data. This is because the internal pointer is pointing at the end of the stream. We must first move it to the start with myStream.seek(0).
After doing this, we have a file on which we can do proper work. You can send it to a server, use PIL features on it or whatever you want.
Summary of the above:
my_stream = BytesIO() #create a bytes object so the capture can output to it
camera.capture(my_stream, 'jpeg', use_video_port=True) #output the camera feed to my_stream
my_stream.seek(0) #go to the start of the stream in order to do work on it
for example, in my case, I could just send this to my websocket server directly with await websocket.send(my_stream)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With