【PixelLib】图像分割

介绍

PixelLib 开源项目以最简单的方式实现图像的分割
开源链接:https://github.com/ayoolaolafenwa/PixelLib
最新的PixelLib可以在图像、视频中提取分割目标;以及利用coco检测模型来实现使用者的分割类别。
提供两种图像分割:语义分割和实例分割

项目实战

安装PixelLib及其依赖

pip install tensorflow--gpu -i https://pypi.douban.com/simple  # 版本2.0 - 2.4.1
pip install pixellib --upgrade  -i https://pypi.douban.com/simple
pip install imgaug numpy pillow scipy opencv-python scikit-image matplotlib labelme2coco -i https://pypi.douban.com/simple

实现一:图像与视频中的前景和背景分割

import pixellib
from pixellib.tune_bg import alter_bg
# 图片
change_bg = alter_bg(model_type = "pb")
change_bg.load_pascalvoc_model("xception_pascalvoc.pb")
change_bg.blur_bg("sample.jpg", extreme = True, detect = "person", output_image_name="blur_img.jpg")
# 视频
change_bg = alter_bg(model_type="pb")
change_bg.load_pascalvoc_model("xception_pascalvoc.pb")
change_bg.change_video_bg("sample_video.mp4", "bg.jpg", frames_per_second = 10, output_video_name="output_video.mp4", detect = "person")

实现二:实例分割

import pixellib
from pixellib.instance import instance_segmentation
# 图片
segment_image = instance_segmentation()
segment_image.load_model("mask_rcnn_coco.h5") 
segment_image.segmentImage("sample.jpg", show_bboxes = True, output_image_name = "image_new.jpg")
# 视频
segment_video = instance_segmentation()
segment_video.load_model("mask_rcnn_coco.h5")
segment_video.process_video("sample_video2.mp4", show_bboxes = True, frames_per_second= 15, output_video_name="output_video.mp4")

实现三:训练自己的数据集

import pixellib
from pixellib.custom_train import instance_custom_training

train_maskrcnn = instance_custom_training()
train_maskrcnn.modelConfig(network_backbone = "resnet101", num_classes= 2, batch_size = 4)
train_maskrcnn.load_pretrained_model("mask_rcnn_coco.h5")
train_maskrcnn.load_dataset("Nature")
train_maskrcnn.train_model(num_epochs = 300, augmentation=True,  path_trained_models = "mask_rcnn_models")

基于自己训练的模型进行测试

import pixellib
from pixellib.instance import custom_segmentation

test_video = custom_segmentation()
test_video.inferConfig(num_classes=  2, class_names=["BG", "butterfly", "squirrel"])
test_video.load_model("Nature_model_resnet101")
test_video.process_video("sample_video1.mp4", show_bboxes = True,  output_video_name="video_out.mp4", frames_per_second=15)

实现四:语义分割(150类:一般常见的前景和背景目标)

import pixellib
from pixellib.semantic import semantic_segmentation
# 图片
segment_image = semantic_segmentation()
segment_image.load_ade20k_model("deeplabv3_xception65_ade20k.h5")
segment_image.segmentAsAde20k("sample.jpg", overlay = True, output_image_name="image_new.jpg")
# 视频
segment_video = semantic_segmentation()
segment_video.load_ade20k_model("deeplabv3_xception65_ade20k.h5")
segment_video.process_video_ade20k("sample_video.mp4", overlay = True, frames_per_second= 15, output_video_name="output_video.mp4")  

实现五:20类常见目标的语义分割(一般常见的前景目标)

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("sample.jpg", output_image_name = "image_new.jpg")
# 视频
import pixellib
from pixellib.semantic import semantic_segmentation

segment_video = semantic_segmentation()
segment_video.load_pascalvoc_model("deeplabv3_xception_tf_dim_ordering_tf_kernels.h5")
segment_video.process_video_pascalvoc("sample_video1.mp4",  overlay = True, frames_per_second= 15, output_video_name="output_video.mp4")

模型下载

xception_pascalvoc.pbhttps://pan.baidu.com/s/120IzoORahij6scz0yXswaA
提取码:4r4p
mask_rcnn_coco.h5https://pan.baidu.com/s/1a3bHIdd8XUgFrqwgyzOVVw
提取码:98kf
deeplabv3_xception65_ade20k.h5https://pan.baidu.com/s/1xO9BrczgM8XKlM4Sn1BN0Q
提取码:9f69
deeplabv3_xception_tf_dim_ordering_tf_kernels.h5https://pan.baidu.com/s/13pEyz_4Sf1ayg4oy_JXHZA
提取码:v0dk

笔者的实战部分

"""
2021.04.08
author:alian
功能:
几行代码实现语义分割
"""
import pixellib
from pixellib.semantic import semantic_segmentation
from pixellib.instance import instance_segmentation
from pixellib.tune_bg import alter_bg
from pixellib.custom_train import instance_custom_training
from pixellib.instance import custom_segmentation
import glob

# 图像与视频中的前景和背景分割
def Alter_BG(input_path,out_path,type):
    # 图片
    if type=='img':
        for img in glob.glob('%s/*.jpg'%input_path):
            change_bg = alter_bg(model_type="pb")
            change_bg.load_pascalvoc_model('xception_pascalvoc.pb')  # "xception_pascalvoc.pb"
            change_bg.blur_bg(img, extreme=True, detect="person", output_image_name=img.replace(input_path,out_path))
    # 视频
    elif type == 'video':
        change_bg = alter_bg(model_type="pb")
        change_bg.load_pascalvoc_model('xception_pascalvoc.pb')
        change_bg.change_video_bg(input_path, "bg.jpg", frames_per_second=10, output_video_name=out_path,
                                  detect="person")
# PixelLib使用Deeplabv3+框架实现语义分割,20类常见目标的语义分割(一般常见的前景目标)
def Xception_semantic_segmentation(input_path,out_path,type):
    if type == 'img':
        for img in glob.glob('%s/*.jpg'%(input_path)):
            print(img)
            # 创建用于执行语义分割的类实例
            segment_image = semantic_segmentation()
            # 调用load_pascalvoc_model()函数加载在Pascal voc上训练的Xception模型
            segment_image.load_pascalvoc_model("deeplabv3_xception_tf_dim_ordering_tf_kernels.h5")
            # 调用segmentAsPascalvoc()函数对图像进行分割,并且分割采用pascalvoc的颜色格式进行
            # path_to_image:分割的目标图像的路径
            # path_to_output_image:保存分割后输出图像的路径
            segment_image.segmentAsPascalvoc(img, output_image_name = img.replace(input_path,out_path))
            # 也可以生成了带有分段叠加层的图像,只需要将segmentAsPascalvoc()函数的overlay属性设置为True
            # segment_image.segmentAsPascalvoc(img, output_image_name = img.replace(input_path,out_path), overlay = True)
    else:
        segment_video = semantic_segmentation()
        segment_video.load_pascalvoc_model("deeplabv3_xception_tf_dim_ordering_tf_kernels.h5")
        segment_video.process_video_pascalvoc(input_path, overlay=True, frames_per_second=15,
                                              output_video_name=out_path)
# 语义分割(150类:一般常见的前景和背景目标)
def Ade20k(input_path,out_path,type):
    if type=='img':
        for img in glob.glob('%s/*.jpg' % input_path):
            segment_image = semantic_segmentation()
            segment_image.load_ade20k_model("deeplabv3_xception65_ade20k.h5")
            segment_image.segmentAsAde20k(img, overlay=True, output_image_name=img.replace(input_path,out_path))
    else:  # 视频
        segment_video = semantic_segmentation()
        segment_video.load_ade20k_model("deeplabv3_xception65_ade20k.h5")
        segment_video.process_video_ade20k(input_path, overlay=True, frames_per_second=15,
                                           output_video_name=out_path)
# PixelLib的实例分割基于MaskRCNN框架实现
def MaskRCNN_instance_segmentation(input_path,out_path,type):
    if type == 'img':
        for img in glob.glob('%s/*.jpg' % input_path):
            # 导入用于执行实例细分的类并创建该类的实例
            segment_image = instance_segmentation()
            # 调用load_model()函数加载Mask RCNN模型以执行实例分割的代码
            segment_image.load_model("mask_rcnn_coco.h5")
            # 调用segmentImage()函数对图像执行实例分割
            # path_to_image:模型要预测的图像的路径
            # output_image_name:保存分割结果的路径
            segment_image.segmentImage(img, output_image_name=img.replace(input_path,out_path))
            # 也可以生成分割蒙版边界框,只需要将show_bboxes()函数的overlay属性设置为True
            # segment_image.segmentImage(img, output_image_name=img.replace(input_path,out_path), show_bboxes=True)
    else: # 视频
        segment_video = instance_segmentation()
        segment_video.load_model("mask_rcnn_coco.h5")
        segment_video.process_video(input_path, show_bboxes=True, frames_per_second=15,
                                    output_video_name=out_path)

if __name__ == '__main__':
    input_path = r'J:\Projects\PixelLib\images'
    out_path =r'J:\Projects\PixelLib\out'
    Alter_BG(input_path, out_path, 'img')  # 实现一
    # MaskRCNN_instance_segmentation(input_path, out_path, 'img') # 实现二
    # Ade20k(input_path, out_path, 'img')  # 实现四
    # Xception_semantic_segmentation(input_path, out_path, 'img') # 实现五

结果展示

原图

在这里插入图片描述

实现一

在这里插入图片描述

实现二

在这里插入图片描述

实现四

在这里插入图片描述

实现五

在这里插入图片描述

  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值