7.6 KiB
Capture image - Raspberry Pi
For dis part of di lesson, you go add camera sensor to your Raspberry Pi, and read image from am.
Hardware
Raspberry Pi need camera.
Di camera wey you go use na Raspberry Pi Camera Module. Dis camera na for Raspberry Pi and e dey connect through special connector for di Pi.
💁 Dis camera dey use Camera Serial Interface, na protocol wey di Mobile Industry Processor Interface Alliance create, dem dey call am MIPI-CSI. Na special protocol for sending image.
Connect di camera
You fit connect di camera to di Raspberry Pi with ribbon cable.
Task - connect di camera
-
Off di Pi.
-
Connect di ribbon cable wey come with di camera to di camera. To do dis one, gently pull di black plastic clip for di holder so e go comot small, then slide di cable enter di socket, make di blue side face away from di lens, di metal pin strips go face di lens. Once e don enter finish, push di black plastic clip back to di place.
You fit see animation wey dey show how to open di clip and put di cable for di Raspberry Pi Getting Started with the Camera module documentation.
-
Remove di Grove Base Hat from di Pi.
-
Pass di ribbon cable through di camera slot for di Grove Base Hat. Make sure say di blue side of di cable dey face di analog ports wey dem label A0, A1 etc.
-
Put di ribbon cable for di camera port for di Pi. Again, pull di black plastic clip up, put di cable, then push di clip back. Di blue side of di cable suppose face di USB and ethernet ports.
-
Put back di Grove Base Hat.
Program di camera
Raspberry Pi fit dey programmed to use di camera with di PiCamera Python library.
Task - enable legacy camera mode
Unfortunately, when dem release Raspberry Pi OS Bullseye, di camera software wey dey come with di OS change, so by default PiCamera no dey work again. Dem dey work on replacement wey dem dey call PiCamera2, but e never ready to use.
For now, you fit set your Pi to legacy camera mode so PiCamera go fit work. Di camera socket dey disabled by default, but once you turn on di legacy camera software, e go automatically enable di socket.
-
Power di Pi and wait make e boot.
-
Open VS Code, either directly for di Pi, or connect am through di Remote SSH extension.
-
Run dis commands for your terminal:
sudo raspi-config nonint do_legacy 0 sudo rebootDis one go change di setting to enable di legacy camera software, then reboot di Pi so di setting go work.
-
Wait make di Pi reboot, then open VS Code again.
Task - program di camera
Program di device.
-
From di terminal, create new folder for di
piuser home directory wey dem callfruit-quality-detector. Create file for dis folder wey dem callapp.py. -
Open dis folder for VS Code.
-
To use di camera, you fit use di PiCamera Python library. Install di Pip package for dis one with dis command:
pip3 install picamera -
Add dis code to your
app.pyfile:import io import time from picamera import PiCameraDis code dey import some libraries wey you need, including di
PiCameralibrary. -
Add dis code below dis one to initialize di camera:
camera = PiCamera() camera.resolution = (640, 480) camera.rotation = 0 time.sleep(2)Dis code dey create PiCamera object, set di resolution to 640x480. Even though higher resolutions dey supported (up to 3280x2464), di image classifier dey work on smaller images (227x227) so e no necessary to capture and send bigger images.
Di
camera.rotation = 0line dey set di rotation of di image. Di ribbon cable dey enter di bottom of di camera, but if your camera rotate to make e point well to di item wey you wan classify, you fit change dis line to di number of degrees wey e rotate.For example, if you suspend di ribbon cable over something so e dey for di top of di camera, then set di rotation to be 180:
camera.rotation = 180Di camera dey take few seconds to start, na why
time.sleep(2)dey. -
Add dis code below dis one to capture di image as binary data:
image = io.BytesIO() camera.capture(image, 'jpeg') image.seek(0)Dis code dey create
BytesIOobject to store binary data. Di image dey read from di camera as JPEG file and e dey store for dis object. Dis object get position indicator to know where e dey for di data so more data fit dey write to di end if e need am, so diimage.seek(0)line dey move dis position back to di start so all di data fit dey read later. -
Below dis one, add dis code to save di image to file:
with open('image.jpg', 'wb') as image_file: image_file.write(image.read())Dis code dey open file wey dem call
image.jpgfor writing, then e dey read all di data from diBytesIOobject and e dey write am to di file.💁 You fit capture di image directly to file instead of
BytesIOobject by passing di file name to dicamera.capturecall. Di reason wey we dey useBytesIOobject na so later for dis lesson you fit send di image to your image classifier. -
Point di camera to something and run dis code.
-
Di image go dey captured and e go save as
image.jpgfor di current folder. You go see dis file for di VS Code explorer. Select di file to see di image. If e need rotation, update dicamera.rotation = 0line as e need and take another picture.
💁 You fit find dis code for di code-camera/pi folder.
😀 Your camera program don work!
Disclaimer:
Dis dokyument don use AI transleshion service Co-op Translator do di transleshion. Even as we dey try make am accurate, abeg make you sabi say automatik transleshion fit get mistake or no dey correct well. Di original dokyument wey dey for im native language na di one wey you go take as di correct source. For important informashon, e good make you use professional human transleshion. We no go fit take blame for any misunderstanding or wrong meaning wey fit happen because you use dis transleshion.




