📂 Step 1: Install Dependencies on Raspberry Pi
Before we can run inference on the Raspberry Pi, we need to install the required libraries. This step ensures that TensorFlow Lite, OpenCV (for handling images), and NumPy (for data manipulation) are available.
pip install opencv-python tensorflow-lite numpy
📂 Step 2: Run Object Classification in Real-time
Now we will use the Raspberry Pi camera to capture an image and classify it using the trained model. The script will take a photo, preprocess it, and pass it through the TensorFlow Lite model to predict which category it belongs to. The result will be displayed on the screen.
import cv2
import numpy as np
import tensorflow.lite as tflite
# Load the TensorFlow Lite model
interpreter = tflite.Interpreter(model_path="recycling_model.tflite")
interpreter.allocate_tensors()
# Get model input and output details
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
# Define class labels (must match those used during training)
categories = ["paper", "plastic", "glass", "organic"]
# Capture an image from the Raspberry Pi camera
camera = cv2.VideoCapture(0)
ret, frame = camera.read()
camera.release()
if not ret:
print("Error capturing image")
exit()
# Preprocess the image (resize and normalize)
image = cv2.resize(frame, (64, 64))
image = np.expand_dims(image, axis=0).astype('float32') / 255.0
# Run inference with TensorFlow Lite
interpreter.set_tensor(input_details[0]['index'], image)
interpreter.invoke()
# Get the prediction result
predictions = interpreter.get_tensor(output_details[0]['index'])
predicted_class = np.argmax(predictions)
# Display the result
cv2.putText(frame, f"Detected: {categories[predicted_class]}", (10, 30),
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
cv2.imshow("Classification Result", frame)
cv2.waitKey(5000)
cv2.destroyAllWindows()