Chapter 3 – Vision and Navigation Technologies with Zumi

3.3 – Color Classification

Welcome to the Color Classification module! Have you ever wondered how computers can recognize and differentiate between colors? In this lesson, we will explore the fascinating world of color classification and teach Zumi how to recognize different colors using a special algorithm. By the end of this module, you will have a better understanding of how computers perceive and categorize colors, and you will be able to program Zumi to identify different colors in the real world. Let’s dive in!

Step 1: KNN Color Classifier

 

The KNN color classifier is a basic example of machine learning, which involves three steps: gathering data, generating a model, and making predictions. In this lesson, you will learn how to use a special algorithm to teach Zumi different colors using its camera.

Gathering Data

To gather data, you will use Zumi’s camera to take multiple pictures of your favorite colors. These pictures will be used to train the KNN color classifier algorithm, which will use the color information to label each color.

Generating a Model

Once you have collected enough data, the KNN algorithm will generate a model that can be used to predict the color of an object based on its RGB values. The model will be generated by calculating the distance between the RGB values of the training data and the RGB values of a new sample.

Making Predictions

The final step involves testing the model and making predictions based on the color of the object in front of Zumi’s camera. You can write code to make Zumi react differently to each color.

To get started, you will need to have your activity color cards handy. Follow the instructions in this lesson to train Zumi’s color classifier and write code to react to different colors.

image

Step 2: What is an Image?

 

Before diving into training Zumi to recognize colors, it is important to understand how Zumi sees colors. Zumi’s way of perceiving colors is quite different from how humans perceive them.

Pixels and RGB Values

 

An image is made up of a grid of small dots called pixels (short for picture element). Each pixel can be a single color or a combination of colors, and these colors are represented by a series of three numbers that make up the RGB (red, green, and blue) value. For instance, the RGB value of a beautiful shade of turquoise could be (27, 209, 197). This means that there is not much red, but there is a lot of green and blue. Since each RGB value can range from 0 to 255, there are 256 different values for each color. As a result, there are 256^3, or 16,777,216, different color combinations that can be created using RGB values.

Step 3: What is a Matrix?

 

In digital images, each pixel can be represented by a set of numbers, which turns a picture into a grid of numbers. However, while humans perceive images based on colors and shapes, computers interpret them solely through these numerical grids, also known as matrices.

Each number in a matrix corresponds to the RGB value of a specific pixel, where RGB stands for red, green, and blue. By combining these three primary colors in different proportions, computers can create a wide range of colors to render an image.

Visually, matrices resemble the image itself, as they are essentially a representation of the pixel grid. Understanding matrices is crucial in image processing, as it is the basis for many techniques used to manipulate and analyze digital images.

image

Step 4: Using HSV Instead of RGB

 

This program converts each RGB image to the HSV colorspace. HSV stands for hue, saturation, and value.

– Hue normally ranges from 0-360 and represents the color. In this application, however, it ranges from 0-180.
– Saturation is the color’s intensity.
– Value is how light or dark the color is.

In computer vision applications, it is better to use the HSV colorspace since it separates values for colors and intensity. This is important because shadows, reflections, and other factors may cause certain colors to look very different. The HSV colorspace takes this into account for more accurate results.

image

Step 5: Teach Zumi 3 colors

 

To teach Zumi to recognize three colors, follow these steps:

1. Access the KNN Color Training Wizard in your Zumi dashboard.
2. Select three colors from the packet of color cards that come with your Zumi. Choose your favorite colors.
3. To access the KNN Color Training Wizard, click on the “Explore” page from the main Zumi dashboard page.

image

4. Once on the Explore page, click on the “Learning Colors” module.

image

5. Follow the prompts to create your own color classifier.
6. When you are done, make sure to save your model with a project name that is easy to remember. You will be using it in the next section, so write down your label names and project name.

Step 6: AI Blocks

 

All the blocks related to AI and machine learning are in the AI Menu:

image

Step 7: Training a model

 

Let’s start training a model for a traffic light. First take the green, red and yellow color cards and take 30 pictures of each from a different angle and then take pictures of what no color looks like.

image

Finally, save the model with a name.

image

After training a model, you can use the Get model block to load the trained model and then the predict from frame block when you want Zumi to say the name of a color.

image

Now we can have Zumi show how a car would move at a traffic light by using blockly code. We will make it perform differently when shown each of the colors. If it sees the color red, it stops. In the case of yellow, it will move ahead at speed 30. If shown green, it will move ahead at speed 70.

image

Activity

 

Try creating your own traffic light with three other colors!

`Hint`: First train the model using your three colors, then using the camera, take a picture and save it to a variable before using the model to predict the picture. Make sure it is in a loop so it will run forever until you manually stop it.

Solution:

 

image

Conclusion

Congratulations, you can start using training Zumi to recognize colors and perform different actions based off those colors in Blockly!

Demo Video

 

Questions

 

License

Share This Book