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

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