Python深度学习之使用Albumentations对图像做增强
作者:AI浩 发布时间:2023-11-24 06:18:23
一、导入所需的库
import random
import cv2
from matplotlib import pyplot as plt
import albumentations as A
二、定义可视化函数显示图像上的边界框和类标签
可视化函数参考https://github.com/facebookresearch/Detectron/blob/master/detectron/utils/vis.py
BOX_COLOR = (255, 0, 0) # Red
TEXT_COLOR = (255, 255, 255) # White
def visualize_bbox(img, bbox, class_name, color=BOX_COLOR, thickness=2):
"""Visualizes a single bounding box on the image"""
x_min, y_min, w, h = bbox
x_min, x_max, y_min, y_max = int(x_min), int(x_min + w), int(y_min), int(y_min + h)
cv2.rectangle(img, (x_min, y_min), (x_max, y_max), color=color, thickness=thickness)
((text_width, text_height), _) = cv2.getTextSize(class_name, cv2.FONT_HERSHEY_SIMPLEX, 0.35, 1)
cv2.rectangle(img, (x_min, y_min - int(1.3 * text_height)), (x_min + text_width, y_min), BOX_COLOR, -1)
cv2.putText(
img,
text=class_name,
org=(x_min, y_min - int(0.3 * text_height)),
fontFace=cv2.FONT_HERSHEY_SIMPLEX,
fontScale=0.35,
color=TEXT_COLOR,
lineType=cv2.LINE_AA,
)
return img
def visualize(image, bboxes, category_ids, category_id_to_name):
img = image.copy()
for bbox, category_id in zip(bboxes, category_ids):
class_name = category_id_to_name[category_id]
img = visualize_bbox(img, bbox, class_name)
plt.figure(figsize=(12, 12))
plt.axis('off')
plt.imshow(img)
三、获取图像和标注
在此示例中,我们将使用来自COCO数据集的图像,该图像具有两个关联的边界框。 该映像位于http://cocodataset.org/#explore?id=386298
从磁盘加载图像
image = cv2.imread('images/000000386298.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
用坐标和类标签定义两个边界框
这些边界框的坐标使用coco格式声明。 每个边界框使用四个值[x_min, y_min, width, height]
进行描述。 有关边界框坐标的不同格式的详细说明,请参阅有关边界框的文档文章-https://albumentations.ai/docs/getting_started/bounding_boxes_augmentation/。
bboxes = [[5.66, 138.95, 147.09, 164.88], [366.7, 80.84, 132.8, 181.84]]
category_ids = [17, 18]
# We will use the mapping from category_id to the class name
# to visualize the class label for the bounding box on the image
category_id_to_name = {17: 'cat', 18: 'dog'}
展示图像的边框
visualize(image, bboxes, category_ids, category_id_to_name)
四、使用RandomSizedBBoxSafeCrop保留原始图像中的所有边界框
RandomSizedBBoxSafeCrop
crops a random part of the image. It ensures that the cropped part will contain all bounding boxes from the original image. Then the transform rescales the crop to height and width specified by the respective parameters. The erosion_rate
parameter controls how much area of the original bounding box could be lost after cropping. erosion_rate = 0.2
means that the augmented bounding box's area could be up to 20% smaller than the area of the original bounding box.
RandomSizedBBoxSafeCrop裁剪图像的随机部分。 它确保裁剪的部分将包含原始图像的所有边界框。 然后,变换会将作物重新缩放为相应参数指定的高度和宽度。 erosion_rate参数控制裁剪后可能丢失原始边界框的面积。 frosting_rate = 0.2表示扩充后的边界框的面积可能比原始边界框的面积小20%。
五、定义增强管道
transform = A.Compose(
[A.RandomSizedBBoxSafeCrop(width=448, height=336, erosion_rate=0.2)],
bbox_params=A.BboxParams(format='coco', label_fields=['category_ids']),
)
六、输入用于增强的图像和边框
我们固定随机种子是为了可视化目的,因此增强将始终产生相同的结果。 在真实的计算机视觉管道中,您不应该在对图像应用转换之前固定随机种子,因为在这种情况下,管道将始终输出相同的图像。 图像增强的目的是每次使用不同的变换。
random.seed(7)
transformed = transform(image=image, bboxes=bboxes, category_ids=category_ids)
visualize(
transformed['image'],
transformed['bboxes'],
transformed['category_ids'],
category_id_to_name,
)
七、其他不同随机种子的示例
random.seed(3)
transformed = transform(image=image, bboxes=bboxes, category_ids=category_ids)
visualize(
transformed['image'],
transformed['bboxes'],
transformed['category_ids'],
category_id_to_name,
)
random.seed(444)
transformed = transform(image=image, bboxes=bboxes, category_ids=category_ids)
visualize(
transformed['image'],
transformed['bboxes'],
transformed['category_ids'],
category_id_to_name,
)
来源:https://blog.csdn.net/hhhhhhhhhhwwwwwwwwww/article/details/117077423


猜你喜欢
- 平时我们在使用MySQL数据库的时候经常会因为操作失误造成数据丢失,MySQL数据库备份可以帮助我们避免由于各种原因造成的数据丢失或着数据库
- 目标能够使用mybatis的标签实现动态SQL拼接分析我们在前边的学习过程中,使用的SQL语句都非常简单。而在实际业务开发中,我们的SQL语
- 1.概述python中的日志库logging使用起来有点像log4j,但配置通常比较复杂,构建日志服务器时也不是方便。标准库logging的
- 在通过拼组sql语句来实现数据插入的应用中,我们很有可能会遇到需要插入大型数据的情况,例如,在oracle中需要插入字节数超过4000的字段
- 直接po截图和代码下面是CheckFormDemo.html<!DOCTYPE html><html><hea
- 前言cookie使用最多的地方想必是保存用户的账号与密码,可以避免用户每次登录时都要重新输入1.vue中cookie的安装在终端中输入命令n
- 本文实例为大家分享了vue移动端图片裁剪上传的具体代码,供大家参考,具体内容如下1.安装cropperjs依赖库npm install cr
- 不少需要用到sql2005的程序,有很多新手还是会操作,这里写个详细的图文教程送个菜鸟们,高手请飘过。适用于独立主机的朋友使用,如果你还没安
- scrollHeight: 获取对象的滚动高度。 scrollLeft:设置或获取位于对象左边界和窗口中目前可见内容的
- 通过session,我们可以在多次浏览器请求中保持数据, 接下来的部分就是用session来处理用户登录了。 当然,不能仅凭用户的一面之词,
- 本文实例为大家分享了小程序自定义弹出框效果的具体代码,供大家参考,具体内容如下my-pop----api:title ------字符串--
- 本文导读:删除表中的数据的方法有delete,truncate, 其中TRUNCATE TABLE用于删除表中的所有行,而不记录单个行删除操
- python中的dir()函数是一个非常重要的函数,它可以帮助我们查看函数的功能和特性。中文说明:不带参数时,返回当前范围内的变量、方法和定
- 画星星程序2-7-7主要使用turtle.forward前进操作和turtle.left左转操作在屏幕上画星星。#!/usr/bin/env
- 前言使用np.savetxt()方法可以将数据保存为txt文件或者是csv文件。1 np.savetxt()存储txt文件1-1 基础参数n
- 本文实例讲述了JS简单实现DIV相对于浏览器固定位置不变的方法。分享给大家供大家参考,具体如下:<!DOCTYPE HTML PUBL
- 主要用到requests和bf4两个库将获得的信息保存在d://hotsearch.txt下import requests;import b
- 一直很想做这个效果,原理是监听鼠标滚轮事件;可将此效果继续发散到其他应用上,如图片缩放,页面缩放等。演示:<!DOCTYPE html
- 锟拷码和口字码说到乱码问题就不得不提到锟斤拷,这算是非常常见的一种乱码形式,那么它到底是经过何种错误操作产生的呢?下面我们一步步探究。看一个
- 前言本文主要基于在Uber的Go monorepo中发现的各种数据竞争模式,分析了其背后的原因与分类,希望能够帮助更多的Go开发人员,去关注