99import cv2
1010
1111# gstreamer_pipeline returns a GStreamer pipeline for capturing from the CSI camera
12- # Defaults to 1280x720 @ 30fps
12+ # Defaults to 1280x720 @ 30fps
1313# Flip the image by setting the flip_method (most common values: 0 and 2)
1414# display_width and display_height determine the size of the window on the screen
1515
16- def gstreamer_pipeline (capture_width = 3280 , capture_height = 2464 , display_width = 820 , display_height = 616 , framerate = 21 , flip_method = 0 ) :
17- return ('nvarguscamerasrc ! '
18- 'video/x-raw(memory:NVMM), '
19- 'width=(int)%d, height=(int)%d, '
20- 'format=(string)NV12, framerate=(fraction)%d/1 ! '
21- 'nvvidconv flip-method=%d ! '
22- 'video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! '
23- 'videoconvert ! '
24- 'video/x-raw, format=(string)BGR ! appsink' % (capture_width ,capture_height ,framerate ,flip_method ,display_width ,display_height ))
25-
26- def face_detect () :
27- face_cascade = cv2 .CascadeClassifier ('/usr/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml' )
28- eye_cascade = cv2 .CascadeClassifier ('/usr/share/OpenCV/haarcascades/haarcascade_eye.xml' )
16+
17+ def gstreamer_pipeline (
18+ capture_width = 3280 ,
19+ capture_height = 2464 ,
20+ display_width = 820 ,
21+ display_height = 616 ,
22+ framerate = 21 ,
23+ flip_method = 0 ,
24+ ):
25+ return (
26+ "nvarguscamerasrc ! "
27+ "video/x-raw(memory:NVMM), "
28+ "width=(int)%d, height=(int)%d, "
29+ "format=(string)NV12, framerate=(fraction)%d/1 ! "
30+ "nvvidconv flip-method=%d ! "
31+ "video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! "
32+ "videoconvert ! "
33+ "video/x-raw, format=(string)BGR ! appsink"
34+ % (
35+ capture_width ,
36+ capture_height ,
37+ framerate ,
38+ flip_method ,
39+ display_width ,
40+ display_height ,
41+ )
42+ )
43+
44+
45+ def face_detect ():
46+ face_cascade = cv2 .CascadeClassifier (
47+ "/usr/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml"
48+ )
49+ eye_cascade = cv2 .CascadeClassifier (
50+ "/usr/share/OpenCV/haarcascades/haarcascade_eye.xml"
51+ )
2952 cap = cv2 .VideoCapture (gstreamer_pipeline (), cv2 .CAP_GSTREAMER )
3053 if cap .isOpened ():
31- cv2 .namedWindow (' Face Detect' , cv2 .WINDOW_AUTOSIZE )
32- while cv2 .getWindowProperty (' Face Detect' , 0 ) >= 0 :
54+ cv2 .namedWindow (" Face Detect" , cv2 .WINDOW_AUTOSIZE )
55+ while cv2 .getWindowProperty (" Face Detect" , 0 ) >= 0 :
3356 ret , img = cap .read ()
3457 gray = cv2 .cvtColor (img , cv2 .COLOR_BGR2GRAY )
3558 faces = face_cascade .detectMultiScale (gray , 1.3 , 5 )
3659
37- for (x ,y , w , h ) in faces :
38- cv2 .rectangle (img ,(x ,y ),( x + w , y + h ),(255 ,0 , 0 ),2 )
39- roi_gray = gray [y : y + h , x : x + w ]
40- roi_color = img [y : y + h , x : x + w ]
60+ for (x , y , w , h ) in faces :
61+ cv2 .rectangle (img , (x , y ), ( x + w , y + h ), (255 , 0 , 0 ), 2 )
62+ roi_gray = gray [y : y + h , x : x + w ]
63+ roi_color = img [y : y + h , x : x + w ]
4164 eyes = eye_cascade .detectMultiScale (roi_gray )
42- for (ex ,ey ,ew ,eh ) in eyes :
43- cv2 .rectangle (roi_color ,(ex ,ey ),(ex + ew ,ey + eh ),(0 ,255 ,0 ),2 )
65+ for (ex , ey , ew , eh ) in eyes :
66+ cv2 .rectangle (
67+ roi_color , (ex , ey ), (ex + ew , ey + eh ), (0 , 255 , 0 ), 2
68+ )
4469
45- cv2 .imshow (' Face Detect' , img )
46- keyCode = cv2 .waitKey (30 ) & 0xff
70+ cv2 .imshow (" Face Detect" , img )
71+ keyCode = cv2 .waitKey (30 ) & 0xFF
4772 # Stop the program on the ESC key
4873 if keyCode == 27 :
4974 break
@@ -53,5 +78,6 @@ def face_detect() :
5378 else :
5479 print ("Unable to open camera" )
5580
56- if __name__ == '__main__' :
81+
82+ if __name__ == "__main__" :
5783 face_detect ()
0 commit comments