环境准备:
# 语义分割Demo
# 安装使用方法:https://pixellib.readthedocs.io/en/latest/index.html
# python=3.8
# TensorFlow
# Windows:pip install tensorflow==2.2.0 (最新版的2.8可能会报错)
# pip install imgaug
# pip install pixellib --upgrade
# pip install protobuf==3.19.0
# conda install -c conda-forge jupyterlab
语义分割示例:
import cv2
# 导入语义分割
from pixellib.semantic import semantic_segmentation
def cv_show(neme, img):
# cv2.namedWindow(neme, cv2.WINDOW_NORMAL)
cv2.imshow(neme, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
img = cv2.imread('./images/baby.jpg')
img_cvt = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# cv_show('neme', img_cvt)
if __name__ == '__main__':
# 实例化
segment = semantic_segmentation()
# 加载模型
segment.load_ade20k_model('./weights/deeplabv3_xception65_ade20k.h5')
# 分割
results, output = segment.segmentFrameAsAde20k(img, overlay=True)
cv_show('neme', output)
实例分割示例:
import cv2
import numpy as np
# 导入实例分割
from pixellib.instance import instance_segmentation
def cv_show(neme, img):
# cv2.namedWindow(neme, cv2.WINDOW_NORMAL)
cv2.imshow(neme, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
# 读取图片
img = cv2.imread('./images/baby.jpg')
img_cvt = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
# cv_show('neme', img)
# 实例化
instance = instance_segmentation()
# 加载模型
instance.load_model('./weights/mask_rcnn_coco.h5')
# 实例分割
target_classes = instance.select_target_classes(person=True)
results, output = instance.segmentFrame(img.copy(), segment_target_classes=target_classes)
# cv_show('neme', output)
# 创建黑色背景图
black_bg = np.zeros((img.shape[:2]))
person_count = len(results['class_ids'])
masks = results['masks']
# 将多个人结果合成一个蒙版
for p_index in range(person_count):
# 单个人蒙版 在循环中合成多个人
black_bg = np.where(masks[:, :, p_index] == True, 255, black_bg)
cv_show('neme', black_bg)
该博客展示了如何在Python环境中使用Pixellib库进行语义分割和实例分割。通过安装必要的依赖库,如TensorFlow和Pixellib,然后实例化分割模型并加载预训练权重,可以对图像进行分割。在语义分割示例中,使用了DeepLabv3_xception65_ade20k模型,而在实例分割示例中,应用了mask_rcnn_coco模型来识别和分离图像中的特定对象。
1028

被折叠的 条评论
为什么被折叠?



