Now that you've created a machine learning model with Teachable Machine, you can export it to use with Python projects. When we show up to the present moment with all of our senses, we invite the world to fill us with joy. The pains of the past are behind us. The future has yet to unfold. But the now is full of beauty simply waiting for our attention.
1. Click Export Model to open the export menu.
2. Select the Tensorflow tab on the export window.
3. Select the option Keras and click Download Model to download a saved version of your model. Don't close the export window yet!
The resulting zip file contains two files:
keras_model.h5- the saved model and parameters. You can load this file with code to run your model in the machine learning library Keras.labels.txt- a text file containing the human-readable labels for each of your classes.
Colab uses a coding environment for Python called Jupyter Notebook.
1. Go to colab.research.google.com
2. Sign into your Google account.
3. If you've never been there before, an example notebook will open. From there, select File > New Notebook.
If you have used Colab before, that link will open to this window: create a new notebook with the New Notebook button.
Once you create a new notebook, you'll see a screen like the one below.
Teachable machine provides example code you can use to run your model. You'll copy it over and modify it slightly to test your model with Python.
1. On your Teachable Machine export window, click the Copy button in the code box labeled "Code to Test your Model"
2. Paste the copied code into the empty cell in the Colab notebook you created.
Once the code is pasted in, you'll need to upload a your model, labels, and a test image to run and test it.
1. Click the folder icon to open the file browser.
2. Drag and drop
keras_model.h5 and labels.txt into the file browser to upload them to the Colab runtime.
Use your webcam camera app to take a photo in one of the poses to test the model.
4. Rename the image to "test_photo.jpg".
5. Drag and drop the resulting file onto the file browser to upload it.
6. Replace the IMAGE_PATH with the name of your image ("test_photo.jpg").
7. Run it.
When you run your test code, you'll get an output array of predictions that corresponds to the model's percentage guesses of each category. This is pretty impossible to read for humans at a glance - that's what the labels.txt is for. You'll add a bit of python code to print out the correct label for your model's prediction. Add the follow code just below the lines that load the model (under the comment # Load the Model)
labels = [line for line in open("labels.txt")]
This will load each line of the labels text file into a list you can access later.
2. Just below the line that prints the predictions array, add the following line:
print(labels[np.argmax(prediction)])This will print whichever entry in the labels list corresponds to the highest value in the predictions array (the model's best guess).
3. Run the program to see the human-readable label of the most likely guess printed in the output!
Now you've got a functional python program that loads an image and guesses which rock, paper, scissors symbol it contains!
Below are some highlighted breakdowns of the example code that might be useful if you wanted to use this code for another project:
Here's what your project could look like at this point:
You can use the models produced by Teachable Machine for any Python project by loading them with Keras.
- Export the model as an .h5 file.
- Copy the sample code to load the model.
- Update the python code to utilize the model however your project needs.












