Chapter 4 – Visual Processing and Distance Measurement Techniques
4.2 – Colour 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.
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.
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.
Step 5: Teach Zumi 3 colors
To teach Zumi to recognize three colors, follow these steps:
- Access the KNN Color Training Wizard in your Zumi dashboard.
- Select three colors from the packet of color cards that come with your Zumi. Choose your favorite colors.
- To access the KNN Color Training Wizard, click on the “Explore” page from the main Zumi dashboard page.
- Once on the Explore page, click on the “Learning Colors” module.
- Follow the prompts to create your own color classifier.
- 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: Import Libraries
In this step, we will import the necessary libraries to access the functions required for driving, camera, and color classification. By doing so, we can use the model we previously trained to have Zumi react differently to each of the colors.
To import the libraries, copy and paste the following code into your Python script:
from zumi.zumi import Zumi
from zumi.util.camera import Camera
from zumi.util.screen import Screen
from zumi.util.color_classifier import ColorClassifier
import time
camera = Camera()
screen = Screen()
zumi = Zumi()
Step 7: Loading a model
To load the model, you will need to use the load_model()
function from the ColorClassifier library. This function requires one parameter, which is the name of your project. If you do not provide the correct name, you will receive an error. Here are the steps to load your model:
- In the code below, replace the empty strings with your
user_name
anddemo_name
. Make sure to include the correct spelling and punctuation.
user_name = ''
demo_name = ''
- Create a new instance of the
ColorClassifier
class using youruser_name
.
knn = ColorClassifier(user_name=user_name)
- Call the
load_model()
function on your instance ofColorClassifier
and pass in yourdemo_name
.
train = knn.load_model(demo_name)
- Fit the model using the HSV color space.
knn.fit("hsv")
Once your model has been loaded and fitted, you can test it using the code below. This code will prompt you to pick a color card to show to Zumi. Press “enter” when you are ready to see the predicted color on the screen. If you press “q”, the program will break out of the loop and turn off the camera.
camera.start_camera()
while True:
user_input = input("Press 'enter' to predict or 'q to quit: ")
if user_input == "q":
break
image = camera.capture()
predict = knn.predict(image)
screen.draw_text_center(predict)
camera.close()
Step 8: Traffic Light
In this step, we will train Zumi to recognize the colors of a traffic light and make decisions based on the color she sees.
A traffic light has three colors: red, yellow, and green. Each color represents a different meaning:
- Red: Stop
- Yellow: Caution or slow down
- Green: Go
To train Zumi to recognize these colors, we will use the Color Training Wizard. Follow these steps:
- Open the Color Training Wizard in the Zumi Dashboard.
- Train the labels “red”, “yellow”, and “green” using the wizard.
- Save the trained model as “trafficlight”.
Once the model is trained and saved, Zumi can use it to recognize the color of a traffic light and make decisions accordingly. For example, if Zumi sees a red light, she will stop. If she sees a green light, she will go.
Step 9: Making Decisions
In this activity, we will program Zumi to respond to different colors by performing various actions. Specifically, we want Zumi to stop when she sees red, drive forward at a normal speed when she sees green, and drive forward at a slower speed when she sees yellow.
To accomplish this, we will use the following functions:
stop()
to make Zumi stopforward(speed=30)
to make Zumi drive forward at a slower speedforward(speed=70)
to make Zumi drive forward at a normal speed
Here’s an example code snippet that demonstrates how we can use these functions to make Zumi respond to the color yellow:
if predict == "yellow":
zumi.forward(speed=30)
Similarly, we can use the stop()
function to make Zumi stop when she sees red and the forward(speed=70)
function to make Zumi drive forward at a normal speed when she sees green.
By using these functions in combination with conditional statements, we can create a program that allows Zumi to respond to different colors and perform various actions accordingly.
Step 10: Load the model
Load the “trafficlight” project here by providing the project name in the quotes.
knn = ColorClassifier(user_name=user_name)
train = knn.load_model("trafficlight")
knn.fit("hsv")
Step 11: Adding If Statements to Control Zumi’s Actions
In this step, we will add if statements to control Zumi’s actions based on the traffic light color. We will use the predicted color from the camera and trigger Zumi to perform the corresponding action.
Here is the updated code with placeholders for the if statements:
camera.start_camera()
while True:
user_input = input("Press 'enter' to predict or 'q to quit: ")
if user_input == "q":
break
image = camera.capture()
predict = knn.predict(image)
screen.draw_text_center(predict)
# Add your if statements here!
camera.close()
You will need to replace the placeholders with the actual code to control Zumi’s actions based on the traffic light color.
Once you have added the if statements, you can test the code by showing Zumi a color card and pressing enter to see Zumi react!
Step 12: Review & Challenge
Have you ever played the game “Red Light Green Light”? It’s a fun game where a referee calls out “red light” or “green light” to players who are trying to race to a finish line. In this challenge, you will be using what you learned in this lesson to have Zumi go when it sees a green card and stop when it sees a red card. Play against your classmates — whoever’s Zumi gets to the finish line first wins!
Rules of the Game:
- Zumi should start moving when it sees a green card and should stop when it sees a red card.
- A green card means “green light” and Zumi should race towards the finish line.
- A red card means “red light” and Zumi should stop immediately.
- The first Zumi to reach the finish line wins.
Challenge
Using the concepts and skills you learned in this lesson, program Zumi to participate in the “Red Light Green Light” game. You can play against your classmates to see who can get their Zumi to the finish line first. Good luck!Demo
Questions