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.1 kB

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