Capsule counting
Real world usage example of counting capsules in manufacturing
Last updated
Real world usage example of counting capsules in manufacturing
Last updated
We want to count capsules placed in a special holder in a machine to see if the holder is full or not. This will be then used to indicate to the machine that the holder is full which triggers an action on the machine side. We will do this by capturing and labeling our data as we did in our hello world example. The AI model we will use will be a more advanced one used for object detection, which we'll deploy and link its output to the controller for sending signals to the machine.
To collect the data, we will use a more automated approach than in our hello world example. We will create a function that captures a defined number of images from the camera at specific times automatically.
Now we can call capture_images
to start collecting the data. It will capture capture a total of n
images, waiting for delay
seconds between individual captures.
We will once again use label studio for labeling. If you haven't already, take a look at our hello world example to see how to install and set up label studio.
We will create a new project in label studio. In the "labeling setup" tab, we will choose "Object Detection with Bounding Boxes" and create classes according to our project. We only need one class "capsule".
First, we will import our captured images. Simply click on "Import" button, and select the images we wish to label. Next we can start labeling our images. Click on "Label All Tasks", select the class you wish to use and draw a rectangle bounding the object in the picture. Do this for every object that you wish to detect as well as for every image on which the model will be trained.
After we're done labeling our images, we can finally export the labeled data which we will use for training our model. Click on "Export" and select "YOLO" as the export format. A zip file containing the exported data will be downloaded.
For the model we will use YOLOv8, which is a pre-built model by ultralytics, instead of building our own.
In order to use this model, we must create a configuration file describing our data. We will save this in a file named dataset.yaml
, which we will reference below, you can use any filename but be sure to change the code accordingly.
Now we can train the model on our custom dataset. First, we must install the model.
Next we can train the model. Feel free to tweak the parameters to your liking.
As we did in the previous example, in order to deploy the model on a Robopipe device, we must first export it in the suitable format. To find out more about model export, take a look at our hello world example.
To deploy the model we will use our API. We will laverage the fact the we use a YOLO model and use configuration specific to this type of model. The benefit is that when reading the model's output from the API, we will get already interpreted results, which are easy to work with.
The model is already running on the device from the point we deployed it. Now we will read and interpret the results and use them to control our machine.
The data received from the websocket is an array, containing objects, each of which has the following fields:
label - an integer indicating the label of the detection, e.g. 0 for capsule in our case, as we specified in the dataset.yaml
above
confidence - a floating point number in the range [0;1] indicating the model's confidence about the detection, higher number means larger confidence indicating that the detection is more accurate.
coords - array of 4 floats, representing the bounding box for the detection
Robopipe controller is not just an ordinary controller, but a fully fledged PLC, which means that we can use it to controll our machine's behaviour. For the purpose of this example, we will indicate the result using all LEDs on the controller, but the use cases can be far beyond that. For example, you can have all sorts of devices connected to analog or digital I/O of the controller, and controll them using the API.
In this example, we will simply turn on the led when a specified number of capsules has been detected, and turn it off when the number gets below that. We will define a function which we will use in our main
function above, which will do this.
All that's left is to modify our main
function to call process_detections
and let it run.