# 坐标顺序: 上-》左-》下-》右
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)

效果

python 绘制对象检测框及中文信息标注的更多相关文章

  1. python imageai 对象检测、对象识别

    imageai库里面提供了目标识别,其实也可以说是目标检测,和现在很多的收集一样就是物体识别.他可以帮你识别出各种各样生活中遇见的事物.比如猫.狗.车.马.人.电脑.收集等等. 感觉imageai有点 ...

  2. 基于 python imageai 对象检测 目标检测 识别 视频

    1.视频连接如下: http://www.iqiyi.com/w_19s6vownit.html

  3. JS window对象 Navigator对象 Navigator 对象包含有关浏览器的信息,通常用于检测浏览器与操作系统的版本。

    Navigator对象 Navigator 对象包含有关浏览器的信息,通常用于检测浏览器与操作系统的版本. 对象属性: 查看浏览器的名称和版本,代码如下: <script type=" ...

  4. 计算机视觉中的对象检测,Python用几段代码就能实现

    目前计算机视觉(CV)与自然语言处理(NLP)及语音识别并列为人工智能三大热点方向,而计算机视觉中的对象检测(objectdetection)应用非常广泛,比如自动驾驶.视频监控.工业质检.医疗诊断等 ...

  5. [object_detect]使用MobileNetSSD进行对象检测

    使用MobileNetSSD进行对象检测 1.单帧图片识别 object_detection.py # 导入必要的包 import numpy as np import argparse import ...

  6. ZPL打印中文信息

    博客来源:http://www.cnblogs.com/Geton/p/3595312.html 相信各位在实际的项目中,需要开发打条码模块的也会有不少,很多同行肯定也一直觉得斑马打印机很不错,但是Z ...

  7. ROC,AUC,PR,AP介绍及python绘制

    这里介绍一下如题所述的四个概念以及相应的使用python绘制曲线: 参考博客:http://kubicode.me/2016/09/19/Machine%20Learning/AUC-Calculat ...

  8. python基础系列教程——Python中的编码问题,中文乱码问题

    python基础系列教程——Python中的编码问题,中文乱码问题 如果不声明编码,则中文会报错,即使是注释也会报错. # -*- coding: UTF-8 -*- 或者 #coding=utf-8 ...

  9. Python 进行目标检测

    一.前言 从学单片机开始鼓捣C语言,到现在为了学CV鼓捣Python,期间在CSDN.简书.博客园和github这些地方得到了很多帮助,所以也想把自己做的一些小东西分享给大家,希望能帮助到别人.记录人 ...

随机推荐

  1. hdjs---laravel 框架使用hdjs 实现多图上传功能

    hdjs---laravel 框架使用hdjs 实现多图上传功能 一.总结 一句话总结: [在网上找hdjs的使用实例]和[能遇见讲hdjs的视频],也是完全搞懂hdjs的不错的方式 1.hdjs中的 ...

  2. Moodle LMS 开源教育系统

    http://docs.websoft9.com/xdocs/moodle-image-guide/   帮助文档 示例图:没有细研究

  3. Uep弹窗showModalDialog的使用

    function imageMaintain() { $.showModalDialog($$pageContextPath + "uepI/imageMaintain.do?service ...

  4. Charles解析https,unknown错误解决办法

    之前一直使用Charles,抓包https的没有问题,enable ssl的时候出现unknown消息,disable ssl的时候出现乱码,解决办法: 在设置–>通用–>关于本机–> ...

  5. 浏览器端-W3School-HTML:HTML DOM Object 对象

    ylbtech-浏览器端-W3School-HTML:HTML DOM Object 对象 1.返回顶部 1. HTML DOM Object 对象 Object 对象 Object 对象代表 HTM ...

  6. phpmyadmin python mysql全部正常显示中文的关键

    1. 建表.列时在phpmyadmin中将编码设置为utf8_general_ci 2. python中使用sql连接时设定charset为utf8,注意不能是utf-8! 例如: def Conne ...

  7. ControlTemplate in WPF —— ComboBox

    <ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" x ...

  8. MYSQL5.5源码包编译安装

    MYSQL5.5源码安装首先安装必要的库yum -y install gcc*###### 安装 MYSQL ######首先安装camke 一.支持YUM,则yum install -y cmake ...

  9. 【Ruby on Rails 学习四】简单的代码快和错误处理

    第一个例子: 1 ... 5000的加法运算 1 sum = 0 2 i = 1 3 while true 4 sum += i 5 i += 1 6 break if i == 5001 7 end ...

  10. 关于confusion_matrix()返回的矩阵的索引顺序(类别顺序)

    转载至:https://blog.csdn.net/m0_38061927/article/details/77198990 1.混淆矩阵 混淆矩阵是机器学习中总结分类模型预测结果的情形分析表,以矩阵 ...