centos7 PaddleHub人像分割模型:AI人像抠图及图像合成

2023-10-25 10:59

本文主要是介绍centos7 PaddleHub人像分割模型:AI人像抠图及图像合成,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

centos 安装抠图环境:

#复制下面命令一条一条的在windows的命令行窗口里面执行,需要一些时间
#执行安装paddlehub第三步后可能会出现一些问题,解决方式在文章下方
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple matplotlib
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pillow
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple PaddlePaddle
pip install paddlehub==1.6.0 -i https://pypi.tuna.tsinghua.edu.cn/simple
hub install deeplabv3p_xception65_humanseg==1.0.0

问题1:在安装pip install -i https://pypi.tuna.tsinghua.edu.cn/simple PaddlePaddle 这一步之后,会自动安装opencv-python,这时候如果在centos环境下进入python,输入impot cv2会报错,windows环境应该没问题

ImportError: libSM.so.6: cannot open shared object file: No such file or directory
ImportError: libXrender.so.1: cannot open shared object file: No such file or directory
ImportError: libXext.so.6: cannot open shared object file: No such file or directory

原因是:缺少共享库,我们直接通过yum来安装libSM解决不了问题, 那是因为yum源默认提供的库是i686的, 如果我们的服务器系统是64位的,应该要安装的是x86_64版而非i686.

解决方式:

来查看一下yum默认提供的libSM, 结果如下

yum whatprovides libSM.so.6

在这里插入图片描述

可以看到默认提供的是i686的, 但是我们需要x86_64, 所以安装的时候把i686改成x86_64, 如下

sudo yum install libSM-1.2.2-2.el7.x86_64 --setopt=protected_multilib=false

剩下的就直接输入吧

sudo yum install libXrender-0.9.10-1.el7.x86_64 --setopt=protected_multilib=false
sudo yum install libXext-1.3.3-3.el7.x86_64 --setopt=protected_multilib=false

问题2:如果安装过程又出现问题,提示【SyntaxError: invalid syntax  由于用户取消而退出

解决方式: 

必须修改的两个yum配置文件

因为yum使用python2,因此替换为python3后可能无法正常工作,继续使用这个python2.7.5,因此需要修改yum相关配置文件。

(1)vi /usr/bin/yum

第一行:#!/usr/bin/python --> #!/usr/bin/python2.7

(2) vi /usr/libexec/urlgrabber-ext-down

第一行:#!/usr/bin/python --> #!/usr/bin/python2.7

完成上面两步,现在使用yum命令基本不会出现这样的错误:SyntaxError: invalid syntax  由于用户取消而退出

安装完成后,试一下可以成功导入cv2了
在这里插入图片描述

问题3: 执行 hub install deeplabv3p_xception65_humanseg==1.0.0 的时候可能会出现hub: command not found

解决方式:配置python3的环境变量,在 /etc/profile加入python的安装路径配置环境变量,具体操作可以看我的另一篇文章https://blog.csdn.net/clz979991314/article/details/107842982

代码片段: 

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from matplotlib import animation
import paddlehub as hub
from PIL import Image, ImageSequence
import numpy as np
import os
import flask, json
from flask import request# 创建一个服务,把当前这个python文件当做一个服务
server = flask.Flask(__name__)# 合成函数
def blend_images(fore_image, output_path, base_size, color):"""将抠出的人物图像换背景fore_image: 前景图片,抠出的人物图片base_size: 图片尺寸"""# 制作指定大小的背景色base_image = Image.new("RGBA", base_size, color)# 读入图片base_image = base_image.convert('RGB')fore_image = Image.open(fore_image).resize(base_image.size)# 图片加权合成scope_map = np.array(fore_image)[:,:,-1] / 255scope_map = scope_map[:,:,np.newaxis]scope_map = np.repeat(scope_map, repeats=3, axis=2)res_image = np.multiply(scope_map, np.array(fore_image)[:,:,:3]) + np.multiply((1-scope_map), np.array(base_image))#保存图片res_image = Image.fromarray(np.uint8(res_image))print(res_image)res_image.save(output_path)# 抠图@server.route('/matting', methods=['get', 'post'])
def matting():# 测试图片路径test_path = 'D:\\image\\'# 输出路径output_path = 'D:\\image\\test\\'# 背景图片地址base_image = (295, 413)# 白色(255,255,255) 红色(182,38,38) 蓝色(67,142,219)color = (67,142,219)# 待预测图片名称test_file_name = "5"# 待预测图片后缀test_file_suffix = ".jpg"# 输入文件名称output_file_name = test_file_name + test_file_suffixtest_img_path = [test_file_name + test_file_suffix]test_img_path = [test_path + img for img in test_img_path]img = mpimg.imread(test_img_path[0]) module = hub.Module(name="deeplabv3p_xception65_humanseg")input_dict = {"image": test_img_path}# execute predict and print the resultresults = module.segmentation(data=input_dict)print(results)output_path_img = output_path + output_file_nameblend_images('humanseg_output/'+test_file_name+'.png', output_path_img, base_image, color)return output_path_imgif __name__ == '__main__':server.run(debug=True, port=8888, host='0.0.0.0')# 指定端口、host,0.0.0.0代表不管几个网卡,任何ip都可以访问

如果安装的deeplabv3p_xception65_humanseg版本不是1.0.0,而是1.1.0或1.1.1

则使用下面的代码:

import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from matplotlib import animation
import paddlehub as hub
from PIL import Image, ImageSequence
import numpy as np
import os
import flask, json
from flask import request
import cv2# 创建一个服务,把当前这个python文件当做一个服务
server = flask.Flask(__name__)# 合成函数
def blend_images(fore_image, output_path, base_size, color):"""将抠出的人物图像换背景fore_image: 前景图片,抠出的人物图片base_size: 图片尺寸"""# 制作指定大小的背景色base_image = Image.new("RGBA", base_size, color)# 读入图片base_image = base_image.convert('RGB')fore_image = Image.open(fore_image).resize(base_image.size)# 图片加权合成scope_map = np.array(fore_image)[:,:,-1] / 255scope_map = scope_map[:,:,np.newaxis]scope_map = np.repeat(scope_map, repeats=3, axis=2)res_image = np.multiply(scope_map, np.array(fore_image)[:,:,:3]) + np.multiply((1-scope_map), np.array(base_image))#保存图片res_image = Image.fromarray(np.uint8(res_image))print(res_image)res_image.save(output_path)# 抠图@server.route('/matting', methods=['get', 'post'])
def matting():# 测试图片路径test_path = 'D:/image/'# 输出路径output_path = 'D:/image/test/'# 背景图片地址base_image = (295, 413)# 白色(255,255,255) 红色(182,38,38) 蓝色(67,142,219)color = (255,255,255)colorType = flask.request.values.get('color_type')if colorType == '1':color = (182,38,38)if colorType == '2':color = (67,142,219)# 待预测图片名称# test_file_name = "5"test_file_name = flask.request.values.get('test_file_name')# 待预测图片后缀# test_file_suffix = ".jpg"test_file_suffix = flask.request.values.get('test_file_suffix')print(color)# 输入文件名称output_file_name = test_file_name + test_file_suffixtest_img_path = [test_file_name + test_file_suffix]test_img_path = [test_path + img for img in test_img_path]human_seg = hub.Module(name="deeplabv3p_xception65_humanseg")results = human_seg.segmentation(images=[cv2.imread(test_img_path[0])], visualization=True)print(results[0]['save_path'])save_path = results[0]['save_path']output_path_img = output_path + output_file_nameblend_images(save_path, output_path_img, base_image, color)return output_path_imgif __name__ == '__main__':server.run(debug=True, port=18778, host='0.0.0.0')# 指定端口、host,0.0.0.0代表不管几个网卡,任何ip都可以访问

通过请求访问: http://127.0.0.1:18778/matting?color_type=1&test_file_name=1&test_file_suffix=.jpg

 

这篇关于centos7 PaddleHub人像分割模型:AI人像抠图及图像合成的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/282097

相关文章

详解如何使用Python从零开始构建文本统计模型

《详解如何使用Python从零开始构建文本统计模型》在自然语言处理领域,词汇表构建是文本预处理的关键环节,本文通过Python代码实践,演示如何从原始文本中提取多尺度特征,并通过动态调整机制构建更精确... 目录一、项目背景与核心思想二、核心代码解析1. 数据加载与预处理2. 多尺度字符统计3. 统计结果可

Python中OpenCV与Matplotlib的图像操作入门指南

《Python中OpenCV与Matplotlib的图像操作入门指南》:本文主要介绍Python中OpenCV与Matplotlib的图像操作指南,本文通过实例代码给大家介绍的非常详细,对大家的学... 目录一、环境准备二、图像的基本操作1. 图像读取、显示与保存 使用OpenCV操作2. 像素级操作3.

C/C++的OpenCV 进行图像梯度提取的几种实现

《C/C++的OpenCV进行图像梯度提取的几种实现》本文主要介绍了C/C++的OpenCV进行图像梯度提取的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的... 目录预www.chinasem.cn备知识1. 图像加载与预处理2. Sobel 算子计算 X 和 Y

c/c++的opencv图像金字塔缩放实现

《c/c++的opencv图像金字塔缩放实现》本文主要介绍了c/c++的opencv图像金字塔缩放实现,通过对原始图像进行连续的下采样或上采样操作,生成一系列不同分辨率的图像,具有一定的参考价值,感兴... 目录图像金字塔简介图像下采样 (cv::pyrDown)图像上采样 (cv::pyrUp)C++ O

SpringBoot整合Sa-Token实现RBAC权限模型的过程解析

《SpringBoot整合Sa-Token实现RBAC权限模型的过程解析》:本文主要介绍SpringBoot整合Sa-Token实现RBAC权限模型的过程解析,本文给大家介绍的非常详细,对大家的学... 目录前言一、基础概念1.1 RBAC模型核心概念1.2 Sa-Token核心功能1.3 环境准备二、表结

Spring AI 实现 STDIO和SSE MCP Server的过程详解

《SpringAI实现STDIO和SSEMCPServer的过程详解》STDIO方式是基于进程间通信,MCPClient和MCPServer运行在同一主机,主要用于本地集成、命令行工具等场景... 目录Spring AI 实现 STDIO和SSE MCP Server1.新建Spring Boot项目2.a

CentOS7增加Swap空间的两种方法

《CentOS7增加Swap空间的两种方法》当服务器物理内存不足时,增加Swap空间可以作为虚拟内存使用,帮助系统处理内存压力,本文给大家介绍了CentOS7增加Swap空间的两种方法:创建新的Swa... 目录在Centos 7上增加Swap空间的方法方法一:创建新的Swap文件(推荐)方法二:调整Sww

Python+wxPython构建图像编辑器

《Python+wxPython构建图像编辑器》图像编辑应用是学习GUI编程和图像处理的绝佳项目,本教程中,我们将使用wxPython,一个跨平台的PythonGUI工具包,构建一个简单的... 目录引言环境设置创建主窗口加载和显示图像实现绘制工具矩形绘制箭头绘制文字绘制临时绘制处理缩放和旋转缩放旋转保存编

python+OpenCV反投影图像的实现示例详解

《python+OpenCV反投影图像的实现示例详解》:本文主要介绍python+OpenCV反投影图像的实现示例详解,本文通过实例代码图文并茂的形式给大家介绍的非常详细,感兴趣的朋友一起看看吧... 目录一、前言二、什么是反投影图像三、反投影图像的概念四、反向投影的工作原理一、利用反向投影backproj

Python中edge-tts实现便捷语音合成

《Python中edge-tts实现便捷语音合成》edge-tts是一个功能强大的Python库,支持多种语言和声音选项,本文主要介绍了Python中edge-tts实现便捷语音合成,具有一定的参考价... 目录安装与环境设置文本转语音查找音色更改语音参数生成音频与字幕总结edge-tts 是一个功能强大的