python工具——pixellib


pixellib 可以非常簡單的實現圖像分割

圖像分割分為:

語義分割:將圖像中每個像素賦予一個類別標簽,用不同的顏色來表示
實例分割:無需對每個像素進行標記,只需要找到感興趣物體的邊緣輪廓

安裝需要的庫

pip3  install tensorflow
pip3  install pillow
pip3  install opencv-python
pip3  install scikit-image
pip3  install pixellib

語義分隔

步驟:

導入PixelLib模塊

創建用於執行語義分割的類實例

調用load_pascalvoc_model()函數加載在Pascal voc上訓練的Xception模型

調用segmentAsPascalvoc()函數對圖像進行分割,並且分割采用pascalvoc的顏色格式進行

segmentAsPascalvoc()的參數

  path_to_image:分割的目標圖像的路徑

  path_to_output_image:保存分割后輸出圖像的路徑

eg:

image.py

import pixellib
from pixellib.semantic import semantic_segmentation

segment_image = semantic_segmentation()
segment_image.load_pascalvoc_model("deeplabv3_xception_tf_dim_ordering_tf_kernels.h5")
segment_image.segmentAsPascalvoc("test.jpg", output_image_name = "new.jpg")

帶有分段疊加層的圖像

添加 overlay=True

import pixellib
from pixellib.semantic import semantic_segmentation
segment_image = semantic_segmentation()
segment_image.load_pascalvoc_model("deeplabv3_xception_tf_dim_ordering_tf_kernels.h5")

segment_image.segmentAsPascalvoc("test.jpg", output_image_name = "new1.jpg", overlay = True)

執行分割所需的推理時間

import pixellib
from pixellib.semantic import semantic_segmentation
import time
segment_image = semantic_segmentation()
segment_image.load_pascalvoc_model("deeplabv3_xception_tf_dim_ordering_tf_kernels.h5")
start = time.time()
segment_image.segmentAsPascalvoc("test.jpg", output_image_name = "new1.jpg", overlay = True)
end = time.time()
print(f"Inference Time: {end-start:.2f}seconds")

xception模型下載地址:

https://github.com/bonlime/keras-deeplab-v3-plus/releases/download/1.1/deeplabv3_xception_tf_dim_ordering_tf_kernels.h5

下載后放在image.py所在目錄下

實例分割

import pixellib
from pixellib.instance import instance_segmentation
import time
segment_image = instance_segmentation()
segment_image.load_model("mask_rcnn_coco.h5")
start = time.time()
segment_image.segmentImage("22.jpeg", output_image_name = "22new.jpg")
end = time.time()
print(f"Inference Time: {end-start:.2f}seconds")

 

 用邊界框(bounding box)來實現分割

import pixellib
from pixellib.instance import instance_segmentation
import time
segment_image = instance_segmentation()
segment_image.load_model("mask_rcnn_coco.h5")
start = time.time()
segment_image.segmentImage("22.jpeg", output_image_name = "22new1.jpg",show_bboxes = True)
end = time.time()
print(f"Inference Time: {end-start:.2f}seconds")

 耗時

 

更多參考 https://github.com/ayoolaolafenwa/PixelLib

 Tensorflow在Windows下使用踩坑

https://gitee.com/babybeibeili/python-tool/tree/master/image


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM