安装依赖:
1. 将依赖保存为requirements.txt 文件
appdirs==1.4.4 astor==0.8.1 Babel==2.8.0 certifi==2020.6.20 cfgv==3.1.0 chardet==3.0.4 click==7.1.2 cma==3.0.3 colorlog==4.1.0 cycler==0.10.0 decorator==4.4.2 distlib==0.3.1 docopt==0.6.2 filelock==3.0.12 flake8==3.8.3 Flask==1.1.2 Flask-Babel==1.0.0 funcsigs==1.0.2 gast==0.3.3 graphviz==0.14 gunicorn==20.0.4 hdfs==2.5.8 identify==1.4.22 idna==2.10 importlib-metadata==1.7.0 itsdangerous==1.1.0 Jinja2==2.11.2 joblib==0.16.0 kiwisolver==1.2.0 MarkupSafe==1.1.1 matplotlib==3.2.2 mccabe==0.6.1 nltk==3.5 nodeenv==1.4.0 numpy==1.19.0 objgraph==3.4.1 opencv-python==4.3.0.36 paddlehub==1.7.1 paddlepaddle==1.8.2 pandas==1.0.5 pathlib==1.0.1 Pillow==7.2.0 pre-commit==2.6.0 prettytable==0.7.2 protobuf==3.12.2 pycodestyle==2.6.0 pyflakes==2.2.0 pyparsing==2.4.7 python-dateutil==2.8.1 pytz==2020.1 PyYAML==5.3.1 rarfile==3.1 regex==2020.6.8 requests==2.24.0 scipy==1.3.1 sentencepiece==0.1.91 six==1.15.0 toml==0.10.1 tqdm==4.47.0 urllib3==1.25.9 virtualenv==20.0.26 visualdl==2.0.0b7 Werkzeug==1.0.1 yapf==0.26.0 zipp==3.1.0requirements.txt
2. 当前目录运行
pip install -r requirements.txt
程序:
import os import paddlehub as hub import matplotlib import matplotlib.pyplot as plt import matplotlib.image as mpimg import matplotlib.figure as figure import numpy as np matplotlib.use('TkAgg') base_dir = os.path.abspath('.') input_img_path = base_dir+"/source/" output_img_path = base_dir+"/target/" humanseg = hub.Module(name="deeplabv3p_xception65_humanseg") input_imgs_dirs = [input_img_path+p for p in os.listdir(input_img_path)] output_img_dirs = [output_img_path+p.split(".")[0]+".png" for p in os.listdir(input_img_path)] print(input_imgs_dirs) print(output_img_dirs) all =len(input_imgs_dirs) i=0 for input in input_imgs_dirs: print("%d%%"%(i/all*100)) results = humanseg.segmentation(data={"image":[input]}) data = results[0]['data'] plt.figure(figsize=[10,10]) plt.imsave(output_img_dirs[i],data) # plt.imshow(data) # plt.axis("off") # plt.show() i+=1
NOTE:
1. MAC 和 Linux 上可以正常运行,windows 上需要安装docker 后运行