Computer Vision Mastery

1. Image Processing Basics

Learn fundamental image operations using OpenCV.

# Basic image operations with OpenCV
import cv2
import numpy as np

# Load and display image
img = cv2.imread('image.jpg')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

# Apply filters
blurred = cv2.GaussianBlur(gray, (15, 15), 0)
edges = cv2.Canny(gray, 50, 150)

2. Object Detection

Detect and classify objects in images using YOLO and other algorithms.

# Object detection with YOLO
import cv2

# Load YOLO model
net = cv2.dnn.readNet('yolov3.weights', 'yolov3.cfg')
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]

# Detect objects
blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
net.setInput(blob)
outputs = net.forward(output_layers)

3. Face Recognition

Build facial recognition systems for security applications.

# Face detection and recognition
import face_recognition

# Load and encode known faces
known_image = face_recognition.load_image_file("person.jpg")
known_encoding = face_recognition.face_encodings(known_image)[0]

# Find faces in new image
unknown_image = face_recognition.load_image_file("unknown.jpg")
face_locations = face_recognition.face_locations(unknown_image)
face_encodings = face_recognition.face_encodings(unknown_image, face_locations)

# Compare faces
matches = face_recognition.compare_faces([known_encoding], face_encodings[0])

4. Real-time Video Processing

Process video streams and build real-time computer vision applications.

# Real-time video processing
cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    
    # Apply computer vision algorithms
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.1, 4)
    
    # Draw rectangles around faces
    for (x, y, w, h) in faces:
        cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
    
    cv2.imshow('Video', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()