1
+ import cv2
2
+ cap = cv2 .VideoCapture (0 )
3
+ face_cascade = cv2 .CascadeClassifier ("haarcascade_frontalface_alt.xml" )
4
+
5
+ while True :
6
+ ## capture camera frame, and ret store true and false
7
+ r , frame = cap .read ()
8
+ gray_frame = cv2 .cvtColor (frame ,cv2 .COLOR_BGR2GRAY )
9
+
10
+ if r == False :
11
+ continue
12
+ faces = face_cascade .detectMultiScale (gray_frame ,1.3 ,5 )
13
+
14
+ ## """ The first argument is the image, the second is the
15
+ ## scalefactor (how much the image size will be reduced at each image scale),
16
+ ## and the third is the minNeighbors (how many neighbors each rectangle should have)"""
17
+
18
+ for (x ,y ,w ,h ) in faces :
19
+ cv2 .rectangle (frame ,(x ,y ),(x + w ,y + h ),(255 ,125 ,0 ),2 )## color and width
20
+ cv2 .putText (frame ,"DETECTED" ,(x ,y - 10 ),cv2 .FONT_HERSHEY_SIMPLEX ,1 ,(100 ,125 ,255 ),1 ,cv2 .LINE_AA )
21
+ cv2 .imshow ("video frame" ,frame )
22
+ key_pressed = cv2 .waitKey (1 ) & 0xFF
23
+
24
+ ## """cv2.waitKey() returns a 32 Bit integer value (might be dependent on the platform).
25
+ ## The key input is in ASCII which is an 8 Bit integer value. So you only care
26
+ ## about these 8 bits and want all other bits to be 0. This you can achieve with:0xFF"""
27
+
28
+ ### ord converts characters in unicode
29
+ if key_pressed == ord ('n' ):
30
+ break
31
+
32
+ cap .release ()
33
+ cv2 .destroyAllWindows ()
0 commit comments