1. 程式人生 > >通過dockerfile構建tensorflow+opencv的映象(ubuntu+python3+ffmpeg)

通過dockerfile構建tensorflow+opencv的映象(ubuntu+python3+ffmpeg)

步驟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

則測試成功