You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

util.py 7.3 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. """
  2. MIT License
  3. Copyright (c) 2020 Ziqiang
  4. Permission is hereby granted, free of charge, to any person obtaining a copy
  5. of this software and associated documentation files (the "Software"), to deal
  6. in the Software without restriction, including without limitation the rights
  7. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. copies of the Software, and to permit persons to whom the Software is
  9. furnished to do so, subject to the following conditions:
  10. The above copyright notice and this permission notice shall be included in all
  11. copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  13. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  14. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  15. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  16. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  17. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  18. SOFTWARE.
  19. """
  20. import numpy as np
  21. import cv2
  22. COLORS_10 = [(144, 238, 144), (178, 34, 34), (221, 160, 221), (0, 255, 0), (0, 128, 0), (210, 105, 30), (220, 20, 60),
  23. (192, 192, 192), (255, 228, 196), (50, 205, 50), (139, 0, 139), (100, 149, 237), (138, 43, 226), (238, 130, 238),
  24. (255, 0, 255), (0, 100, 0), (127, 255, 0), (255, 0, 255), (255, 140, 0), (255, 239, 213), (0, 230, 100),
  25. (199, 21, 133), (124, 252, 0), (147, 112, 219), (106, 90, 205), (176, 196, 222), (65, 105, 225), (173, 255, 47),
  26. (255, 20, 147), (219, 112, 147), (186, 85, 211), (199, 21, 133), (148, 0, 211), (255, 99, 71), (144, 238, 144),
  27. (255, 255, 0), (230, 230, 250), (0, 0, 255), (128, 128, 0), (189, 183, 107), (255, 255, 224), (128, 128, 128),
  28. (105, 105, 105), (64, 224, 208), (205, 133, 63), (0, 128, 128), (72, 209, 204), (139, 69, 19), (255, 245, 238),
  29. (250, 240, 230), (152, 251, 152), (0, 255, 255), (135, 206, 235), (0, 191, 255), (176, 224, 230), (0, 250, 154),
  30. (245, 255, 250), (240, 230, 140), (245, 222, 179), (0, 139, 139), (143, 188, 143), (255, 0, 0), (240, 128, 128),
  31. (102, 205, 170), (60, 179, 113), (46, 139, 87), (165, 42, 42), (178, 34, 34), (175, 238, 238), (255, 248, 220),
  32. (218, 165, 32), (255, 250, 240), (253, 245, 230), (244, 164, 96), (210, 105, 30)]
  33. def draw_bbox(img, box, cls_name, identity=None, offset=(0, 0)):
  34. """
  35. draw box of an id
  36. """
  37. x1, y1, x2, y2 = [int(i + offset[idx % 2]) for idx, i in enumerate(box)]
  38. # set color and label text
  39. color = COLORS_10[identity %
  40. len(COLORS_10)] if identity is not None else COLORS_10[0]
  41. label = '{} {}'.format(cls_name, identity)
  42. # box text and bar
  43. t_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_PLAIN, 1, 1)[0]
  44. cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)
  45. cv2.rectangle(
  46. img, (x1, y1), (x1 + t_size[0] + 3, y1 + t_size[1] + 4), color, -1)
  47. cv2.putText(img, label, (x1, y1 +
  48. t_size[1] +
  49. 4), cv2.FONT_HERSHEY_PLAIN, 1, [255, 255, 255], 1)
  50. return img
  51. def draw_bboxes(img, bbox, identities=None, offset=(0, 0)):
  52. for i, box in enumerate(bbox):
  53. x1, y1, x2, y2 = [int(i) for i in box]
  54. x1 += offset[0]
  55. x2 += offset[0]
  56. y1 += offset[1]
  57. y2 += offset[1]
  58. # box text and bar
  59. id = int(identities[i]) if identities is not None else 0
  60. color = COLORS_10[id % len(COLORS_10)]
  61. label = '{} {}'.format("id", id)
  62. t_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_PLAIN, 2, 2)[0]
  63. cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)
  64. # cv2.rectangle(img,(x1, y1),(x1+t_size[0]+3,y1+t_size[1]+4), color,-1)
  65. cv2.putText(img, label, (x1, y1 +
  66. t_size[1] +
  67. 4), cv2.FONT_HERSHEY_PLAIN, 2, [255, 255, 255], 2)
  68. return img
  69. def draw_bboxes_conf(img, bbox, conf, identities=None, offset=(0, 0)):
  70. for i, box in enumerate(bbox):
  71. x1, y1, x2, y2 = [int(i) for i in box]
  72. x1 += offset[0]
  73. x2 += offset[0]
  74. y1 += offset[1]
  75. y2 += offset[1]
  76. # box text and bar
  77. id = int(identities[i]) if identities is not None else 0
  78. color = COLORS_10[id % len(COLORS_10)]
  79. label = '{}|{:.2f}'.format(id, conf[i])
  80. t_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_PLAIN, 2, 2)[0]
  81. cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)
  82. # cv2.rectangle(img,(x1, y1),(x1+t_size[0]+3,y1+t_size[1]+4), color,-1)
  83. cv2.putText(img, label, (x1, y1 +
  84. t_size[1] +
  85. 4), cv2.FONT_HERSHEY_PLAIN, 2, [255, 255, 255], 2)
  86. return img
  87. def draw_bboxes_conf_cls(
  88. img,
  89. bbox,
  90. conf,
  91. identities=None,
  92. offset=(
  93. 0,
  94. 0),
  95. cls_id_=0):
  96. for i, box in enumerate(bbox):
  97. x1, y1, x2, y2 = [int(i) for i in box]
  98. x1 += offset[0]
  99. x2 += offset[0]
  100. y1 += offset[1]
  101. y2 += offset[1]
  102. # box text and bar
  103. id = int(identities[i]) if identities is not None else 0
  104. color = COLORS_10[cls_id_ % len(COLORS_10)]
  105. label = '{}|{:.2f}'.format(id, conf[i])
  106. t_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_PLAIN, 2, 2)[0]
  107. cv2.rectangle(img, (x1, y1), (x2, y2), color, 2)
  108. # cv2.rectangle(img,(x1, y1),(x1+t_size[0]+3,y1+t_size[1]+4), color,-1)
  109. cv2.putText(img, label, (x1, y1 +
  110. t_size[1] +
  111. 4), cv2.FONT_HERSHEY_PLAIN, 2, [255, 255, 255], 2)
  112. return img
  113. def draw_bboxes_color(
  114. img, bbox, identities=None, color=(
  115. 255, 0, 0), offset=(
  116. 0, 0)):
  117. for i, box in enumerate(bbox):
  118. x1, y1, x2, y2 = [int(i) for i in box]
  119. x1 += offset[0]
  120. x2 += offset[0]
  121. y1 += offset[1]
  122. y2 += offset[1]
  123. # box text and bar
  124. id = int(identities[i]) if identities is not None else 0
  125. name = 'det'
  126. dx = 0
  127. if color == (0, 0, 255):
  128. name = 'reg'
  129. dx = 40
  130. elif color == (0, 255, 0):
  131. name = 'pred'
  132. dx = 20
  133. label = '{} {}'.format(name, id)
  134. t_size = cv2.getTextSize(label, cv2.FONT_HERSHEY_PLAIN, 2, 2)[0]
  135. cv2.rectangle(img, (x1, y1), (x2, y2), color, 3)
  136. # cv2.rectangle(img,(x1, y1),(x1+t_size[0]+3,y1+t_size[1]+4), color,-1)
  137. cv2.putText(img, label, (x1 +
  138. dx, y1 +
  139. t_size[1] +
  140. 4), cv2.FONT_HERSHEY_PLAIN, 2, [255, 255, 255], 2)
  141. return img
  142. def softmax(x):
  143. assert isinstance(x, np.ndarray), "expect x be a numpy array"
  144. x_exp = np.exp(x * 5)
  145. return x_exp / x_exp.sum()
  146. def softmin(x):
  147. assert isinstance(x, np.ndarray), "expect x be a numpy array"
  148. x_exp = np.exp(-x)
  149. return x_exp / x_exp.sum()
  150. if __name__ == '__main__':
  151. x = np.arange(10) / 10.
  152. x = np.array([0.5, 0.5, 0.5, 0.6, 1.])
  153. y = softmax(x)
  154. z = softmin(x)
  155. import ipdb
  156. ipdb.set_trace()

一站式算法开发平台、高性能分布式深度学习框架、先进算法模型库、视觉模型炼知平台、数据可视化分析平台等一系列平台及工具,在模型高效分布式训练、数据处理和可视分析、模型炼知和轻量化等技术上形成独特优势,目前已在产学研等各领域近千家单位及个人提供AI应用赋能

Contributors (1)