文章目录
简介安装初试语义分割Pascalvoc(20类)Ade20k(150类)实例分割COCO(80类)遇到的坑应用抠图换背景参考文献本文模型、代码、测试图片下载地址
简介
Pixellib
库可对图像或视频执行图像分割,分割类型有:
语义分割(Semantic Segmentation)
对图像中的每个像素打上类别标签,如下图,把图像分为人(红色)、树木(深绿)、草地(浅绿)、天空(蓝色)标签
实例分割(Instance Segmentation)
目标检测和语义分割的结合,将目标检测出来(目标检测),然后对每个像素打上标签(语义分割)。
安装
安装库
pip install tensorflowpip install pillowpip install opencv-pythonpip install scikit-imagepip install pixellib
模型
Deeplabv3+ 预训练模型
用于语义分割,训练数据为 150类的Ade20k 和 20类的Pascalvoc。Mask RCNN 预训练模型
用于实例分割,训练数据为 80类的COCO。
上述模型亦可在 PixelLib 预训练模型 找到。
下载速度太慢可参考:GitHub下载加速
初试
photo.jpg
sample1.jpg
from pixellib.semantic import semantic_segmentationsegment_image = semantic_segmentation()segment_image.load_pascalvoc_model('deeplabv3_xception_tf_dim_ordering_tf_kernels.h5')segment_image.segmentAsPascalvoc('photo.jpg', output_image_name='photo_semantic.jpg')segment_image.segmentAsPascalvoc('sample1.jpg', output_image_name='sample1_semantic.jpg')
效果
语义分割
Pascalvoc(20类)
叠加图层,添加参数overlay=True
from pixellib.semantic import semantic_segmentationsegment_image = semantic_segmentation()segment_image.load_pascalvoc_model('deeplabv3_xception_tf_dim_ordering_tf_kernels.h5')segment_image.segmentAsPascalvoc('sample1.jpg', output_image_name='sample1_semantic.jpg')segment_image.segmentAsPascalvoc('sample1.jpg', output_image_name='sample1_semantic_overlay.jpg', overlay=True) # 叠加图层
效果
Pascalvoc 预训练模型包含的类有:
Ade20k(150类)
sample2.jpg
sample3.jpg
叠加图层,添加参数overlay=True
from pixellib.semantic import semantic_segmentationsegment_image = semantic_segmentation()segment_image.load_ade20k_model('deeplabv3_xception65_ade20k.h5')segment_image.segmentAsAde20k('sample2.jpg', output_image_name='sample2_semantic.jpg')segment_image.segmentAsAde20k('sample3.jpg', output_image_name='sample3_semantic.jpg')segment_image.segmentAsAde20k('sample2.jpg', output_image_name='sample2_semantic_overlay.jpg', overlay=True) # 叠加图层segment_image.segmentAsAde20k('sample3.jpg', output_image_name='sample3_semantic_overlay.jpg', overlay=True) # 叠加图层
效果
实例分割
COCO(80类)
sample4.jpg
显示边界,添加参数show_bboxes=True
from pixellib.instance import instance_segmentationsegment_image = instance_segmentation()segment_image.load_model('mask_rcnn_coco.h5')segment_image.segmentImage('sample4.jpg', output_image_name='sample4_instance.jpg')segment_image.segmentImage('sample4.jpg', output_image_name='sample4_instance_box.jpg', show_bboxes=True)
效果
遇到的坑
GPU模式下报错,可强制使用CPU,添加代码import osos.environ["CUDA_VISIBLE_DEVICES"] = "-1"
报错UnboundLocalError: local variable 'labels' referenced before assignment
换张图片试试。
应用
抠图换背景
import cv2import numpy as npfrom pixellib.semantic import semantic_segmentationfilename = 'photo.jpg'color = {'red': (0, 0, 255),'blue': (219, 142, 67),'white': (255, 255, 255)}# 读取图片img = cv2.imread(filename)# 语义分割segment_image = semantic_segmentation()segment_image.load_pascalvoc_model('deeplabv3_xception_tf_dim_ordering_tf_kernels.h5')output, seg_img = segment_image.segmentAsPascalvoc(filename)output, overlay = segment_image.segmentAsPascalvoc(filename, overlay=True)# 黑色遮罩lowerb = np.array([0, 0, 0]) # 下限upperb = np.array([0, 0, 0]) # 上限hsv = cv2.cvtColor(seg_img, cv2.COLOR_BGR2HSV) # BGR转HSV,处理更精确mask = cv2.inRange(hsv, lowerb, upperb) # 遮罩kernel = np.ones((3, 3), np.uint8) # 卷积核mask = cv2.erode(mask, kernel, iterations=1) # 腐蚀# 替换颜色rows, cols, channels = img.shaperesult = img.copy()result[mask != 0] = color['blue'] # OpenCV通道为BGR# 可视化cv2.imshow('1', img)cv2.imshow('2', overlay)cv2.imshow('3', mask)cv2.imshow('4', result)# 保存cv2.waitKey(0)cv2.destroyAllWindows()cv2.imwrite("photo_recompose.jpg", result)
效果
效果仍需优化,减少图3的黑色边缘
完美效果可以用 remove.bg,阅读:Python调用PIL实现图片合成(含证件照换背景)