Learning Goals
3 min- Open a webcam with
VideoCapture. - Run the read → process → show → wait loop.
- Quit cleanly and release the camera.
- Run face detection live on the stream.
Warm-Up · A Video Is a Loop
5 minopen camera
while running:
read a frame (one image / NumPy array)
process the frame (anything from Lessons 30-31)
show the frame
check for quit key
release camera, close windowsReal-time vision is just the single-image pipeline run in a tight loop on each frame. The only new things are opening the camera, the loop, and cleaning up afterwards. Note: this needs a local machine with a webcam — it won't run in Colab.
New Concept · The Capture Loop
14 minThe minimal loop
import cv2 cap = cv2.VideoCapture(0) # 0 = default camera while True: ok, frame = cap.read() # ok=False if the camera fails if not ok: break cv2.imshow("Webcam", frame) # show the frame in a window if cv2.waitKey(1) & 0xFF == ord("q"): # press q to quit break cap.release() cv2.destroyAllWindows()
The pieces explained
VideoCapture(0) open camera index 0 cap.read() returns (success, frame); frame is a BGR array cv2.imshow draws the frame in a named window cv2.waitKey(1) wait 1ms for a key; required for the window to update cap.release() free the camera so other apps can use it
Process each frame
Insert any Lesson 30/31 operation between read and show:
ok, frame = cap.read() gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) edges = cv2.Canny(gray, 80, 160) cv2.imshow("Edges", edges)
Overlay live text (e.g., FPS)
import time prev = time.time() # inside loop: now = time.time(); fps = 1 / (now - prev); prev = now cv2.putText(frame, f"{fps:.0f} FPS", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
Always release
Forgetting cap.release() locks the camera so nothing else can use it (you'll have to restart). Wrap in try/finally if your processing might crash.
Worked Example · Live Face Boxes
12 min# live_faces.py — webcam + real-time face detection import cv2 face_cascade = cv2.CascadeClassifier( cv2.data.haarcascades + "haarcascade_frontalface_default.xml") cap = cv2.VideoCapture(0) try: while True: ok, frame = cap.read() if not ok: break gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) faces = face_cascade.detectMultiScale(gray, 1.2, 5, minSize=(60, 60)) for (x, y, w, h) in faces: cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2) cv2.putText(frame, f"faces: {len(faces)}", (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2) cv2.imshow("Live Faces (press q)", frame) if cv2.waitKey(1) & 0xFF == ord("q"): break finally: cap.release() cv2.destroyAllWindows()
Read the diff
The face-detection code from Lesson 31, now inside the capture loop — boxes follow you in real time. Two robustness touches: a higher scaleFactor (1.2) keeps it fast enough for live video, and the try/finally guarantees the camera is released even if something throws. That's the difference between a demo and a tool.
Try It Yourself
13 minShow your webcam flipped horizontally (selfie-mirror) with cv2.flip(frame, 1).
Press s to save the current frame to a numbered file, q to quit.
Hint
key = cv2.waitKey(1) & 0xFF if key == ord("s"): cv2.imwrite(f"snap_{count}.jpg", frame); count += 1 elif key == ord("q"): break
Press number keys to switch the live filter: 1=normal, 2=gray, 3=edges, 4=blur. Show the active mode as text overlay.
Mini-Challenge · Record a Clip
8 minUse cv2.VideoWriter to record the processed webcam stream (e.g., with face boxes) to an .mp4 file while it plays. Stop recording on q.
Show the writer setup
fourcc = cv2.VideoWriter_fourcc(*"mp4v") w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) writer = cv2.VideoWriter("out.mp4", fourcc, 20.0, (w, h)) # inside loop, after processing: writer.write(frame) # after loop: writer.release()
Non-negotiables: match the writer's frame size to the capture, release the writer too. Now you can save demos of your vision apps.
Recap
3 minWebcam = VideoCapture(0) + a read/process/show/waitKey loop + release at the end. Each frame is a BGR array you can run any Lesson 30/31 operation on. waitKey(1) is mandatory for the window to refresh and read keys. Always release the camera (try/finally). Next: combine it into a smile detector.
Vocabulary Card
- VideoCapture
- OpenCV's camera/video reader; index 0 = default webcam.
- frame
- One image from the video stream — a BGR NumPy array.
- waitKey
- Waits N ms for a keypress; required to refresh the display window.
- release
- Frees the camera/writer so other programs can use it.
Homework
4 minBuild a live webcam app with: a mirror toggle, a filter switcher (normal/gray/edges), live face boxes, an on-screen FPS counter, snapshot on s, and quit on q. Clean release with try/finally.
Combine live_faces.py with the snapshot, FPS, mirror and filter-switch snippets from the exercises into one loop.