欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python深度學(xué)習(xí)之使用Albumentations對(duì)圖像做增強(qiáng)

 更新時(shí)間:2021年05月27日 14:17:36   作者:AI浩  
諸如RandomCrop和CenterCrop之類的某些增強(qiáng)功能可能會(huì)變換圖像,使其不包含所有原始邊界框. 本示例說明如何使用名為RandomSizedBBoxSafeCrop的變換來裁剪圖像的一部分,但保留原始圖像的所有邊界框,需要的朋友可以參考下

一、導(dǎo)入所需的庫

import random
 
import cv2
from matplotlib import pyplot as plt
 
import albumentations as A

二、定義可視化函數(shù)顯示圖像上的邊界框和類標(biāo)簽

可視化函數(shù)參考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)

三、獲取圖像和標(biāo)注

在此示例中,我們將使用來自COCO數(shù)據(jù)集的圖像,該圖像具有兩個(gè)關(guān)聯(lián)的邊界框。 該映像位于http://cocodataset.org/#explore?id=386298

從磁盤加載圖像

image = cv2.imread('images/000000386298.jpg')
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

用坐標(biāo)和類標(biāo)簽定義兩個(gè)邊界框

這些邊界框的坐標(biāo)使用coco格式聲明。 每個(gè)邊界框使用四個(gè)值[x_min, y_min, width, height]進(jìn)行描述。 有關(guān)邊界框坐標(biāo)的不同格式的詳細(xì)說明,請(qǐng)參閱有關(guān)邊界框的文檔文章-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裁剪圖像的隨機(jī)部分。 它確保裁剪的部分將包含原始圖像的所有邊界框。 然后,變換會(huì)將作物重新縮放為相應(yīng)參數(shù)指定的高度和寬度。 erosion_rate參數(shù)控制裁剪后可能丟失原始邊界框的面積。 frosting_rate = 0.2表示擴(kuò)充后的邊界框的面積可能比原始邊界框的面積小20%。

五、定義增強(qiáng)管道

transform = A.Compose(
    [A.RandomSizedBBoxSafeCrop(width=448, height=336, erosion_rate=0.2)],
    bbox_params=A.BboxParams(format='coco', label_fields=['category_ids']),
)

六、輸入用于增強(qiáng)的圖像和邊框

我們固定隨機(jī)種子是為了可視化目的,因此增強(qiáng)將始終產(chǎn)生相同的結(jié)果。 在真實(shí)的計(jì)算機(jī)視覺管道中,您不應(yīng)該在對(duì)圖像應(yīng)用轉(zhuǎn)換之前固定隨機(jī)種子,因?yàn)樵谶@種情況下,管道將始終輸出相同的圖像。 圖像增強(qiáng)的目的是每次使用不同的變換。

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,
)

七、其他不同隨機(jī)種子的示例

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,
)

到此這篇關(guān)于Python深度學(xué)習(xí)之使用Albumentations對(duì)目標(biāo)檢測任務(wù)做增強(qiáng)的文章就介紹到這了,更多相關(guān)用Albumentations對(duì)目標(biāo)做增強(qiáng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Flask框架通過Flask_login實(shí)現(xiàn)用戶登錄功能示例

    Flask框架通過Flask_login實(shí)現(xiàn)用戶登錄功能示例

    這篇文章主要介紹了Flask框架通過Flask_login實(shí)現(xiàn)用戶登錄功能,結(jié)合實(shí)例形式較為詳細(xì)的分析了flask框架使用Flask_login實(shí)現(xiàn)用戶登陸功能的具體操作步驟、相關(guān)實(shí)現(xiàn)技巧與操作注意事項(xiàng),需要的朋友可以參考下
    2018-07-07
  • Django中STATIC_ROOT和STATIC_URL及STATICFILES_DIRS淺析

    Django中STATIC_ROOT和STATIC_URL及STATICFILES_DIRS淺析

    這篇文章主要給大家介紹了關(guān)于Django中STATIC_ROOT和STATIC_URL及STATICFILES_DIRS的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧
    2018-05-05
  • python基于json文件實(shí)現(xiàn)的gearman任務(wù)自動(dòng)重啟代碼實(shí)例

    python基于json文件實(shí)現(xiàn)的gearman任務(wù)自動(dòng)重啟代碼實(shí)例

    這篇文章主要介紹了python基于json文件實(shí)現(xiàn)的gearman任務(wù)自動(dòng)重啟代碼實(shí)例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-08-08
  • python實(shí)戰(zhàn)項(xiàng)目scrapy管道學(xué)習(xí)爬取在行高手?jǐn)?shù)據(jù)

    python實(shí)戰(zhàn)項(xiàng)目scrapy管道學(xué)習(xí)爬取在行高手?jǐn)?shù)據(jù)

    這篇文章主要為介紹了python實(shí)戰(zhàn)項(xiàng)目scrapy管道學(xué)習(xí)拿在行練手爬蟲項(xiàng)目,爬取在行高手?jǐn)?shù)據(jù),本篇博客的重點(diǎn)為scrapy管道pipelines的應(yīng)用,學(xué)習(xí)時(shí)請(qǐng)重點(diǎn)關(guān)注
    2021-11-11
  • Python實(shí)現(xiàn)拉格朗日插值法的示例詳解

    Python實(shí)現(xiàn)拉格朗日插值法的示例詳解

    插值法是一種數(shù)學(xué)方法,用于在已知數(shù)據(jù)點(diǎn)(離散數(shù)據(jù))之間插入數(shù)據(jù),以生成連續(xù)的函數(shù)曲線,而格朗日插值法是一種多項(xiàng)式插值法。本文就來用Python實(shí)現(xiàn)拉格朗日插值法,希望對(duì)大家有所幫助
    2023-02-02
  • Pycharm使用Database?Navigator連接mysql數(shù)據(jù)庫全過程

    Pycharm使用Database?Navigator連接mysql數(shù)據(jù)庫全過程

    這篇文章主要介紹了Pycharm使用Database?Navigator連接mysql數(shù)據(jù)庫全過程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-07-07
  • 從零開始制作PyTorch的Singularity容器鏡像的解決方案

    從零開始制作PyTorch的Singularity容器鏡像的解決方案

    本文主要介紹Facebook所主導(dǎo)的機(jī)器學(xué)習(xí)框架PyTorch的容器化安裝方法,基于HPC環(huán)境常用的Singularity高性能容器,并且兼容與結(jié)合了Docker容器鏡像的生態(tài),感興趣的朋友跟隨小編一起看看吧
    2024-05-05
  • Keras存在自定義loss或layer怎樣解決load_model報(bào)錯(cuò)問題

    Keras存在自定義loss或layer怎樣解決load_model報(bào)錯(cuò)問題

    這篇文章主要介紹了Keras存在自定義loss或layer怎樣解決load_model報(bào)錯(cuò)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • Python利用VideoCapture讀取視頻或攝像頭并進(jìn)行保存

    Python利用VideoCapture讀取視頻或攝像頭并進(jìn)行保存

    這篇文章主要為大家介紹一下OpenCV中cv2.VideoCapture函數(shù)的使用,并利用cv2.VideoCapture讀取視頻或攝像頭以及進(jìn)行保存幀圖像或視頻,感興趣的小伙伴可以了解一下
    2022-07-07
  • python實(shí)現(xiàn)翻譯word表格小程序

    python實(shí)現(xiàn)翻譯word表格小程序

    這篇文章主要為大家詳細(xì)介紹了python翻譯word表格小程序,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-02-02

最新評(píng)論