How to draw a box and a label on a video frame (Face recognition, version 6)?
Version 5 proved that a frame can be changed before it is shown. Version 6 uses that gap to draw: a red rectangle and the words "This is a test", fixed in one place on the picture. The box does not follow anything yet. It is there so that version 7 has somewhere to put its result: the box will move onto a face, and the caption will become a name.
Where the corners are
An image has no bottom-left origin. Positions are counted in pixels from the top-left
corner: x grows to the right and y grows down.
That is the opposite of a graph drawn in mathematics, and it is the single most common reason
a box appears somewhere unexpected.
How the program works
1. Open the camera and read a frame
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, img = cap.read()
Unchanged from version 4. Each pass of the loop gives a fresh frame in img,
which is what makes the drawing safe: whatever is drawn now disappears with the frame, and
the next frame arrives clean.
2. Choose the two corners
x1, y1 = 100, 200
x2, y2 = x1 + 200, y1 + 150
The first line is the top-left corner: 100 pixels from the left edge, 200 from the top. The second line works out the bottom-right corner from a size instead of a position: 200 pixels wide and 150 tall, giving (300, 350). Writing it this way makes the size easy to change later.
3. Draw the rectangle
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
The arguments are the image, the two corners, a colour and a thickness. The colour is a triple
in BGR order, not RGB: (0, 0, 255) is no blue, no green, full
red. The final 2 is the line width in pixels; a larger number gives a fatter
frame around the box.
4. Write the label
cv2.putText(img, "This is a test", (x1, y1 - 10), cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 255), 2)
Seven arguments, in this order: the image, the text, the position, the font, the size, the
colour, and the thickness. The position is the bottom-left corner of the text,
not its top-left, so y1 - 10 lifts the words ten pixels above the top edge of the
box instead of letting them overlap it.
cv2.FONT_HERSHEY_DUPLEX is one of the simple fonts built into OpenCV, and the
1 is a size multiplier, so 2 would double it. The colour and
thickness mean the same as in the rectangle call.
5. Show it, then quit and tidy up
cv2.imshow("Frame", img)
key = cv2.waitKey(1)
if key in (ord('q'), ord('Q')):
break
cap.release()
cv2.destroyAllWindows()
cv2.imshow now receives img — the frame the drawing went onto
— so the box appears in the window. The key press and the clean-up are the same as in
version 4.
The whole program
The pieces above, put back together. This is the complete file, 14 lines in all:
import cv2
cap = cv2.VideoCapture(0)
while True:
ret, img = cap.read()
x1, y1 = 100, 200
x2, y2 = x1 + 200, y1 + 150
cv2.rectangle(img, (x1, y1), (x2, y2), (0, 0, 255), 2)
cv2.putText(img, "This is a test", (x1, y1 - 10), cv2.FONT_HERSHEY_DUPLEX, 1, (0, 0, 255), 2)
cv2.imshow("Frame", img)
key = cv2.waitKey(1)
if key in (ord('q'), ord('Q')):
break
cap.release()
cv2.destroyAllWindows()
The drawing changes the frame itself
Version 5 made a second variable and left the original alone. This version is different:
cv2.rectangle and cv2.putText change img directly and
return nothing useful. There is no img_with_box.
That is harmless here for one reason: the frame is thrown away at the end of each pass and replaced by a new one at the top of the loop, so the box is drawn once per frame on a clean picture. It never accumulates. If you drew on a saved photograph instead and wrote it back to disk, every run would leave another box behind.
Moving the box
Change 100, 200 and the box moves; change 200, 150 and it resizes.
Values outside the frame are not an error: the part of the box that falls outside is simply
not drawn, which is worth knowing before version 7 hands the program boxes that touch the edge
of the picture.
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 face06.py. The first
time in this folder, run all three lines:
uv python pin 3.12.10
uv sync
uv run face06.py
Those three lines are needed only once. The next time you want to run the program again,
type just uv run face06.py.
A red box with "This is a test" above it should sit near the upper-left of the video. If the
box is off-screen, check which corner you changed: y1 larger than the frame
height pushes it out of the bottom.
What comes next
Version 7 does the work of version 3 inside this loop: it finds faces in each frame, draws the box around a face instead of at a fixed spot, and puts the recognised name where the test label is now.