6.0 KiB
Capture image - Virtual IoT Hardware
For dis part of di lesson, you go add camera sensor to your virtual IoT device, and you go dey read images from am.
Hardware
Di virtual IoT device go use one simulated camera wey dey send images either from files or from your webcam.
Add di camera to CounterFit
To use virtual camera, you need to add am to di CounterFit app.
Task - Add di camera to CounterFit
Add di Camera to di CounterFit app.
-
Create new Python app for your computer inside one folder wey dem call
fruit-quality-detectorwey get one file wey dem callapp.pyand one Python virtual environment, then add di CounterFit pip packages.⚠️ You fit check di instructions for how to create and setup CounterFit Python project for lesson 1 if you need am.
-
Install one extra Pip package to install CounterFit shim wey fit talk to Camera sensors by simulating some of di Picamera Pip package. Make sure say you dey install am from terminal wey get di virtual environment activated.
pip install counterfit-shims-picamera -
Make sure say di CounterFit web app dey run.
-
Create camera:
-
For di Create sensor box for di Sensors pane, drop down di Sensor type box and select Camera.
-
Set di Name to
Picamera. -
Select di Add button to create di camera.
Di camera go dey created and e go show for di sensors list.
-
Program di camera
Di virtual IoT device fit now dey programmed to use di virtual camera.
Task - Program di camera
Program di device.
-
Make sure say di
fruit-quality-detectorapp dey open for VS Code. -
Open di
app.pyfile. -
Add di code wey dey below to di top of
app.pyto connect di app to CounterFit:from counterfit_connection import CounterFitConnection CounterFitConnection.init('127.0.0.1', 5000) -
Add di code wey dey below to your
app.pyfile:import io from counterfit_shims_picamera import PiCameraDis code dey import some libraries wey you need, including di
PiCameraclass from di counterfit_shims_picamera library. -
Add di code wey dey below dis one to initialize di camera:
camera = PiCamera() camera.resolution = (640, 480) camera.rotation = 0Dis code dey create one PiCamera object, set di resolution to 640x480. Even though higher resolutions dey supported, 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 in degrees. If you need to rotate di image from di webcam or di file, set am as e dey appropriate. For example, if you wan change di image of banana for webcam wey dey landscape mode to portrait, setcamera.rotation = 90. -
Add di code wey dey below dis one to capture di image as binary data:
image = io.BytesIO() camera.capture(image, 'jpeg') image.seek(0)Dis code dey create one
BytesIOobject to store binary data. Di image dey read from di camera as JPEG file and e dey stored for dis object. Dis object get one position indicator to know where e dey for di data so dat more data fit dey written to di end if e dey needed, so diimage.seek(0)line dey move dis position go back to di start so dat all di data fit dey read later. -
Below dis one, add di code wey dey below to save di image to file:
with open('image.jpg', 'wb') as image_file: image_file.write(image.read())Dis code dey open one 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 why we dey use diBytesIOobject na so dat later for dis lesson you fit send di image to your image classifier. -
Configure di image wey di camera for CounterFit go capture. You fit either set di Source to File, then upload one image file, or set di Source to WebCam, and images go dey captured from your webcam. Make sure say you select di Set button after you select picture or select your webcam.
-
One image go dey captured and e go dey saved as
image.jpgfor di current folder. You go see dis file for di VS Code explorer. Select di file to view di image. If e need rotation, update dicamera.rotation = 0line as e dey necessary and take another picture.
💁 You fit find dis code for di code-camera/virtual-iot-device folder.
😀 Your camera program don work well!
Disclaimer:
Dis dokyument don use AI translation service Co-op Translator do di translation. Even as we dey try make am accurate, abeg sabi say machine translation fit get mistake or no dey correct well. Di original dokyument for im native language na di main source wey you go fit trust. For important information, e good make professional human translator check am. We no go fit take blame for any misunderstanding or wrong interpretation wey fit happen because you use dis translation.


