[Python] camera can't open while running a flask app from docker on a windows system...

Discussão em 'Python' iniciado por Stack, Março 18, 2025.

  1. Stack

    Stack Membro Participativo

    i get this error when trying to open a camera from a flask app running in docker:

    172.17.0.1 - - [17/Mar/2025 11:24:10] "GET / HTTP/1.1" 200 -
    [2025-03-17 11:24:19,498] ERROR in app: Exception on /detect_faces [POST]
    Traceback (most recent call last):
    File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1511, in wsgi_app
    response = self.full_dispatch_request()
    File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 919, in full_dispatch_request
    rv = self.handle_user_exception(e)
    File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 917, in full_dispatch_request
    rv = self.dispatch_request()
    File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 902, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return]
    File "/face_app/face_recognition_.py", line 114, in detect_faces_api
    result = detect_faces(image_path, video_capture)
    File "/face_app/face_recognition_.py", line 48, in detect_faces
    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
    cv2.error: OpenCV(4.11.0) /io/opencv/modules/imgproc/src/resize.cpp:4208: error: (-215:Assertion failed) !ssize.empty()


    my code:

    def detect_faces_api():

    timestamp = int(time.time())
    video_capture = cv2.VideoCapture(0,cv2.CAP_DSHOW)

    image = request.files['image']
    image_path = '/face_app/'+str(timestamp)+ '.jpg'
    image.save(image_path)

    result = detect_faces(image_path, video_capture)

    video_capture.release()
    cv2.destroyAllWindows()


    i need to run this app in a Linux server once it's done can someone help me

    the app works fine when running on my computer but i get this error in docker i did have video_capture = cv2.VideoCapture(0) first but i get the error

    72.17.0.1 - - [14/Mar/2025 15:45:53] "GET / HTTP/1.1" 200 -
    [ WARN:0@13.565] global cap_v4l.cpp:913 open VIDEOIO(V4L2:/dev/video0): can''t open camera by index
    [ERROR:0@13.566] global obsensor_uvc_stream_channel.cpp:158 getStreamChannelGroup Camera index out of range
    [2025-03-14 15:46:02,507] ERROR in app: Exception on /detect_faces [POST]
    Traceback (most recent call last):
    File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 1511, in wsgi_app
    response = self.full_dispatch_request()
    File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 919, in full_dispatch_request
    rv = self.handle_user_exception(e)
    File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 917, in full_dispatch_request
    rv = self.dispatch_request()
    File "/usr/local/lib/python3.9/site-packages/flask/app.py", line 902, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args) # type: ignore[no-any-return]
    File "/face_app/face_recognition_.py", line 113, in detect_faces_api
    result = detect_faces(image_path, video_capture)
    File "/face_app/face_recognition_.py", line 48, in detect_faces
    small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
    cv2.error: OpenCV(4.11.0) /io/opencv/modules/imgproc/src/resize.cpp:4208: error: (-215:Assertion failed) !ssize.empty() in function 'resize'


    i also tried cv2.VideoCapture(1), cv2.VideoCapture(2), cv2.VideoCapture(-1), cv2.VideoCapture(1,cv2.CAP_V4L2), and every thing

    my dockerfile

    FROM python:3.9-slim

    RUN apt-get update && apt-get install -y \
    build-essential \
    cmake \
    gfortran \
    wget \
    libboost-all-dev \
    libatlas-base-dev \
    libopenblas-dev \
    liblapack-dev \
    libx11-dev \
    tesseract-ocr \
    libgl1-mesa-glx \
    libglib2.0-0 \
    libsm6 \
    libxrender1 \
    libxext6 \
    && rm -rf /var/lib/apt/lists/*


    COPY requirements.txt /face_app/
    RUN pip install --no-cache-dir -r /face_app/requirements.txt

    WORKDIR /face_app

    COPY . /face_app

    EXPOSE 5000

    ENV NAME=World

    CMD ["python", "face_recognition_.py"]


    docker build -t docker-face-app .

    how i run the app:

    docker run -p 5000:5000 docker-face-app

    Continue reading...

Compartilhe esta Página