In this session, we will explore practical applications of AI using Google’s Teachable Machine. We’ll also discuss how to create models for various use cases like image, pose, and sound detection, followed by brainstorming additional use cases.
keyboard_arrow_down
1. Introduction to Teachable Machine
Teachable Machine is an easy-to-use tool by Google that allows anyone to train a machine learning model without needing extensive coding knowledge. The platform supports three types of models:
- Image Classification: Categorizes images into predefined labels.
- Pose Detection: Identifies human poses.
- Audio Classification: Classifies sounds into categories.
We’ll focus on image classification for this session and then discuss potential applications for other types.
2. Hands-On Activity: Training an Image Classification Model
Step 1: Access Teachable Machine
Visit Teachable Machine and select the Image Project option.
Step 2: Collect Training Data
- Define three categories for your project (e.g., Happy, Sad, Neutral expressions).
- Use your webcam to capture multiple images for each category. Make sure to:
- Vary lighting conditions.
- Use different angles.
- Include diverse expressions.
Step 3: Train the Model
- Once you’ve collected the images, click Train Model.
- Wait for the training to complete.
Step 4: Test the Model
- Use the webcam to test how well the model classifies your expressions.
- Refine the training data if necessary to improve accuracy.
3. Python Integration: Using the Model
After training, you can export the model and use it in Python projects. Here’s how you can integrate the Teachable Machine model into your code:
Export the Model
- Click on the Export Model button.
- Choose TensorFlow.js or TensorFlow Lite for Python integration.
Example Python Code for TensorFlow Lite
import tensorflow as tf
import numpy as np
from PIL import Image
# Load the TFLite model
def load_model(model_path):
interpreter = tf.lite.Interpreter(model_path=model_path)
interpreter.allocate_tensors()
return interpreter
# Preprocess image
def preprocess_image(image_path, input_shape):
image = Image.open(image_path).convert('RGB')
image = image.resize((input_shape[1], input_shape[2]))
image_array = np.array(image) / 255.0
return np.expand_dims(image_array, axis=0)
# Make predictions
def predict(image_path, model_path):
interpreter = load_model(model_path)
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
input_shape = input_details[0]['shape']
input_data = preprocess_image(image_path, input_shape)
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
predictions = interpreter.get_tensor(output_details[0]['index'])
return predictions
# Example usage
model_path = 'path_to_your_tflite_model.tflite'
image_path = 'test_image.jpg'
predictions = predict(image_path, model_path)
print("Predictions:", predictions)
---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-164-96878ade8f50> in <cell line: 0>()
35 image_path = 'test_image.jpg'
36
---> 37 predictions = predict(image_path, model_path)
38 print("Predictions:", predictions)
2 frames
/usr/local/lib/python3.11/dist-packages/tensorflow/lite/python/interpreter.py in __init__(self, model_path, model_content, experimental_delegates, num_threads, experimental_op_resolver_type, experimental_preserve_all_tensors, experimental_disable_delegate_clustering, experimental_default_delegate_latest_features)
468 x for x in self._custom_op_registerers if not isinstance(x, str)
469 ]
--> 470 self._interpreter = _interpreter_wrapper.CreateWrapperFromFile(
471 model_path,
472 op_resolver_id,
ValueError: Could not open 'path_to_your_tflite_model.tflite'.
4. Brainstorm Additional Use Cases
Here are some exciting possibilities to explore using Teachable Machine:
Fitness Activity Tracker
- Train a model to classify and count exercises (e.g., squats, push-ups).
- Real-world example: Use pose detection to ensure proper form during workouts.
Plant Disease Detection
- Train a model to identify plant diseases based on leaf patterns.
- Real-world example: Help farmers detect diseases early and save crops.
Custom Hand Gesture Controls
- Train a model to control devices using gestures like thumbs-up or open palm.
- Real-world example: Use gestures to control a slideshow or play music.
5. Key Takeaways
- Teachable Machine simplifies training models for real-world use cases.
- You can integrate models into Python for broader applications.
- Brainstorming additional use cases helps spark creativity and innovation.