# 坐标顺序: 上-》左-》下-》右
def draw_bounding_box_on_image(image,
ymin,
xmin,
ymax,
xmax,
color='red',
thickness=4,
display_str_list=(),
use_normalized_coordinates=True):
"""
Args:
image: a cv2 object.
ymin: ymin of bounding box.
xmin: xmin of bounding box.
ymax: ymax of bounding box.
xmax: xmax of bounding box.
color: color to draw bounding box. Default is red.
thickness: line thickness. Default value is 4.
display_str_list: list of strings to display in box
(each to be shown on its own line).
use_normalized_coordinates: If True (default), treat coordinates
ymin, xmin, ymax, xmax as relative to the image. Otherwise treat
coordinates as absolute.
"""
image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
draw = ImageDraw.Draw(image)
# 获取图像的宽度与高度
im_width, im_height = image.size
if use_normalized_coordinates:
(left, right, top, bottom) = (xmin * im_width, xmax * im_width,
ymin * im_height, ymax * im_height)
else:
(left, right, top, bottom) = (xmin, xmax, ymin, ymax) # 绘制Box框
draw.line([(left, top), (left, bottom), (right, bottom),
(right, top), (left, top)], width=thickness, fill=color) # 加载字体
try:
font = ImageFont.truetype("font/simsun.ttc", 24, encoding="utf-8")
except IOError:
font = ImageFont.load_default() # 计算显示文字的宽度集合 、高度集合
display_str_width = [font.getsize(ds)[0] for ds in display_str_list]
display_str_height = [font.getsize(ds)[1] for ds in display_str_list]
# 计算显示文字的总宽度
total_display_str_width = sum(display_str_width) + max(display_str_width) * 1.1
# 计算显示文字的最大高度
total_display_str_height = max(display_str_height) if top > total_display_str_height:
text_bottom = top
else:
text_bottom = bottom + total_display_str_height # 计算文字背景框最右侧可到达的像素位置
if right < (left + total_display_str_width):
text_right = right
else:
text_right = left + total_display_str_width # 绘制文字背景框
draw.rectangle(
[(left, text_bottom), (text_right, text_bottom - total_display_str_height)],
fill=color) # 计算文字背景框可容纳的文字,若超出部分不显示,改为补充“..”
for index in range(len(display_str_list[::1])):
current_right = (left + (max(display_str_width)) + sum(display_str_width[0:index + 1])) if current_right < text_right:
print(current_right)
display_str = display_str_list[:index + 1]
else:
display_str = display_str_list[0:index - 1] + '...'
break # 绘制文字
draw.text(
(left + max(display_str_width) / 2, text_bottom - total_display_str_height),
display_str,
fill='black',
font=font) return cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2BGR)

效果

最新文章

  1. iOS 开发之路(AES/DES加密实现) 三
  2. windows下用一台机器配置分布式redis(主从服务器)
  3. ruby中proc和lambda的return区别
  4. 低功耗蓝牙4.0BLE编程-nrf51822开发(6)-Battery Service
  5. ktv
  6. QTabWidget 实现类似QQ聊天窗口(拖动分离出新的窗口)
  7. 小强的HTML5移动开发之路(9)——坦克大战游戏3
  8. Docker最全教程之使用Tencent Hub来完成CI(九)
  9. 莫烦sklearn学习自修第七天【交叉验证】
  10. IdentityServer4 中文文档 -8- (快速入门)设置和概览
  11. pb数据窗口之间的传参
  12. LOJ.6282.数列分块入门6(块状链表/分块)
  13. BFC(块级格式化上下文)
  14. @transactional作用和事务
  15. Git 的安装步骤
  16. OpenGL学习--05--纹理立方体--BMP文件格式详解(转载)
  17. shell教程一:字符串操作
  18. ios 在https情况下,使用webview加载url出错的解决方法 ios9 适配问题
  19. Unity5 Shader Stripping 导致 LightMap 全部丢失的解决方法
  20. javascript的解析顺序

热门文章

  1. 【Spark机器学习速成宝典】模型篇08保序回归【Isotonic Regression】(Python版)
  2. Cassandra 如何处理跨数据中心的数据库延时问题
  3. 使用SNMP监控服务器运行情况
  4. P3373 【模板】线段树 2 (未完待续)
  5. vue 钩子函数中获取不到DOM节点
  6. Python中转换角度为弧度的radians()方法
  7. harbor设置开机自启
  8. oracle 11g错误ora-01033:oracle initialization or shutdown in progress解决办法
  9. mapreduce的shufflue过程
  10. 小程序图片预览 wx.previewImage