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")
耗时
更多参考 IT虾米网
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/20473.html