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