使用5行代码提取图像和视频中的对象


计算机视觉是计算机查看和识别对象的媒介,目标是使计算机能够分析图像和视频中的对象,以解决不同的视觉问题。对象分割为方便的分析图像和视频中的对象奠定了基础,极大地促进了不同领域的应用,例如医疗、自动驾驶汽车的视觉以及图像和视频中的背景编辑。

PixelLib是在现实生活应用程序中,一个用来创建简易的集成图像和视频分割的库。PixelLib利用强大的对象分割技术使每个人都可以使用计算机视觉,新版本的PixelLib使计算机视觉中的对象分析变得更加容易。PixelLib使用分段技术即五行代码来提取图像和视频中的对象。

安装PixelLib及其依赖项:

使用以下方式安装Tensorflow:(PixelLib支持tensorflow 2.0及更高版本)

  • pip3安装tensorflow

使用以下方法安装PixelLib

  • pip3安装pixellib

如果已安装,请使用以下命令升级到最新版本:

  • pip3安装 pixellib —升级

使用Mask R-CNN COCO模型提取图像中的对象

import pixellib

from pixellib.instance import instance_segmentation

segment_image=instance_segmentation()
segment_image.load_model(“mask_rcnn_coco.h5”)

第1-4行: 我们导入了PixelLib程序包,创建了 实例细分类 的实例,并加载了预训练的Coco模型。从这里下载模型。

segment_image.segmentImage("image_path", extract_segmented_objects=True,                       save_extracted_objects=True, show_bboxes=True,  output_image_name="output.jpg")

这是代码的最后一行,我们使用以下参数调用了 segmentImage 函数:

image_path: 这是要分割图像的路径。

extract_segmented_objects: 此参数告知函数提取图像中分割的对象,并将其设置为 true

save_extracted_objects: 这是用于保存提取分段对象的可选参数。

show_bboxes: 该参数用于显示带有边界框的分段对象。如果将其设置为false,则仅显示分段掩膜。

output_image_name: 这是保存输出图像的路径。

样本图片

1_xgJMGBk8Zu5cDwkwIkxf-A
资料来源:保罗·克鲁 格(CCO)

对象提取的完整代码

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", extract_segmented_objects=True,
save_extracted_objects=True, show_bboxes=True,  output_image_name="output.jpg")

输出图像

1_777YuEfEX8XlOKwe7xittQ

从图像中提取对象

1_2FK1tRLDr9EOl9XGLRaxkA

注意: 原图像中所有提取的对象都应作为新图像单独保存。我只展示了其中的几个。

Coco模型中特定类的细分

我们使用预训练的Mask R-CNN coco模型执行图像分割,coco模型支持80类对象,但是在某些应用程序中,我们可能并不希望它将所有支持的对象进行细分,而PixelLib能过滤未利用的检测并细分特定类。

修改后的代码用于细分特定类

import pixellib
from pixellib.instance import instance_segmentation

segment_image = instance_segmentation()
segment_image.load_model("mask_rcnn_coco.h5")
target_classes = segment_image.select_target_classes(person=True)
segment_image.segmentImage("sample.jpg", segment_target_classes=target_classes, extract_segmented_objects=True,
save_extracted_objects=True, show_bboxes=True,  output_image_name="output.jpg")
target_classes = segment_image.select_target_classes(person=True)

除了我们调用的一个新函数 select_target_classes 之外,其他部分仍然是相同的代码 ,并且 可以过滤未使用的检测,仅分割我们的目标类 person

segment_image.segmentImage("sample.jpg", segment_target_classes=target_classes, extract_segmented_objects=True,save_extracted_objects=True, show_bboxes=True, output_image_name="output.jpg")

segmentImage 函数中,我们引入了一个名为 segment_target_classes 的新参数 以对从 select_target_classes 函数调用的目标类执行分段

1_2c2FE5XzmTh8WTiY1IH_FA

哇!我们只能在此图像中检测到人。
如果我们仅对检测图中人员的交通工具感兴趣,该怎么办?

target_classes = segment_image.select_target_classes(car=True, bicycle = True)

我们将目标从 变成了 汽车自行车

1_AF4tEBkj1VdRv50hNEC_qQ

完美!我们真的只检测到该图片中的自行车和小汽车。

注意: 如果你过滤coco模型检测结果,则将提取图像中分割目标类的对象。

使用Coco模型提取视频中的对象

PixelLib支持提取视频和摄像机中的分段对象。

import pixellib
from pixellib.instance import instance_segmentation

segment_video = instance_segmentation()
segment_video.load_model("mask_rcnn_coco.h5")
segment_video.process_video("sample.mp4", show_bboxes=True,  extract_segmented_objects=True,
save_extracted_objects=True, frames_per_second= 5,  output_video_name="output.mp4")

样本视频

segment_video.process_video("sample.mp4", show_bboxes=True,  extract_segmented_objects=True,save_extracted_objects=True, frames_per_second= 5,  output_video_name="output.mp4")

除了我们从 segmentImage 更改为 process_video函数 外,代码仍是一样的。它采用以下参数:

  • show_bboxes: 用于显示带有边界框的分段对象的参数。如果设置为false,则仅显示分段掩膜。
  • frames_per_second: 用于设置保存视频文件的每秒帧数。在这种情况下,它设置为5,即保存的视频文件每秒将有5帧/秒。
  • extract_segmented_objects: 告诉函数提取图像中分割的对象的参数,它将被设置为 true
  • save_extracted_objects: 用于保存提取的分段对象的可选参数。
  • output_video_name: 已保存的分段视频名称

输出视频

从视频中提取对象
1_zjYFfD6aXkxf16S1OZZZEA

注意: 视频中的所有对象均被提取并单独保存为图像。我只展示了其中一部分。

视频中特定类的细分

PixelLib可以过滤未使用的检测,并对视频和摄像机素材中的特定类进行细分。

target_classes = segment_video.select_target_classes(person=True)

segment_video.process_video("sample.mp4", show_bboxes=True, segment_target_classes= target_classes, extract_segmented_objects=True,save_extracted_objects=True, frames_per_second= 5,  output_video_name="output.mp4")

用于检测的目标类别设置为人员,因此我们只能对视频中的人员进行细分。

target_classes = segment_video.select_target_classes(car = True) 
segment_video.process_video("sample.mp4", show_bboxes=True, segment_target_classes= target_classes, extract_segmented_objects=True,save_extracted_objects=True, frames_per_second= 5, output_video_name="output.mp4")

细分的目标类设置为car,那我们就只能对视频中的car进行细分。

用于在视频中分割特定类和对象提取的完整代码

import pixellib
from pixellib.instance import instance_segmentation


segment_video = instance_segmentation()
segment_video.load_model("mask_rcnn_coco.h5")
target_classes = segment_video.select_target_classes(person=True)
segment_video.process_video("sample.mp4", show_bboxes=True, segment_target_classes= target_classes, extract_segmented_objects=True,
save_extracted_objects=True, frames_per_second= 5,  output_video_name="output.mp4")

完整代码,用于细分相机素材中的特定类和对象提取

import pixellib
from pixellib.instance import instance_segmentation
import cv2

capture = cv2.VideoCapture(0)
segment_camera = instance_segmentation()
segment_camera.load_model("mask_rcnn_coco.h5")
target_classes = segment_camera.select_target_classes(person=True)
seg, out = segment_camera.process_camera(capture, show_bboxes=True, show_frames=True, segment_target_classes=target_classes,
extract_segmented_objects=True, save_extracted_objects=True,frame_name="frame", frames_per_second=5, output_video_name="output.mp4")
import cv2 capture = cv2.VideoCapture(0)

我们导入了cv2,并包含了捕获相机帧的代码。

segment_camera.process_camera(capture, show_bboxes=True, 
show_frames=True, extract_segmented_objects=True, 
save_extracted_objects=True,frame_name="frame", 
frames_per_second=5, output_video_name="output.mp4")

在执行分段的代码中,我们替换了视频的文件路径以进行捕获,即我们正在处理由摄像机捕获的帧流。我们添加了额外的参数以显示相机的帧:

  • show_frames: 用于处理分段相机帧所显示的参数。
  • frame_name: 显示的相机框的指定名称。

用PixelLib训练的自定义模型提取图像中的对象

PixelLib支持训练自定义细分模型,并且可以使用自定义模型提取细分对象。

import pixellib from
pixellib.instance import custom_segmentation 
segment_image = custom_segmentation() segment_image.inferConfig(num_classes=2, class_names=['BG', 'butterfly', 'squirrel']) 
segment_image.load_model("Nature_model_resnet101.h5")

Line1-4: 我们引用PixelLib包,并创建一个 定制分割类 的实例 调用推理配置函数( nferConfig 并加载自定义模式。从此处下载定制模型。

定制模型支持两个类,如下所示:

  • 蝴蝶
  • 松鼠
segment_image.segmentImage("image_path", 
extract_segmented_objects=True, 
ave_extracted_objects=True, show_bboxes=True, 
output_image_name="output.jpg")

我们调用了用于coco模型检测的同一个函数 segmentImage

使用自定义模型进行对象提取的完整代码

import pixellib
from pixellib.instance import custom_segmentation

segment_image = custom_segmentation()
segment_image.inferConfig(num_classes=2, class_names=['BG', 'butterfly', 'squirrel'])
segment_image.load_model("Nature_model_resnet101.h5")
segment_image.segmentImage("sample.jpg", show_bboxes = True, extract_segmented_objects = True,
save_extracted_objects = True, output_image_name="output.jpg")

sample image

样本图片

1_CPvSsLQPFyLByfOMTKk0Jg

来源:Wikicommons(CCO)上的Peter Trimming

Output

输出

1_QZzhez1m2_O4hjlxyRKSsg

从图像中提取对象

1_0oSIxkI5lPWL3zrsTLqHrQ

用PixelLib训练的自定义模型提取视频中的对象

样本视频

使用自定义模型在视频中提取对象的完整代码。

import pixellib
from pixellib.instance import custom_segmentation

segment_video = custom_segmentation()
segment_video.inferConfig(num_classes=2, class_names=['BG', 'butterfly', 'squirrel'])
segment_video.load_model("Nature_model_resnet101.h5")
segment_video.process_video("sample.mp4", show_bboxes=True, extract_segmented_objects=True, save_extracted_objects=True,
frames_per_second= 5,  output_video_name="output.mp4")

输出

提取对象

1_aCqFvyTpxu2A45M4_k7lNQ%20(1)

使用自定义模型在相机源中提取对象的完整代码

import pixellib
from pixellib.instance import custom_segmentation
import cv2

capture = cv2.VideoCapture(0)
segment_frame = custom_segmentation()
segment_frame.inferConfig(num_classes=2, class_names=['BG', 'butterfly', 'squirrel'])
segment_frame.load_model("Nature_model_resnet101.h5")
seg, out = segment_frame.process_camera(capture, show_bboxes=True, show_frames=True, extract_segmented_objects=True,
save_extracted_objects=True,frame_name="frame", frames_per_second=5, output_video_name="output.mp4")

阅读本文,了解如何使用PixelLib训练自定义模型。
Custom Instance Segmentation Training With 7 Lines Of Code.
towardsdatascience.com

原始作者 Ayoola Olafenwa
原始链接 https://towardsdatascience.com/extraction-of-objects-in-images-and-videos-using-5-lines-of-code-6a9e35677a31

推荐阅读
相关专栏
开发者实践
182 文章
本专栏仅用于分享音视频相关的技术文章,与其他开发者和声网 研发团队交流、分享行业前沿技术、资讯。发帖前,请参考「社区发帖指南」,方便您更好的展示所发表的文章和内容。