How to modify a frame from camera (Face recognition, version 5)?
Version 4 opened the camera and showed every frame exactly as it arrived. Version 5 changes that: between reading a frame and displaying it, the program now modifies it. The modification used here is a conversion to gray, but the gray is not the lesson. The lesson is the gap in the middle of the loop, where the frame sits in a variable that can be changed before anyone sees it.
Once that gap exists, the rest of the series is easier to see. Fill it with a colour conversion and the window shows gray. Fill it with the face search from version 3 and the window can show a name. Fill it with a box and the window shows a box. Same camera, same loop, same key press; only the middle line changes.
How the program works
1. Open the camera
import cv2
cap = cv2.VideoCapture(0)
The same two lines as version 4: load OpenCV, connect to the first camera. If the window never appears or stays black, the cause is here, not in the new line — permission or another program using the camera.
2. Read a frame, change it, then show the changed one
while True:
ret, img = cap.read()
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("Frame", gray_img)
Three things happen in order. cap.read() takes one colour frame and stores it in
img. The next line makes a changed copy of it, gray_img. The third
line displays that copy. The original img is left alone; the changed version is
a second variable.
cv2.cvtColor is the same function version 2 used to reorder colour channels. This
time the target is cv2.COLOR_BGR2GRAY: three channels in, one channel out. The
conversion is not a plain average; green is weighted most heavily, then red, then blue, which
is why a red apple and a green apple can end up at similar brightness.
What decides the picture on screen is which variable goes into cv2.imshow. Pass
img and you see the camera unchanged, as in version 4. Pass
gray_img and you see the change. That single choice is the whole mechanism, and
every later version makes it again with a different middle line.
3. Quit, release, close
key = cv2.waitKey(1)
if key in (ord('q'), ord('Q')):
break
cap.release()
cv2.destroyAllWindows()
Unchanged from version 4. waitKey(1) both waits for a key and lets the window
redraw; q or Q breaks the loop from the keyboard;
release and destroyAllWindows tidy up after the loop. Remember to
click the video window before typing, or the key goes to the editor.
The whole program
The pieces above, put back together. This is the complete file, 11 lines in all:
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, img = cap.read()
gray_img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow("Frame", gray_img)
key = cv2.waitKey(1)
if key in (ord('q'), ord('Q')):
break
cap.release()
cv2.destroyAllWindows()
Any change works the same way
Gray was chosen because it is one line and cannot go wrong, but the gap accepts any operation
that returns an image of the same size. Swap the middle line and the window changes; nothing
else in the program moves. Three one-line examples, each in place of the
cvtColor line:
flipped = cv2.flip(img, 1) # mirror image
blurred = cv2.GaussianBlur(img, (9, 9), 0) # soften the picture
brighter = cv2.convertScaleAbs(img, alpha=1.2, beta=20) # raise brightness
Remember to pass the new variable to cv2.imshow, otherwise the window still
shows the original frame and it looks as if nothing happened.
One small point about gray itself: it keeps every shade between black and white, usually 256 of them. Black and white in the strict sense would keep only two, by forcing each pixel to 0 or 255. That operation is called thresholding and discards far more than this program does.
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 face05.py. The first
time in this folder, run all three lines:
uv python pin 3.12.10
uv sync
uv run face05.py
Those three lines are needed only once. The next time you want to run the program again,
type just uv run face05.py.
Try one of the three swaps above before moving on: it is the quickest way to see that the camera part never changes, only the line in the middle.
What comes next
Version 6 fills the same gap with a drawing operation: a red box and a label, at a fixed position. That is practice for version 7, where the box will be placed around a real face and the label will be the person's name.