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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. import ncnn
  18. from ncnn.model_zoo import get_model
  19. from ncnn.utils import draw_detection_objects
  20. def draw_result(image, class_names, boxes, masks, classes, scores):
  21. colors = [
  22. [56, 0, 255],
  23. [226, 255, 0],
  24. [0, 94, 255],
  25. [0, 37, 255],
  26. [0, 255, 94],
  27. [255, 226, 0],
  28. [0, 18, 255],
  29. [255, 151, 0],
  30. [170, 0, 255],
  31. [0, 255, 56],
  32. [255, 0, 75],
  33. [0, 75, 255],
  34. [0, 255, 169],
  35. [255, 0, 207],
  36. [75, 255, 0],
  37. [207, 0, 255],
  38. [37, 0, 255],
  39. [0, 207, 255],
  40. [94, 0, 255],
  41. [0, 255, 113],
  42. [255, 18, 0],
  43. [255, 0, 56],
  44. [18, 0, 255],
  45. [0, 255, 226],
  46. [170, 255, 0],
  47. [255, 0, 245],
  48. [151, 255, 0],
  49. [132, 255, 0],
  50. [75, 0, 255],
  51. [151, 0, 255],
  52. [0, 151, 255],
  53. [132, 0, 255],
  54. [0, 255, 245],
  55. [255, 132, 0],
  56. [226, 0, 255],
  57. [255, 37, 0],
  58. [207, 255, 0],
  59. [0, 255, 207],
  60. [94, 255, 0],
  61. [0, 226, 255],
  62. [56, 255, 0],
  63. [255, 94, 0],
  64. [255, 113, 0],
  65. [0, 132, 255],
  66. [255, 0, 132],
  67. [255, 170, 0],
  68. [255, 0, 188],
  69. [113, 255, 0],
  70. [245, 0, 255],
  71. [113, 0, 255],
  72. [255, 188, 0],
  73. [0, 113, 255],
  74. [255, 0, 0],
  75. [0, 56, 255],
  76. [255, 0, 113],
  77. [0, 255, 188],
  78. [255, 0, 94],
  79. [255, 0, 18],
  80. [18, 255, 0],
  81. [0, 255, 132],
  82. [0, 188, 255],
  83. [0, 245, 255],
  84. [0, 169, 255],
  85. [37, 255, 0],
  86. [255, 0, 151],
  87. [188, 0, 255],
  88. [0, 255, 37],
  89. [0, 255, 0],
  90. [255, 0, 170],
  91. [255, 0, 37],
  92. [255, 75, 0],
  93. [0, 0, 255],
  94. [255, 207, 0],
  95. [255, 0, 226],
  96. [255, 245, 0],
  97. [188, 255, 0],
  98. [0, 255, 18],
  99. [0, 255, 75],
  100. [0, 255, 151],
  101. [255, 56, 0],
  102. [245, 255, 0],
  103. ]
  104. color_index = 0
  105. for box, mask, label, score in zip(boxes, masks, classes, scores):
  106. if score < 0.15:
  107. continue
  108. print(
  109. "%s = %.5f at %.2f %.2f %.2f x %.2f\n"
  110. % (label, score, box[0], box[1], box[2], box[3])
  111. )
  112. cv2.rectangle(
  113. image,
  114. (int(box[0]), int(box[1])),
  115. (int(box[0] + box[2]), int(int(box[1] + box[3]))),
  116. (255, 0, 0),
  117. )
  118. text = "%s %.1f%%" % (class_names[int(label)], score * 100)
  119. label_size, baseLine = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
  120. x = box[0]
  121. y = box[1] - label_size[1] - baseLine
  122. if y < 0:
  123. y = 0
  124. if x + label_size[0] > image.shape[1]:
  125. x = image.shape[1] - label_size[0]
  126. cv2.rectangle(
  127. image,
  128. (int(x), int(y)),
  129. (int(x + label_size[0]), int(y + label_size[1] + baseLine)),
  130. (255, 255, 255),
  131. -1,
  132. )
  133. cv2.putText(
  134. image,
  135. text,
  136. (int(x), int(y + label_size[1])),
  137. cv2.FONT_HERSHEY_SIMPLEX,
  138. 0.5,
  139. (0, 0, 0),
  140. )
  141. image[mask] = image[mask] * 0.5 + np.array(colors[color_index]) * 0.5
  142. color_index += 1
  143. cv2.imshow("image", image)
  144. cv2.waitKey(0)
  145. if __name__ == "__main__":
  146. if len(sys.argv) != 2:
  147. print("Usage: %s [imagepath]" % (sys.argv[0]))
  148. sys.exit(0)
  149. imagepath = sys.argv[1]
  150. m = cv2.imread(imagepath)
  151. if m is None:
  152. print("cv2.imread %s failed\n" % (imagepath))
  153. sys.exit(0)
  154. net = get_model(
  155. "yolact",
  156. target_size=550,
  157. confidence_threshold=0.05,
  158. nms_threshold=0.5,
  159. keep_top_k=200,
  160. num_threads=4,
  161. use_gpu=True,
  162. )
  163. boxes, masks, classes, scores = net(m)
  164. draw_result(m, net.class_names, boxes, masks, classes, scores)