FastAPI + OpenCV/MediaPipe deployment failing - cv2 import errors
roralsorom
FREEOP
19 days 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:
- Nixpacks failed with: undefined variable 'libxcb'
- Switched to Dockerfile, got: ImportError: libxcb.so.1 cannot open shared object file
- Found mediapipe was pulling in full opencv-python, overriding headless
- Fixed by force-uninstalling opencv-python after mediapipe install
- 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-headlessENV 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:
- Is mediapipe known to conflict with opencv-python-headless on Railway?
- Is there a recommended way to deploy MediaPipe on Railway?
- Could the Root Directory setting be causing COPY to fail silently?
Any help appreciated!