📸 Step 1: Setting Up the Camera
Begin by setting up the Pi Camera or USB webcam. Ensure it is enabled in system settings and test it using Python:
import cv2
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
cv2.imshow('Camera Feed', frame)
Ensure the camera image is clear and the frame rate is acceptable for real-time use.
📁 Step 2: Load the Trained Model
Use a trained .tflite
model that can classify a few specific objects — for example: apple, bottle, pencil, etc. Use TensorFlow Lite interpreter to load and prepare it:
import tflite_runtime.interpreter as tflite
interpreter = tflite.Interpreter(model_path="object_model.tflite")
interpreter.allocate_tensors()
🖼️ Step 3: Image Preprocessing
Resize and normalize camera frames to match the model input requirements:
resized = cv2.resize(frame, (224, 224))
input_data = np.expand_dims(resized, axis=0).astype(np.float32) / 255.0
interpreter.set_tensor(input_details[0]['index'], input_data)
interpreter.invoke()
📊 Step 4: Interpret Results
Get prediction values and identify the most probable class:
output_data = interpreter.get_tensor(output_details[0]['index'])
predicted_index = np.argmax(output_data)
object_labels = ["Apple", "Bottle", "Pencil"]
print("Detected:", object_labels[predicted_index])
🤖 Step 5: Take Action Based on Prediction
Now that your robot can recognize objects, trigger different outputs:
- Turn on specific LEDs for different objects
- Move a servo to drop the item into a specific bin
- Display results on an LCD or log to a file
Example: Sorting objects with servo rotation:
if predicted_index == 0:
servo.angle = 30 # Apple
elif predicted_index == 1:
servo.angle = 90 # Bottle
else:
servo.angle = 150 # Pencil
🧪 Testing and Troubleshooting
- Ensure lighting is consistent to avoid misclassification
- Test with real-world objects that match your training images
- Calibrate servo angles and GPIO outputs before final deployment
✅ Outcomes and Learnings
By completing this project, you’ve successfully connected machine learning predictions to physical robotic actions. This end-to-end implementation demonstrates the real power of AI in robotics — perception, classification, and interaction. You’re now ready to take on more complex applications in smart automation and robotics intelligence.