Today we hear a lot about AI and machine learning. So, I wanted to learn more so I thought let's find something to make and learn more about it. I have a Raspberry Pi Zero W so my project is about my PI recognising me and alerting me if I am not at my office desk.
To do that, I searched for how to train a machine-learning model without writing any machine-learning code, by providing some images of me. Raspberry Pi Zero cannot be used to train a machine-learning model due to its limited resources. So after some searching, I found teachable machine experiment tool from Google.
Google’s Teachable MachineGoogle’s teachable machine is an online platform where you can train a computer to recognize images, sounds and poses. After you train your model you can export it and use it for your implementation. We will see below how you can do this.
I've chosen the images project. For the machine to train I had to provide images of me sitting at my office desk and images without being there. So I created 2 classes one with the name Me and the other one with the name Not me. You can create as many classes as you want depending on your project. In order to achieve high training results you must provide as many pictures as you can. You can either let the camera record for you or upload your own.
I had pictures taken from my laptop and also from my Raspberry Pi so as to have a variety of colours, sizes etc. I took various poses, looking down, straight, drinking etc. See the image below.
After I had all my images I trained the model with the default options.
After the training is completed you can preview the results and if you are happy you can keep the model or add more pictures and keep on training. The model training happens on your browser
I was happy with the results so I proceeded to export it to use it on my raspberry pi. I've chosen Tensorflow lite and quantized conversion type.
You will receive on your machine a zip folder which has inside the train model as well as a text with the labels (class names) you provided.
I copied this folder to my raspberry pi.
Raspberry piTo prepare Raspberry Pi to use the trained machine-learning model we have to install the following libraries:
- tflite-runtime: the official solution for running machine learning models on mobile and embedded devices. It enables on-device machine learning inference with low latency and a small binary size
- python3-opencv: OpenCV (Open Source Computer Vision Library: http://opencv.org) is an open-source library that includes several hundreds of computer vision algorithms.
You can learn more about them on their respective sites.
sudo python3 -m pip install tflite-runtime
sudo apt-get install python3-opencv
After the libraries were installed I executed the following Python script:
from tflite_runtime.interpreter import Interpreter
import numpy as np
import time
from picamera.array import PiRGBArray
from picamera import PiCamera
import cv2
import os
from PIL import Image, ImageDraw, ImageFont
import datetime
def load_labels(path): # Read the labels from the text file as a Python list.
with open(path, 'r') as f:
return [line.strip() for i, line in enumerate(f.readlines())]
def set_input_tensor(interpreter, image):
tensor_index = interpreter.get_input_details()[0]['index']
input_tensor = interpreter.tensor(tensor_index)()[0]
input_tensor[:, :] = image
def classify_image(interpreter, image, top_k=1):
set_input_tensor(interpreter, image)
interpreter.invoke()
output_details = interpreter.get_output_details()[0]
output = np.squeeze(interpreter.get_tensor(output_details['index']))
scale, zero_point = output_details['quantization']
output = scale * (output - zero_point)
ordered = np.argpartition(-output, 1)
return [(i, output[i]) for i in ordered[:top_k]][0]
data_folder = "/home/pi/dev/AI/teachable_machine/model/"
model_path = data_folder + "model.tflite"
label_path = data_folder + "labels.txt"
interpreter = Interpreter(model_path)
interpreter.allocate_tensors()
_, height, width, _ = interpreter.get_input_details()[0]['shape'])
camera = PiCamera()
camera.resolution = (224, 224)
camera.framerate = 32
rawCapture = PiRGBArray(camera, size=(224, 224))
for frame in camera.capture_continuous(rawCapture, format="rgb", use_video_port=True):
image = frame.array
cv2.imshow("Frame", image)
label_id, prob = classify_image(interpreter, image)
labels = load_labels(label_path)
classification_label = labels[label_id]
print("Result: ", label_id, ", Accuracy: ", np.round(prob*100, 2), "%. At: ", datetime.datetime.now())
if label_id==1:
print("Yannis where are you?")
# clear the stream in preparation for the next frame
rawCapture.truncate(0)
camera.release()
cv2.destroyAllWindows()
As the script runs, it loads the model from the file, opens the camera and tries to classify what it sees. If the classification label is Not me it prints “Yannis where are you?”
This was a very easy example of how to train a machine-learning model and with some basic experience how to use it on your own projects. Hope you liked it and you can follow me for more.
Comments