OpenCV Python Workshop

June 20, 2026 ยท View on GitHub

Build Status OpenCV Python YouTube

Hands-on OpenCV workshop using Python. Covers image processing, feature detection, face recognition, video analysis, object detection, segmentation, and edge deployment.

Workshop by Dr. Farshid Pirahansiah โ€” www.tiziran.com | YouTube

Setup

Quick Install (2025-2026)

# Create virtual environment
python -m venv cv_workshop
source cv_workshop/bin/activate  # Linux/Mac
cv_workshop\Scripts\activate     # Windows

# Install dependencies
pip install -r requirements.txt

Legacy Install (from notebooks)

pip install numpy pandas matplotlib
pip install opencv-python opencv-contrib-python
pip install Pillow bokeh seaborn

Contents

FileDescription
opencv_functions.pyReusable utility functions (cartoon, face detection, display)
opencv_python_visualcode.pyVS Code integration examples
list_files_directories.pyFile/directory listing utilities
mat2numpy.pyOpenCV Mat to NumPy array conversion
read_all_image_in_folder.pyBatch image loading
progress_bar.pyProgress bar utility
yolo_detector.pyYOLOv11 real-time object detection
segmentation.pySAM-2 style image/video segmentation
video_analyzer.pyMotion detection, tracking, optical flow
edge_deploy.pyONNX export, INT8 quantization, benchmarking
augmentation.pyImage augmentation pipeline (Albumentations + OpenCV)
features.pySIFT/ORB/AKAZE, feature matching, panorama stitching
haarcascades/Haar cascade classifiers for face/eye detection
lbpcascades/LBP cascade classifiers (faster, lighter)
tests/Pytest test suite

New Modules (v1.1)

YOLO Detector (yolo_detector.py)

from yolo_detector import YOLODetector

det = YOLODetector(confidence=0.25)
result = det.detect("photo.jpg")
for d in result.detections:
    print(f"{d.class_name}: {d.confidence:.2f} at {d.bbox}")

# Video/webcam
stats = det.detect_webcam(camera_index=0)
stats = det.detect_video("traffic.mp4", output_path="annotated.mp4")

# Export to ONNX
det.export_onnx("yolo11n.onnx")

Segmentation (segmentation.py)

from segmentation import ImageSegmenter

seg = ImageSegmenter("sam2_n")
result = seg.segment("photo.jpg")
overlay = ImageSegmenter.visualize(cv2.imread("photo.jpg"), result)
cv2.imwrite("segmented.jpg", overlay)

# Interactive: click foreground/background points
result = seg.segment_interactive("photo.jpg")

# Video segmentation
stats = seg.segment_video("video.mp4", output_path="seg_out.mp4")

Video Analyzer (video_analyzer.py)

from video_analyzer import VideoAnalyzer

analyzer = VideoAnalyzer("video.mp4")
result = analyzer.detect_motion(method="mog2", min_area=500)
print(f"Motion frames: {result.motion_frames}/{result.total_frames}")

# Object tracking
result = analyzer.track_objects(tracker_type="csrt")

# Optical flow
result = analyzer.compute_optical_flow(method="farneback")

Edge Deployment (edge_deploy.py)

from edge_deploy import EdgeDeployer

deployer = EdgeDeployer("yolo11n.pt")

# Export
onnx_path = deployer.convert_to_onnx("yolo11n.onnx")

# Quantize
result = deployer.quantize_int8("yolo11n.onnx")
print(f"Compression: {result.compression_ratio:.2f}x")

# Benchmark
bench = deployer.benchmark("yolo11n.onnx", num_runs=200)
print(f"Latency: {bench.avg_latency_ms:.1f}ms, FPS: {bench.throughput_fps:.1f}")

# Jetson deployment
deployer.deploy_jetson("yolo11n.onnx", "jetson_deploy/")

Augmentation Pipeline (augmentation.py)

from augmentation import AugmentationPipeline

pipeline = AugmentationPipeline()
pipeline.create_transforms(rotate_limit=45, color_jitter=True)

augmented = pipeline.augment(image, num_variations=5)

# Batch augment directory
pipeline.augment_directory("images/", "augmented/", num_variations=3)

# Visualize pipeline
vis = pipeline.visualize_pipeline(image, num_samples=8, output_path="vis.jpg")

Feature Detection (features.py)

from features import FeatureExtractor

extractor = FeatureExtractor()

# Extract features
kp, desc = extractor.extract_sift(image)

# Match two images
result = match_result = extractor.match_images(img1, img2, method="sift")
vis = extractor.draw_matches(img1, img2, result)

# Panorama stitching
panorama = extractor.panorama_stitch([img1, img2, img3])

Topics Covered

Fundamentals

  • Image I/O (read, write, display)
  • Color space conversion (BGR, HSV, GRAY)
  • Geometric transformations (resize, rotate, warp)
  • Drawing and annotations

Image Processing

  • Filtering (Gaussian, median, bilateral)
  • Thresholding (adaptive, Otsu)
  • Morphological operations
  • Edge detection (Canny, Sobel, Laplacian)
  • Histogram equalization and analysis

Feature Detection & Matching

  • ORB, SIFT, AKAZE feature extraction
  • BFMatcher and FLANN matching
  • Lowe's ratio test
  • Homography estimation (RANSAC)
  • Panorama stitching

Object Detection

  • Haar/LBP cascade face/eye detection
  • HOG pedestrian detection
  • Contour-based object detection
  • YOLOv11 real-time detection with batch support

Segmentation

  • SAM-2 interactive and auto-segmentation
  • Mask visualization and polygon extraction
  • Video frame-by-frame segmentation

Video Processing

  • Webcam capture and processing
  • Background subtraction (MOG2, KNN)
  • Optical flow (Farneback, Lucas-Kanade)
  • Object tracking (CSRT, KCF, MOSSE)
  • FPS and performance statistics

Deep Learning & Edge Deployment

  • ONNX Runtime inference in Python
  • INT8 dynamic quantization
  • Benchmark comparison (latency, memory, throughput)
  • TensorRT export hints
  • Jetson deployment package generation

Data Augmentation

  • Rotation, flip, crop, color jitter
  • Albumentations integration with OpenCV fallback
  • Batch augmentation from directories
  • Dataset export with CSV labels

Modern Python CV Stack (2025-2026)

LibraryUse Case
opencv-pythonCore image processing and CV
ultralyticsYOLOv11 detection/segmentation
onnxruntimeCross-platform DNN inference
albumentationsAdvanced image augmentation
supervisionVideo annotation and tracking
mediapipeFace/hand/pose landmarks
torchvisionPyTorch vision utilities

CLI Usage

Each module can be run standalone:

# YOLO detection
python yolo_detector.py photo.jpg --conf 0.3 --output result.jpg
python yolo_detector.py video.mp4 --output annotated.mp4
python yolo_detector.py 0  # webcam

# Segmentation
python segmentation.py photo.jpg --interactive
python segmentation.py video.mp4 --output seg_out.mp4

# Video analysis
python video_analyzer.py video.mp4 --mode motion --motion-method knn
python video_analyzer.py video.mp4 --mode track --tracker csrt
python video_analyzer.py video.mp4 --mode flow --flow-method farneback

# Edge deployment
python edge_deploy.py yolo11n.pt --convert --quantize yolo11n.onnx
python edge_deploy.py --benchmark yolo11n.onnx --num-runs 200

# Augmentation
python augmentation.py images/ --num 5
python augmentation.py photo.jpg --visualize

# Feature matching
python features.py img1.jpg img2.jpg --method sift --output matches.jpg
python features.py img1.jpg img2.jpg img3.jpg --stitch --output panorama.jpg

Resources

12-Month Roadmap (2025-2026)

MonthMilestoneStatus
Jul 2025Python 3.10+ migration, type hints, pathlibDone
Aug 2025Pytest test suite >80% coverage, CI/CDDone
Sep 2025Docker multi-stage builds, GPU supportDone
Oct 2025YOLOv11 integration, ONNX Runtime examplesDone
Nov 2025SAM 2 segmentation, real-time inferenceDone
Dec 2025Edge deployment guide (Jetson, Raspberry Pi)Done
Jan 2026INT8 quantization, benchmark comparisonDone
Feb 2026Feature matching, panorama stitchingDone
Mar 2026Video analysis (motion, tracking, optical flow)Done
Apr 2026Augmentation pipeline (Albumentations + OpenCV)Done
May 2026Multi-camera system examples, 3D reconstructionPending
Jun 2026v1.0 release, comprehensive documentationPending

Version History

VersionDateChanges
1.1.02026Add YOLO detector, segmentation, video analyzer, edge deploy, augmentation, features
1.0.02025Full modernization: Python 3.10+, type hints, pathlib, pytest, Docker
0.x2019-2024Initial workshop materials

License

See repository for license details.