Skip to content

Commit 47ee41a

Browse files
committed
Add Python 3 Support
1 parent 3be3d52 commit 47ee41a

File tree

3 files changed

+98
-49
lines changed

3 files changed

+98
-49
lines changed

README.md

+6-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Haar Cascades is a machine learning based approach where a cascade function is t
2323

2424
See: https://docs.opencv.org/3.3.1/d7/d8b/tutorial_py_face_detection.html
2525

26-
The third example is a simple C++ prgoram which reads from the camera and displays to a window on the screen using OpenCV:
26+
The third example is a simple C++ program which reads from the camera and displays to a window on the screen using OpenCV:
2727

2828
```
2929
$ g++ -std=c++11 -Wall -I/usr/lib/opencv simple_camera.cpp -L/usr/lib -lopencv_core -lopencv_highgui -lopencv_videoio -o simple_camera
@@ -80,7 +80,11 @@ flip-method : video flip methods
8080

8181
<h2>Release Notes</h2>
8282

83-
Initial Release March, 2019
83+
V2 Release September, 2019
84+
* L4T 32.2.1 (JetPack 4.2.2)
85+
* Tested on Jetson Nano
86+
87+
Initial Release (V1) March, 2019
8488
* L4T 32.1.0 (JetPack 4.2)
8589
* Tested on Jetson Nano
8690

face_detect.py

+51-25
Original file line numberDiff line numberDiff line change
@@ -9,41 +9,66 @@
99
import 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()

simple_camera.py

+41-22
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,66 @@
11
# MIT License
22
# Copyright (c) 2019 JetsonHacks
33
# See license
4-
# Using a CSI camera (such as the Raspberry Pi Version 2) connected to a
4+
# Using a CSI camera (such as the Raspberry Pi Version 2) connected to a
55
# NVIDIA Jetson Nano Developer Kit using OpenCV
66
# Drivers for the camera and OpenCV are included in the base image
77

88
import cv2
99

1010
# gstreamer_pipeline returns a GStreamer pipeline for capturing from the CSI camera
11-
# Defaults to 1280x720 @ 60fps
11+
# Defaults to 1280x720 @ 60fps
1212
# Flip the image by setting the flip_method (most common values: 0 and 2)
1313
# display_width and display_height determine the size of the window on the screen
1414

15-
def gstreamer_pipeline (capture_width=1280, capture_height=720, display_width=1280, display_height=720, framerate=60, flip_method=0) :
16-
return ('nvarguscamerasrc ! '
17-
'video/x-raw(memory:NVMM), '
18-
'width=(int)%d, height=(int)%d, '
19-
'format=(string)NV12, framerate=(fraction)%d/1 ! '
20-
'nvvidconv flip-method=%d ! '
21-
'video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! '
22-
'videoconvert ! '
23-
'video/x-raw, format=(string)BGR ! appsink' % (capture_width,capture_height,framerate,flip_method,display_width,display_height))
15+
16+
def gstreamer_pipeline(
17+
capture_width=1280,
18+
capture_height=720,
19+
display_width=1280,
20+
display_height=720,
21+
framerate=60,
22+
flip_method=0,
23+
):
24+
return (
25+
"nvarguscamerasrc ! "
26+
"video/x-raw(memory:NVMM), "
27+
"width=(int)%d, height=(int)%d, "
28+
"format=(string)NV12, framerate=(fraction)%d/1 ! "
29+
"nvvidconv flip-method=%d ! "
30+
"video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx ! "
31+
"videoconvert ! "
32+
"video/x-raw, format=(string)BGR ! appsink"
33+
% (
34+
capture_width,
35+
capture_height,
36+
framerate,
37+
flip_method,
38+
display_width,
39+
display_height,
40+
)
41+
)
42+
2443

2544
def show_camera():
2645
# To flip the image, modify the flip_method parameter (0 and 2 are the most common)
27-
print gstreamer_pipeline(flip_method=0)
46+
print(gstreamer_pipeline(flip_method=0))
2847
cap = cv2.VideoCapture(gstreamer_pipeline(flip_method=0), cv2.CAP_GSTREAMER)
2948
if cap.isOpened():
30-
window_handle = cv2.namedWindow('CSI Camera', cv2.WINDOW_AUTOSIZE)
31-
# Window
32-
while cv2.getWindowProperty('CSI Camera',0) >= 0:
33-
ret_val, img = cap.read();
34-
cv2.imshow('CSI Camera',img)
35-
# This also acts as
36-
keyCode = cv2.waitKey(30) & 0xff
49+
window_handle = cv2.namedWindow("CSI Camera", cv2.WINDOW_AUTOSIZE)
50+
# Window
51+
while cv2.getWindowProperty("CSI Camera", 0) >= 0:
52+
ret_val, img = cap.read()
53+
cv2.imshow("CSI Camera", img)
54+
# This also acts as
55+
keyCode = cv2.waitKey(30) & 0xFF
3756
# Stop the program on the ESC key
3857
if keyCode == 27:
39-
break
58+
break
4059
cap.release()
4160
cv2.destroyAllWindows()
4261
else:
43-
print 'Unable to open camera'
62+
print("Unable to open camera")
4463

4564

46-
if __name__ == '__main__':
65+
if __name__ == "__main__":
4766
show_camera()

0 commit comments

Comments
 (0)