FastAPI + OpenCV/MediaPipe deployment failing - cv2 import errors
roralsorom
FREEOP

2 months ago

Having trouble deploying a FastAPI app that uses MediaPipe and OpenCV.

Stack:

  • FastAPI + Uvicorn
  • MediaPipe
  • opencv-python-headless
  • Python 3.10
  • Deploying via Dockerfile, Root Directory: /backend

Issues I've hit in order:

  1. Nixpacks failed with: undefined variable 'libxcb'
  2. Switched to Dockerfile, got: ImportError: libxcb.so.1 cannot open shared object file
  3. Found mediapipe was pulling in full opencv-python, overriding headless
  4. Fixed by force-uninstalling opencv-python after mediapipe install
  5. Now getting: ModuleNotFoundError: No module named 'cv2' at runtime

Current Dockerfile:

FROM python:3.10-slim

WORKDIR /app

RUN apt-get update && apt-get install -y \

libgl1 \

libglib2.0-0 \

libgthread-2.0-0 \

&& rm -rf /var/lib/apt/lists/*

COPY . .

RUN pip install --no-cache-dir mediapipe && \

pip uninstall -y opencv-python opencv-contrib-python || true && \

pip install --no-cache-dir opencv-python-headless && \

pip install --no-cache-dir -r requirements.txt && \

pip uninstall -y opencv-python opencv-contrib-python || true && \

pip install --no-cache-dir opencv-python-headless

ENV PYTHONUNBUFFERED=1

ENV QT_QPA_PLATFORM=offscreen

EXPOSE 8000

CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "8000"]

requirements.txt:

fastapi

uvicorn

sqlalchemy

pydantic

passlib[bcrypt]

python-jose[cryptography]

python-multipart

mediapipe

opencv-python-headless

numpy

pillow

rich

httpx

Railway settings:

  • Builder: Dockerfile
  • Root Directory: /backend
  • Build Command: empty
  • Start Command: empty

Questions:

  1. Is mediapipe known to conflict with opencv-python-headless on Railway?
  2. Is there a recommended way to deploy MediaPipe on Railway?
  3. Could the Root Directory setting be causing COPY to fail silently?

Any help appreciated!

Solved$10 Bounty

1 Replies

Railway
BOT

2 months ago

This thread has been opened as a bounty so the community can help solve it.

Status changed to Open Railway 2 months ago


roralsorom
FREEOP

2 months ago

already fixed


Status changed to Solved 0x5b62656e5d 28 days ago


Welcome!

Sign in to your Railway account to join the conversation.

Loading...