OpenCV (Open Source Computer Vision Library) is an extensive
collection of algorithms for real‑time image & video
processing. Python bindings (cv2
module) offer a clean,
readable API that integrates seamlessly with the broader
NumPy / SciPy ecosystem.
Note ▸ Use the opencv-python
wheel for most projects.
The opencv-contrib-python
wheel includes extra
“contrib” modules (e.g. SIFT, SURF, aruco).
# Terminal — latest stable OpenCV 4.x
pip install opencv-python
# Extra modules
pip install opencv-contrib-python
import cv2
img = cv2.imread("lenna.png", cv2.IMREAD_COLOR) # BGR 8‑bit
cv2.imshow("Lenna (BGR)", img)
cv2.waitKey(0)
cv2.imwrite("lenna_gray.jpg",
cv2.cvtColor(img, cv2.COLOR_BGR2GRAY))
import cv2, time
cap = cv2.VideoCapture(0) # webcam 0
four = cv2.VideoWriter_fourcc(*"mp4v")
size = (int(cap.get(3)), int(cap.get(4)))
out = cv2.VideoWriter("capture.mp4", four, 30, size)
while cap.isOpened():
ret, frame = cap.read()
if not ret: break
out.write(frame) # save
cv2.imshow("Live", frame) # preview
if cv2.waitKey(1) == 27: # ESC ↯
break
cap.release(); out.release(); cv2.destroyAllWindows()
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
small = cv2.resize(img, (0, 0), fx=0.25, fy=0.25,
interpolation=cv2.INTER_AREA)
face = img[80:260, 70:210] # simple crop
annot = img.copy()
cv2.rectangle(annot, (70, 80), (210, 260), (0,255,0), 2)
cv2.putText(annot, "Face", (70, 75),
cv2.FONT_HERSHEY_SIMPLEX, 0.7,
(0,255,0), 2, cv2.LINE_AA)
Haar cascades are classic but still useful for low‑power demos. A pre‑trained XML file is shipped with OpenCV.
import cv2
detector = cv2.CascadeClassifier(
cv2.data.haarcascades + "haarcascade_frontalface_default.xml")
cam = cv2.VideoCapture(0)
while True:
ret, frame = cam.read()
if not ret: break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = detector.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(frame, (x,y), (x+w,y+h), (255,0,0), 2)
cv2.imshow("Haar Face ↯ ESC to exit", frame)
if cv2.waitKey(1) == 27:
break
cam.release(); cv2.destroyAllWindows()
For modern accuracy‑speed trade‑offs choose a YOLO model. Either use Ultralytics pip install ultralytics wrapper or load ONNX weights directly with cv2.dnn.readNet.
from ultralytics import YOLO # pip install ultralytics
import cv2, numpy as np
model = YOLO("yolov8n.pt") # nano model < 5 MB
cap = cv2.VideoCapture(0)
while True:
ok, frame = cap.read()
if not ok: break
results = model(frame, imgsz=640)[0] # predict
annotated = results.plot() # draw boxes
cv2.imshow("YOLOv8 Live", annotated)
if cv2.waitKey(1) == 27: break
cap.release(); cv2.destroyAllWindows()
Note ▸ OpenCV’s cv2.dnn can import ONNX, TensorFlow, Caffe, and Darknet formats with the same API.