-
Notifications
You must be signed in to change notification settings - Fork 208
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Feature]: support image visualization for tensorboard and wandb #15
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,7 +130,14 @@ | |
] | ||
|
||
# evaluation | ||
eval_config = dict(interval=10, gpu_collect=False) | ||
eval_config = dict( | ||
interval=10, | ||
gpu_collect=False, | ||
visualization_config=dict( | ||
vis_num=10, | ||
score_thr=0.5, | ||
) # show by TensorboardLoggerHookV2 and WandbLoggerHookV2 | ||
) | ||
eval_pipelines = [ | ||
dict( | ||
mode='test', | ||
|
@@ -168,7 +175,8 @@ | |
interval=100, | ||
hooks=[ | ||
dict(type='TextLoggerHook'), | ||
dict(type='TensorboardLoggerHook') | ||
dict(type='TensorboardLoggerHookV2'), | ||
# dict(type='WandbLoggerHookV2'), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wandb config example should be added in another file or using comment format There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add comment There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. wandb initialization config is missing |
||
]) | ||
|
||
export = dict(use_jit=False) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
# Copyright (c) Alibaba, Inc. and its affiliates. | ||
from .image import imshow_bboxes, imshow_keypoints | ||
from .image import imshow_bboxes, imshow_keypoints, imshow_label | ||
|
||
__all__ = ['imshow_bboxes', 'imshow_keypoints'] | ||
__all__ = ['imshow_bboxes', 'imshow_keypoints', 'imshow_label'] |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,119 @@ | ||
# Copyright (c) OpenMMLab. All rights reserved. | ||
# Adapt from https://github.com/open-mmlab/mmpose/blob/master/mmpose/core/visualization/image.py | ||
import math | ||
import os | ||
from urllib import request | ||
|
||
import cv2 | ||
import mmcv | ||
import numpy as np | ||
from mmcv.utils.misc import deprecated_api_warning | ||
from PIL import Image, ImageDraw, ImageFont | ||
from torch.hub import DEFAULT_CACHE_DIR | ||
|
||
|
||
def download_font(save_path=None): | ||
url_path = 'http://pai-vision-data-hz.oss-accelerate.aliyuncs.com/EasyCV/pkgs/simhei.ttf' | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add this font to git and auto package it into our python whl There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. put simhei.ttf to resource dir, and support package to whl and zip |
||
if save_path is None: | ||
save_dir = DEFAULT_CACHE_DIR | ||
if not os.path.exists(save_dir): | ||
os.makedirs(save_dir) | ||
save_path = os.path.join(save_dir, 'simhei.ttf') | ||
|
||
if os.path.exists(save_path): | ||
return save_path | ||
|
||
f = request.urlopen(url_path) | ||
with open(save_path, 'wb') as fw: | ||
fw.write(f.read()) | ||
|
||
return save_path | ||
|
||
|
||
def put_text(img, xy, text, fill, font_path, size=20): | ||
"""support chinese text | ||
""" | ||
img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB)) | ||
draw = ImageDraw.Draw(img) | ||
fontText = ImageFont.truetype(font_path, size=size, encoding='utf-8') | ||
draw.text(xy, text, fill=fill, font=fontText) | ||
img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR) | ||
return img | ||
|
||
|
||
def imshow_label(img, | ||
labels, | ||
text_color='blue', | ||
font_size=20, | ||
thickness=1, | ||
font_scale=0.5, | ||
intervel=5, | ||
show=True, | ||
win_name='', | ||
wait_time=0, | ||
out_file=None): | ||
"""Draw images with labels on an image. | ||
|
||
Args: | ||
img (str or ndarray): The image to be displayed. | ||
labels (str or list[str]): labels of each image. | ||
text_color (str or tuple or :obj:`Color`): Color of texts. | ||
font_size (int): Size of font. | ||
thickness (int): Thickness of lines. | ||
font_scale (float): Font scales of texts. | ||
intervel(int): interval pixels between multiple labels | ||
show (bool): Whether to show the image. | ||
win_name (str): The window name. | ||
wait_time (int): Value of waitKey param. | ||
out_file (str, optional): The filename to write the image. | ||
|
||
Returns: | ||
ndarray: The image with bboxes drawn on it. | ||
""" | ||
font_path = download_font() | ||
img = mmcv.imread(img) | ||
img = np.ascontiguousarray(img) | ||
labels = [labels] if isinstance(labels, str) else labels | ||
|
||
cur_height = 0 | ||
for label in labels: | ||
# roughly estimate the proper font size | ||
text_size, text_baseline = cv2.getTextSize(label, | ||
cv2.FONT_HERSHEY_DUPLEX, | ||
font_scale, thickness) | ||
|
||
org = (text_baseline + text_size[1], | ||
text_baseline + text_size[1] + cur_height) | ||
|
||
# support chinese text | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. support chinese font |
||
# TODO: Unify the font of cv2 and PIL, and auto get font_size according to the font_scale | ||
img = put_text( | ||
img, | ||
org, | ||
text=label, | ||
fill=text_color, | ||
font_path=font_path, | ||
size=font_size) | ||
|
||
# cv2.putText(img, label, org, cv2.FONT_HERSHEY_DUPLEX, font_scale, | ||
# mmcv.color_val(text_color), thickness) | ||
|
||
cur_height += text_baseline + text_size[1] + intervel | ||
|
||
if show: | ||
mmcv.imshow(img, win_name, wait_time) | ||
if out_file is not None: | ||
mmcv.imwrite(img, out_file) | ||
|
||
return img | ||
|
||
|
||
def imshow_bboxes(img, | ||
bboxes, | ||
labels=None, | ||
colors='green', | ||
text_color='white', | ||
font_size=20, | ||
thickness=1, | ||
font_scale=0.5, | ||
show=True, | ||
|
@@ -29,6 +130,7 @@ def imshow_bboxes(img, | |
labels (str or list[str], optional): labels of each bbox. | ||
colors (list[str or tuple or :obj:`Color`]): A list of colors. | ||
text_color (str or tuple or :obj:`Color`): Color of texts. | ||
font_size (int): Size of font. | ||
thickness (int): Thickness of lines. | ||
font_scale (float): Font scales of texts. | ||
show (bool): Whether to show the image. | ||
|
@@ -39,6 +141,7 @@ def imshow_bboxes(img, | |
Returns: | ||
ndarray: The image with bboxes drawn on it. | ||
""" | ||
font_path = download_font() | ||
|
||
# adapt to mmcv.imshow_bboxes input format | ||
bboxes = np.split( | ||
|
@@ -58,11 +161,10 @@ def imshow_bboxes(img, | |
out_file=None) | ||
|
||
if labels is not None: | ||
if not isinstance(labels, list): | ||
labels = [labels for _ in range(len(bboxes))] | ||
assert len(labels) == len(bboxes) | ||
|
||
for bbox, label, color in zip(bboxes, labels, colors): | ||
label = str(label) | ||
bbox_int = bbox[0, :4].astype(np.int32) | ||
# roughly estimate the proper font size | ||
text_size, text_baseline = cv2.getTextSize(label, | ||
|
@@ -74,9 +176,18 @@ def imshow_bboxes(img, | |
text_y2 = text_y1 + text_size[1] + text_baseline | ||
cv2.rectangle(img, (text_x1, text_y1), (text_x2, text_y2), color, | ||
cv2.FILLED) | ||
cv2.putText(img, label, (text_x1, text_y2 - text_baseline), | ||
cv2.FONT_HERSHEY_DUPLEX, font_scale, | ||
mmcv.color_val(text_color), thickness) | ||
# cv2.putText(img, label, (text_x1, text_y2 - text_baseline), | ||
# cv2.FONT_HERSHEY_DUPLEX, font_scale, | ||
# mmcv.color_val(text_color), thickness) | ||
|
||
# support chinese text | ||
# TODO: Unify the font of cv2 and PIL, and auto get font_size according to the font_scale | ||
img = put_text( | ||
img, (text_x1, text_y1), | ||
text=label, | ||
fill=text_color, | ||
font_path=font_path, | ||
size=font_size) | ||
|
||
if show: | ||
mmcv.imshow(img, win_name, wait_time) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
has nothing to do with this pr, fix previous bug, please refer to: #6