Building and Understanding How the DIY AI Fruit Classifier Works (Step-by-Step)
Artificial Intelligence (AI) is transforming the world — from recognizing faces on smartphones to identifying fruits in your kitchen! In this beginner-friendly guide, you’ll dive into the exciting world of AI and Machine Learning by creating your own DIY Fruit Classifier using Python and Scikit-learn. This project will help you understand how image classification, data preprocessing, and AI algorithms work together to make smart predictions. Whether you’re a student, educator, or tech enthusiast, this hands-on tutorial will give you real-world exposure to AI, data science, and computer vision — all within Google Colab, step by step.
This mini-project is a great way to understand how data, algorithms, and machine learning come together in the real world. Let’s get started!

🍎 Step 1: Collect Your Dataset
Every AI project starts with data — it’s the foundation of how your model learns.
In this case, you’re collecting images of fruits — apples, bananas, and oranges. The model doesn’t understand “fruit” as humans do; instead, it learns by recognizing patterns in the image pixels.
When you collect around 20 images of each fruit, you’re giving your AI examples of what each fruit looks like from different angles, lighting conditions, and sizes. This diversity helps your model become robust and accurate.
📸 Tip: Create three folders:
dataset/apples
dataset/bananas
dataset/oranges
Once uploaded to Google Colab, Python reads these images and converts them into numerical arrays that represent color and brightness values. That’s the language AI understands — numbers, not pictures.
We’ll need images of apples, bananas, and oranges.
You can easily collect 20 images per fruit using:
- Google Images (search “apple fruit”, “banana fruit”, “orange fruit”)
- Or use a free dataset like Kaggle Fruits Dataset
To save them easily in Google Colab:
!mkdir -p dataset/apples dataset/bananas dataset/oranges
Then upload your images into respective folders.
💻 Step 2: Install Required Libraries
Use the following command in python editor or Google colab.
!pip install scikit-learn opencv-python numpy matplotlib
After data collection, your next step is feature extraction — a process that converts the images into measurable patterns the model can use.
In simple terms:
- The code uses Python libraries like Scikit-learn and OpenCV to resize each image and extract pixel-level details.
- Each image becomes a matrix of numbers (pixels) that reflect how bright or dark each part of the image is.
- These numerical values are flattened into a feature vector, allowing the algorithm to compare one image with another mathematically.
This preprocessing step ensures that every image has the same dimensions and structure, helping the classifier learn consistently.
Think of it like giving the AI “equal-sized flashcards” of apples, bananas, and oranges — it can only compare fairly if all images follow the same pattern.
🤖 Step 3: Load and Process the Images
We’ll convert images into numerical arrays that our AI model can understand.
import cv2
import os
import numpy as np
from sklearn.model_selection import train_test_split
from sklearn.svm import SVC
from sklearn.metrics import accuracy_score
def load_images(folder, label):
data = []
for file in os.listdir(folder):
img_path = os.path.join(folder, file)
img = cv2.imread(img_path)
if img is not None:
img = cv2.resize(img, (64, 64))
data.append((img.flatten(), label))
return data
data = []
data += load_images(‘/content/dataset/apples’, 0)
data += load_images(‘/content/dataset/bananas’, 1)
data += load_images(‘/content/dataset/oranges’, 2)
X, y = zip(*data)
X = np.array(X)
y = np.array(y)
🧩 Step 4: Train the Classifier
Once your data is ready, it’s time to teach your AI model to recognize patterns — this is called training.
Using Scikit-learn, we apply a machine learning algorithm such as Support Vector Machine (SVM) or K-Nearest Neighbors (KNN).
Here’s what happens during training:
- The model looks at all the feature vectors (the numerical data).
- It tries to find patterns that distinguish apples from bananas and oranges.
- Over time, it learns boundaries — mathematical rules that help it decide, for any new image, which fruit it’s most likely to be.
This step is like a teacher showing a student several examples and asking them to spot the differences. The more examples (data) you give, the smarter the model becomes.
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=42)
model = SVC(kernel=’linear’)
model.fit(X_train, y_train)
y_pred = model.predict(X_test)
print(“Accuracy:”, accuracy_score(y_test, y_pred))
You should get an accuracy between 80–95% depending on image quality.
📷 Step 5: Test the Model
Now that your model has “learned,” it’s time to check how well it performs — this is testing.
You’ll provide the model with new images it hasn’t seen before, and ask it to predict which fruit it is.
Here’s how:
- The model compares the new image’s features with what it has learned.
- Based on its training, it predicts a label — apple, banana, or orange.
- The output can be displayed on the screen, showing both the image and the predicted label.
📊 Evaluation:
You can measure performance using metrics like:
- Accuracy — how often it guesses correctly.
- Confusion matrix — helps visualize which fruits are being misclassified.
If accuracy is low, it means your dataset may be too small or not diverse enough. You can fix this by adding more varied images or refining preprocessing.
Upload a new fruit image and test it:
test_img = cv2.imread(‘/content/test_orange.jpg’)
test_img = cv2.resize(test_img, (64, 64)).flatten().reshape(1, -1)
prediction = model.predict(test_img)
labels = [‘Apple’, ‘Banana’, ‘Orange’]
print(“Predicted Fruit:”, labels[prediction[0]])
Making Predictions in Real-Time
This is where the magic happens! Once your model is trained and tested, you can use it to classify fruits in real time — even from your webcam or uploaded images.
In this step:
- The image you upload (say, a new apple) is preprocessed just like training data.
- The model predicts instantly, thanks to the learned rules stored in memory.
- You see the fruit label printed on screen, confirming your AI’s decision.
You’ve now built a miniature AI image classifier — similar to the systems used in agriculture, retail sorting, or even robotic vision systems that recognize products automatically.
🚀 What You Learned
✅ How AI learns from data
✅ How to preprocess and classify images
✅ How to test and evaluate your own model
This project is a small yet powerful glimpse into the world of Artificial Intelligence — where you teach machines to “see” and “think” like humans!
Start building your AI journey today — and don’t forget to share your results in the comments!
Stay tuned to our blog for upcoming posts! Soon, we will be sharing live demonstrations on our official channels, along with detailed video guides to help you learn step-by-step. Be sure to follow us for these valuable updates coming your way soon.