本dockerfile是在《通过dockerfile构建tensorflow的镜像(ubuntu+python3)》基础上增加是opencv的相关安装。
步骤1:编写Dockerfile
步骤2:安装镜像
步骤3:验证安装
步骤1:编写Dockerfile
1、在目录/dockerfiles/tensorflow-opencv下创建一个Dockerfile
mkdir /dockerfiles
mkdir /dockerfiles/tensorflow-opencv
vi /dockerfiles/tensorflow-opencv/Dockerfile
2、Dockerfile的内容如下:
FROM ubuntu:16.04
RUN apt-get update
RUN apt-get upgrade -y
# Install python3
RUN apt-get install -y python3
# Install pip
RUN apt-get install -y wget vim
RUN wget -O /tmp/get-pip.py https://bootstrap.pypa.io/get-pip.py
RUN python3 /tmp/get-pip.py
RUN pip install --upgrade pip
# Install tensorflow
RUN pip install -U tensorflow
# Install ffmpeg
RUN apt-get install -y ffmpeg
# Install moviepy
RUN pip install -U moviepy
# Install cmake
RUN apt-get update
RUN apt-get install -y build-essential
RUN apt-get install -y cmake
# Install OpenCV
RUN apt-get install -y unzip
RUN wget -P /usr/local/src/ https://github.com/opencv/opencv/archive/3.4.1.zip
RUN cd /usr/local/src/ && unzip 3.4.1.zip && rm 3.4.1.zip
RUN cd /usr/local/src/opencv-3.4.1/ && mkdir build
RUN cd /usr/local/src/opencv-3.4.1/build && cmake -D CMAKE_INSTALL_TYPE=Release -D CMAKE_INSTALL_PREFIX=/usr/local/ .. && make -j4 && make install
# Install opencv-python
RUN pip install opencv-python
RUN apt-get install -y libsm6
RUN apt-get install -y libxrender1
RUN apt-get install -y libxext-dev
注意:libsm6 libxrender1 libxext-dev必须安装,否则 import cv2时会报错
步骤2:安装镜像
docker build -t lld2002/python3-tensorflow-opencv /dockerfiles/tensorflow-opencv
步骤3:验证安装
1、通过镜像生成容器,并运行
mkdir /notebooks
docker run -v /notebooks:/root/notebooks -it lld2002/python3-tensorflow-opencv:latest /bin/bash
2、 放置一个mp4文件到/notebooks目录下,并改名为tp_merge.mp4
3、在容器的终端中,创建一个/test.py的文件
vi /test.py
test.py的内容如下:
import cv2
import os
try:
#url = 'rtsp://192.168.0.107:554/stream1'
url='/root/notebooks/tp_merge.mp4'
if (os.path.exists(url) == False):
print("file not exist")
cap = cv2.VideoCapture()
print("created")
print(cap.open(url))
print(cap.isOpened())
while(cap.isOpened()):
print("open")
# Capture frame-by-frame
ret, frame = cap.read()
#print(ret)
#print(frame)
if(ret):
# Display the resulting frame
# cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break;
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
except:
print("error")
3、运行/test.py
python3 /test.py
控制台输出:
created
True
True
open
则测试成功