Docker Images: Centos7 + Python3.6 + Tensorflow + Opencv + Dlib

本文档主要是构建图像处理相关项目的 docker 镜像,包含:基础镜像 Centos7 ,以及所涉及的 Python3.6 + Tensorflow + Opencv + Dlib 等开发环境,并记录了构建过程中遇到的各种问题。

基础镜像及作者信息

这里使用的是 centos7 作为基础镜像,作者是 ELN ,还可以添加电子邮箱。

FROM centos:7
MAINTAINER ELN

解决基础镜像中文乱码

在 centos7 基础镜像中直接安装 python3.6 ,进入 python3.6 中 print 中文字符时报如下错误,检查 python3.6 的默认编码为 utf-8 ,后来发现是 docker 中的 centos7 基础镜像出现中文乱码。

[root@b47729122507 /]# python3.6
Python 3.6.5 (default, Apr 10 2018, 17:08:37)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print(u"������")  # print(u"中文")
  File "<stdin>", line 0

    ^
SyntaxError: 'ascii' codec can't decode byte 0xe4 in position 8: ordinal not in range(128)
>>> import sys
>>> print(sys.getdefaultencoding())
utf-8
>>> exit()
[root@b47729122507 /]# echo "������"
中文

查看 /etc/localtime 的结果是 lrwxrwxrwx. 1 root root 25 Oct 6 19:15 localtime -> ../usr/share/zoneinfo/UTC ,需要修改时区,安装中文支持,配置显示中文。

在 dockerfile 中 修改时区,安装中文支持,配置显示中文 :

# 修改时区,安装中文支持,配置显示中文
RUN rm -rf /etc/localtime  && \
    ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime  && \
    yum -y install kde-l10n-Chinese  && \
    yum -y reinstall glibc-common  && \
    localedef -c -f UTF-8 -i zh_CN zh_CN.utf8  && \
    yum clean all  &&  rm -rf /var/cache/yum

ENV LC_ALL zh_CN.utf8
# 在终端执行: export LC_ALL=zh_CN.utf8

安装 python3.6

在 dockerfile 中,安装 python3.6 的纯净环境,安装一些基础的 python 包:

# Install Python 3.6
RUN yum -y install https://centos7.iuscommunity.org/ius-release.rpm && \
    yum -y install python36u && \
    yum -y install python36u-pip && \
    yum -y install vim && \
    yum clean all  &&  rm -rf /var/cache/yum

RUN pip3.6 --no-cache-dir install \
        Pillow \
        h5py \
        ipykernel \
        jupyter \
        matplotlib \
        numpy \
        pandas \
        scipy==0.18.1 \
        sklearn \
        && \
    python3.6 -m ipykernel.kernelspec

安装 TensorFlow CPU 版

# Install TensorFlow CPU version from central repo
RUN pip3.6 --no-cache-dir install tensorflow==1.8.0

直接安装 TensorFlow 可能会报错 ImportError: libSM.so.6: cannot open shared object file: No such file or directory,通过安装 libSM 可以解决该错误,具体命令见 “安装 opencv-python” 。

安装 opencv-python

直接安装 opencv-python 后,在 python3.6 中执行 import cv2 会报错,原因是 centos7 基础镜像缺乏相关依赖,而且在安装相关依赖的时候发现,类似 yum install libXrender.so.1 -y 这种命令默认安装的是 libXrender.i686 版本( 32 位),无法解决错误,需要安装 64 位的版本才能解决问题。 通过 yum list | grep libXrender 检查可安装的软件清单,然后选择 64 位的版本进行安装即可。

在 dockerfile 中,安装 opencv-python (下面是在没有真正找到报错原因时偶然发现能解决问题的方法,实际上不需要 yum 那么多的软件,具体解决报错的方法参见上述笔记):

# Install opencv-python
RUN yum -y install libSM-1.2.2-2.el7.x86_64 --setopt=protected_multilib=false  && \
    yum install ksh -y  && \
    yum install libXext.so.6 -y  && \
    yum install libXtst.so.6 -y  && \
    yum install libXt.so.6 -y  && \
    yum install libGLU.so.1 --setopt=protected_multilib=false -y  && \
    yum install libelf.so.1 -y  && \
    yum install libXrender.so.1 -y  && \
    yum install libXp.so.6 -y  && \
    yum install libXrandr.so.2 -y  && \
    yum install *xorg* -y --skip-broken  && \
    yum install libXp -y  && \
    yum install ld-linux.so.2 -y  && \
    yum install openmotif -y  && \
    yum install libstdc++.so.5 -y  && \
    yum install xterm -y  && \
    yum clean all  &&  rm -rf /var/cache/yum

RUN pip3.6 --no-cache-dir install opencv-python==3.4.1.15

安装 dlib

直接安装 dlib 报错复现与解决:

  • 直接安装 dlib 报错 CMake must be installed to build the following extensions: dlib
  • yum -y install cmake 安装 cmake 后再安装 dlib 报错 subprocess.CalledProcessError: Command '['cmake', '/tmp/pip-build-g_ptsyo_/dlib/tools/python', '-DCMAKE_LIBRARY_OUTPUT_DIRECTORY=/tmp/pip-build-g_ptsyo_/dlib/build/lib.linux-x86_64-3.6', '-DPYTHON_EXECUTABLE=/usr/bin/python3.6', '-DCMAKE_BUILD_TYPE=Release']' returned non-zero exit status 1.
  • yum install -y python36u-devel.x86_64 依然报上面的错误
    yum -y groupinstall "Development tools" ,再安装 dlib 则安装成功

在 dockerfile 中,安装 opencv-python (经测试,下面有些是不需要 yum 的,具体参见上述报错复现):

# Install dlib
RUN yum -y groupinstall "Development tools"  && \
    yum -y install cmake && \
    yum install -y boost boost-devel boost-doc  && \
    yum install -y libXdmcp libXdmcp-devel  && \
    yum clean all  &&  rm -rf /var/cache/yum

RUN yum search python3 | grep devel  && \
    yum install -y python36u-devel.x86_64  && \
    yum clean all  # &&  rm -rf /var/cache/yum

RUN pip3.6 --no-cache-dir install dlib

安装其他 python 依赖包

# Install keras ...
RUN pip3.6 --no-cache-dir install Cython

RUN pip3.6 --no-cache-dir install \
        keras \
        flask \
        flask_cors \
        flask_socketio \
        scikit-image \
        mrcnn \
        imgaug \
        pycocotools

其他设置

RUN mkdir /cutimages

CMD ["/bin/bash"]

# COPY testcutimg-master/ /cutimages/

# WORKDIR /cutimages/Mask_RCNN-master/samples

# CMD ["python3.6", "img_cut_api.py"]

构建镜像并测试

将上述内容写入 dockerfile 中,构建镜像并测试:

[eln@localhost docker]$ vim dockerfile
[eln@localhost docker]$ sudo docker build -t="test1" .

[eln@localhost docker]$ sudo docker images
REPOSITORY          TAG                 IMAGE ID            CREATED              SIZE
test1               latest              3d738bfad2f7        About a minute ago   1.94 GB
docker.io/centos    7                   75835a67d134        5 weeks ago          200 MB

[eln@localhost docker]$ sudo docker run -it --rm -p 8080:8080 --privileged=true -v /home/eln/docker/testcutimg-master:/cutimages:rw --name testci test1
[root@d27ab26e38a3 /]# cd cutimages/Mask_RCNN-master/samples/
[root@d27ab26e38a3 samples]# python3.6 img_cut_api.py
Using TensorFlow backend.
2018-11-20 17:21:42.853504: I tensorflow/core/platform/cpu_feature_guard.cc:140] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
 * Serving Flask app "img_cut_api" (lazy loading)
 * Environment: production
   WARNING: Do not use the development server in a production environment.
   Use a production WSGI server instead.
 * Debug mode: off
 * Running on http://0.0.0.0:8080/ (Press CTRL+C to quit)
172.17.0.1 - - [20/Nov/2018 17:21:52] "GET / HTTP/1.1" 404 -      # 打开链接 http://0.0.0.0:8080/
172.17.0.1 - - [20/Nov/2018 17:21:58] "GET /cut_img HTTP/1.1" 200 -      # 打开链接 http://0.0.0.0:8080/cut_img

[root@d27ab26e38a3 samples]# pip3.6 -V
pip 9.0.1 from /usr/lib/python3.6/site-packages (python 3.6)
[root@d27ab26e38a3 samples]# python3.6
Python 3.6.5 (default, Apr 10 2018, 17:08:37)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-16)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import tensorflow
>>> import cv2
>>> import dlib
>>> print("中文")
中文
>>> exit()
[root@d27ab26e38a3 samples]# exit
exit
[eln@localhost docker]$

后面将介绍如何构建可使用 GPUs 的容器,包含: centos7 + cuda 9.0 + cudnn 7.0.5 + Python 3.6 + Tensorflow-GPU 1.5.0 + Opencv-Python + Dlib 等开发环境,并记录了构建过程中遇到的各种问题,见文档《 Nvidia-Docker 构建可使用 GPUs 的容器:Python3 + Tensorflow-GPU + Opencv + Dlib 》。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值