Easy Machine Learning & Object Detection With Teachable Machine

17K4343


Intro: Easy Machine Learning & Object Detection With Teachable Machine

Teachable Machine is an easy, but powerful tool to create machine learning models. It allows for easy data capture to create training data sets and uses state of the art algorithms to train machine learning models right in your browser. It is done in a very intuitive web interface. You can generate image, sound, or pose detection models. In this project, I will provide you with a step-by-step guide for setting up an OpenCV/TensorFlow Python development environment and a Python script framework to easily incorporate Teachable Machine image models into your projects.

The goal of this project is to greatly reduce the barrier to entry to use machine learning. This Instructable should give you the tools you need to make some exciting machine learning projects. I hope to make some more interesting tutorials and demos in the future that use this development environment and framework.

You can follow along and generate your own model or you can use the Teachable Machine model I generated. The model I provided detects what flavor La Croix you have.

Github repository for the project: https://github.com/mjdargen/Teachable-Machine-Object-Detection

EDIT: I have now created a version that sets up the same environment on the Raspberry Pi: https://www.instructables.com/id/La-Croix-Flavor-Detector-Easy-Object-Detection-on-/

STEP 1: Generating a Model With Teachable Machine


Setting up the Project

Teachable Machine is a fairly easy-to-use tool with a very intuitive interface. For this project, we will be working with image detection. Go to https://teachablemachine.withgoogle.com/ and click on Get Started. Now select Image Project. This will open up the image model training window.

Creating the Dataset

You will add and name the classes (i.e. objects) you want to train the model to detect. Name the classes well with an intuitive name. The name of the classes is what the later program will call out when that object appears in the frame.

It is a good idea to make a "Background" class. This can help train the model to not attribute details from the background with one of the other classes. If you name this class "Background", the final program, which uses text-to-speech to say the name of the object in the frame, will ignore the background class and not call out "background" every time it is solely the background in frame.

To add image samples to a class, you can either use your webcam to capture images in Teachable Machine or upload images from another source. In order to produce a model, you want a lot of high-quality data. You can see in my example of the "La Croix Flavor Detector Model", I had no less than 600 samples for each class. I used the webcam to quickly capture many different samples. I made sure to capture the object from every angle in different lighting situations with a variety of backgrounds to generate an accurate model.

Training the Model

Once you have set up all of your classes and are happy with your datasets, it is time to train the model! Click the "Train Model" button. In order to train the model, you must leave the tab open in your browser. Training the model can take a while. In this project where I had 7 classes with >600 samples, it took about ~20 minutes to train. Your browser may occasionally complain that the Teachable Machine tab is slowing down your browser. Just acknowledge notification and say it's fine so your browser does not cancel the training (different browsers word this notification differently). Once it's complete, it's time to test out your model!

Previewing the Model

Now it's time to test out your trained model and see how well it does! Go to the Preview pane and turn the input on. Present the various objects to the webcam and see if the model accurately guesses what object is in the frame. Remember, the model cannot detect more than one object unless you made a single class for when two objects are present. If it's not performing well, try providing more photos to the model. If you're happy, it's time to export the model!

Exporting the Model

To export the model, click the "Export Model" button. A new window will pop up. Click the "Tensorflow" tab and select the "Keras" model conversion type. Now click "Download my model". It can take about a minute or so to compress the model and prepare it for download. You should get a pop-up window asking you to save a zip file. Save the file and unzip it. You should see a "keras_model.h5" file and a "labels.txt" file. Hang onto these and we will use them once you have your Python environment set up on your computer!

STEP 2: Installing Python 3 & Git

Python 3 Installation & Set-up

The first thing you will need to do is install Python 3 if it is not already installed on your machine. Go to https://www.python.org/downloads/ and download and run the correct installation for your operating system. I have tested this development environment in Python 3.6 and Python 3.7 and everything seemed to work appropriately. However, Python 3.8 did not seem to fully support some of these libraries fully yet. I would recommend installing the latest version of Python 3.7 for your environment. During installation, make sure you check the box to add Python to Path.

Once you have fully installed Python and added Python to Path, open up your terminal or command prompt and type "python --version" and then "python3 --version". This is important because we want to know whether "python" or "python3" command maps to your Python 3 installation. You will need to know this moving forward to run your Python scripts, install new Python packages, etc. If no executable is mapped to python or python3, look up adding environment variables to Path for your operating system.

In the first example in the image above, you can see "python" invokes Python 3 and "python3" invokes nothing. In the second example in the image, "python3" invokes your Python 3 installation. This is because there is a Python 2 installation that maps to the "python" command in the second example.

Git Project Files

Now you will need to retrieve the installation files, machine learning models, and the demo Python program from my Github repository. You can either install a git client and clone the repository or you can download a zip file of the repository from your browser.

https://github.com/mjdargen/Teachable-Machine-Obj...

git clone https://github.com/mjdargen/RPi-La-Croix-Flavor-Detector

STEP 3: Setting Up Python Environment: OpenCV and TensorFlow

I have written installation scripts to simplify the installation process for this development environment. The installation scripts are listed above. Just select the appropriate script for your operating system.

I have now created a version that sets up the same environment on the Raspberry Pi here: https://www.instructables.com/id/La-Croix-Flavor-...

Windows Install
  • Assumes you have Python 3 installed.
  • Assumes that your Python 3 executable is invoked with "python". If that is not the case, you will need to edit the batch script and replace every instance of the "python" command with "python3".
  • Run the batch script from Powershell: "./installation_scripts/windows_install.bat" (don't run as administrator).
  • Can take ~30 minutes or more depending upon your system and internet connection.
Mac Install
  • Assumes you have Python 3 installed.
  • Assumes that your Python 3 executable is invoked with "python3". If that is not the case, you will need to edit the shell script and replace every instance of the "python3" command with "python".
  • Navigate to the folder of the repository in your terminal.
  • You will need to make the script executable by running the following command: "sudo chmod +x ./installation_scripts/mac_install.sh"
  • Run the shell script with the command: "./installation_scripts/mac_install.sh".
  • This installation script also installs the Homebrew package manager.
  • Can take ~30 minutes or more depending upon your system and internet connection.
Linux Install
  • Assumes you have Python 3 installed.
  • Assumes that your Python 3 executable is invoked with "python3". If that is not the case, you will need to edit the shell script and replace every instance of the "python3" command with "python".
  • Navigate to the folder of the repository in your terminal.
  • You will need to make the script executable by running the following command: "sudo chmod +x ./installation_scripts/linux_install.sh"
  • Run the shell script with the command: "./installation_scripts/inux_install.sh".
  • Can take ~30 minutes or more depending upon your system and internet connection.
Using Python Virtual Environments

If the installation script executed successfully, you have now installed all necessary dependencies to run OpenCV and Tensorflow in a Python virtual environment on your machine. The virtual environment is called TMenv and is located in the top-level directory of the cloned repository entitled "Teachable-Machine-Object-Detection".

The Python packages were installed in a virtual environment so as not to disrupt your packages associated with your main installation of Python in case you had other programs that depended upon a specific version of a package.

To use the packages you installed to run the demos, you will need to activate your virtual environment.

  • Mac/Linux: "source TMenv\bin\activate"
  • Windows: "TMenv\Scripts\activate"

Once you have activated your environment, it will show the name of your virtual environment in parenthesis before the prompt in your terminal. Anything you do related to Python at this point will only affect your TMenv virtual environment. You can now run Python scripts in your virtual environment. To exit your virtual environment, just run the command "deactivate".

STEP 4: OpenCV Common Object Detection Test

To make sure we set everything up correctly, we will run this OpenCV Object Detection model that Arun Ponnusamy developed. His source code and description of the project is below. We will use a script I wrote that uses the cvlib detect_common_objects() wrapper. It uses your webcam and will detect, label, and say the name of the detected objects. It can detect 80 of the most common objects.

https://github.com/arunponnusamy/object-detection-opencv

https://www.arunponnusamy.com/yolo-object-detectio...

To run the code, navigate to the directory where you cloned the Github repository. Proceed with the following commands.

cd ~/Documents/Teachable-Machine-Object-Detection     	# change directory to cloned repo
source TMenv/bin/activate  		# activate venv for Mac/Linux OR
TMenv/Scripts/activate			# activate venv for Windows
python yolo_obj_det.py			# executes script, press ctrl+c to quit
deactivate				# to exit the virtual environment

Note: the Python script will run forever until you hit ctrl+c to close the program.

STEP 5: Setting Up Source Code

Now that we have our OpenCV/Tensorflow development environment setup and we have tested it to make sure it works, it's time to move on to running a Teachable Machine model. You can either use the sample model I provided or one that you created and exported.

Once you have successfully exported the model as described in the first step, you will need to unzip the model to extract both the .h5 file and the labels.txt. You will need to update the "model_path" and "labels_path" variables to point to these files in tm_obj_det.py. You will need to determine the width and height of your webcam's video feed in pixels and update the "frameWidth" and "frameHeight" variables. You may also need to mirror the video feed for your webcam depending upon your setup. To do this, uncomment the line "frame = cv2.flip(frame, 1)".

Next, you will need to set your confidence threshold (conf_threshold). This variable is a percentage value of how certain you want the model to be before it labels the image and speaks the prediction. By default, the confidence threshold is 90%.

Finally, if you have any issues with the video showing up properly, you can use the matplotlib implementation. You will need to comment out the "cv2.imshow" and "cv2.waitKey" lines. Then you will need to uncomment "import matplotlib" as well as the plt lines of code towards the end.

That's it, your code is ready to run!

STEP 6: Run!


Now your code should be all set up to run. Navigate to the directory, activate your virtual environment, and run the code! After about 10 seconds, it should load a video feed. The program will label what object it recognizes and will use text-to-speech to say the name of the object.

cd ~/Documents/Teachable-Machine-Object-Detection     	# change directory to cloned repo
source TMenv/bin/activate  		# activate venv for Mac/Linux OR
TMenv/Scripts/activate			# activate venv for Windows
python tm_obj_det.py			# executes script, press ctrl+c to quit
deactivate				# to exit the virtual environment

Note: the Python script will run forever until you hit ctrl+c to close the program.

STEP 7: Remix!

These packages installed in your virtual environment and the scripts I provided should hopefully give you a useful framework to develop lots of exciting things. You can now easily incorporate object detection into all of your projects! I hope to continue doing more projects in this space to make some more fun projects that use image detection and leverage this framework.

Here are some project ideas. Feel free to take them and run with them or come up with your own!

  • A program to recognize your friends and greet them by name as they come to your house.
  • A program to detect when you are leaving the house and ask you to present your phone, keys, wallet, etc. to make sure you have everything when you leave the house.
  • Build a sorter that uses a motor to divert objects in a particular direction based on which objects they are.
  • A program that will detect letters in sign language and write these out to a text file.

STEP 8: More Projects

STEP 9: Source Code

To view the source code, visit this Github repository or see the code below.

35 Comments

I don't have much experience with using different cameras for computer vision. I have really only used my Logitech webcam for most things. Sorry, I can't provide more help in that department!
Your model seems to work perfectly. I'm facing problems. When i train my model using teachable machine. I only recognize 1 class but before. Exporting it's working well. I have been doing this for more than 20 models. Evertime i export it start giving errors classifying every image in one class will. Please help
It is hard to tell what your issue is without more information about the errors. From what you've described, it sounds like you may not be updating the label path correctly for your new model. Double-check that you have done so correctly. If it still does not work, please try to provide more information about the error.
I want to use a usb webcam, do i have to modify the python code? now i have the laptop webcam, i want to install a usb webcam. could you help me with this? thanks for the guide
The one line you may have to change is the line below. The argument to the VideoCapture method tells OpenCV which video capture device you want to use. Typically, 0 is going to be your computer's native webcam. Try changing it to 1 and see if the feed from your USB webcam appears. If it is still not working, try iterating through different values until you find the correct one. Best of luck!

# instantiate video capture object
cap = cv2.VideoCapture(0)
I can use your model it's great but when i change the model_path to mine it doesn't work. Do you know what caused it?
- The first picture is your model, In use normal
- two picture is your model for me it error
you can help me
It is very hard to tell what the issue is without more information. I would assume it is how you are describing the new path to your model. Where is the model in relation to the tm_obj_det.py script? What did labels_path and model_path to be?

Try unzipping the model and place it in a folder in the same directory as the Python script. Name the folder "my_model". Then what you can do is update labels_path and model_path to be as shown below:

labels_path = f"{DIR_PATH}/my_model/labels.txt"
model_path = f"{DIR_PATH}/my_model/keras_model.h5"

Best of luck! Hope it works!
I am trying to run export and run the model on google Collab as my PC cant run it.
How do i do that, any help would be appreciated!
thx
It is incredibly difficult to run image detection models through Google Colab. Google Colab runs your code on a remote Linux server making it incredibly hard to feed your images from your webcam to the model. There are some weird workarounds for it that require you to use your browser and javascript to capture the image and eventually feed it into the Python script running on the remote machine. It doesn't work great and will only process single frames at a time as opposed to the video demo you see here. A full description and example is linked below. Sorry I can't be of much more help. Best of luck!

https://colab.research.google.com/notebooks/snippets/advanced_outputs.ipynb#scrollTo=2viqYx97hPMi
Oh okay thanks i will try to look in to it and make it work : )
Sorry, the installation script doesn't seem to have worked properly. Numpy and tensorflow and some other packages don't seem to have appeared. Nice tutorial though :)
Thanks! The installation script only installs the packages (numpy, tensorflow, and others) into your virtual environment. I did this in an effort to avoid polluting your standard Python environment with package versions you might not need for other programs.

To use those packages, you will need to activate your virtual environment. You should then be able to type "pip list" and see the list of packages installed in your virtual environment. Let me know if that doesn't work and I can try to help you out some more! You may need to run the installation script again and monitor the messages to see if there are any specific errors I can help you with. Thanks for checking out the project!
Thanks for the advice, that fixed most modules being absent, but tensorflow is still not there even in my virtual enviroment. There is keras, though.
Hmm. Try activating your virtual environment, then running the command "pip install tensorflow==2.0.0". Make sure you are in your virtual environment. It should say the virtual environment name in parenthesis at the beginning of the line in your terminal: (TMenv). Let me know if that works or if you get any specific error messages.
ERROR: Could not find a version that satisfies the requirement tensorflow==2.0.0 (from versions: none)
ERROR: No matching distribution found for tensorflow==2.0.0
Ahhhh... I believe we just found your issue. It looks like there is not a TensorFlow distribution for your operating system distribution. In the message you received, it lists the supported versions as "none". If there were supported versions for your operating system, it would list out all the version numbers "(from versions: 1.13.1, 1.13.2, 2.0.0, etc....)". See the attached image for when I try to install version 7.0.0 (which doesn't exist) on my system. It shows a list of all of the supported TensorFlow versions for my system. What operating system and version of Python are you running? We may be able to find a workaround solution to install it.
Windows ten home python3.7.7
could I not just go into the virtual enviroment and do pip install tensorflow without the version?
I thought that's what you had been doing. First, make sure pip is up to date by running "pip install --upgrade pip". Try installing TensorFlow in the virtual environment by saying "pip install tensorflow==2.0.0". If that doesn't work, try installing TensorFlow outside of the virtual environment by deactivating the environment and saying "pip install tensorflow==2.0.0".

If those two things don't work, you can specify the direct location of the package from Google. Run "python -m pip install --upgrade https://storage.googleapis.com/tensorflow/windows/cpu/tensorflow-2.0.0-cp35-cp35m-win_amd64.whl" in your virtual environment. If that doesn't work, try it again outside of the virtual environment. If none of that works, try and send me a copy of the error messages so I can help you further. Hopefully, one of those steps does the trick for you! Nor sure why your Python environment is having issues finding the TensorFlow package.
More Comments