How to show live video from your camera (Face recognition, version 4)?
Versions 1 to 3 work on photographs that are already on disk. To recognise the person sitting in front of your computer, the program needs a supply of new pictures, and the camera is that supply: it delivers one still frame after another, many times a second. Version 4 builds that supply. It opens the camera and shows the video, and it does nothing else. No face is found, no name is printed.
That may look like a step backwards, but it separates two problems. If recognition fails in version 7, this program tells you whether the camera part was ever working. If you have read How to get live video from your webcam?, most of this note will be familiar; here it is placed in the series, because every later version adds its work to this loop.
The camera sends frames, not a video
Your eye sees smooth movement. The program receives separate still pictures, each replacing the one before quickly enough that the change is invisible. That is useful: a frame is the same kind of object as a photo file, so everything you learned about measuring a face in a photo applies unchanged to a frame.
How the program works
1. Open the camera
import cv2
cap = cv2.VideoCapture(0)
cv2.VideoCapture connects to a camera; cap is short for capture and
holds that connection. The 0 means the first camera, normally the built-in
webcam; 1 would be a second camera, such as a USB one.
On a Mac the system asks for permission the first time, and the program shows nothing until you allow it. If the window stays black, check the camera permission for your terminal or editor in System Settings, and check that no other program is already using the camera.
2. Repeat without end
while True:
ret, img = cap.read()
cv2.imshow("Frame", img)
while True starts a loop that never stops by itself, because
True is always true. Everything indented under it runs once per frame, for as
long as the program runs.
cap.read() takes one frame and returns two things. ret, short for
return value, says whether the read worked. img, short for image, is the frame:
a grid of pixel values, in the blue-green-red order that OpenCV uses.
cv2.imshow puts that frame in a window titled "Frame", and the next
pass of the loop replaces it. Show enough frames per second and the window looks like video.
3. Stop when q is pressed
key = cv2.waitKey(1)
if key in (ord('q'), ord('Q')):
break
cv2.waitKey(1) waits one millisecond for a key press. It returns the key as a
number, or -1 if nothing was pressed. That one millisecond is also what gives
OpenCV time to draw the window; without a call to waitKey the window would appear
frozen or empty.
ord() turns a letter into its number, ord('q') being 113, so a
letter can be compared with the number that waitKey returned. Checking both
q and Q means Caps Lock makes no difference.
break leaves the loop.
The key press only reaches the program if the video window is the active window. After you
run it, the editor's terminal usually still has the focus, so click once on the video window
and then type q. If you forget, Ctrl and C together in
the terminal stops the program as well.
4. Let go of the camera
cap.release()
cv2.destroyAllWindows()
These two lines are not indented, so they run once after the loop ends.
cap.release() disconnects from the camera, turns off its light, and lets other
programs use it. 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()
Why one millisecond
waitKey(0) would wait until a key is pressed, and the picture would never
update. waitKey(30) would update roughly 33 times a second, which looks smooth
but adds delay. One millisecond keeps the window responsive and lets the loop run as fast as
the camera allows.
Running it on your own computer
Use the same download as version 1:
Download the zip file, unzip it,
and open the folder in Visual Studio Code. This note uses face04.py. The first
time in this folder, run all three lines:
uv python pin 3.12.10
uv sync
uv run face04.py
Those three lines are needed only once. The next time you want to run the program again,
type just uv run face04.py.
A window opens with your camera picture. Click it once, then press q to close
it. The camera light should go out at the same moment.
What comes next
The next two versions stay inside this loop. Version 5 turns each frame grey, and version 6 draws a box and a label on it, so that when recognition arrives in version 7 there is already a place to show the result.