cv_unet_image-matting|人像抠图

cv_unet_image-matting是一个自动识别人像AI抠图的模型,可以无需任何额外输入,直接自动推理含有人像的图像,输出四通道人像抠图结果,基于原图和粗语义分割mask预测精细的抠图结果。

cv_unet_image-matting|自动识别人像抠图

人像抠图对输入含有人像的图像进行处理,无需任何额外输入,实现端到端人像抠图,输出四通道人像抠图结果。模型分为粗分割和精细抠图两个子网络,将人像抠图复杂问题拆解,先粗分割再精细化分割,两部分网络均为unet结构。粗分割网络从输入图片预测人像语义分割mask,精细分割网络基于原图和粗语义分割mask预测精细的抠图结果。

适用范围

使用方式:

  • 直接推理,输入图像直接进行推理

使用范围:

  • 适合含有人像的图像分割,期望图像中人像占比不要过小
  • 在分辨率小于2000×2000图像上可取得期望效果

目标场景:

  • 需要图像抠图的场景,如换背景等

如何使用

本模型基于tensorflow进行训练和推理,在ModelScope框架上,提供输入图片,即可以通过简单的Pipeline调用来使用人像抠图模型。

部署教程

如果您是初学者,对于命令行不太理解,那么请按下键盘上的Win键+R键后,在弹出的新窗口内输入CMD并按下回车,打开CMD窗口,按顺序执行如下的每一条命令。

首先我们需要确认一个工作目录,用来存放cv_unet_image-matting的相关环境依赖文件。本站所选择的目录为D盘的根目录下openai.wiki文件夹,完整路径为:D:\openai.wiki。

检测D盘是否在openai.wiki目录下有没有cv_unet_image文件夹,没有则创建该文件夹。

if not exist D:\openai.wiki\cv_unet_image-matting mkdir D:\openai.wiki\cv_unet_image-matting

强制切换工作路径为D盘的openai.wiki\cv_unet_image-matting文件夹。

cd /d D:\openai.wiki\cv_unet_image-matting

为不影响电脑中的现有环境,请一定要安装Conda,如果您不知道什么是Conda,或者未安装过Conda,请参考如下文章,安装部署Conda之后再继续以下步骤。

在CMD中执行下面的命令行,创建Conda虚拟环境至该项目的目录中,方便日后重装系统也能够正常使用,无需重新部署环境。

conda create -p D:\openai.wiki\cv_unet_image-matting\MyENV

执行完成上面的命令之后,将会在CMD窗口中看到Proceed ([y]/n)?提示,我们直接按下回车即可。

初始化Conda环境,避免后续可能报错。

conda init cmd.exe

激活已创建的Conda环境,这样我们可以将我们后续所需要的所有环境依赖都安装至此环境下。

conda activate D:\openai.wiki\cv_unet_image-matting\MyENV

执行如下命令,安装阿里达摩院相关依赖。

pip install modelscope

关于tensorflow的安装,支持CPU和GPU双版本,以下两行代码选择其中一个执行安装即可。

如果你想用CPU来计算,请复制第一行内容执行。如果你想用GPU来计算,请复制第二行内容执行。

pip install --upgrade tensorflow
pip install --upgrade tensorflow-gpu

执行如下命令,安装torch依赖。

pip3 install torch torchvision torchaudio -i https://pypi.tuna.tsinghua.edu.cn/simple

安装CV依赖库,也可以说是本模型中最重要的依赖啦。

pip install "modelscope[cv]" -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html

使用教程

此模型官方并未给出一个详细的脚本,本站写了一个简单的Python脚本,使用VSCode运行该Python脚本即可。在运行该脚本之前,请先对以下Python脚本中的名称格式路径根据自身情况进行修改。

注意:使用VS Code执行该脚本时,VS Code会提示您选择一个Python环境,此时您可以选择路径为D:\openai.wiki\cv_unet_image-matting\MyENV的Python环境,这就是我们刚刚使用Conda所生成的虚拟环境。

# 此脚本由openai.wiki提供,转载请注明出处,谢谢配合。

from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
import cv2

# 原图名称
resultIMGName = 'openai.wiki'
# 原图格式
resultIMGFormat = '.jpg'
# 原图路径
resultIMGPath = 'D:\openai.wiki\cv_unet_image-matting'

# 输出图像名称
outputIMGName = 'openai.wiki'
# 输出格式 | 尽量不要改
outputFormat = '.png'
# 输出路径
outputIMGPath = 'D:\openai.wiki\cv_unet_image-matting'

# 下面的内容尽量不要改动
p = pipeline('portrait-matting', 'damo/cv_unet_image-matting')
print (('原图路径:%s/%s%s'%(resultIMGPath,resultIMGName,resultIMGFormat)).replace('\\', '/'))

result = p(('%s/%s%s'%(resultIMGPath,resultIMGName,resultIMGFormat)).replace('\\', '/'))

output_path = ('%s/%s%s'%(outputIMGPath,outputIMGName,outputFormat)).replace('\\', '/')
print (('输出路径:%s/%s%s'%(outputIMGPath,outputIMGName,resultIMGFormat)).replace('\\', '/'))
cv2.imwrite(output_path, result['output_img'])
print ('输出完成')

⚠️警告:这里有一个坑,如果您输出的图像格式不为PNG,那么您将会得到一张未经过任何处理的原图,所以关于输出的格式,请尽量不要去手动修改。

效果展示

其实有瑕疵的图片是经常通过遇到的,如果是侧身体位,或者四肢并不明显,那么就可能会导致如下图这种,胳膊缺失的问题。

本站有意找来一张清晰度很差的,但是可以看到该抠图效果还是不错的。

如果人物与背景的颜色过于相似,那么就很有可能出现抠图不干净的问题。

下图中对于边缘的抠图效果都很好,甚至双腿交叉蹭的空隙都有所处理,但是胳膊却有所缺失了。

其它效果展示,大致如下,总的来说,还是挺不错的。

此文章由OpenAI开源维基百科原创发布,如若转载请注明出处:https://openai.wiki/cv_unet_image-matting.html

(0)
上一篇 2023-04-05 03:10
下一篇 2023-04-06 12:49

相关推荐

  • CV_ORC-Text-Spotting|多场景文字ORC识别

    CV_ORC-Text-Spotting是一个多场景文字识别模型,可用于提取图片中的文字并输出对应字符串。该模型可适用于多种场景的文字识别,包括手写、自然、车牌和文档印刷,并提供了相应的文本检测模型。

    2023-04-12
    001.6K
  • cv_unet_universal-matting|通用抠图

    cv_unet_universal-matting可以对包括人、物、植物、车辆等在内的多种物体进行抠图处理,无需额外输入。该模型基于粗分割和精细抠图两个子网络实现,具有较好的抠图效果。

    2023-04-08
    021.8K
  • cv_hrnetocr_skychange|天空图像替换

    cv_hrnetocr_skychange是一个基于hrnet-ocr框架的天空图像替换模型,可实现换天算法,自动获取天空区域的alpha图,并利用融合算法将原天空进行替换,从而改变原图的天空样式。

    2023-04-08
    001.3K
  • Segment-Anything|万物分割

    分割任何物体(Segment Anything Model),可输入提示生成高质量的对象掩码,用于图像中所有对象的分割任务。该模型有望取代传统的OpenCV,成为未来图像分割抠图领域的主流模型。

    2023-04-10
    0154.2K
  • mPLUG|图像中文描述

    AI根据图片信息生成对应描述自动图像Tag标签,mplug_image-captioning_coco_base_zh模型是生成多模态的基础,基于skip-connections高效跨模态融合框架。

    2023-07-09
    021.1K

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

评论列表(12条)

  • 伊藤靖香
    伊藤靖香 2023-05-05 11:11

    蟹蟹~~跑成功啦,有没有图片修复相关的教程鸭?比如图片人物上面有水印,先inpaint后抠图

    • PhiltreX
      PhiltreX 2023-05-05 15:17

      @伊藤靖香用Stable Diffusion重绘局部可以实现,但是稍等麻烦一点。

    • 伊藤靖香
      伊藤靖香 2023-05-06 17:41

      @PhiltreX尝试了一个流程:先用OCR模型(cv_resnet18_ocr-detection-line-level_damo)跑出图片上的文字坐标,然后用(cv_fft_inpainting_lama)对图片进行修复(需要对修补地方进行掩码操作),效果还不错,最后再进行人物抠图。

    • PhiltreX
      PhiltreX 2023-05-06 19:36

      @伊藤靖香其实我对这个效果还是比较乐观的,最起码速度也挺快。

  • strayheart
    strayheart 2023-07-01 22:37

    为什么我在webui上用zenny_申才恩_抠像进行裁剪时,会还原成抠图前的原图?真的奇怪。

    • PhiltreX
      PhiltreX 2023-07-08 07:05

      @123网络问题,这样试一下pip install modelscope -f https://modelscope.oss-cn-beijing.aliyuncs.com/releases/repo.html

    • 123
      123 2023-07-08 19:40

      @PhiltreX这个我用手机共享过去了,不过到了选择gpu计算那一步又有问题,挂了tz也不行
      (C:\openai.wiki\cv_unet_image-matting\MyENV) C:\openai.wiki\cv_unet_image-matting>pip install –upgrade tensorflow-gpu
      Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
      Collecting tensorflow-gpu
      Using cached https://pypi.tuna.tsinghua.edu.cn/packages/8a/45/fa31ced1db38f9424f262dfbf35747fe5378b5c808cecb373c8cb8e515d3/tensorflow-gpu-2.12.0.tar.gz (2.6 kB)
      Preparing metadata (setup.py) … error
      error: subprocess-exited-with-error

      × python setup.py egg_info did not run successfully.
      │ exit code: 1
      ╰─> [39 lines of output]
      Traceback (most recent call last):
      File “C:\Users\Administrator\anaconda3\lib\site-packages\setuptools\_vendor\packaging\requirements.py”, line 35, in __init__
      parsed = _parse_requirement(requirement_string)
      File “C:\Users\Administrator\anaconda3\lib\site-packages\setuptools\_vendor\packaging\_parser.py”, line 64, in parse_requirement
      return _parse_requirement(Tokenizer(source, rules=DEFAULT_RULES))
      File “C:\Users\Administrator\anaconda3\lib\site-packages\setuptools\_vendor\packaging\_parser.py”, line 82, in _parse_requirement
      url, specifier, marker = _parse_requirement_details(tokenizer)
      File “C:\Users\Administrator\anaconda3\lib\site-packages\setuptools\_vendor\packaging\_parser.py”, line 126, in _parse_requirement_details
      marker = _parse_requirement_marker(
      File “C:\Users\Administrator\anaconda3\lib\site-packages\setuptools\_vendor\packaging\_parser.py”, line 147, in _parse_requirement_marker
      tokenizer.raise_syntax_error(
      File “C:\Users\Administrator\anaconda3\lib\site-packages\setuptools\_vendor\packaging\_tokenizer.py”, line 165, in raise_syntax_error
      raise ParserSyntaxError(
      setuptools.extern.packaging._tokenizer.ParserSyntaxError: Expected end or semicolon (after name and no valid version specifier)
      python_version>”3.7″
      ^

      The above exception was the direct cause of the following exception:

      Traceback (most recent call last):
      File “”, line 2, in
      File “”, line 34, in
      File “C:\Users\Administrator\AppData\Local\Temp\pip-install-phtgeykh\tensorflow-gpu_2ebf3598010d44d19e4db0815e9d8cc1\setup.py”, line 40, in
      setuptools.setup()
      File “C:\Users\Administrator\anaconda3\lib\site-packages\setuptools\__init__.py”, line 106, in setup
      _install_setup_requires(attrs)
      File “C:\Users\Administrator\anaconda3\lib\site-packages\setuptools\__init__.py”, line 77, in _install_setup_requires
      dist.parse_config_files(ignore_option_errors=True)
      File “C:\Users\Administrator\anaconda3\lib\site-packages\setuptools\dist.py”, line 900, in parse_config_files
      self._finalize_requires()
      File “C:\Users\Administrator\anaconda3\lib\site-packages\setuptools\dist.py”, line 597, in _finalize_requires
      self._move_install_requirements_markers()
      File “C:\Users\Administrator\anaconda3\lib\site-packages\setuptools\dist.py”, line 637, in _move_install_requirements_markers
      inst_reqs = list(_reqs.parse(spec_inst_reqs))
      File “C:\Users\Administrator\anaconda3\lib\site-packages\setuptools\_vendor\packaging\requirements.py”, line 37, in __init__
      raise InvalidRequirement(str(e)) from e
      setuptools.extern.packaging.requirements.InvalidRequirement: Expected end or semicolon (after name and no valid version specifier)
      python_version>”3.7″
      ^
      [end of output]

      note: This error originates from a subprocess, and is likely not a problem with pip.
      error: metadata-generation-failed

      × Encountered error while generating package metadata.
      ╰─> See above for output.

      note: This is an issue with the package mentioned above, not pip.
      hint: See above for details.

    • PhiltreX
      PhiltreX 2023-07-09 07:13

      @123根据提供的错误日志,似乎是由于命令中的减号(-)被替换成了特殊的字符,导致安装命令无法正确执行。请尝试使用正确的减号(-)重新输入命令:
      pip install -–upgrade tensorflow-gpu

    • 123
      123 2023-07-09 17:24

      @PhiltreX我是复制粘贴的呀,用了您刚给的命令又有别的报错😢
      Usage:
      pip install [options] [package-index-options] …
      pip install [options] -r [package-index-options] …
      pip install [options] [-e] …
      pip install [options] [-e] …
      pip install [options] …

      no such option: -–

  • 皮皮
    皮皮 2023-08-31 22:36

    这边运行后没有对应结果,想咨询下是否有办法解决。

    2023-08-31 22:35:07,133 – modelscope – INFO – PyTorch version 2.0.1 Found.
    2023-08-31 22:35:07,136 – modelscope – INFO – TensorFlow version 2.10.0 Found.
    2023-08-31 22:35:07,136 – modelscope – INFO – Loading ast index from C:\Users\木偶\.cache\modelscope\ast_indexer
    2023-08-31 22:35:07,276 – modelscope – INFO – Loading done! Current index file version is 1.8.4, with md5 004c5d7eaa56ff72e07c9cda264175a3 and a total number of 902 components indexed
    2023-08-31 22:35:08,992 – modelscope – INFO – Model revision not specified, use the latest revision: v1.0.0
    2023-08-31 22:35:11,582 – modelscope – INFO – initiate model from C:\Users\木偶\.cache\modelscope\hub\damo\cv_unet_image-matting
    2023-08-31 22:35:11,582 – modelscope – INFO – initiate model from location C:\Users\木偶\.cache\modelscope\hub\damo\cv_unet_image-matting.
    2023-08-31 22:35:11,590 – modelscope – WARNING – No preprocessor field found in cfg.
    2023-08-31 22:35:11,590 – modelscope – WARNING – No val key and type key found in preprocessor domain of configuration.json file.
    2023-08-31 22:35:11,590 – modelscope – WARNING – Cannot find available config to build preprocessor at mode inference, current config: {‘model_dir’: ‘C:\\Users\\木偶\\.cache\\modelscope\\hub\\damo\\cv_unet_image-matting’}. trying to build by task and model information.
    2023-08-31 22:35:11,590 – modelscope – WARNING – Find task: portrait-matting, model type: None. Insufficient information to build preprocessor, skip building preprocessor
    WARNING:tensorflow:From C:\project\image\env\lib\site-packages\modelscope\utils\device.py:60: is_gpu_available (from tensorflow.python.framework.test_util) is deprecated and will be removed in a future version.
    Instructions for updating:
    Use `tf.config.list_physical_devices(‘GPU’)` instead.
    2023-08-31 22:35:11.594261: I tensorflow/core/platform/cpu_feature_guard.cc:193] This TensorFlow binary is optimized with oneAPI Deep Neural Network Library (oneDNN) to use the following CPU instructions in performance-critical operations: AVX AVX2
    To enable them in other operations, rebuild TensorFlow with the appropriate compiler flags.
    2023-08-31 22:35:11,597 – modelscope – INFO – loading model from C:\Users\木偶\.cache\modelscope\hub\damo\cv_unet_image-matting\tf_graph.pb
    WARNING:tensorflow:From C:\project\image\env\lib\site-packages\modelscope\pipelines\cv\image_matting_pipeline.py:45: FastGFile.__init__ (from tensorflow.python.platform.gfile) is deprecated and will be removed in a future version.
    Instructions for updating:
    Use tf.gfile.GFile.
    2023-08-31 22:35:11,799 – modelscope – INFO – load model done
    原图路径:C:/Users/木偶/Pictures/素材/zenny_申才恩_原图/openai.wiki.jpg
    2023-08-31 22:35:11.939563: I tensorflow/compiler/mlir/mlir_graph_optimization_pass.cc:354] MLIR V1 optimization pass is not enabled
    输出路径:C:/Users/木偶/Pictures/素材/cv/openai.wiki.jpg
    输出完成

微信