|
| 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 |
0 commit comments