From 1a6c4c65582baf097bb0e2428b197632503f0258 Mon Sep 17 00:00:00 2001 From: Valeriu Lacatusu Date: Fri, 19 Jan 2018 15:26:07 +0100 Subject: [PATCH 1/3] Draw labels name Draw labels name next to corresponding bounding boxes when the mapping of id to names is specified. --- python/mxnet/image/detection.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/python/mxnet/image/detection.py b/python/mxnet/image/detection.py index caaa4006302d..9d88eea5c5c4 100644 --- a/python/mxnet/image/detection.py +++ b/python/mxnet/image/detection.py @@ -803,7 +803,7 @@ def check_label_shape(self, label_shape): raise ValueError(msg) def draw_next(self, color=None, thickness=2, mean=None, std=None, clip=True, - waitKey=None, window_name='draw_next'): + waitKey=None, window_name='draw_next', id2labels=None): """Display next image with bounding boxes drawn. Parameters @@ -822,6 +822,8 @@ def draw_next(self, color=None, thickness=2, mean=None, std=None, clip=True, Hold the window for waitKey milliseconds if set, skip ploting if None window_name : str Plot window name if waitKey is set. + id2labels : dict + Mapping of labels id to labels name. Returns ------- @@ -889,6 +891,18 @@ def draw_next(self, color=None, thickness=2, mean=None, std=None, clip=True, y2 = int(label[i, 4] * height) bc = np.random.rand(3) * 255 if not color else color cv2.rectangle(image, (x1, y1), (x2, y2), bc, thickness) + + if id2labels is not None: + cls_id = int(label[i, 0]) + if cls_id in id2labels: + cls_name = id2labels[cls_id] + text = "{:s}".format(cls_name) + font = cv2.FONT_HERSHEY_SIMPLEX + font_scale = 0.5 + text_height = cv2.getTextSize(text, font, font_scale, 2)[0][1] + cv2.putText(image, text, (x1 + 5, y1 + text_height + 5), + font, font_scale, (255, 255, 255), 2) + if waitKey is not None: cv2.imshow(window_name, image) cv2.waitKey(waitKey) From 595c7277713d1f7c8e7b1b7477f27052f8960595 Mon Sep 17 00:00:00 2001 From: Valeriu Lacatusu Date: Fri, 19 Jan 2018 15:35:32 +0100 Subject: [PATCH 2/3] Update detection.py --- python/mxnet/image/detection.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/python/mxnet/image/detection.py b/python/mxnet/image/detection.py index 9d88eea5c5c4..f866d430c821 100644 --- a/python/mxnet/image/detection.py +++ b/python/mxnet/image/detection.py @@ -900,8 +900,7 @@ def draw_next(self, color=None, thickness=2, mean=None, std=None, clip=True, font = cv2.FONT_HERSHEY_SIMPLEX font_scale = 0.5 text_height = cv2.getTextSize(text, font, font_scale, 2)[0][1] - cv2.putText(image, text, (x1 + 5, y1 + text_height + 5), - font, font_scale, (255, 255, 255), 2) + cv2.putText(image, text, (x1 + 5, y1 + text_height + 5), font, 0.5, (255, 255, 255), 2) if waitKey is not None: cv2.imshow(window_name, image) From 3b34b5031e083bece75fadea72e9018923a2455c Mon Sep 17 00:00:00 2001 From: Valeriu Lacatusu Date: Fri, 19 Jan 2018 15:42:17 +0100 Subject: [PATCH 3/3] Update detection.py Fixed missing variable declaration Fixing lint issues --- python/mxnet/image/detection.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python/mxnet/image/detection.py b/python/mxnet/image/detection.py index f866d430c821..3b9f64e1220f 100644 --- a/python/mxnet/image/detection.py +++ b/python/mxnet/image/detection.py @@ -891,7 +891,6 @@ def draw_next(self, color=None, thickness=2, mean=None, std=None, clip=True, y2 = int(label[i, 4] * height) bc = np.random.rand(3) * 255 if not color else color cv2.rectangle(image, (x1, y1), (x2, y2), bc, thickness) - if id2labels is not None: cls_id = int(label[i, 0]) if cls_id in id2labels: @@ -900,8 +899,9 @@ def draw_next(self, color=None, thickness=2, mean=None, std=None, clip=True, font = cv2.FONT_HERSHEY_SIMPLEX font_scale = 0.5 text_height = cv2.getTextSize(text, font, font_scale, 2)[0][1] - cv2.putText(image, text, (x1 + 5, y1 + text_height + 5), font, 0.5, (255, 255, 255), 2) - + tc = (255, 255, 255) + tpos = (x1 + 5, y1 + text_height + 5) + cv2.putText(image, text, tpos, font, font_scale, tc, 2) if waitKey is not None: cv2.imshow(window_name, image) cv2.waitKey(waitKey)