Skip to content

Commit ecedc32

Browse files
committed
Optimize 'Camera' code
1 parent 78ab865 commit ecedc32

8 files changed

+197
-173
lines changed

README.md

+20-18
Original file line numberDiff line numberDiff line change
@@ -91,19 +91,24 @@ Step-by-step:
9191

9292
```shell
9393
$ cd ${HOME}/project/tensorrt_demos
94-
$ python3 trt_googlenet.py --usb --vid 0 --width 1280 --height 720
94+
$ python3 trt_googlenet.py --usb 0 --width 1280 --height 720
9595
```
9696

9797
Here's a screenshot of the demo (JetPack-4.2.2, i.e. TensorRT 5).
9898

9999
![A picture of a golden retriever](https://raw.githubusercontent.com/jkjung-avt/tensorrt_demos/master/doc/golden_retriever.png)
100100

101-
5. The demo program supports a number of different image inputs. You could do `python3 trt_googlenet.py --help` to read the help messages. Or more specifically, the following inputs could be specified:
101+
5. The demo program supports 5 different image/video inputs. You could do `python3 trt_googlenet.py --help` to read the help messages. Or more specifically, the following inputs could be specified:
102102

103-
* `--file --filename test_video.mp4`: a video file, e.g. mp4 or ts.
104-
* `--image --filename test_image.jpg`: an image file, e.g. jpg or png.
105-
* `--usb --vid 0`: USB webcam (/dev/video0).
106-
* `--rtsp --uri rtsp://admin:[email protected]/live.sdp`: RTSP source, e.g. an IP cam.
103+
* `--image test_image.jpg`: an image file, e.g. jpg or png.
104+
* `--video test_video.mp4`: a video file, e.g. mp4 or ts. An optional `--video_looping` flag could be enabled if needed.
105+
* `--usb 0`: USB webcam (/dev/video0).
106+
* `--rtsp rtsp://admin:[email protected]/live.sdp`: RTSP source, e.g. an IP cam. An optional `--rtsp_latency` argument could be used to adjust the latency setting in this case.
107+
* `--onboard 0`: Jetson onboard camera.
108+
109+
In additional, you could use `--width` and `--height` to specify the desired input image size, and use `--do_resize` to force resizing of image/video file source.
110+
111+
The `--usb`, `--rtsp` and `--onboard` video sources usually produce image frames at 30 FPS. If the TensorRT engine inference code runs faster than that (which happens easily on a x86_64 PC with a good GPU), one particular image could be inferenced multiple times before the next image frame becomes available. This causes problem in the object detector demos, since the original image could have been altered (bounding boxes drawn) and the altered image is taken for inference again. To cope with this problem, use the optional `--copy_frame` flag to force copying/cloning image frames internally.
107112

108113
6. Check out my blog post for implementation details:
109114

@@ -131,7 +136,7 @@ Assuming this repository has been cloned at "${HOME}/project/tensorrt_demos", fo
131136

132137
```shell
133138
$ cd ${HOME}/project/tensorrt_demos
134-
$ python3 trt_mtcnn.py --image --filename ${HOME}/Pictures/avengers.jpg
139+
$ python3 trt_mtcnn.py --image ${HOME}/Pictures/avengers.jpg
135140
```
136141

137142
Here's the result (JetPack-4.2.2, i.e. TensorRT 5).
@@ -169,9 +174,8 @@ Assuming this repository has been cloned at "${HOME}/project/tensorrt_demos", fo
169174

170175
```shell
171176
$ cd ${HOME}/project/tensorrt_demos
172-
$ python3 trt_ssd.py --model ssd_mobilenet_v1_coco \
173-
--image \
174-
--filename ${HOME}/project/tf_trt_models/examples/detection/data/huskies.jpg
177+
$ python3 trt_ssd.py --image ${HOME}/project/tf_trt_models/examples/detection/data/huskies.jpg \
178+
--model ssd_mobilenet_v1_coco
175179
```
176180

177181
Here's the result (JetPack-4.2.2, i.e. TensorRT 5). Frame rate was good (over 20 FPS).
@@ -187,9 +191,8 @@ Assuming this repository has been cloned at "${HOME}/project/tensorrt_demos", fo
187191
I also tested the "ssd_mobilenet_v1_egohands" (hand detector) model with a video clip from YouTube, and got the following result. Again, frame rate was pretty good. But the detection didn't seem very accurate though :-(
188192

189193
```shell
190-
$ python3 trt_ssd.py --model ssd_mobilenet_v1_egohands \
191-
--file \
192-
--filename ${HOME}/Videos/Nonverbal_Communication.mp4
194+
$ python3 trt_ssd.py --video ${HOME}/Videos/Nonverbal_Communication.mp4 \
195+
--model ssd_mobilenet_v1_egohands
193196
```
194197

195198
(Click on the image below to see the whole video clip...)
@@ -202,9 +205,8 @@ Assuming this repository has been cloned at "${HOME}/project/tensorrt_demos", fo
202205

203206
```shell
204207
$ cd ${HOME}/project/tensorrt_demos
205-
$ python3 trt_ssd_async.py --model ssd_mobilenet_v1_coco \
206-
--image \
207-
--filename ${HOME}/project/tf_trt_models/examples/detection/data/huskies.jpg
208+
$ python3 trt_ssd_async.py --image ${HOME}/project/tf_trt_models/examples/detection/data/huskies.jpg \
209+
--model ssd_mobilenet_v1_coco
208210
```
209211

210212
5. To verify accuracy (mAP) of the optimized TensorRT engines and make sure they do not degrade too much (due to reduced floating-point precision of "FP16") from the original TensorFlow frozen inference graphs, you could prepare validation data and run "eval_ssd.py". Refer to [README_mAP.md](README_mAP.md) for details.
@@ -278,8 +280,8 @@ Assuming this repository has been cloned at "${HOME}/project/tensorrt_demos", fo
278280

279281
```shell
280282
$ wget https://raw.githubusercontent.com/pjreddie/darknet/master/data/dog.jpg -O ${HOME}/Pictures/dog.jpg
281-
$ python3 trt_yolo.py -m yolov4-416 \
282-
--image --filename ${HOME}/Pictures/dog.jpg
283+
$ python3 trt_yolo.py --image ${HOME}/Pictures/dog.jpg \
284+
-m yolov4-416
283285
```
284286

285287
This is a screenshot of the demo against JetPack-4.4, i.e. TensorRT 7.

trt_googlenet.py

+11-12
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,13 @@ def loop_and_classify(cam, net, labels, do_cropping):
8888
if cv2.getWindowProperty(WINDOW_NAME, 0) < 0:
8989
break
9090
img = cam.read()
91-
if img is not None:
92-
top_probs, top_labels = classify(img, net, labels, do_cropping)
93-
show_top_preds(img, top_probs, top_labels)
94-
if show_help:
95-
show_help_text(img, help_text)
96-
cv2.imshow(WINDOW_NAME, img)
91+
if img is None:
92+
break
93+
top_probs, top_labels = classify(img, net, labels, do_cropping)
94+
show_top_preds(img, top_probs, top_labels)
95+
if show_help:
96+
show_help_text(img, help_text)
97+
cv2.imshow(WINDOW_NAME, img)
9798
key = cv2.waitKey(1)
9899
if key == 27: # ESC key: quit program
99100
break
@@ -108,19 +109,17 @@ def main():
108109
args = parse_args()
109110
labels = np.loadtxt('googlenet/synset_words.txt', str, delimiter='\t')
110111
cam = Camera(args)
111-
cam.open()
112-
if not cam.is_opened:
112+
if not cam.isOpened():
113113
raise SystemExit('ERROR: failed to open camera!')
114114

115115
# initialize the tensorrt googlenet engine
116116
net = PyTrtGooglenet(DEPLOY_ENGINE, ENGINE_SHAPE0, ENGINE_SHAPE1)
117117

118-
cam.start()
119-
open_window(WINDOW_NAME, args.image_width, args.image_height,
120-
'Camera TensorRT GoogLeNet Demo for Jetson Nano')
118+
open_window(
119+
WINDOW_NAME, 'Camera TensorRT GoogLeNet Demo',
120+
cam.img_width, cam.img_height)
121121
loop_and_classify(cam, net, labels, args.crop_center)
122122

123-
cam.stop()
124123
cam.release()
125124
cv2.destroyAllWindows()
126125

trt_googlenet_async.py

+7-7
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
RESIZED_SHAPE = (224, 224)
2727

2828
WINDOW_NAME = 'TrtGooglenetDemo'
29-
MAIN_THREAD_TIMEOUT = 30.0 # 30 seconds
29+
MAIN_THREAD_TIMEOUT = 10.0 # 10 seconds
3030

3131
# 'shared' global variables
3232
s_img, s_probs, s_labels = None, None, None
@@ -99,6 +99,8 @@ def run(self):
9999
self.running = True
100100
while self.running:
101101
img = self.cam.read()
102+
if img is None:
103+
break
102104
top_probs, top_labels = classify(
103105
img, self.net, self.labels, self.do_cropping)
104106
with self.condition:
@@ -162,20 +164,18 @@ def main():
162164
args = parse_args()
163165
labels = np.loadtxt('googlenet/synset_words.txt', str, delimiter='\t')
164166
cam = Camera(args)
165-
cam.open()
166-
if not cam.is_opened:
167+
if not cam.isOpened():
167168
raise SystemExit('ERROR: failed to open camera!')
168169

169-
cam.start() # let camera start grabbing frames
170-
open_window(WINDOW_NAME, args.image_width, args.image_height,
171-
'Camera TensorRT GoogLeNet Demo for Jetson Nano')
170+
open_window(
171+
WINDOW_NAME, 'Camera TensorRT GoogLeNet Demo',
172+
cam.img_width, cam.img_height)
172173
condition = threading.Condition()
173174
trt_thread = TrtGooglenetThread(condition, cam, labels, args.crop_center)
174175
trt_thread.start() # start the child thread
175176
loop_and_display(condition)
176177
trt_thread.stop() # stop the child thread
177178

178-
cam.stop()
179179
cam.release()
180180
cv2.destroyAllWindows()
181181

trt_ssd.py

+15-16
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,17 @@ def loop_and_detect(cam, trt_ssd, conf_th, vis):
5858
if cv2.getWindowProperty(WINDOW_NAME, 0) < 0:
5959
break
6060
img = cam.read()
61-
if img is not None:
62-
boxes, confs, clss = trt_ssd.detect(img, conf_th)
63-
img = vis.draw_bboxes(img, boxes, confs, clss)
64-
img = show_fps(img, fps)
65-
cv2.imshow(WINDOW_NAME, img)
66-
toc = time.time()
67-
curr_fps = 1.0 / (toc - tic)
68-
# calculate an exponentially decaying average of fps number
69-
fps = curr_fps if fps == 0.0 else (fps*0.95 + curr_fps*0.05)
70-
tic = toc
61+
if img is None:
62+
break
63+
boxes, confs, clss = trt_ssd.detect(img, conf_th)
64+
img = vis.draw_bboxes(img, boxes, confs, clss)
65+
img = show_fps(img, fps)
66+
cv2.imshow(WINDOW_NAME, img)
67+
toc = time.time()
68+
curr_fps = 1.0 / (toc - tic)
69+
# calculate an exponentially decaying average of fps number
70+
fps = curr_fps if fps == 0.0 else (fps*0.95 + curr_fps*0.05)
71+
tic = toc
7172
key = cv2.waitKey(1)
7273
if key == 27: # ESC key: quit program
7374
break
@@ -79,20 +80,18 @@ def loop_and_detect(cam, trt_ssd, conf_th, vis):
7980
def main():
8081
args = parse_args()
8182
cam = Camera(args)
82-
cam.open()
83-
if not cam.is_opened:
83+
if not cam.isOpened():
8484
raise SystemExit('ERROR: failed to open camera!')
8585

8686
cls_dict = get_cls_dict(args.model.split('_')[-1])
8787
trt_ssd = TrtSSD(args.model, INPUT_HW)
8888

89-
cam.start()
90-
open_window(WINDOW_NAME, args.image_width, args.image_height,
91-
'Camera TensorRT SSD Demo for Jetson Nano')
89+
open_window(
90+
WINDOW_NAME, 'Camera TensorRT SSD Demo',
91+
cam.img_width, cam.img_height)
9292
vis = BBoxVisualization(cls_dict)
9393
loop_and_detect(cam, trt_ssd, conf_th=0.3, vis=vis)
9494

95-
cam.stop()
9695
cam.release()
9796
cv2.destroyAllWindows()
9897

trt_ssd_async.py

+9-9
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424

2525

2626
WINDOW_NAME = 'TrtSsdDemoAsync'
27-
MAIN_THREAD_TIMEOUT = 30.0 # 30 seconds
27+
MAIN_THREAD_TIMEOUT = 20.0 # 20 seconds
2828
INPUT_HW = (300, 300)
2929
SUPPORTED_MODELS = [
3030
'ssd_mobilenet_v1_coco',
@@ -98,6 +98,8 @@ def run(self):
9898
self.running = True
9999
while self.running:
100100
img = self.cam.read()
101+
if img is None:
102+
break
101103
boxes, confs, clss = self.trt_ssd.detect(img, self.conf_th)
102104
with self.condition:
103105
s_img, s_boxes, s_confs, s_clss = img, boxes, confs, clss
@@ -156,25 +158,23 @@ def loop_and_display(condition, vis):
156158
def main():
157159
args = parse_args()
158160
cam = Camera(args)
159-
cam.open()
160-
if not cam.is_opened:
161+
if not cam.isOpened():
161162
raise SystemExit('ERROR: failed to open camera!')
162163

163-
cls_dict = get_cls_dict(args.model.split('_')[-1])
164-
165164
cuda.init() # init pycuda driver
166165

167-
cam.start() # let camera start grabbing frames
168-
open_window(WINDOW_NAME, args.image_width, args.image_height,
169-
'Camera TensorRT SSD Demo for Jetson Nano')
166+
cls_dict = get_cls_dict(args.model.split('_')[-1])
167+
168+
open_window(
169+
WINDOW_NAME, 'Camera TensorRT SSD Demo',
170+
cam.img_width, cam.img_height)
170171
vis = BBoxVisualization(cls_dict)
171172
condition = threading.Condition()
172173
trt_thread = TrtThread(condition, cam, args.model, conf_th=0.3)
173174
trt_thread.start() # start the child thread
174175
loop_and_display(condition, vis)
175176
trt_thread.stop() # stop the child thread
176177

177-
cam.stop()
178178
cam.release()
179179
cv2.destroyAllWindows()
180180

trt_yolo.py

+15-16
Original file line numberDiff line numberDiff line change
@@ -58,16 +58,17 @@ def loop_and_detect(cam, trt_yolo, conf_th, vis):
5858
if cv2.getWindowProperty(WINDOW_NAME, 0) < 0:
5959
break
6060
img = cam.read()
61-
if img is not None:
62-
boxes, confs, clss = trt_yolo.detect(img, conf_th)
63-
img = vis.draw_bboxes(img, boxes, confs, clss)
64-
img = show_fps(img, fps)
65-
cv2.imshow(WINDOW_NAME, img)
66-
toc = time.time()
67-
curr_fps = 1.0 / (toc - tic)
68-
# calculate an exponentially decaying average of fps number
69-
fps = curr_fps if fps == 0.0 else (fps*0.95 + curr_fps*0.05)
70-
tic = toc
61+
if img is None:
62+
break
63+
boxes, confs, clss = trt_yolo.detect(img, conf_th)
64+
img = vis.draw_bboxes(img, boxes, confs, clss)
65+
img = show_fps(img, fps)
66+
cv2.imshow(WINDOW_NAME, img)
67+
toc = time.time()
68+
curr_fps = 1.0 / (toc - tic)
69+
# calculate an exponentially decaying average of fps number
70+
fps = curr_fps if fps == 0.0 else (fps*0.95 + curr_fps*0.05)
71+
tic = toc
7172
key = cv2.waitKey(1)
7273
if key == 27: # ESC key: quit program
7374
break
@@ -84,8 +85,7 @@ def main():
8485
raise SystemExit('ERROR: file (yolo/%s.trt) not found!' % args.model)
8586

8687
cam = Camera(args)
87-
cam.open()
88-
if not cam.is_opened:
88+
if not cam.isOpened():
8989
raise SystemExit('ERROR: failed to open camera!')
9090

9191
cls_dict = get_cls_dict(args.category_num)
@@ -102,13 +102,12 @@ def main():
102102

103103
trt_yolo = TrtYOLO(args.model, (h, w), args.category_num)
104104

105-
cam.start()
106-
open_window(WINDOW_NAME, args.image_width, args.image_height,
107-
'Camera TensorRT YOLO Demo')
105+
open_window(
106+
WINDOW_NAME, 'Camera TensorRT YOLO Demo',
107+
cam.img_width, cam.img_height)
108108
vis = BBoxVisualization(cls_dict)
109109
loop_and_detect(cam, trt_yolo, conf_th=0.3, vis=vis)
110110

111-
cam.stop()
112111
cam.release()
113112
cv2.destroyAllWindows()
114113

0 commit comments

Comments
 (0)