How to get live video from your webcam?
The camera on your computer is the starting point for anything that has to look at the real world and then act on it. The lipstick example shows how: a video is many still frames arriving one after another. Find the face in each frame, put lipstick on it, and show that frame before moving on to the next. Do that for every frame, and the result is a moving face with lipstick on it.
The program below is the foundation: 10 lines that open your webcam and show the live video in
a window, until you press q or Q. It contains no AI. It only gets
frames from the camera and shows them, deliberately, so that the camera part is clear before
AI is added on top.
What your eye sees and what the program sees
Your eye sees a smooth moving image. The program sees a long series of separate frames, each replacing the one before so quickly that the change is invisible.
How the program works
1. Turn on the camera
import cv2
cap = cv2.VideoCapture(0)
cv2 is OpenCV (Open Source Computer Vision), the library used
throughout this series to open photos and video. The first line loads it.
The second line connects to a camera and keeps that connection in cap, short for
capture. The 0 means the first camera on the computer, which is usually the built-in
webcam. On a Mac, the computer asks for permission the first time you run it; on Windows, check
that camera access is allowed in Settings if nothing appears.
2. Read one frame at a time
while True:
ret, img = cap.read()
cv2.imshow("Frame", img)
The first line starts a loop that never stops by itself, because True is always
true. Everything indented under it runs again and again, once per frame.
cap.read() takes one frame. ret (return) says whether a frame was
read, and img (image) is the frame itself: a grid of pixel values. That
img is the frame you would process in any camera-based program — exactly
like the photo files in the earlier notes.
cv2.imshow shows the frame in a window titled "Frame". Each new frame
replaces the previous one, so the window looks like live video.
3. Stop when you press q
key = cv2.waitKey(1)
if key in (ord('q'), ord('Q')):
break
cv2.waitKey(1) waits 1 millisecond for a key press, then continues.
key is a code that tells you which key was pressed, or -1 if no key
was pressed. This short wait also gives the window time to draw the frame, which is why the
video appears at all.
The next line checks for q or Q. ord() turns a letter
into its number — ord('q') is 113 — so a letter can be compared with
the number in key. Testing both means Caps Lock makes no difference.
break leaves the loop and moves on to the last two lines. After you run the
program, the terminal in Visual Studio Code is still the selected window, so a q or
Q you type goes to Visual Studio Code, not to the program that shows the video.
Move your mouse pointer over the video window and click once to select it. Then type
q or Q to end the program.
There is also a quicker way to stop a running program: in the Visual Studio Code terminal,
press the two keys Ctrl and C at the same time.
4. Turn the camera off
cap.release()
cv2.destroyAllWindows()
cap.release() disconnects from the camera, so other programs can use it again and
the camera light goes out. cv2.destroyAllWindows() closes the window.
The whole program
The pieces above, put back together. This is the complete file, 10 lines in all:
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, img = cap.read()
cv2.imshow("Frame", img)
key = cv2.waitKey(1)
if key in (ord('q'), ord('Q')):
break
cap.release()
cv2.destroyAllWindows()
Save one frame
Many jobs need a single frame, not a stream: take one frame, then analyse it. You do not need
a different program for that. Inside the loop, img is already one frame, so one
extra line saves it to a file:
cv2.imwrite("camera_frame.jpg", img)
Add that line just after cap.read(), and the program writes a new file for every
frame. To keep just one, add it above the break line instead, so the file is saved
when you press q: what you see on screen at that moment is what you get.
Once the frame is a file, everything in the earlier notes applies to it — including
face_recognition.load_image_file in
How does AI find the key points on your face?.
Running it on your own computer
Download the zip file and unzip it to
a folder. The zip holds all six programs in this series; this note uses
face05.py. This one does not need a photo in the folder. Open the folder in Visual
Studio Code, then type these three lines in the terminal:
uv python pin 3.12.10
uv sync
uv run face05.py
The first line records which Python version this folder uses, and the second installs every
package that pyproject.toml lists, including OpenCV. The third line opens the
camera window. Press q in that window to close it.
If you have not set up uv yet, start with How to install Visual Studio Code? and then the uv note for Windows or Mac.
What comes next
The camera is only the eye; the thinking comes next, in the note that adds the AI on top.