Elf detector A Raspberry Pi powered robot that can find elves! 9 December 2023 11 minute read By Kevin McAleer Share this article on Table of Contents The process of training a modelSetting up ViamThe robotCreating a robot in ViamInstalling Viam on the Raspberry PiAdd a Pi board to the robotConfiguring the robotAdd Data-Management ServiceAdd Data Capture ConfigurationCapturing imagesPrepare the images for trainingTrain the model from the datasetDeploying the modelAdd Vision ServiceEvaluating the modelFine tuning the modelAdding extra objects to the model Tags robots ai machine learning opencv viam
Viam has a really easy to use machine learning training system that can be used to train a model to recognise objects. I thought it would be fun to train a model to recognise Elves, and then use it on a robot that could find them. The process of training a model The process of training a model to recognise objects contains following steps: Data Collection - Capture images of the object you want to train the model to recognise Labeling - Data Preparation Prepare the images for training Model Selection - Choose the type of model you want to train Train Model - Train the model using the images you captured Model Evaluation - Test the model to see how well it performs Fine Tuning - Improve the model by adding more images Model Deployment - Deploy the model to a robot Setting up Viam First we need to setup Viam on our Raspberry Pi. Viam is a cloud based software platform that allows you to easily create and manage robots. It also has a really easy to use machine learning training system that can be used to train a model to recognise objects. The robot I’m using an off the shelf Rover robot from Viam, which is powered by the Raspberry Pi 4. The robot has a webcam connected and this can be used to capture images our our elf from different positions to train our model. It then use the camera later to detect elves within the robots field of view. Creating a robot in Viam To create a robot in Viam, you need to log into the Viam software at app.viam.com. Once you have logged in, you can create a new robot by adding the name of the new robot and clicking on the Add Robot button in the top left corner of the screen. Installing Viam on the Raspberry Pi To install the viam-server on the Raspberry Pi, follow 3 the onscreen instructions below. This assumes you’ve some knowledge about how to setup and use an SSH session to connect to your Raspberry Pi. Luckily, Viam has a really easy to follow guide to help you do this, available here. Download the Viam app - copy and paste the command into an ssh session connected to your raspberry pi Download and install the viam-server service - again copy and paste the command into an ssh session connected to your raspberry pi. Check for connectivity - Finally, Viam will report back that it has detected that the viam-server is now running on the Raspberry Pi and will present a button to configure the robot. Add a Pi board to the robot Next we need to add a Pi board to our robot. This will allow us to control the robot from the Viam software. To do this: Click on the Config tab on the robot we want to add the Pi board to. This will open up the configuration screen for the robot Click the + Create Component button to add a new component to the robot Select board, and then Pi from the list of components Give the Pi board a name; be sure to avoid spaces and special characters. Click the Save config button Configuring the robot Next we need to add a camera to our robot so that it can see. The easiest way to do this on Viam and Raspberry Pi is to plug in a cheap USB Webcam. To add this to our robot in Viam, we need to: Click on the Config tab on the robot we want to add the camera to. This will open up the configuration screen for the robot Click the + Create Component button to add a new component to the robot Select Camera from the list of components Select Webcam from the list of cameras Give the camera a name, be sure to avoid spaces and special characters. Choose video0 for the Video Path Click Create Component Add Data-Management Service Next we need to add a data-management service to our robot. This will allow us to store the images we capture from the camera and use them to train our model. To add a data-management service to our robot in Viam, we need to: Click on the Config tab on the robot we want to add the camera to. This will open up the configuration screen for the robot Click on the Services tab Click on the + Create Service button Select Data Management from the list of services Give the service a name (remember to avoid spaces and special characters) Click the Create button in the new Data Management service, review the directory the data (our images) will be captured to, its ok to leave this as the default ~/.viam/capture folder Notice the Cloud sync settings, we want this to be left on so that all the images we capture are sent to the Viam cloud service, as we will use this to quickly build our model. Click the Save config button Add Data Capture Configuration Next we need to add a data capture configuration to our robot. This will allow us to capture images from the camera and use them to train our model. We can set this to on or off, as we don’t want to continually capture images, just when we want to train our model. To add a data capture configuration to our robot in Viam, we need to: In the Camera component we just created, click on the Add method button set the Type to ReadImage and the frequency to 0.3 (this will capture an image every 0.3 seconds) set the Mine Type to image/jpeg click on the Save config button at the bottom of the screen Capturing images Now we have our robot configured, we can capture images from the camera and use them to train our model. To do this, we need to: Position the robot so that the object we want to train it to recognise is in the centre of the camera’s field of view Move the object to different positions in the camera’s field of view The robot will capture a new image every 0.3 seconds, so we can move the object around and capture images from different angles, pretty quickly Be sure to capture images from different angles, and different distances from the camera, in different lighting conditions, etc Once we have captured enough images, we can use these to train our model. Go back to the Viam app, Select the robot from the list, and click on the Config tab Click on the Components tab, and then click on the Off button in the Camera -> Data capture configuration section. This will stop the robot from capturing images Prepare the images for training The Viam software has a really easy to use training system. You can train a model to recognise objects in just a few minutes. I used the webcam on the robot to capture images of our elf from different angles, and then used the Viam software to train the model. To do this: log into app.viam.com Click on the data tab In the All Data tab you will see a list of all the images that have been captured by your robot Click on the first image Click on the Bounding box button in the top right corner of the screen Click and drag a box around the object you want to train the model to recognise Click on the Current label button in the top right corner of the screen, and enter the name of the label (in this case elf) Add this to a dataset, by clicking on the Datasets textbox and giving the dataset a name (in this case elf-dataset) Repeat for all the images captured Train the model from the dataset Once we have labelled all the images, we can train the model from the dataset. To do this: Click on the Datasets tab, within the Data tab Click on the elf-dataset we created earlier Click on the Train model button to star the model training process The model will be trained using the images we captured earlier, this may take 5-10 minutes depending on the size of the dataset Click on the Models tab to see the status of the model training process Deploying the model We can send the trained model to our robot, so that it can use it to recognise objects. To do this: Click on the Fleet tab, and select the robot to deploy the model to in the list Click on the config tab, then the Services tab Click on the + Create Service button Click on the ML model item in the list of services Click on the TFLite CPU item in the list of models Give the model a name (in this case elf-model) Click on the Deploy model on robot button Choose the elf-model we just created from the list of models Click on the Save config button Add Vision Service Next we need to add a vision service to our robot. This will allow us to use the model we just trained to recognise objects. To do this: Click on the Config tab on the robot we want to add the camera to. This will open up the configuration screen for the robot Click on the Services tab Click the + Create Service button to add a new component to the robot Select Vision from the list of services, then ML model Give the service a name (remember to avoid spaces and special characters). Click the Create button in the new Vision service, choose the elf-model we just created from the list of models Click the Save config button Evaluating the model We can test the model to see how well it performs. To do this: Click on the Fleet tab, and select the robot to deploy the model to in the list Click on the config tab, then the Components tab Click on the + Create Component button Click on the Camera item in the list of components, then transform Give the component a name (in this case elf-transform) Click on the Save config button Add the following JSON to the attributes textbox: { "pipeline": [ { "attributes": { "detector_name": "elf-detector", "confidence_threshold": 0.5 }, "type": "detections" } ], "source": "cam" } Click the Save config button Fine tuning the model You can adjust the threshold confidence level to improve the accuracy of the model. To do this: Change the value of the confidence_threshold attribute in the elf-transform component to 0.7 and see if the model detects the elf more accurately. Adding extra objects to the model You can add extra objects to the model by capturing images of the new object, labelling them, and then training the model again. Think about all the potential uses for this model. You could train it to recognise all sorts of objects, and then use it to control a robot to find them.