# 坐标顺序: 上-》左-》下-》右
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. Ajax+PHP实现的进度条--实例

    之前重点学习PHP,所以javascript.Ajax都比较弱一点.现在也开始补课了,今天实现了一个进度条的例子,感觉Ajax实现动态页面真的很厉害,并没有想象中的那么难理解. 进度条作为反应实时传输 ...

  2. 后盾网lavarel视频项目---模型一对多关联简单实例

    后盾网lavarel视频项目---模型一对多关联简单实例 一.总结 一句话总结: 在模型中定义一个方法来设置一对多关联:return $this->hasMany(Video::class); ...

  3. UVA 572 -- Oil Deposits(DFS求连通块+种子填充算法)

    UVA 572 -- Oil Deposits(DFS求连通块) 图也有DFS和BFS遍历,由于DFS更好写,所以一般用DFS寻找连通块. 下述代码用一个二重循环来找到当前格子的相邻8个格子,也可用常 ...

  4. android打包生成apk时自定义文件名版本号。自定义项目字段等等

    早期的AS2.0版本左右中这样配置: app---->build.gradle中设置 applicationVariants.all { variant -> variant.output ...

  5. 介绍一下 NDK?

    1.NDK 是一系列工具的集合 NDK 提供了一系列的工具,帮助开发者快速开发 C(或 C++)的动态库,并能自动将 so 和 java 应用一起打包成 apk.NDK 集成了交叉编译器,并提供了相应 ...

  6. Windows 10下怎么远程连接 Ubuntu 16.0.4(小白级教程)

    前言: 公司因为用Ruby做开发,所有适用了Ubuntu系统,但是自己笔记本是W10,又不想装双系统,搭建开发环境,便想到倒不如自己远程操控公司电脑,这样在家的时候也可以处理一些问题.故此便有了下面的 ...

  7. 【flask】使用类组织配置-使用工厂函数创建程序实例

    [需求] 使用配置类管理flask管理测试环境, 通过1个参数即可控制Flask是运行develpment环境还是production环境(数据库配置,邮件配置也要根据环境的变化而变化) [思路] 1 ...

  8. Prism 订阅事件 IEventAggregator 说明

    本节学习了Event Aggregation事件聚合,这个在Prism中很重要,特别是对于Module间的通信.除了前面介绍的Command可以用于模块间的通信,还有我们这一节介绍的Event Agg ...

  9. web开发(四) 一次性验证码的代码实现

    在网上看见一篇不错的文章,写的详细. 以下内容引用那篇博文.转载于<http://www.cnblogs.com/whgk/p/6426072.html>,在此仅供学习参考之用. 其实实现 ...

  10. Golang基础(6):go的net/http用法

    http包提供了HTTP客户端和服务端的实现 一:http客户端的几种方法 1. func (c *Client) Get(url string) (resp *Response, err error ...