Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

#970 Update YoloNASQuickstart.md

Merged
Ghost merged 1 commits into Deci-AI:master from deci-ai:bugfix/SG-000_fix_readme_yolonas_snippets
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
  1. from typing import Tuple, List
  2. import cv2
  3. import numpy as np
  4. import matplotlib.pyplot as plt
  5. def draw_text_box(
  6. image: np.ndarray,
  7. text: str,
  8. x: int,
  9. y: int,
  10. font: int,
  11. font_size: float,
  12. background_color: Tuple[int, int, int],
  13. thickness: int = 1,
  14. ) -> np.ndarray:
  15. """Draw a text inside a box
  16. :param image: The image on which to draw the text box.
  17. :param text: The text to display in the text box.
  18. :param x: The x-coordinate of the top-left corner of the text box.
  19. :param y: The y-coordinate of the top-left corner of the text box.
  20. :param font: The font to use for the text.
  21. :param font_size: The size of the font to use.
  22. :param background_color: The color of the text box and text as a tuple of three integers representing RGB values.
  23. :param thickness: The thickness of the text.
  24. :return: Image with the text inside the box.
  25. """
  26. text_color = best_text_color(background_color)
  27. (text_width, text_height), baseline = cv2.getTextSize(text, font, font_size, thickness)
  28. text_left_offset = 7
  29. image = cv2.rectangle(image, (x, y), (x + text_width + text_left_offset, y - text_height - int(15 * font_size)), background_color, -1)
  30. image = cv2.putText(image, text, (x + text_left_offset, y - int(10 * font_size)), font, font_size, text_color, thickness, lineType=cv2.LINE_AA)
  31. return image
  32. def best_text_color(background_color: Tuple[int, int, int]) -> Tuple[int, int, int]:
  33. """Determine the best color for text to be visible on a given background color.
  34. :param background_color: RGB values of the background color.
  35. :return: RGB values of the best text color for the given background color.
  36. """
  37. # If the brightness is greater than 0.5, use black text; otherwise, use white text.
  38. if compute_brightness(background_color) > 0.5:
  39. return (0, 0, 0) # Black
  40. else:
  41. return (255, 255, 255) # White
  42. def compute_brightness(color: Tuple[int, int, int]) -> float:
  43. """Computes the brightness of a given color in RGB format. From https://alienryderflex.com/hsp.html
  44. :param color: A tuple of three integers representing the RGB values of the color.
  45. :return: The brightness of the color.
  46. """
  47. return (0.299 * color[0] + 0.587 * color[1] + 0.114 * color[0]) / 255
  48. def generate_color_mapping(num_classes: int) -> List[Tuple[int, ...]]:
  49. """Generate a unique BGR color for each class
  50. :param num_classes: The number of classes in the dataset.
  51. :return: List of RGB colors for each class.
  52. """
  53. cmap = plt.cm.get_cmap("gist_rainbow", num_classes)
  54. colors = [cmap(i, bytes=True)[:3][::-1] for i in range(num_classes)]
  55. return [tuple(int(v) for v in c) for c in colors]
Discard
Tip!

Press p or to see the previous file or, n or to see the next file