intermediate12 min·ai

Computer Vision — How Machines See the World

Learn how computer vision works with Python examples. Understand image recognition, object detection, CNNs, and real-world applications of computer vision AI.

What is Computer Vision?

Computer Vision (CV) is a field of AI that enables machines to interpret and understand visual information from the world — images, videos, and real-time camera feeds. It’s the technology behind facial recognition, self-driving cars, and medical imaging.

Core Computer Vision Tasks

Image Classification

Determining what is in an image:

import torch
import torch.nn as nn
import torchvision.models as models
import torchvision.transforms as transforms
from PIL import Image

# Load a pre-trained image classifier
def classify_image(image_path):
    """Classify an image using a pre-trained ResNet model"""
    model = models.resnet18(pretrained=True)
    model.eval()

    # Preprocess the image
    transform = transforms.Compose([
        transforms.Resize(256),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                           std=[0.229, 0.224, 0.225]),
    ])

    image = Image.open(image_path)
    input_tensor = transform(image).unsqueeze(0)

    # Make prediction
    with torch.no_grad():
        output = model(input_tensor)
        probabilities = torch.nn.functional.softmax(output[0], dim=0)

    # Get top 5 predictions
    with open('imagenet_classes.txt') as f:
        classes = [line.strip() for line in f.readlines()]

    top5_prob, top5_idx = torch.topk(probabilities, 5)
    for prob, idx in zip(top5_prob, top5_idx):
        print(f"  {classes[idx]}: {prob:.1%}")

# classify_image('photo.jpg')

Object Detection

Finding and localizing objects within an image:

# Object detection concepts
def object_detection_concept():
    """
    Object detection finds objects AND their locations in an image.
    Output: List of (class, confidence, bounding_box)
    """

    # YOLO-style detection output
    detections = [
        {"class": "person", "confidence": 0.95,
         "bbox": [100, 50, 400, 500]},  # [x, y, width, height]
        {"class": "car", "confidence": 0.88,
         "bbox": [500, 200, 300, 250]},
        {"class": "dog", "confidence": 0.82,
         "bbox": [50, 300, 150, 200]},
    ]

    for det in detections:
        print(f"Detected: {det['class']} ({det['confidence']:.0%}) "
              f"at position {det['bbox']}")

object_detection_concept()

Image Segmentation

Classifying every pixel in an image:

import torch
import torch.nn as nn

class SimpleSegmentationNet(nn.Module):
    """U-Net style architecture for image segmentation"""

    def __init__(self, in_channels=3, num_classes=21):
        super().__init__()

        # Encoder (downsampling)
        self.enc1 = self._block(in_channels, 64)
        self.enc2 = self._block(64, 128)
        self.enc3 = self._block(128, 256)

        self.pool = nn.MaxPool2d(2)

        # Bottleneck
        self.bottleneck = self._block(256, 512)

        # Decoder (upsampling)
        self.up3 = nn.ConvTranspose2d(512, 256, 2, stride=2)
        self.dec3 = self._block(512, 256)
        self.up2 = nn.ConvTranspose2d(256, 128, 2, stride=2)
        self.dec2 = self._block(256, 128)
        self.up1 = nn.ConvTranspose2d(128, 64, 2, stride=2)
        self.dec1 = self._block(128, 64)

        # Output: one channel per class
        self.output = nn.Conv2d(64, num_classes, 1)

    def _block(self, in_ch, out_ch):
        return nn.Sequential(
            nn.Conv2d(in_ch, out_ch, 3, padding=1),
            nn.BatchNorm2d(out_ch),
            nn.ReLU(inplace=True),
            nn.Conv2d(out_ch, out_ch, 3, padding=1),
            nn.BatchNorm2d(out_ch),
            nn.ReLU(inplace=True),
        )

    def forward(self, x):
        # Encode
        e1 = self.enc1(x)
        e2 = self.enc2(self.pool(e1))
        e3 = self.enc3(self.pool(e2))

        # Bottleneck
        b = self.bottleneck(self.pool(e3))

        # Decode with skip connections
        d3 = self.up3(b)
        d3 = torch.cat([d3, e3], dim=1)
        d3 = self.dec3(d3)

        d2 = self.up2(d3)
        d2 = torch.cat([d2, e2], dim=1)
        d2 = self.dec2(d2)

        d1 = self.up1(d2)
        d1 = torch.cat([d1, e1], dim=1)
        d1 = self.dec1(d1)

        return self.output(d1)

# Create model
model = SimpleSegmentationNet(num_classes=21)  # 21 classes (Pascal VOC)
test_input = torch.randn(1, 3, 256, 256)
output = model(test_input)
print(f"Input: {test_input.shape}")    # [1, 3, 256, 256]
print(f"Output: {output.shape}")        # [1, 21, 256, 256]

Computer Vision with OpenCV

import cv2
import numpy as np

def cv_basics():
    """Essential OpenCV operations for computer vision"""

    # Create a sample image
    image = np.zeros((400, 600, 3), dtype=np.uint8)
    cv2.rectangle(image, (50, 50), (200, 200), (0, 255, 0), 2)
    cv2.circle(image, (400, 200), 80, (255, 0, 0), -1)
    cv2.putText(image, "OpenCV", (150, 350),
                cv2.FONT_HERSHEY_SIMPLEX, 1.5, (255, 255, 255), 3)

    # Convert to grayscale
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    # Edge detection (Canny)
    edges = cv2.Canny(gray, 50, 150)

    # Find contours
    contours, _ = cv2.findContours(
        edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE
    )
    print(f"Found {len(contours)} objects in the image")

    # Resize
    resized = cv2.resize(image, (300, 200))
    print(f"Resized from {image.shape} to {resized.shape}")

cv_basics()

Real-Time Object Detection with YOLO

# YOLO (You Only Look Once) — real-time object detection
# pip install ultralytics

def yolo_detection_concept():
    """How YOLO-style detection works"""
    print("YOLO Object Detection Pipeline:")
    print("1. Resize input image to fixed size (e.g., 640x640)")
    print("2. Divide image into grid cells")
    print("3. Each cell predicts bounding boxes + class probabilities")
    print("4. Apply Non-Maximum Suppression (NMS) to remove duplicates")
    print("5. Output final detections with confidence scores")
    print()
    print("Speed: ~30-100 FPS on modern GPUs")
    print("Accuracy: mAP 50-60% on COCO dataset")

# from ultralytics import YOLO
# model = YOLO('yolov8n.pt')
# results = model('image.jpg')
# results[0].show()
yolo_detection_concept()

Applications of Computer Vision

# Industry applications of computer vision
applications = {
    "Healthcare": [
        "X-ray analysis for pneumonia detection",
        "Retinal scan for diabetic retinopathy",
        "Pathology slide analysis",
    ],
    "Automotive": [
        "Lane detection for self-driving",
        "Pedestrian detection",
        "Traffic sign recognition",
    ],
    "Manufacturing": [
        "Quality control defect detection",
        "Assembly line inspection",
        "Robotic guidance",
    ],
    "Retail": [
        "Cashier-less checkout (Amazon Go)",
        "Customer behavior analysis",
        "Inventory monitoring",
    ],
    "Agriculture": [
        "Crop disease detection",
        "Weed identification",
        "Yield prediction from drone imagery",
    ],
}

for industry, use_cases in applications.items():
    print(f"\n{industry}:")
    for use_case in use_cases:
        print(f"  • {use_case}")

Performance Optimization

# Optimizing computer vision models for production
def optimization_techniques():
    """Key techniques for deploying CV models efficiently"""

    techniques = {
        "Model Quantization": {
            "description": "Reduce precision from float32 to int8",
            "speedup": "2-4x faster",
            "accuracy_loss": "~1-2%",
        },
        "Model Pruning": {
            "description": "Remove redundant weights/neurons",
            "speedup": "1.5-3x faster",
            "accuracy_loss": "~1-3%",
        },
        "Knowledge Distillation": {
            "description": "Train small model to mimic large model",
            "speedup": "5-10x faster",
            "accuracy_loss": "~3-5%",
        },
        "TensorRT Optimization": {
            "description": "GPU-specific optimization by NVIDIA",
            "speedup": "3-8x faster",
            "accuracy_loss": "~0.5-1%",
        },
    }

    for name, info in techniques.items():
        print(f"\n{name}:")
        print(f"  {info['description']}")
        print(f"  Speedup: {info['speedup']}")
        print(f"  Accuracy loss: {info['accuracy_loss']}")

optimization_techniques()

Frequently Asked Questions

What is computer vision in simple terms?

Computer vision is a field of AI that gives machines the ability to “see” and understand visual information from images and videos, similar to how humans interpret what they see.

What is the difference between image classification and object detection?

Image classification tells you what’s in an image (one label for the whole image). Object detection tells you what’s in the image AND where each object is located (with bounding boxes).

What is the best library for computer vision in Python?

OpenCV is the most popular library for general computer vision tasks. For deep learning-based CV, use PyTorch with torchvision or TensorFlow with tf.keras. For object detection, Ultralytics YOLO is excellent.

How fast is real-time object detection?

Modern YOLO models run at 30-100+ FPS on GPUs, making real-time detection feasible. On edge devices like Jetson Nano, optimized models achieve 15-30 FPS.


Want to build computer vision projects? Join our Telegram Community for CV project tutorials, dataset resources, and discussions with ML engineers.