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.

yolact.py 4.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # Tencent is pleased to support the open source community by making ncnn available.
  2. #
  3. # Copyright (C) 2020 THL A29 Limited, a Tencent company. All rights reserved.
  4. #
  5. # Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. # in compliance with the License. You may obtain a copy of the License at
  7. #
  8. # https://opensource.org/licenses/BSD-3-Clause
  9. #
  10. # Unless required by applicable law or agreed to in writing, software distributed
  11. # under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. # CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. # specific language governing permissions and limitations under the License.
  14. import sys
  15. import cv2
  16. import numpy as np
  17. from ncnn.model_zoo import get_model
  18. from ncnn.utils import draw_detection_objects
  19. def draw_result(image, class_names, boxes, masks, classes, scores):
  20. colors = [
  21. [56, 0, 255],
  22. [226, 255, 0],
  23. [0, 94, 255],
  24. [0, 37, 255],
  25. [0, 255, 94],
  26. [255, 226, 0],
  27. [0, 18, 255],
  28. [255, 151, 0],
  29. [170, 0, 255],
  30. [0, 255, 56],
  31. [255, 0, 75],
  32. [0, 75, 255],
  33. [0, 255, 169],
  34. [255, 0, 207],
  35. [75, 255, 0],
  36. [207, 0, 255],
  37. [37, 0, 255],
  38. [0, 207, 255],
  39. [94, 0, 255],
  40. [0, 255, 113],
  41. [255, 18, 0],
  42. [255, 0, 56],
  43. [18, 0, 255],
  44. [0, 255, 226],
  45. [170, 255, 0],
  46. [255, 0, 245],
  47. [151, 255, 0],
  48. [132, 255, 0],
  49. [75, 0, 255],
  50. [151, 0, 255],
  51. [0, 151, 255],
  52. [132, 0, 255],
  53. [0, 255, 245],
  54. [255, 132, 0],
  55. [226, 0, 255],
  56. [255, 37, 0],
  57. [207, 255, 0],
  58. [0, 255, 207],
  59. [94, 255, 0],
  60. [0, 226, 255],
  61. [56, 255, 0],
  62. [255, 94, 0],
  63. [255, 113, 0],
  64. [0, 132, 255],
  65. [255, 0, 132],
  66. [255, 170, 0],
  67. [255, 0, 188],
  68. [113, 255, 0],
  69. [245, 0, 255],
  70. [113, 0, 255],
  71. [255, 188, 0],
  72. [0, 113, 255],
  73. [255, 0, 0],
  74. [0, 56, 255],
  75. [255, 0, 113],
  76. [0, 255, 188],
  77. [255, 0, 94],
  78. [255, 0, 18],
  79. [18, 255, 0],
  80. [0, 255, 132],
  81. [0, 188, 255],
  82. [0, 245, 255],
  83. [0, 169, 255],
  84. [37, 255, 0],
  85. [255, 0, 151],
  86. [188, 0, 255],
  87. [0, 255, 37],
  88. [0, 255, 0],
  89. [255, 0, 170],
  90. [255, 0, 37],
  91. [255, 75, 0],
  92. [0, 0, 255],
  93. [255, 207, 0],
  94. [255, 0, 226],
  95. [255, 245, 0],
  96. [188, 255, 0],
  97. [0, 255, 18],
  98. [0, 255, 75],
  99. [0, 255, 151],
  100. [255, 56, 0],
  101. [245, 255, 0],
  102. ]
  103. color_index = 0
  104. for box, mask, label, score in zip(boxes, masks, classes, scores):
  105. if score < 0.15:
  106. continue
  107. print(
  108. "%s = %.5f at %.2f %.2f %.2f x %.2f\n"
  109. % (label, score, box[0], box[1], box[2], box[3])
  110. )
  111. cv2.rectangle(
  112. image,
  113. (int(box[0]), int(box[1])),
  114. (int(box[0] + box[2]), int(int(box[1] + box[3]))),
  115. (255, 0, 0),
  116. )
  117. text = "%s %.1f%%" % (class_names[int(label)], score * 100)
  118. label_size, baseLine = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
  119. x = box[0]
  120. y = box[1] - label_size[1] - baseLine
  121. if y < 0:
  122. y = 0
  123. if x + label_size[0] > image.shape[1]:
  124. x = image.shape[1] - label_size[0]
  125. cv2.rectangle(
  126. image,
  127. (int(x), int(y)),
  128. (int(x + label_size[0]), int(y + label_size[1] + baseLine)),
  129. (255, 255, 255),
  130. -1,
  131. )
  132. cv2.putText(
  133. image,
  134. text,
  135. (int(x), int(y + label_size[1])),
  136. cv2.FONT_HERSHEY_SIMPLEX,
  137. 0.5,
  138. (0, 0, 0),
  139. )
  140. image[mask] = image[mask] * 0.5 + np.array(colors[color_index]) * 0.5
  141. color_index += 1
  142. cv2.imshow("image", image)
  143. cv2.waitKey(0)
  144. if __name__ == "__main__":
  145. if len(sys.argv) != 2:
  146. print("Usage: %s [imagepath]" % (sys.argv[0]))
  147. sys.exit(0)
  148. imagepath = sys.argv[1]
  149. m = cv2.imread(imagepath)
  150. if m is None:
  151. print("cv2.imread %s failed\n" % (imagepath))
  152. sys.exit(0)
  153. net = get_model(
  154. "yolact",
  155. target_size=550,
  156. confidence_threshold=0.05,
  157. nms_threshold=0.5,
  158. keep_top_k=200,
  159. num_threads=4,
  160. use_gpu=True,
  161. )
  162. boxes, masks, classes, scores = net(m)
  163. draw_result(m, net.class_names, boxes, masks, classes, scores)