Showing posts with label Image Classification. Show all posts
Showing posts with label Image Classification. Show all posts

Computer Vision with Python: Analyze Images Using OpenCV and Deep Learning

 


๐Ÿง  Part 7: Computer Vision with OpenCV and Deep Learning


๐Ÿ‘️ What Is Computer Vision?

Computer Vision (CV) enables machines to “see” and understand images or videos. It’s used in:

  • Face detection

  • Object recognition

  • Medical imaging

  • Self-driving cars


๐Ÿงฐ Tools We'll Use

  • OpenCV – Image processing library

  • TensorFlow/Keras – Deep learning models (CNNs)

  • Pre-trained Models – For fast and accurate image classification

Install with:

pip install opencv-python tensorflow

๐Ÿ–ผ️ Step-by-Step: Image Classification with a CNN


๐Ÿ—‚️ Step 1: Load & Preprocess the CIFAR-10 Dataset

import tensorflow as tf
from tensorflow.keras.datasets import cifar10
import matplotlib.pyplot as plt

(X_train, y_train), (X_test, y_test) = cifar10.load_data()
X_train, X_test = X_train / 255.0, X_test / 255.0

# Show a sample image
plt.imshow(X_train[0])
plt.title(f"Label: {y_train[0][0]}")
plt.show()

๐Ÿง  Step 2: Build a Convolutional Neural Network (CNN)

model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(32, (3,3), activation='relu', input_shape=(32,32,3)),
    tf.keras.layers.MaxPooling2D((2,2)),
    tf.keras.layers.Conv2D(64, (3,3), activation='relu'),
    tf.keras.layers.MaxPooling2D((2,2)),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(64, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])

⚙️ Step 3: Compile & Train the Model

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

model.fit(X_train, y_train, epochs=10, validation_split=0.2)

๐Ÿ“ˆ Step 4: Evaluate and Predict

test_loss, test_acc = model.evaluate(X_test, y_test)
print(f"Test Accuracy: {test_acc:.2f}")

predictions = model.predict(X_test)
print("Predicted class:", predictions[0].argmax())

๐Ÿงฐ Bonus: Load and Process Custom Images with OpenCV

import cv2

image = cv2.imread('your_image.jpg')
resized = cv2.resize(image, (32, 32)) / 255.0
reshaped = resized.reshape(1, 32, 32, 3)

prediction = model.predict(reshaped)
print("Predicted class:", prediction.argmax())

๐Ÿ”„ Alternative: Use Pretrained Model (MobileNet)

from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input, decode_predictions

model = MobileNetV2(weights='imagenet')
from tensorflow.keras.preprocessing import image
import numpy as np

img = image.load_img('your_image.jpg', target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

preds = model.predict(x)
print("Predicted:", decode_predictions(preds, top=1)[0])

๐Ÿงช Practice Challenge

  • Try using other datasets like Fashion MNIST or CelebA

  • Detect faces using cv2.CascadeClassifier

  • Implement edge detection with OpenCV


๐ŸŽ“ What You’ve Learned:

  • Image classification using CNNs

  • Using OpenCV for image processing

  • How to use pre-trained models for instant predictions


๐Ÿงญ What’s Next?

In Part 8, we’ll explore Reinforcement Learning (RL)—where agents learn to make decisions through trial and error using rewards and penalties.



Deep Learning with Python: Build Neural Networks Using TensorFlow and Keras

 

← Back to Home

๐Ÿง  Part 5: Deep Learning and Neural Networks with TensorFlow and Keras


๐Ÿ” What Is Deep Learning?

Deep Learning is a subfield of machine learning that uses artificial neural networks—inspired by the human brain—to recognize patterns and make decisions.

It's especially effective in handling:

  • Images

  • Audio

  • Text

  • Complex data with high dimensionality


๐Ÿง  What Is a Neural Network?

A neural network is made up of layers of interconnected "neurons":

  • Input Layer – takes in raw data (e.g., pixels)

  • Hidden Layers – extract patterns using weights and activation functions

  • Output Layer – makes predictions (e.g., class label)


๐Ÿš€ Setting Up TensorFlow and Keras

Install TensorFlow (Keras is included):

pip install tensorflow

๐Ÿ“Š Project: Image Classification with MNIST Dataset

The MNIST dataset is a set of 70,000 handwritten digits (0–9), perfect for beginners.


✅ Step 1: Load Data

import tensorflow as tf
from tensorflow.keras.datasets import mnist

(X_train, y_train), (X_test, y_test) = mnist.load_data()

๐Ÿงผ Step 2: Preprocess Data

# Normalize pixel values to [0, 1]
X_train = X_train / 255.0
X_test = X_test / 255.0

๐Ÿง  Step 3: Build the Neural Network

model = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(28, 28)),   # Input layer
    tf.keras.layers.Dense(128, activation='relu'),   # Hidden layer
    tf.keras.layers.Dense(10, activation='softmax')  # Output layer
])

๐Ÿ› ️ Step 4: Compile the Model

model.compile(optimizer='adam',
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])

๐ŸŽฏ Step 5: Train the Model

model.fit(X_train, y_train, epochs=5)

๐Ÿ“ˆ Step 6: Evaluate Performance

test_loss, test_acc = model.evaluate(X_test, y_test)
print(f"Test accuracy: {test_acc:.2f}")

๐Ÿ”ฎ Step 7: Make Predictions

predictions = model.predict(X_test)
import numpy as np

# Predict and show the first test digit
print("Predicted digit:", np.argmax(predictions[0]))

๐Ÿ’ก Practice Challenge

Try changing the network architecture:

  • Add another hidden layer

  • Use different activation functions (sigmoid, tanh)

  • Increase or decrease the number of neurons

# Add more layers and experiment

๐ŸŽ“ What You’ve Learned:

  • What neural networks are and how they work

  • How to build, train, and evaluate a deep learning model using Keras

  • How to classify images with high accuracy


๐Ÿงญ What’s Next?

In Part 6, we’ll explore Natural Language Processing (NLP) using Python. You’ll learn how to process text, analyze sentiment, and even build a basic chatbot.


Featured Post

Extra Challenge: Using References Between Documents

  ๐ŸŽฏ ๐Ÿ’ก Extra Challenge: Using References Between Documents Here's an  Extra Challenge  designed to build on the original MongoDB mode...

Popular Posts