Skip to content

Commit 5de8283

Browse files
author
JoeSiu
committed
Added info overlay part
1 parent 3373b3d commit 5de8283

File tree

3 files changed

+91
-4
lines changed

3 files changed

+91
-4
lines changed

donkeycar/parts/info_overlay.py

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
import cv2
2+
3+
4+
class InfoOverlayer(object):
5+
"""
6+
Add a info overlay to the camera image
7+
"""
8+
9+
# Store the infos which will be write to image
10+
info_list = []
11+
12+
def __init__(self, w=None, h=None):
13+
14+
# Camera image's size
15+
self.img_width = w
16+
self.img_height = h
17+
18+
# Overlay text's properties
19+
self.text_offset = (5, 100)
20+
self.font = cv2.FONT_HERSHEY_SIMPLEX
21+
self.font_size_multiplier = 2
22+
self.text_color = (255, 255, 255)
23+
self.text_thickness = 1
24+
25+
def add(self, string):
26+
self.info_list.append(string)
27+
28+
# truncate input so it won't cover up the screen
29+
def slice(self, input, start=0, end=5, step=1):
30+
if input is not None:
31+
input = str(input)[start:end:step]
32+
return input
33+
34+
def writeToImg(self, input_img):
35+
# Config overlay texts
36+
text_x = int(self.text_offset[0])
37+
text_y = int(self.text_offset[1] * self.img_height / 1000) # Text's gap relative to the image size
38+
font = self.font
39+
font_size = self.font_size_multiplier * self.img_width / 1000 # Font's size relative to the image size
40+
color = self.text_color
41+
thickness = self.text_thickness
42+
43+
# Write each info onto images
44+
for idx, info in enumerate(self.info_list):
45+
cv2.putText(input_img, info, (text_x, text_y * (idx + 1)),
46+
font, font_size, color, thickness)
47+
48+
def run(self, img_arr, fps, user_mode, user_throttle, user_angle, pilot_throttle, pilot_angle):
49+
# Default infos
50+
if fps is not None:
51+
self.add(f"current fps = {fps}")
52+
if user_mode == "user":
53+
self.add(f"user throttle = {self.slice(user_throttle)}")
54+
self.add(f"user angle = {self.slice(user_angle)}")
55+
elif user_mode == "local":
56+
self.add(f"pilot throttle = {self.slice(pilot_throttle)}")
57+
self.add(f"pilot angle = {self.slice(pilot_angle)}")
58+
elif user_mode == "local_angle":
59+
self.add(f"user throttle = {self.slice(user_throttle)}")
60+
self.add(f"pilot angle = {self.slice(pilot_angle)}")
61+
62+
# Write infos
63+
new_img_arr = None
64+
if img_arr is not None:
65+
new_img_arr = img_arr.copy()
66+
67+
if len(self.info_list) > 0:
68+
self.writeToImg(new_img_arr)
69+
self.info_list.clear()
70+
71+
return new_img_arr

donkeycar/templates/cfg_complete.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -373,3 +373,6 @@
373373
STOP_SIGN_DETECTOR = False
374374
STOP_SIGN_MIN_SCORE = 0.2
375375
STOP_SIGN_SHOW_BOUNDING_BOX = True
376+
377+
# Info overlay
378+
INFO_OVERLAY = False

donkeycar/templates/complete.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,14 +188,27 @@ def drive(cfg, model_path=None, use_joystick=False, model_type=None,
188188
if cfg.LIDAR_TYPE == 'YD':
189189
print("YD Lidar not yet supported")
190190

191+
if cfg.INFO_OVERLAY:
192+
from donkeycar.parts.info_overlay import InfoOverlayer
193+
V.add(InfoOverlayer(cfg.IMAGE_W, cfg.IMAGE_H),
194+
inputs=['cam/image_array', 'fps/current', 'user/mode',
195+
'user/throttle', 'user/angle', 'pilot/throttle', 'pilot/angle'],
196+
outputs=['overlay/image_array'])
197+
191198
#This web controller will create a web server that is capable
192199
#of managing steering, throttle, and modes, and more.
193200
ctr = LocalWebController(port=cfg.WEB_CONTROL_PORT, mode=cfg.WEB_INIT_MODE)
194201

195-
V.add(ctr,
196-
inputs=['cam/image_array', 'tub/num_records'],
197-
outputs=['user/angle', 'user/throttle', 'user/mode', 'recording'],
198-
threaded=True)
202+
if cfg.INFO_OVERLAY:
203+
V.add(ctr,
204+
inputs=['overlay/image_array', 'tub/num_records'],
205+
outputs=['user/angle', 'user/throttle', 'user/mode', 'recording'],
206+
threaded=True)
207+
else:
208+
V.add(ctr,
209+
inputs=['cam/image_array', 'tub/num_records'],
210+
outputs=['user/angle', 'user/throttle', 'user/mode', 'recording'],
211+
threaded=True)
199212

200213
if use_joystick or cfg.USE_JOYSTICK_AS_DEFAULT:
201214
#modify max_throttle closer to 1.0 to have more power

0 commit comments

Comments
 (0)