通过dockerfile构建tensorflow+opencv的镜像(ubuntu+python3+ffmpeg)

本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

则测试成功

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值