Docker Images: Centos7 + Python3.6 + Tensorflow + Opencv + Dlib
本文件主要是構建影象處理相關專案的 docker 映象,包含:基礎映象 Centos7 ,以及所涉及的 Python3.6 + Tensorflow + Opencv + Dlib 等開發環境。
Docker Images: Centos7 + Python3.6 + Tensorflow + Opencv + Dlib
基礎映象及作者資訊
這裡使用的是 centos7 作為基礎映象,作者是 Kyla ,還可以新增電子郵箱。
FROM centos:7
MAINTAINER Kyla
解決基礎映象中文亂碼
在 centos7 基礎映象中直接安裝 python3.6 ,進入 python3.6 中 print 中文字元時報如下錯誤,檢查 python3.6 的預設編碼為 utf-8 ,後來發現是 docker 中的 centos7 基礎映象出現中文亂碼。
[[email protected] /]# 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()
[[email protected] /]# 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 中,構建映象並測試:
[[email protected] docker]$ vim dockerfile
[[email protected] docker]$ sudo docker build -t="test1" .
[[email protected] 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
[[email protected] docker]$ sudo docker run -it --rm -p 8080:8080 --privileged=true -v /home/eln/docker/testcutimg-master:/cutimages:rw --name testci test1
[[email protected] /]# cd cutimages/Mask_RCNN-master/samples/
[[email protected] 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
[[email protected] samples]# pip3.6 -V
pip 9.0.1 from /usr/lib/python3.6/site-packages (python 3.6)
[[email protected] 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()
[[email protected] samples]# exit
exit
[[email protected] docker]$