参考资料:
https://blog.csdn.net/weixin_41735859/article/details/106599903
在目标检测等CV领域的任务里,经常会涉及到在图片上绘制BBox,也就是画一个矩形框把目标框起来,并且还可能会打上标签。这篇随笔记录一下在Python里如何在图片上绘制BoundingBox。
我的主要参考对象是参考资料里的方法,参考资料里给出了opencv和PIL来绘制的方法,考虑到大多数机器学习环境里都会有PIL/pillow(这玩意儿都快和python绑定了吧,是python原生的),所以我就记录一下用PIL绘制的方法(不用装新的包嘛!)废话不多说,直接放代码和效果图,并且附上详细的注释:
from PIL import Image, ImageDraw, ImageFont # 1.读取图片 real_im = T.ToPILImage()(XXX_1) bbox_list = XXX_2 label_list = XXX_3 # 2.设置字体格式及大小 font = ImageFont.truetype(font='./Courier-12.ttf', size=np.floor(1.5e-2 * np.shape(real_im)[1] + 10).astype('int32')) # 3.给画笔添加句柄(我就是要在real_im上画画!) draw = ImageDraw.Draw(real_im) for (label_, bbox_) in zip(label_list, bbox_list): # 4.获取边框坐标 # 最终边框格式 bbox = [xl, yl, xr, yr] bbox_[0], bbox_[1], bbox_[2], bbox_[3] = bbox_[0], bbox_[1], bbox_[0] + bbox_[2], bbox_[1] + bbox_[3] bbox_[0] = int(bbox_[0] * real_im.size[0]) bbox_[1] = int(bbox_[1] * real_im.size[1]) bbox_[2] = int(bbox_[2] * real_im.size[0]) bbox_[3] = int(bbox_[3] * real_im.size[1]) # 5.获取label长宽 label_size = draw.textsize(label_, font) # 6.设置label起点 text_origin = np.array([bbox_[0], bbox_[1] + 0.2 * label_size[1]]) # 7.随机一个RGB值作颜色 color_for_draw = tuple(np.random.randint(0, 255, size=[3])) # 8. 绘制矩形框,加入label文本 draw.rectangle([bbox_[0], bbox_[1], bbox_[2], bbox_[3]],outline=color_for_draw, width=2) draw.rectangle([tuple(text_origin), tuple(text_origin + label_size)], fill=color_for_draw) draw.text(text_origin, str(label_), fill=(255, 255, 255), font=font) # 9.删除画笔句柄 del draw
下面说一下几个要点: