-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathjetson_stream.py
91 lines (66 loc) · 2.55 KB
/
jetson_stream.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
import argparse
from os import path
import time
import logging
import sys
import cv2
# from object_detector_detection_api_lite import ObjectDetectorLite
from object_detector_trt import ObjectDetectorTRT
logging.basicConfig(
stream=sys.stdout,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
datefmt=' %I:%M:%S ',
level="INFO"
)
logger = logging.getLogger('detector')
basepath = path.dirname(__file__)
def open_cam_onboard(width, height):
# On versions of L4T prior to 28.1, add 'flip-method=2' into gst_str
gst_str = ('nvcamerasrc ! '
'video/x-raw(memory:NVMM), '
'width=(int)2592, height=(int)1458, '
'format=(string)I420, framerate=(fraction)30/1 ! '
'nvvidconv ! '
'video/x-raw, width=(int){}, height=(int){}, '
'format=(string)BGRx ! '
'videoconvert ! appsink').format(width, height)
return cv2.VideoCapture(gst_str, cv2.CAP_GSTREAMER)
if __name__ == '__main__':
# initiate the parser
parser = argparse.ArgumentParser(prog='test_models.py')
# add arguments
# read arguments from the command line
args = parser.parse_args()
# initialize detector
logger.info('Model loading...')
predictor = ObjectDetectorTRT()
cap = open_cam_onboard(640, 480)
if not cap.isOpened():
sys.exit('Failed to open camera!')
# allow the camera to warmup
time.sleep(0.1)
frame_rate_calc = 1
freq = cv2.getTickFrequency()
while (cap.isOpened()):
t1 = cv2.getTickCount()
ret, frame = cap.read()
logger.info("FPS: {0:.2f}".format(frame_rate_calc))
cv2.putText(frame, "FPS: {0:.2f}".format(frame_rate_calc), (20, 20),
cv2.FONT_HERSHEY_PLAIN, 1, (255, 255, 0), 2, cv2.LINE_AA)
result = predictor.detect(frame)
for obj in result:
logger.info('coordinates: {} {}. class: "{}". confidence: {:.2f}'.
format(obj[0], obj[1], obj[3], obj[2]))
cv2.rectangle(frame, obj[0], obj[1], (0, 255, 0), 2)
cv2.putText(frame, '{}: {:.2f}'.format(obj[3], obj[2]),
(obj[0][0], obj[0][1] - 5),
cv2.FONT_HERSHEY_PLAIN, 1, (0, 255, 0), 2)
# show the frame
cv2.imshow("Stream", frame)
key = cv2.waitKey(1) & 0xFF
t2 = cv2.getTickCount()
time1 = (t2 - t1) / freq
frame_rate_calc = 1 / time1
# if the `q` key was pressed, break from the loop
if key == ord("q"):
break