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