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.

peleenetssd.py 3.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. def draw_detection_objects_seg(image, class_names, objects, mat_map):
  20. color = [128, 255, 128, 244, 35, 232]
  21. color_count = len(color)
  22. for obj in objects:
  23. print(
  24. "%d = %.5f at %.2f %.2f %.2f x %.2f\n"
  25. % (obj.label, obj.prob, obj.rect.x, obj.rect.y, obj.rect.w, obj.rect.h)
  26. )
  27. cv2.rectangle(
  28. image,
  29. (int(obj.rect.x), int(obj.rect.y)),
  30. (int(obj.rect.x + obj.rect.w), int(obj.rect.y + obj.rect.h)),
  31. (255, 0, 0),
  32. )
  33. text = "%s %.1f%%" % (class_names[int(obj.label)], obj.prob * 100)
  34. label_size, baseLine = cv2.getTextSize(text, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
  35. x = obj.rect.x
  36. y = obj.rect.y - label_size[1] - baseLine
  37. if y < 0:
  38. y = 0
  39. if x + label_size[0] > image.shape[1]:
  40. x = image.shape[1] - label_size[0]
  41. cv2.rectangle(
  42. image,
  43. (int(x), int(y)),
  44. (int(x + label_size[0]), int(y + label_size[1] + baseLine)),
  45. (255, 255, 255),
  46. -1,
  47. )
  48. cv2.putText(
  49. image,
  50. text,
  51. (int(x), int(y + label_size[1])),
  52. cv2.FONT_HERSHEY_SIMPLEX,
  53. 0.5,
  54. (0, 0, 0),
  55. )
  56. width = mat_map.w
  57. height = mat_map.h
  58. size = mat_map.c
  59. img_index2 = 0
  60. threshold = 0.45
  61. ptr2 = np.array(mat_map)
  62. for i in range(height):
  63. ptr1 = image[i].flatten()
  64. img_index1 = 0
  65. for j in range(width):
  66. maxima = threshold
  67. index = -1
  68. for c in range(size):
  69. # const float* ptr3 = ptr2 + c*width*height
  70. ptr3 = ptr2[c].flatten()
  71. if ptr3[img_index2] > maxima:
  72. maxima = ptr3[img_index2]
  73. index = c
  74. if index > -1:
  75. color_index = (index) * 3
  76. if color_index < color_count:
  77. b = color[color_index]
  78. g = color[color_index + 1]
  79. r = color[color_index + 2]
  80. ptr1[img_index1] = b / 2 + ptr1[img_index1] / 2
  81. ptr1[img_index1 + 1] = g / 2 + ptr1[img_index1 + 1] / 2
  82. ptr1[img_index1 + 2] = r / 2 + ptr1[img_index1 + 2] / 2
  83. img_index1 += 3
  84. img_index2 += 1
  85. image[i] = ptr1.reshape(image[i].shape)
  86. cv2.imshow("image", image)
  87. cv2.waitKey(0)
  88. if __name__ == "__main__":
  89. if len(sys.argv) != 2:
  90. print("Usage: %s [imagepath]\n" % (sys.argv[0]))
  91. sys.exit(0)
  92. imagepath = sys.argv[1]
  93. m = cv2.imread(imagepath)
  94. if m is None:
  95. print("cv2.imread %s failed\n" % (imagepath))
  96. sys.exit(0)
  97. net = get_model("peleenet_ssd", num_threads=4, use_gpu=True)
  98. objects, seg_out = net(m)
  99. draw_detection_objects_seg(m, net.class_names, objects, seg_out)