9
9
import cv2
10
10
11
11
# gstreamer_pipeline returns a GStreamer pipeline for capturing from the CSI camera
12
- # Defaults to 1280x720 @ 30fps
12
+ # Defaults to 1280x720 @ 30fps
13
13
# Flip the image by setting the flip_method (most common values: 0 and 2)
14
14
# display_width and display_height determine the size of the window on the screen
15
15
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
+ )
29
52
cap = cv2 .VideoCapture (gstreamer_pipeline (), cv2 .CAP_GSTREAMER )
30
53
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 :
33
56
ret , img = cap .read ()
34
57
gray = cv2 .cvtColor (img , cv2 .COLOR_BGR2GRAY )
35
58
faces = face_cascade .detectMultiScale (gray , 1.3 , 5 )
36
59
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 ]
41
64
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
+ )
44
69
45
- cv2 .imshow (' Face Detect' , img )
46
- keyCode = cv2 .waitKey (30 ) & 0xff
70
+ cv2 .imshow (" Face Detect" , img )
71
+ keyCode = cv2 .waitKey (30 ) & 0xFF
47
72
# Stop the program on the ESC key
48
73
if keyCode == 27 :
49
74
break
@@ -53,5 +78,6 @@ def face_detect() :
53
78
else :
54
79
print ("Unable to open camera" )
55
80
56
- if __name__ == '__main__' :
81
+
82
+ if __name__ == "__main__" :
57
83
face_detect ()
0 commit comments