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

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