-
Notifications
You must be signed in to change notification settings - Fork 1
/
trt_yolo_cam.py
197 lines (158 loc) · 5.33 KB
/
trt_yolo_cam.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import os
import time
import argparse
import cv2
import numpy as np
import pycuda.autoinit # This is needed for initializing CUDA driver
from utils.yolo_classes import get_cls_dict
from utils.camera import add_camera_args, Camera
from utils.display import open_window, set_display, show_fps
from utils.visualization import BBoxVisualization
from utils.yolo_with_plugins import TrtYOLO
from datetime import datetime
import boto3
def parse_args():
"""Parse input arguments"""
desc = ('Capture and display live camera video using Jetson Nano, while doing '
'real-time object detection with TensorRT optimized '
'YOLO model on Jetson')
parser = argparse.ArgumentParser(description=desc)
parser.add_argument(
'-s', '--show_window', type=int, default=0,
help='[0|1] to display output window. Default is 0.')
parser.add_argument(
'-t', '--transmit', type=int, default=0,
help='[0|1] to send images and data to aws s3 & dynamoDB as set in '
'awscli. It needs to be set before running. Default is 0.')
parser.add_argument(
'-f', '--flip_val', type=int, default=0,
help='[2|0|1|3] camera flip value. Usually 2 or 0. flip_val=0 for camera towards '
'Jetson, and flip_val=2 for camera outwards Jetson. Default is 0.')
parser.add_argument(
'-c', '--capture_size', type=int, default=608,
help='[416|608...] camera capture size. default is 608. You should set this value to your yolo input size.')
# Set your wanted default model name here
model_default='yolov4-tiny-hwan_608'
parser.add_argument(
'-m', '--model', type=str, default=model_default,
help='put your TensorRT yolo model name here. Current defualt is \"'+model_default+'\"')
return parser.parse_args()
def gstreamer_pipeline(
capture_width=416,
capture_height=416,
framerate=30,
flip_method=2,
display_width=416,
display_height=416,
):
return (
"nvarguscamerasrc ! "
"video/x-raw(memory:NVMM), "
"width=(int)%d, height=(int)%d, "
"format=(string)NV12, framerate=(fraction)%d/1 ! "
"nvvidconv flip-method=%d ! "
"video/x-raw, width=(int)%d, height=(int)%d, format=(string)BGRx !"
"videoconvert ! "
"video/x-raw, format=(string)BGR ! appsink"
% (
capture_width,
capture_height,
framerate,
flip_method,
display_width,
display_height,
)
)
def upload_s3(img):
s3 = boto3.client('s3')
now = datetime.now()
#change to your file name and saving output directory
file_name="__your_file_name__"
file_dir="/home/nano/project/darknet/%s.jpg"%(file_name)
cv2.imwrite(file_dir,img)
# change to your bucket name
bucket_name="__your_bucket_name__"
s3.upload_file(file_dir,bucket_name, '%s'%(file_name),"ContentType": 'image/jpeg'})
s3_url = "https://"+bucket_name+".s3.ap-northeast-2.amazonaws.com/"+file_name
print("Saved at: "+s3_url)
dynamodb = boto3.resource('dynamodb')
#change to your table name
dynamoTable = dynamodb.Table('__your_table_name__')
dynamoTable.put_item(
Item={
'image' : s3_url,
'time' : '%d-%02d-%02d'%(now.year,now.month,now.day),
'serial' : '화전',
'times' : '%d:%02d:%02d'%(now.hour,now.minute,now.second)
})
def detect(cam, trt_yolo, conf_th, vis):
full_scrn = False
fps = 0.0
tic = time.time()
save_time = time.time()
while True:
if args.show_window:
if cv2.getWindowProperty(WINDOW_NAME, 0) < 0:
break
ret, img = cam.read()
if img is None:
break
boxes, confs, clss = trt_yolo.detect(img, conf_th)
img = vis.draw_bboxes(img, boxes, confs, clss)
img = show_fps(img, fps)
if(args.show_window):
cv2.imshow(WINDOW_NAME, img)
toc = time.time()
curr_fps = 1.0 / (toc - tic)
# calculate an exponentially decaying average of fps number
fps = curr_fps if fps == 0.0 else (fps*0.95 + curr_fps*0.05)
tic = toc
if(np.count_nonzero(clss == 0) > 0 and time.time()-save_time>=3):
print("detected no helmet")
if(args.transmit):
print("saving image to s3")
upload_s3(img)
save_time=time.time()
key = cv2.waitKey(1)
if key == 27: # ESC key: quit program
break
elif ((key == ord('F') or key == ord('f')) and args.show_window): # Toggle fullscreen
full_scrn = not full_scrn
set_display(WINDOW_NAME, full_scrn)
cv2.destroyAllWindows()
cam.release()
##########################
def showimg(img):
cv2.imshow("Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()
#quit()
# get args from cmd line
args = parse_args()
jnID='A'
model_dir = ""
WINDOW_NAME = 'TrtYOLODemo'
cls_dict = get_cls_dict(1)
print(cls_dict)
vis = BBoxVisualization(cls_dict)
#trt_yolo = TrtYOLO(args.model, args.category_num, args.letter_box)
trt_yolo = TrtYOLO(args.model, 1, False)
#gspipe = ""
gpip = gstreamer_pipeline(
capture_width=args.capture_size,
capture_height=args.capture_size,
flip_method=args.flip_val,
display_width=args.capture_size,
display_height=args.capture_size)
cap = cv2.VideoCapture(gpip, cv2.CAP_GSTREAMER)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, args.capture_size)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, args.capture_size)
if not cap.isOpened():
raise IOError("We cannot open Camera")
if(args.show_window):
open_window(
WINDOW_NAME, 'Camera TensorRT YOLO Demo',
args.capture_size, args.capture_size)
detect(cap, trt_yolo,conf_th=0.5,vis=vis)
cv2.destroyAllWindows()
cap.release()