🧠 AI & MLKI & ML

How Our AI Mirror Works: Training a Model on Facial ExpressionsWie unser KI-Spiegel funktioniert: Ein Modell auf Gesichtsausdrücke trainieren

The AI Mirror is one of our most popular Foundation projects. Students build a Raspberry Pi camera system that detects a person's facial expression in real time — happy, surprised, neutral — and triggers a corresponding physical response: lights, sounds, a servo movement, or a display. Here's a full breakdown of the machine learning pipeline behind it. Der KI-Spiegel ist eines unserer beliebtesten Grundstufenprojekte. Schüler bauen ein Raspberry Pi-Kamerasystem, das in Echtzeit Gesichtsausdrücke erkennt — glücklich, überrascht, neutral — und eine entsprechende physische Reaktion auslöst: Lichter, Töne, eine Servobewegeung oder eine Anzeige. Hier ist die vollständige Machine-Learning-Pipeline dahinter.

The Full Pipeline at a GlanceDie vollständige Pipeline auf einen Blick

📸 Camera captures frame📸 Kamera nimmt Frame auf
OpenCV detects face regionOpenCV erkennt Gesichtsbereich
TF Lite model classifies expressionTF Lite Modell klassifiziert Ausdruck
GPIO triggers physical responseGPIO löst physische Reaktion aus

Step 1: Data CollectionSchritt 1: Datenerfassung

Every ML model starts with data. We collect around 200–300 photos per expression class — typically Happy, Neutral, and Surprised — using the Pi camera itself. Students take turns posing in front of the camera while a script saves a labelled frame every 0.5 seconds. This is the most time-consuming step, and also the most educational: students viscerally understand why data quality matters. Jedes ML-Modell beginnt mit Daten. Wir sammeln rund 200–300 Fotos pro Ausdrucksklasse — typischerweise Glücklich, Neutral und Überrascht — mit der Pi-Kamera selbst. Schüler posieren abwechselnd, während ein Skript alle 0,5 Sekunden einen beschrifteten Frame speichert.

Python · Data collection scriptDatenerfassungs-Skript
import cv2, os, time label = "happy" # change per session os.makedirs(f"data/{label}", exist_ok=True) cap = cv2.VideoCapture(0) count = 0 while count < 250: ret, frame = cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') faces = face_cascade.detectMultiScale(gray, 1.3, 5) for (x,y,w,h) in faces: face_img = frame[y:y+h, x:x+w] cv2.imwrite(f"data/{label}/{count}.jpg", face_img) count += 1 time.sleep(0.5)

Step 2: Model TrainingSchritt 2: Modelltraining

We train the model on a laptop (not the Pi — training is GPU-intensive). We use Transfer Learning with MobileNetV2 — a compact neural network originally trained on 1.4 million images. We keep its feature extraction layers frozen and add our own classification head on top. This means we only need ~250 photos per class instead of tens of thousands. Wir trainieren das Modell auf einem Laptop (nicht auf dem Pi — Training ist GPU-intensiv). Wir verwenden Transfer Learning mit MobileNetV2 — ein kompaktes neuronales Netz, das ursprünglich auf 1,4 Millionen Bildern trainiert wurde. Wir halten seine Feature-Extraction-Schichten eingefroren und fügen unseren eigenen Klassifikationskopf hinzu. So brauchen wir nur ~250 Fotos pro Klasse statt zehntausende.

Why MobileNetV2?Warum MobileNetV2?

MobileNetV2 is specifically designed for deployment on mobile and embedded devices. It achieves near-state-of-the-art accuracy on image classification while being small enough (14MB) to load and run on a Raspberry Pi 4 at around 15 frames per second — fast enough for real-time expression detection. MobileNetV2 ist speziell für den Einsatz auf mobilen und eingebetteten Geräten entwickelt. Es erreicht nahezu modernste Genauigkeit bei der Bildklassifikation und ist klein genug (14MB), um auf einem Raspberry Pi 4 mit etwa 15 Frames pro Sekunde zu laufen — schnell genug für Echtzeit-Ausdruckserkennung.

Step 3: Convert to TensorFlow LiteSchritt 3: Konvertierung zu TensorFlow Lite

After training, we convert the model to TF Lite format — a compressed, optimised version that runs efficiently on the Pi without a GPU. The conversion reduces model size by ~4x and inference latency significantly. Nach dem Training konvertieren wir das Modell in das TF Lite-Format — eine komprimierte, optimierte Version, die effizient auf dem Pi ohne GPU läuft. Die Konvertierung reduziert die Modellgröße um ~4x.

Step 4: Deploy on the PiSchritt 4: Deployment auf dem Pi

Python · Real-time inference on Raspberry PiEchtzeit-Inferenz auf Raspberry Pi
import tflite_runtime.interpreter as tflite import cv2, numpy as np, RPi.GPIO as GPIO interp = tflite.Interpreter(model_path="expression_model.tflite") interp.allocate_tensors() classes = ["happy", "neutral", "surprised"] cap = cv2.VideoCapture(0) while True: _, frame = cap.read() img = cv2.resize(frame, (96, 96)) / 255.0 inp = interp.get_input_details() interp.set_tensor(inp[0]['index'], img.reshape(1,96,96,3).astype(np.float32)) interp.invoke() result = interp.get_tensor(interp.get_output_details()[0]['index']) predicted = classes[np.argmax(result)] if predicted == "happy": GPIO.output(17, GPIO.HIGH) # green LED
Student InsightSchüler-Einblick

The moment a student sees the LED light up green because they smiled — and understands that it's their own trained model making that decision — is genuinely transformative. It demystifies AI completely. It's no longer magic; it's maths, data, and code they wrote themselves. Der Moment, in dem ein Schüler sieht, wie die LED grün leuchtet, weil er gelächelt hat — und versteht, dass es sein eigenes trainiertes Modell ist, das diese Entscheidung trifft — ist wirklich transformativ. KI wird völlig entmystifiziert.

Want to build this yourself?Selbst bauen?

Join a MINT Yantra Labs session and turn theory into a working project.Nehmen Sie an einer Session teil und machen Sie Theorie zum Projekt.

🔬 See Programs🔬 Programme✉️ Get in Touch✉️ Kontakt