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

← Back to Home

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 shall 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.