【问题标题】:OpenCV in AWS Lambda container imageAWS Lambda 容器映像中的 OpenCV
【发布时间】:2021-08-01 18:49:02
【问题描述】:

我正在尝试构建一个 docker 映像,该映像将作为一个函数部署在 AWS Lambda 上。能够成功构建和测试映像,但当我尝试在函数中导入 OpenCV 时遇到问题。

当我从 app.py 中删除 import 语句时,我没有遇到这个问题

我面临的错误 -

{"errorMessage": "Unable to import module 'app': libGL.so.1: cannot open shared object file: No such file or directory", "errorType": "Runtime.ImportModuleError"}

我的 Dockerfile -

# Define custom function directory
ARG FUNCTION_DIR="/function"

FROM python:3.9 as build-image

# Include global arg in this stage of the build
ARG FUNCTION_DIR

# Install aws-lambda-cpp build dependencies
RUN apt-get update && \
  apt-get install -y \
  g++ \
  make \
  cmake \
  unzip \
  libcurl4-openssl-dev

RUN apt-get install -y --fix-missing \
    build-essential \
    cmake \
    gfortran \
    git \
    wget \
    curl \
    graphicsmagick \
    libgraphicsmagick1-dev \
    libatlas-base-dev \
    libavcodec-dev \
    libavformat-dev \
    libgtk2.0-dev \
    libjpeg-dev \
    liblapack-dev \
    libswscale-dev \
    pkg-config \
    python3-dev \
    python3-numpy \
    software-properties-common \
    zip \
    && apt-get clean && rm -rf /tmp/* /var/tmp/*

# Copy function code
RUN mkdir -p ${FUNCTION_DIR}
COPY app/* ${FUNCTION_DIR}

WORKDIR ${FUNCTION_DIR}

RUN pip install -r requirements.txt --target ${FUNCTION_DIR}

# Install the function's dependencies
RUN pip install \
    --target ${FUNCTION_DIR} \
        awslambdaric


FROM python:3.9

# Include global arg in this stage of the build
ARG FUNCTION_DIR
# Set working directory to function root directory
WORKDIR ${FUNCTION_DIR}

# Copy in the built dependencies
COPY --from=build-image ${FUNCTION_DIR} ${FUNCTION_DIR}

ENTRYPOINT [ "/usr/local/bin/python", "-m", "awslambdaric" ]
CMD [ "app.handler" ]

我的要求.txt -

mediapipe<=0.8.3.1
numpy<=1.19.4
opencv-python<=4.4.0.46
boto3<=1.17.64

我的 app.py

import cv2

def handler(event, context):
    return cv2.__version__

【问题讨论】:

  • 不太相关。在那篇文章中,他们似乎将其安装在 lambda 层中。
  • 它们包括安装和定位 libGL.so.1 的步骤,这是您所缺少的。
  • 哦,是的,您是对的,使用此问题中提供的解决方案设法解决了它 - Stackoverflow question

标签: python-3.x amazon-web-services docker opencv aws-lambda


【解决方案1】:

设法通过将这些添加到 Dockerfile 来解决它 -

RUN apt-get install ffmpeg libsm6 libxext6  -y

新的 Dockerfile 看起来像这样 -

# Define custom function directory
ARG FUNCTION_DIR="/function"

FROM python:3.9 as build-image

# Include global arg in this stage of the build
ARG FUNCTION_DIR

# Install aws-lambda-cpp build dependencies
RUN apt-get update && \
  apt-get install -y \
  g++ \
  make \
  cmake \
  unzip \
  libcurl4-openssl-dev

RUN apt-get install -y --fix-missing \
    build-essential \
    cmake \
    gfortran \
    git \
    wget \
    curl \
    ffmpeg \ 
    libsm6 \
    libxext6 \
    graphicsmagick \
    libgraphicsmagick1-dev \
    libatlas-base-dev \
    libavcodec-dev \
    libavformat-dev \
    libgtk2.0-dev \
    libjpeg-dev \
    liblapack-dev \
    libswscale-dev \
    pkg-config \
    python3-dev \
    python3-numpy \
    software-properties-common \
    zip \
    && apt-get clean && rm -rf /tmp/* /var/tmp/*

# Copy function code
RUN mkdir -p ${FUNCTION_DIR}
COPY app/* ${FUNCTION_DIR}

WORKDIR ${FUNCTION_DIR}

RUN pip install -r requirements.txt --target ${FUNCTION_DIR}

# Install the function's dependencies
RUN pip install \
    --target ${FUNCTION_DIR} \
        awslambdaric


FROM python:3.9

# Include global arg in this stage of the build
ARG FUNCTION_DIR
# Set working directory to function root directory
WORKDIR ${FUNCTION_DIR}

# Copy in the built dependencies
COPY --from=build-image ${FUNCTION_DIR} ${FUNCTION_DIR}

ENTRYPOINT [ "/usr/local/bin/python", "-m", "awslambdaric" ]
CMD [ "app.handler" ]

【讨论】:

    【解决方案2】:

    我在尝试使用媒体管道容器时遇到了同样的问题,得到了同样的错误。 pip 安装 python-opencv-headless 为我解决了这个问题,我不需要安装任何额外的依赖项。

    FROM public.ecr.aws/lambda/python:3.8
    
    # Copy function code
    COPY app.py ${LAMBDA_TASK_ROOT}
    
    # Install the function's dependencies using file requirements.txt
    # from your project folder.
    
    COPY requirements.txt  .
    RUN  pip3 install mediapipe opencv-python-headless --target "${LAMBDA_TASK_ROOT}"
    
    # Set the CMD to your handler (could also be done as a parameter override outside of the Dockerfile)
    CMD [ "app.handler" ]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-10-06
      • 1970-01-01
      • 2021-09-06
      • 1970-01-01
      • 1970-01-01
      • 2016-06-26
      • 2021-04-19
      • 1970-01-01
      相关资源
      最近更新 更多