Visualization tool for ego-centric images. This class serves as the API for visualizing ego-centric images. Args: image (str): Path of image to be visualized. verbose (bool): Whether to print related messages. Defaults to False.
| 6 | |
| 7 | |
| 8 | class ImageDrawer: |
| 9 | """Visualization tool for ego-centric images. |
| 10 | |
| 11 | This class serves as the API for visualizing ego-centric images. |
| 12 | |
| 13 | Args: |
| 14 | image (str): Path of image to be visualized. |
| 15 | verbose (bool): Whether to print related messages. Defaults to False. |
| 16 | """ |
| 17 | |
| 18 | def __init__(self, image, verbose=False): |
| 19 | self.verbose = verbose |
| 20 | if self.verbose: |
| 21 | print('Loading image', image) |
| 22 | img = cv2.imread(image) |
| 23 | if self.verbose: |
| 24 | print('Loading image Complete') |
| 25 | img = img[:, :, ::-1].astype(np.float32) # BGR to RGB |
| 26 | self.occupied = np.zeros((img.shape[0], img.shape[1]), dtype=bool) |
| 27 | self.img = img |
| 28 | self.EPS = 1e-4 |
| 29 | self.ALPHA = 0.75 |
| 30 | |
| 31 | def draw_text(self, |
| 32 | text, |
| 33 | font=cv2.FONT_HERSHEY_SIMPLEX, |
| 34 | pos=(0, 0), |
| 35 | size=(0, 0), |
| 36 | font_scale=1, |
| 37 | font_thickness=2, |
| 38 | text_color=(0, 255, 0), |
| 39 | text_color_bg=(0, 0, 0)): |
| 40 | """Draw the category text on the image. |
| 41 | |
| 42 | Args: |
| 43 | text (str): Text to be drawn. |
| 44 | font (cv2.FONT_TYPE): The font type to be used to plot. |
| 45 | Defaults to cv2.FONT_HERSHEY_SIMPLEX. |
| 46 | pos (tuple): The drawing position. Defaults to (0, 0). |
| 47 | size (tuple): Image size. Defaults to (0, 0). |
| 48 | font_scale (int): Font scale. Defaults to 1. |
| 49 | font_thickness (int): Font thickness. Defaults to 2. |
| 50 | text_color (tuple): Font color. Defaults to (0, 255, 0). |
| 51 | text_color_bg (tuple): Background color for drawing texts. |
| 52 | Defaults to (0, 0, 0). |
| 53 | """ |
| 54 | |
| 55 | x, y = pos |
| 56 | w, h = size |
| 57 | text_size, _ = cv2.getTextSize(text, font, font_scale, font_thickness) |
| 58 | text_w, text_h = text_size |
| 59 | if y * 2 > h: |
| 60 | dy = -10 |
| 61 | else: |
| 62 | dy = 10 |
| 63 | |
| 64 | try: |
| 65 | while self.occupied[y, x] or self.occupied[ |