訂閱
糾錯(cuò)
加入自媒體

使用5行代碼的實(shí)時(shí)圖像分割

目錄

1. 計(jì)算機(jī)視覺應(yīng)用中的圖像分割

2. 圖像分割

3. 批處理圖像分割

4. 視頻分割

5. 攝像機(jī)視頻的分割

計(jì)算機(jī)視覺應(yīng)用中的圖像分割

計(jì)算機(jī)視覺是計(jì)算機(jī)看到和分析他們所看到的東西的能力。圖像分割是計(jì)算機(jī)視覺的一個(gè)方面,它處理將計(jì)算機(jī)可視化對(duì)象的內(nèi)容分割為不同的類別,以便更好地進(jìn)行分析。

圖像分割過(guò)程的一個(gè)很好的例子是圖像中對(duì)象的前景-背景分離,這是一種將對(duì)象與其背景分離的技術(shù),用于分析對(duì)象及其背景。圖像分割實(shí)現(xiàn)前景-背景分離的能力使其成為解決醫(yī)學(xué)圖像分析、背景編輯、自動(dòng)駕駛汽車視覺和衛(wèi)星圖像分析等許多計(jì)算機(jī)視覺問題的重要領(lǐng)域。

PixelLib庫(kù)是一個(gè)庫(kù),它允許使用幾行python代碼輕松集成圖像和視頻中的對(duì)象分割。它支持許多驚人的功能,例如:

1. 圖像和視頻中對(duì)象的語(yǔ)義和實(shí)例分割。

2. 細(xì)分模型的定制訓(xùn)練。

3. 圖像和視頻中的背景編輯。

4. 提取圖像和視頻中的對(duì)象。

實(shí)時(shí)圖像分割應(yīng)用的需求

Pytorch PixelLib:計(jì)算機(jī)視覺中最大的挑戰(zhàn)之一是保持實(shí)時(shí)應(yīng)用程序的精度和速度性能之間的距離。在計(jì)算機(jī)視覺領(lǐng)域,計(jì)算機(jī)視覺解決方案要么更精確、速度慢,要么精度更低、速度更快,這是一個(gè)兩難的問題。

以前版本的深度學(xué)習(xí)lib庫(kù)使用tensorflow深度學(xué)習(xí)庫(kù)作為后臺(tái),采用Mask R-CNN進(jìn)行實(shí)例分割。Mask R-CNN是一種非常好的體系結(jié)構(gòu),但在實(shí)時(shí)應(yīng)用中無(wú)法平衡精度和速度性能。

PixelLib現(xiàn)在支持pytorch后端,使用PointRend分割架構(gòu)對(duì)圖像和視頻中的對(duì)象執(zhí)行更快、更準(zhǔn)確的分割和提取。

Alexander Kirillov等人的PointRend對(duì)象分割體系結(jié)構(gòu)用于替代Mask R-CNN來(lái)執(zhí)行對(duì)象的實(shí)例分割。PointRend是一種用于實(shí)現(xiàn)對(duì)象分割的先進(jìn)神經(jīng)網(wǎng)絡(luò)。它生成精確的分割模板,并以高推理速度運(yùn)行,以滿足對(duì)精確和實(shí)時(shí)計(jì)算機(jī)視覺應(yīng)用日益增長(zhǎng)的需求。

多操作系統(tǒng)支持

PixelLib是一個(gè)為支持不同操作系統(tǒng)而構(gòu)建的庫(kù)。我通過(guò)Detectron2將PixelLib與PointRend的python實(shí)現(xiàn)集成在一起,后者只支持Linux操作系統(tǒng)。

我對(duì)最初的Detectron2 PointRend實(shí)現(xiàn)進(jìn)行了修改,以支持Windows操作系統(tǒng)。我很高興告訴大家一個(gè)好消息,用于PixelLib的PointRend實(shí)現(xiàn)同時(shí)支持Linux和Windows操作系統(tǒng)。

上面的示例圖像是PointRend與Mask RCNN分割結(jié)果差異的示例。很明顯,與左側(cè)的Mask R-CNN結(jié)果相比,右側(cè)的PointRend圖像結(jié)果是更好的分割輸出。

注意:本文基于使用pytorch和PointRend執(zhí)行實(shí)例分割。

下載Python

PixelLib pytorch版本支持python 3.7及以上版本。下載兼容的python版本。

安裝PixelLib及其依賴項(xiàng)

安裝Pytorch

PixelLib Pytorch版本支持Pytorch的這些版本(1.6.0、1.7.1、1.8.0和1.9.0)。注意:Pytorch 1.7.0不受支持,請(qǐng)勿使用任何低于1.6.0的Pytorch版本。安裝兼容的Pytorch版本。

安裝Pycocotools

pip3 install pycocotools

安裝pixellib

pip3 install pixellib

如果已安裝,請(qǐng)使用以下軟件升級(jí)至最新版本:

pip3 install pixellib — upgrade

圖像分割

PixelLib使用五行python代碼在具有PointRend模型的圖像和視頻中執(zhí)行對(duì)象分割。下載PointRend模型。這是圖像分割的代碼。

import pixellib

from pixellib.torchbackend.instance import instanceSegmentation


ins = instanceSegmentation()

ins.load_model("pointrend_resnet50.pkl")

ins.segmentImage("image.jpg", show_bboxes=True, output_image_name="output_image.jpg")

第1-4行:導(dǎo)入了PixelLib包,我們還從模塊pixellib.torchbackend.instance導(dǎo)入了instanceSegmentation類(從pytorch支持導(dǎo)入實(shí)例分段類)。我們創(chuàng)建了該類的一個(gè)實(shí)例,并最終加載了下載的PointRend模型。

第5行:我們調(diào)用函數(shù)segmentImage來(lái)執(zhí)行圖像中對(duì)象的分割,并在函數(shù)中添加了以下參數(shù):

image_path:這是要分割的圖像的路徑。

show_bbox:這是一個(gè)可選參數(shù),用于顯示帶邊框的分段結(jié)果。

output_image_name:這是保存的分段圖像的名稱。

用于分割的樣本圖像

ins.segmentImage("image.jpg",output_image_name="output.jpg")

分割后的圖像

The checkpoint state_dict contains keys that are not used by the model: proposal_generator.a(chǎn)nchor_generator.cell_anchors.{0, 1, 2, 3, 4}

如果你正在運(yùn)行分段代碼,上面的日志可能會(huì)出現(xiàn)!這不是一個(gè)錯(cuò)誤,代碼可以正常工作!

獲得分割結(jié)果

results, output = ins.segmentImage("image.jpg", show_bboxes=True, output_image_name="result.jpg")
print(results)

分割結(jié)果返回一個(gè)字典,其中包含許多與圖像中分割的對(duì)象相關(guān)的值。打印的結(jié)果將采用以下格式:

{'boxes':  array([[ 579,  462, 1105,  704],
                        [   1,  486,  321,  734],
                        [ 321,  371,  423,  742],
                        [ 436,  369,  565,  788],
                        [ 191,  397,  270,  532],
                        [1138,  357, 1197,  482],
                        [ 877,  382,  969,  477],),
      'class_ids': array([ 2,  2,  0,  0,  0,  0,  0,  2,  0,  0,  0,  0,  2, 24, 24,2,  2,2,  0,  0,  0,  0,  0,  0], dtype=int64), 


'class_names': ['car', 'car', 'person', 'person', 'person', 'person', 'person', 'car', 'person', 'person','person', 'person', 'car', 'backpack', 'backpack', 'car', 'car', 'car', 'person', 'person', 'person', 'person', 'person', 'person'],



'object_counts': Counter({'person': 15, 'car': 7, 'backpack': 2}), 


'scores': array([100., 100., 100., 100.,  99.,  99.,  98.,  98.,  97.,  96.,  95.,95.,  95.,  95.,  94.,  94.,  93.,  91.,  90.,  88.,  82.,  72.,69.,  66.], dtype=float32), 


'masks': array([[[False, False, False, ..., False, False, False],


[False, False, False, ..., False, False, False],


'extracted_objects': []

上面打印的結(jié)果值包括:

{'boxes': array([[5.790e+02, 4.620e+02, 1.105e+03, 7.050e+02],[1.000e+00, 4.870e+02, 3.220e+02, 7.340e+02],[1.142e+03, 3.640e+02, 1.161e+03, 4.060e+02]], dtype=float32),

boxes:這些是分割對(duì)象的邊界框坐標(biāo)。我沒有顯示所有框的坐標(biāo),這是因?yàn)榱斜硖L(zhǎng)了。

'class_ids': array([ 2,  2,  0,  0,  0,  0,  0,  2,  0,  0,  0,  0,  2, 24, 24,2,  2,2,  0,  0,  0,  0,  0,  0], dtype=int64),

class_ids:這些是分段對(duì)象的類ID。

'class_names': ['car', 'car', 'person', 'person', 'person', 'person', 'person', 'car', 'person', 'person', 'person', 'person', 'car', 'backpack', 'backpack', 'car', 'car', 'car', 'person', 'person', 'person', 'person', 'person', 'person'],

class_names:這些是分段對(duì)象的類名。

'object_counts': Counter({'person': 15, 'car': 7, 'backpack': 2}),

object_counts:這是圖像中分割的每個(gè)類的計(jì)數(shù)。我使用python內(nèi)置計(jì)數(shù)器對(duì)對(duì)象進(jìn)行計(jì)數(shù)。在本例中,圖像中分割了15個(gè)人、7輛汽車和2個(gè)背包。

'scores': array([100., 100., 100., 100.,  99.,  99.,  98.,  98.,  97.,  96.,  95.,95.,  95.,  95.,  94.,  94.,  93.,  91.,  90.,  88.,  82.,  72.,69.,  66.], dtype=float32),

scores:這些是每個(gè)分割對(duì)象的置信度分?jǐn)?shù)。

'masks': array([[[False, False, False, ..., False, False, False],

[False, False, False, ..., False, False, False],

masks:這些是每個(gè)分割對(duì)象的掩碼值。我沒有顯示所有的掩碼值,這是因?yàn)榱斜硖L(zhǎng)了。

注意:返回的掩碼的默認(rèn)值以bolean為單位。可以使用新參數(shù)mask_points_values獲取掩碼的坐標(biāo)。

ins.segmentImage("sample.jpg", show_bboxes=True, mask_points_values=True,  output_image_name="output.jpg")

mask_points_values參數(shù)已添加到segmentImage函數(shù)并設(shè)置為True,新的mask值將為:

[[array([[295, 497]])

       array([[422, 114],
                      [421, 115],  
                      [417, 115],
                        ...,
                      [436, 115],
                      [433, 115],
                      [432, 114]])]]

extracted_objects: This is the container

提取的對(duì)象:如果我們提取對(duì)象,這是提取對(duì)象值的容器列表。它是空的,因?yàn)槲覀儧]有提取任何內(nèi)容。我們將在本文后面討論如何提取這些分段對(duì)象。

results, output = ins.segmentImage("image.jpg", show_bboxes=True, output_image_name="result.jpg")

訪問分段結(jié)果呈現(xiàn)的值

邊界框坐標(biāo)值

results["boxes"]

類ID值

results["class_ids"]

類名值

results["class_names"]

對(duì)象計(jì)算值

results["object_counts"]

掩碼值

results["masks"]

檢測(cè)閾值

PixelLib使得確定目標(biāo)分割的檢測(cè)閾值成為可能。

ins.load_model("pointrend_resnet50.pkl", confidence = 0.3)

置信度:這是在load_model函數(shù)中引入的一個(gè)新參數(shù),設(shè)置為0.3可將檢測(cè)閾值設(shè)置為30%。我為檢測(cè)閾值設(shè)置的默認(rèn)值為0.5,可以使用置信度參數(shù)增加或減少該值。

速度記錄

PixelLib使執(zhí)行實(shí)時(shí)對(duì)象分割成為可能,并增加了調(diào)整推理速度以適應(yīng)實(shí)時(shí)預(yù)測(cè)的能力。使用4GB容量的Nvidia GPU處理單個(gè)圖像的默認(rèn)推斷速度約為0.26秒。

速度調(diào)整

PixelLib支持速度調(diào)整,有兩種速度調(diào)整模式,即快速模式和快速模式:

1 快速模式

ins.load_model("pointrend_resnet50.pkl", detection_speed = "fast")

在load_model函數(shù)中,我們添加了參數(shù)detection_speed并將該值設(shè)置為fast?焖倌J教幚韱蝹(gè)圖像的時(shí)間達(dá)到0.20秒。

快速模式檢測(cè)的完整代碼

import pixellib

from pixellib.torchbackend.instance import instanceSegmentation


ins = instanceSegmentation()

ins.load_model("pointrend_resnet50.pkl", detection_speed = "fast")

ins.segmentImage("image.jpg", show_bboxes=True, output_image_name="output_image.jpg")

2 快速模式

ins.load_model("pointrend_resnet50.pkl", detection_speed = "fast")

在load_model函數(shù)中,我們添加了參數(shù)detection_speed并將該值設(shè)置為rapid?焖倌J教幚韱蝹(gè)圖像的時(shí)間達(dá)到0.15秒。

快速模式檢測(cè)的完整代碼

import pixellib

from pixellib.torchbackend.instance import instanceSegmentation


ins = instanceSegmentation()

ins.load_model("pointrend_resnet50.pkl", detection_speed = "rapid")

ins.segmentImage("image.jpg", show_bboxes=True, output_image_name="output_image.jpg")

PointRend模型

有兩種類型的PointRend模型用于對(duì)象分割,它們是resnet50變體和resnet101變體。

本文通篇都使用resnet50變體,因?yàn)樗俣雀,?zhǔn)確性好。resnet101變型更精確,但比resnet50變型慢。根據(jù)Detectron2模型的官方報(bào)告,resnet50變型在COCO上的mAP達(dá)到38.3,resnet101變型在COCO上的mAP達(dá)到40.1

Resnet101的速度記錄:分段的默認(rèn)速度為0.5秒,快速模式為0.3秒,而快速模式為0.25秒。

Resnet101變體的代碼import pixellib

from pixellib.torchbackend.instance import instanceSegmentation


ins = instanceSegmentation()

ins.load_model("pointrend_resnet101.pkl", network_backbone="resnet101")

ins.segmentImage("sample.jpg",  show_bboxes = True, output_image_name="output.jpg")


ins.load_model("pointrend_resnet101.pkl", network_backbone = "resnet101")

使用resnet101模型執(zhí)行推斷的代碼是相同的,只是我們?cè)趌oad_model函數(shù)中加載了PointRend resnet101模型。從這里下載resnet101模型。我們還添加了一個(gè)額外的參數(shù)network_backbone,并將該值設(shè)置為resnet101。

注意:如果你希望實(shí)現(xiàn)高推斷速度和高精度,請(qǐng)使用PointRend resnet50變體,但如果你更關(guān)心精度,請(qǐng)使用PointRend resnet101變體。所有這些推斷報(bào)告都基于使用4GB容量的Nvidia GPU。

圖像分割中的自定義目標(biāo)檢測(cè)

所使用的PointRend模型是一個(gè)預(yù)訓(xùn)練COCO模型,支持80類對(duì)象。PixelLib支持自定義對(duì)象檢測(cè),這使得過(guò)濾檢測(cè)和確保目標(biāo)對(duì)象的分割成為可能。我們可以從支持的80類對(duì)象中進(jìn)行選擇,以符合我們的目標(biāo)。以下是支持的80類對(duì)象:

person, bicycle, car, motorcycle, airplane,

bus, train, truck, boat, traffic_light, fire_h(yuǎn)ydrant, stop_sign,

parking_meter, bench, bird, cat, dog, horse, sheep, cow, elephant, bear, zebra,

giraffe, backpack, umbrella, handbag, tie, suitcase, frisbee, skis, snowboard,

sports_ball, kite, baseball_bat, baseball_glove, skateboard, surfboard, tennis_racket,

bottle, wine_glass, cup, fork, knife, spoon, bowl, banana, apple, sandwich, orange,

broccoli, carrot, hot_dog, pizza, donut, cake, chair, couch, potted_plant, bed,

dining_table, toilet, tv, laptop, mouse, remote, keyboard, cell_phone, microwave,

oven, toaster, sink, refrigerator, book, clock, vase, scissors, teddy_bear, hair_dryer,

toothbrush.

我們想要過(guò)濾樣本圖像的檢測(cè)結(jié)果,以便只檢測(cè)圖像中的人。

target_classes = ins.select_target_classes(person = True)

ins.segmentImage("image.jpg", segment_target_classes = target_classes, output_image_name="output.jpg")

調(diào)用函數(shù)select_target_classes來(lái)選擇要分割的目標(biāo)對(duì)象。函數(shù)segmentImage得到了一個(gè)新的參數(shù)segmente_target_classes,可以從目標(biāo)類中進(jìn)行選擇,并根據(jù)它們過(guò)濾檢測(cè)。

PixelLib僅檢測(cè)圖像中的人物。

自定義對(duì)象檢測(cè)的完整代碼

import pixellib

from pixellib.torchbackend.instance import instanceSegmentation


ins = instanceSegmentation()

ins.load_model("pointrend_resnet50.pkl")

target_classes = ins.select_target_classes(person = True)

ins.segmentImage("image.jpg", show_bboxes=True, segment_target_classes = target_classes, output_image_name="output_image.jpg")

圖像中的對(duì)象提取

PixelLib使提取和分析圖像中分割的對(duì)象成為可能。

對(duì)象提取代碼

import pixellib

from pixellib.torchbackend.instance import instanceSegmentation


ins = instanceSegmentation()

ins.load_model("pointrend_resnet50.pkl")

ins.segmentImage("image.jpg", show_bboxes=True, extract_segmented_objects=True,

save_extracted_objects=True, output_image_name="output_image.jpg" )

圖像分割的代碼是相同的,只是我們?cè)趕egmentImage函數(shù)中添加了額外的參數(shù)來(lái)執(zhí)行對(duì)象提取。

ins.segmentImage("image.jpg", extract_segmented_objects = True, save_extracted_objects = True, output_image_name="output.jpg")

提取分割對(duì)象:這是處理分割對(duì)象提取的參數(shù)。使用以下命令訪問提取的對(duì)象值:

results, output = ins.segmentImage("image.jpg", show_bboxes=True, output_image_name="result.jpg")


# 從結(jié)果中訪問提取的對(duì)象

results["extracted_objects"]

save_extracted_objects(保存提取的對(duì)象):這是將每個(gè)提取的對(duì)象保存為圖像的參數(shù)。每個(gè)分段對(duì)象將保存為分段對(duì)象索引,例如分段對(duì)象1。對(duì)象按提取順序保存。

segmented_object_1.jpg

segmented_object_2.jpg

segmented_object_3.jpg

segmented_object_4.jpg

segmented_object_5.jpg

segmented_object_6.jpg

從掩碼坐標(biāo)提取對(duì)象

注意:圖像中的所有對(duì)象都已提取,我選擇僅顯示其中兩個(gè)對(duì)象。

邊界框坐標(biāo)的提取

默認(rèn)提取方法從掩碼的坐標(biāo)中提取對(duì)象。提取只給我們提供有關(guān)對(duì)象本身的信息,不包括其周圍環(huán)境。

考慮一個(gè)問題,我們要分析它在圖像中的對(duì)象和區(qū)域。PixelLib能夠通過(guò)使用邊界框坐標(biāo)提取分割對(duì)象及其在圖像中的遠(yuǎn)程位置來(lái)解決此問題。

修改提取代碼

ins.segmentImage("image.jpg", extract_segmented_objects = True, extract_from_box = True, save_extracted_objects = True, output_image_name="output.jpg")

我們引入了一個(gè)新的參數(shù)extract_from_box來(lái)提取從邊界框坐標(biāo)分割的對(duì)象。每個(gè)提取的對(duì)象將保存為對(duì)象提取索引,例如對(duì)象提取1。對(duì)象按提取順序保存

object_extract1.jpg

object_extract2.jpg

object_extract3.jpg

object_extract4.jpg

object_extract5.jpg

object_extract6.jpg

使用邊界框坐標(biāo)提取對(duì)象的完整代碼

import pixellib

from pixellib.torchbackend.instance import instanceSegmentation


ins = instanceSegmentation()

ins.load_model("pointrend_resnet50.pkl")

ins.segmentImage("image.jpg", show_bboxes=True, extract_segmented_objects=True, extract_from_box = True,

save_extracted_objects=True, output_image_name="output_image.jpg" )

圖像分割輸出可視化。PixelLib可以根據(jù)圖像的分辨率調(diào)節(jié)圖像的可視化。

ins.segmentImage("sample.jpg", show_bboxes=True, output_image_name= "output.jpg")

可視化不可見,因?yàn)槲谋敬笮『涂蚝穸忍。我們可以調(diào)整文本大小、厚度和框厚度來(lái)調(diào)整可視化效果。

為更好的可視化而進(jìn)行的修改

ins.segmentImage(“sample.jpg”, show_bboxes=True, text_size=5, text_thickness=4, box_thickness=10, output_image_name=”output.jpg”)

segmentImage函數(shù)接受了調(diào)整文本和邊框厚度的新參數(shù)。

text_size:默認(rèn)文本大小為0.6,中等分辨率的圖像也可以。它對(duì)于高分辨率的圖像來(lái)說(shuō)太小了。我將值增加到5。

text_thickness:默認(rèn)文本厚度為1。我將其增加到4以匹配圖像分辨率。

box_thickness:默認(rèn)長(zhǎng)方體厚度為2,我將其更改為10以匹配圖像分辨率。以更好的可視化效果輸出圖像

注意:根據(jù)圖像的分辨率調(diào)整參數(shù)。如果圖像分辨率較低,我用于分辨率為5760 x 3840的示例圖像的值可能太大。如果有分辨率非常高的圖像,則可以將參數(shù)值增加到我在本示例代碼中設(shè)置的參數(shù)值之外。

text_thickness和box_thickness參數(shù)的值必須是整數(shù),不能用浮點(diǎn)數(shù)表示。文本大小值可以用整數(shù)和浮點(diǎn)數(shù)表示。

批處理圖像分割

PixelLib可以對(duì)位于同一文件夾中的一批圖像執(zhí)行預(yù)測(cè)。批量分割代碼

import pixellib

from pixellib.torchbackend.instance import instanceSegmentation


ins = instanceSegmentation()

ins.load_model("pointrend_resnet50.pkl")

ins.segmentBatch("inputfolder", show_bboxes=True, output_folder_name="outputfolder")

ins.segmentBatch("inputfolder",  show_bboxes=True, output_folder_name = "outputfolder")

批量分割的代碼與單幅圖像分割非常相似,只是我們用segmentBatch函數(shù)替換了segmentImage函數(shù)。我們向segmentBatch添加了以下參數(shù):

folder_path:這是包含我們要分割的圖像的文件夾。

output_folder_name:這是我們將保存所有分割圖像的文件夾的名稱。示例文件夾結(jié)構(gòu)

--input_folder

   --test1.jpg
          --test2.jpg
          --test3.jpg

--output_folder
          --test1.jpg
          --test2.jpg
          --test3.jpg

批處理圖像分割中的對(duì)象提取代碼

import pixellib

from pixellib.torchbackend.instance import instanceSegmentation


ins = instanceSegmentation()

ins.load_model("pointrend_resnet50.pkl")

ins.segmentBatch("inputfolder", show_bboxes=True, extract_segmented_objects=True,

save_extracted_objects=True, output_folder_name="outputfolder")

ins.segmentBatch("inputfolder", show_bboxes=True, extract_segmented_objects=True, save_extracted_objects=True, output_folder_name="outputfolder")

我們?cè)趕egmentBatch函數(shù)中添加了extract_segmented_objectssave_extracted_objects參數(shù),分別用于提取和保存提取的對(duì)象。輸入文件夾中每個(gè)圖像的提取對(duì)象將保存在單獨(dú)的文件夾中,其名稱為imagename_extracts。例如,如果圖像名稱為test1.jpg,這意味著提取的對(duì)象將保存在名為test1_extracts的文件夾中。

注意:提取對(duì)象的文件夾是在圖像的同一輸入文件夾中創(chuàng)建的。

--input_folder
           --test1.jpg
           --test1_extracts
   
           --test2.jpg
           --test2_extracts
   
           --test3.jpg
           --test3_extracts


--output_folder   

     --test1.jpg
           --test2.jpg
          --test3.jpg

從邊界框坐標(biāo)提取對(duì)象的代碼

import pixellib

from pixellib.torchbackend.instance import instanceSegmentation


ins = instanceSegmentation()

ins.load_model("pointrend_resnet50.pkl")

ins.segmentBatch("inputfolder", show_bboxes=True, extract_segmented_objects=True, extract_from_box=True,

save_extracted_objects=True, output_folder_name="outputfolder")

ins.segmentBatch("inputfolder", show_bboxes=True, extract_segmented_objects=True, extract_from_box=True,save_extracted

objects=True, output_folder_name="outputfolder")

我們?cè)趕egmentBatch函數(shù)中添加了extract_from_boxsave_extracted_objects參數(shù),以分別提取和保存提取的對(duì)象。

注意:從邊界框坐標(biāo)提取的對(duì)象的文件夾也在圖像的同一輸入文件夾中創(chuàng)建。示例文件夾結(jié)構(gòu)

--input_folder
           --test1.jpg
           --test1_extracts


--test2.jpg
           --test2_extracts
   

--test3.jpg
           --test3_extracts

--output_folder  
           --test1.jpg
           --test2.jpg
           --test3.jpg

批處理圖像分割中自定義對(duì)象分割的代碼

import pixellib

from pixellib.torchbackend.instance import instanceSegmentation


ins = instanceSegmentation()

ins.load_model("pointrend_resnet50.pkl")

target_classes = ins.select_target_classes(person = True)

ins.segmentBatch("inputfolder", show_bboxes=True, segment_target_classes = target_classes, output_folder_name="outputfolder")

target_classes = ins.select_target_classes(person = True)

ins.segmentBatch("inputfolder", show_bboxes=True, segment_target_classes = target_classes, output_folder_name="outputfolder")

我們調(diào)用函數(shù)select_target_classes來(lái)選擇要分割的目標(biāo)對(duì)象。函數(shù)segmentBatch函數(shù)獲得了一個(gè)新的參數(shù)segmente_target_classes,可以從目標(biāo)類中進(jìn)行選擇,并根據(jù)它們過(guò)濾檢測(cè)。

批處理圖像分割中的快速模式檢測(cè)代碼

import pixellib

from pixellib.torchbackend.instance import instanceSegmentation


ins = instanceSegmentation()

ins.load_model("pointrend_resnet50.pkl", detection_speed = "fast")

ins.segmentBatch("inputfolder", show_bboxes=True, output_folder_name="outputfolder")

ins.load_model(“pointrend_resnet50.pkl”, detection_speed = "fast")

在load_model函數(shù)中,我們添加了參數(shù)detection_speed并將該值設(shè)置為fast?焖倌J教幚韱蝹(gè)圖像的時(shí)間達(dá)到0.20秒。

批處理圖像分割中的快速模式檢測(cè)代碼

import pixellib

from pixellib.torchbackend.instance import instanceSegmentation


ins = instanceSegmentation()

ins.load_model("pointrend_resnet50.pkl", detection_speed = "rapid")

ins.segmentBatch("inputfolder", show_bboxes=True, output_folder_name="outputfolder")

ins.load_model(“pointrend_resnet50.pkl”, detection_speed = "rapid")

我們將推理的檢測(cè)速度設(shè)置為快速模式。

在load_model函數(shù)中,我們添加了參數(shù)detection_speed并將該值設(shè)置為rapid?焖倌J教幚韱蝹(gè)圖像的時(shí)間達(dá)到0.15秒。

注:PixelLib所有支持功能的批處理圖像分割的代碼實(shí)現(xiàn)與單個(gè)圖像分割相同,只是segmentImage函數(shù)被segmentBatch替換。

視頻和攝影機(jī)中的對(duì)象分割

PixelLib使得在實(shí)時(shí)攝像機(jī)和視頻文件中執(zhí)行實(shí)時(shí)對(duì)象分割成為可能。

視頻分割

import pixellib

from pixellib.torchbackend.instance import instanceSegmentation

ins = instanceSegmentation()

ins.load_model("pointrend_resnet50.pkl")

ins.process_video("sample_video.mp4", show_bboxes=True, frames_per_second=3, output_video_name="output_video.mp4")

第1-4行:導(dǎo)入了PixelLib包,我們還從模塊PixelLib.torchbackend.instance導(dǎo)入了instanceSegmentation類(從pytorch支持導(dǎo)入實(shí)例分段類)。我們創(chuàng)建了該類的一個(gè)實(shí)例,并最終加載了PointRend模型。如果模型尚未下載,請(qǐng)從此處下載。

第5行:我們調(diào)用函數(shù)process_video對(duì)視頻中的對(duì)象進(jìn)行分割,并將以下參數(shù)添加到該函數(shù)中:

video_path:這是要分割的視頻的路徑。

show_bboxes:這是一個(gè)可選參數(shù),用于在結(jié)果中顯示帶邊界框的分段對(duì)象。

frames_per_second:此參數(shù)將設(shè)置已保存視頻的每秒幀數(shù)。

output_video_name:這是輸出分段視頻的名稱。ins.process_video("sample_video.mp4", show_bboxes=True, frames_per_second=3, output_video_name="output_video.mp4")

視頻中的對(duì)象提取代碼

import pixellib

from pixellib.torchbackend.instance import instanceSegmentation


ins = instanceSegmentation()

ins.load_model("pointrend_resnet50.pkl")

ins.process_video("sample.mp4", show_bboxes=True, extract_segmented_objects=True,

save_extracted_objects=True, frames_per_second=3, output_video_name="output_video.mp4")

ins.process_video(“sample_video.mp4”, show_bboxes=True,  extract_segmented_objectsframes_per_second=5, output_video_name=”output_video.mp4")

process_video函數(shù)具有新參數(shù)extract_segmented_objects和save_extracted_objects,分別用于提取和保存分割對(duì)象。

視頻中邊界框坐標(biāo)的提取

修改提取代碼

import pixellib

from pixellib.torchbackend.instance import instanceSegmentation


ins = instanceSegmentation()

ins.load_model("pointrend_resnet50.pkl")

ins.process_video("sample.mp4", show_bboxes=True, extract_segmented_objects=True, extract_from_box=True,

save_extracted_objects=True, frames_per_second=5, output_video_name="output_video.mp4")

ins.process_video(“sample.mp4”, show_bboxes=True, extract_segmented_objects=True, extract_from_box=True,save_extracted_objects=True, frames_per_second=5, output_video_name=”output_video.mp4")

將extract_from_box添加到函數(shù)中,以提取從其邊界框坐標(biāo)分割的對(duì)象。

視頻中的自定義對(duì)象分割

PixelLib可以在視頻中執(zhí)行自定義對(duì)象分割,以過(guò)濾未使用的檢測(cè)并分割目標(biāo)類。

視頻中的自定義檢測(cè)代碼

import pixellib

from pixellib.torchbackend.instance import instanceSegmentation


ins = instanceSegmentation()

ins.load_model("pointrend_resnet50.pkl")

target_classes = ins.select_target_classes(person = True, bicycle =True)

ins.process_video("sample_video.mp4", show_bboxes=True, segment_target_classes = target_classes,

frames_per_second=5, output_video_name="output_video.mp4")

target_classes = ins.select_target_classes(person = True, bicycle =True)

ins.process_video("sample_video.mp4", show_bboxes=True, segment_target_classes = target_classes,frames_per_second=5,

output_video_name="output_video.mp4")

調(diào)用函數(shù)select_target_classes來(lái)選擇要分割的目標(biāo)對(duì)象。函數(shù)process_video獲得了一個(gè)新的參數(shù)segment_target_classes,可以從目標(biāo)類中進(jìn)行選擇,并基于它們過(guò)濾檢測(cè)。

視頻分割中的快速模式檢測(cè)代碼

import pixellib

from pixellib.torchbackend.instance import instanceSegmentation

ins = instanceSegmentation()

ins.load_model("pointrend_resnet50.pkl", detection_speed = "fast")

ins.process_video("sample_video.mp4", show_bboxes=True, frames_per_second=5, output_video_name="output_video.mp4")

ins.load_model(“pointrend_resnet50.pkl”, detection_speed = "fast")

我們將視頻處理的檢測(cè)速度設(shè)置為快速模式。

視頻分割中的快速模式檢測(cè)代碼

import pixellib

from pixellib.torchbackend.instance import instanceSegmentation


ins = instanceSegmentation()

ins.load_model("pointrend_resnet50.pkl", detection_speed = "rapid")

ins.process_video("sample_video.mp4", show_bboxes=True, frames_per_second=5, output_video_name="output_video.mp4")

ins.load_model(“pointrend_resnet50.pkl”, detection_speed = "rapid")

我們將視頻處理的檢測(cè)速度設(shè)置為快速模式。

實(shí)時(shí)攝影機(jī)中的對(duì)象分割

PixelLib為實(shí)時(shí)分割實(shí)時(shí)攝影機(jī)提供了極好的支持。用于分割實(shí)時(shí)攝影機(jī)的代碼

import pixellib

from pixellib.torchbackend.instance import instanceSegmentation

import cv2


capture = cv2.VideoCapture(0)


segment_video = instanceSegmentation()

segment_video.load_model("pointrend_resnet50.pkl")

segment_video.process_camera(capture,  show_bboxes = True, frames_per_second= 5, check_fps=True, show_frames= True,

frame_name= "frame", output_video_name="output_video.mp4")

import cv2 capture = cv2.VideoCapture(0)

我們導(dǎo)入了cv2并包含了捕獲相機(jī)幀的代碼。

segment_video.process_camera(capture,  show_bboxes = True, frames_per_second= 5, check_fps=True, show_frames= True,frame_name= "frame", output_video_name="output_video.mp4")

在執(zhí)行分割的代碼中,我們將視頻的文件路徑替換為捕獲,也就是說(shuō),我們正在處理攝像機(jī)捕獲的幀流。我們添加了額外的參數(shù)以顯示相機(jī)的幀:

show_frames:該參數(shù)用于處理分段攝影機(jī)幀的顯示。

frame_name:這是為顯示的攝影機(jī)幀指定的名稱。

check_fps:這是在相機(jī)饋送處理結(jié)束時(shí)每秒打印幀數(shù)的參數(shù)。

show_bboxes:這是一個(gè)可選參數(shù),用于顯示帶有邊界框的分段對(duì)象。

frames_per_second:此參數(shù)用于設(shè)置保存的視頻文件每秒幀數(shù)。在這種情況下,它被設(shè)置為5,即保存的視頻文件每秒有5幀。

output_video_name:這是保存的分段視頻的名稱。

實(shí)時(shí)相機(jī)饋送處理的速度調(diào)整

默認(rèn)速度模式達(dá)到4fps?焖倌J竭_(dá)到6fps,快速模式達(dá)到9fps。這些報(bào)告基于使用4GB容量的Nvidia GPU

攝像機(jī)饋送中的快速模式檢測(cè)代碼

import pixellib

from pixellib.torchbackend.instance import instanceSegmentation

import cv2


capture = cv2.VideoCapture(0)


segment_video = instanceSegmentation()

segment_video.load_model("pointrend_resnet50.pkl", detection_speed = "fast")

segment_video.process_camera(capture,  show_bboxes = True, frames_per_second= 5, check_fps=True, show_frames= True,

frame_name= "frame", output_video_name="output_video.mp4")

ins.load_model(“pointrend_resnet50.pkl”, detection_speed = "fast")

我們將處理實(shí)時(shí)攝像機(jī)饋送的檢測(cè)速度設(shè)置為快速模式,推斷速度為6fps?焖倌J綑z測(cè)代碼

import pixellib

from pixellib.torchbackend.instance import instanceSegmentation

import cv2


capture = cv2.VideoCapture(0)


segment_video = instanceSegmentation()

segment_video.load_model("pointrend_resnet50.pkl", detection_speed = "rapid")

segment_video.process_camera(capture,  show_bboxes = True, frames_per_second= 5, check_fps=True, show_frames= True,

frame_name= "frame", output_video_name="output_video.mp4")

ins.load_model(“pointrend_resnet50.pkl”, detection_speed = "rapid")

我們將處理實(shí)時(shí)攝像機(jī)饋送的檢測(cè)速度設(shè)置為快速模式,推斷速度為9fps。實(shí)時(shí)攝影機(jī)中自定義對(duì)象分割的代碼

import pixellib

from pixellib.torchbackend.instance import instanceSegmentation

import cv2


capture = cv2.VideoCapture(0)


segment_video = instanceSegmentation()

segment_video.load_model("pointrend_resnet50.pkl")

target_classes = segment_video.select_target_classes(person=True)

segment_video.process_camera(capture,  show_bboxes = True, frames_per_second= 5, segment_target_classes = target_classes,

show_frames= True,frame_name= "frame", output_video_name="output_video.mp4")

target_classes = segment_video.select_target_classes(person=True)

segment_video.process_camera(capture,  show_bboxes = True, frames_per_second= 5, segment_target_classes = target_classes,show_frames= True,frame_name= "frame", output_video_name="output_video.mp4")

調(diào)用函數(shù)select_target_classes來(lái)選擇要分割的目標(biāo)對(duì)象。函數(shù)process_camera獲得了一個(gè)新的參數(shù)segment_target_classes,可以從目標(biāo)類中進(jìn)行選擇,并基于它們過(guò)濾檢測(cè)。

實(shí)時(shí)攝影機(jī)中的對(duì)象提取代碼

import pixellib

from pixellib.torchbackend.instance import instanceSegmentation
import cv2


capture = cv2.VideoCapture(0)


segment_video = instanceSegmentation()

segment_video.load_model("pointrend_resnet50.pkl")

segment_video.process_camera(capture,  show_bboxes = True, frames_per_second= 5, extract_segmented_objects=True, save_extracted_objects=True,

show_frames= True,frame_name= "frame", output_video_name="output_video.mp4")segment_video.process_

camera(capture,  show_bboxes = True, frames_per_second= 5, extract_segmented_objects=True, save_extracted_objects=True,show_frames= True,frame_name= "frame", output_video_name="output_video.mp4")

process_camera函數(shù)具有新參數(shù)extract_segmented_Object和save_extracted_Object,分別用于提取和保存分割對(duì)象。將extract_from_box添加到函數(shù)中,以提取從其邊界框坐標(biāo)分割的對(duì)象。

從實(shí)時(shí)攝影機(jī)中的框坐標(biāo)提取對(duì)象的代碼

import pixellib

from pixellib.torchbackend.instance import instanceSegmentation
import cv2


capture = cv2.VideoCapture(0)


segment_video = instanceSegmentation()

segment_video.load_model("pointrend_resnet50.pkl")

segment_video.process_camera(capture,  show_bboxes = True, frames_per_second= 5, extract_segmented_objects=True, extract_from_box=True,

save_extracted_objects=True, show_frames= True,frame_name= "frame", output_video_name="output_video.mp4")

segment_video.process_camera(capture,  show_bboxes = True, frames_per_second= 5, extract_segmented_objects=True, extract_from_box=True,save_extracted_objects=True, show_frames= True,frame_name= "frame", output_video_name="output_video.mp4")

將extract_from_box添加到函數(shù)中,以提取從其邊界框坐標(biāo)分割的對(duì)象。

在本文中,我們?cè)敿?xì)討論了如何使用PixelLib Pytorch版本對(duì)圖像和實(shí)時(shí)攝影機(jī)中的對(duì)象執(zhí)行準(zhǔn)確、快速的圖像分割和提取。我們還介紹了使用PointRend網(wǎng)絡(luò)體系結(jié)構(gòu)添加到PixelLib的升級(jí),這使得該庫(kù)能夠滿足不斷增長(zhǎng)的需求,以平衡計(jì)算機(jī)視覺中的精度和速度性能。

注意:PixelLib pytorch版本不支持自定義訓(xùn)練,使用PointRend的自定義訓(xùn)練將很快發(fā)布。

image.png


聲明: 本文由入駐維科號(hào)的作者撰寫,觀點(diǎn)僅代表作者本人,不代表OFweek立場(chǎng)。如有侵權(quán)或其他問題,請(qǐng)聯(lián)系舉報(bào)。

發(fā)表評(píng)論

0條評(píng)論,0人參與

請(qǐng)輸入評(píng)論內(nèi)容...

請(qǐng)輸入評(píng)論/評(píng)論長(zhǎng)度6~500個(gè)字

您提交的評(píng)論過(guò)于頻繁,請(qǐng)輸入驗(yàn)證碼繼續(xù)

  • 看不清,點(diǎn)擊換一張  刷新

暫無(wú)評(píng)論

暫無(wú)評(píng)論

人工智能 獵頭職位 更多
掃碼關(guān)注公眾號(hào)
OFweek人工智能網(wǎng)
獲取更多精彩內(nèi)容
文章糾錯(cuò)
x
*文字標(biāo)題:
*糾錯(cuò)內(nèi)容:
聯(lián)系郵箱:
*驗(yàn) 證 碼:

粵公網(wǎng)安備 44030502002758號(hào)