Activity 1: Image Recognition
Train and Test the Image Classifier


The first step is to open Google Teachable Machine in order to create an image recognition model.
Decide how many categories, or classes, you want and name them. Then use the webcam to take pictures for each class. For example, you might take pictures of you and your partner, or you with and without a stuffed animal. Aim for about 300 pictures for each class (this only takes a few seconds) and try to move around slightly so the camera gets a variety of angles.
The training will take about a minute. Make sure to leave the tab open while the model is training, even if your browser pops up a warning that the window is unresponsive.
When the training is complete, you will be able to test your model in the Preview panel. Make sure that your model works the way that you want it to before moving on. If it doesn’t, you may need to add more image samples for each class and train again. When you are happy with your model, click Export Model.
It is a good idea to save your model in case you want to reference or change it later. To do this, click on the Teachable Machine menu and either download the file or save it to your Google drive.
Using the Image Classifier in Python


Open brython.birdbraintechnologies.com and connect to the Finch. You should use the browser-based version of Python for this activity because it already has all of the machine learning methods that you will need.
Then use MachineLearningModel.load() to import your image recognition model. This function takes two parameters. The first is a string that is the http address for your model. The second parameter is a string that denotes the type of Google Teachable Machine you are using, which should be “image” here.
Once you have loaded the model, you can use MachineLearningModel.getPrediction() to get the results of your machine learning model for the current image on the webcam. Print these results to the screen. Before the program ends, call MachineLearningModel.unload() to stop the model. Run your program to try it out!
MachineLearningModel.getPrediction() returns a dictionary. Each class label is a key for the dictionary, and the value that corresponds to that key is the probability that the current image on the webcam belongs to that class. A probability close to 1 tells you that the image probably belongs to that class, while a probability close to 0 tells you that the image probably doesn’t belong to that class. For example, here the probability of “Mandalorian” is greater than 0.99, while the probabilities of all other classes are very small. Note that extremely small values are shown in scientific notation.
Challenge: Write a program to make the Finch respond to each of your images. For example, this video shows the Finch responding to three Star Wars characters (turn your sound on). As you test your program, notice what happens if the webcam sees an image that is not similar to something in your training data.