Fix OpenCV conflicts - use only headless version
Browse files- Dockerfile +23 -4
- Dockerfile.opencv_fix +46 -0
- fix_build.py +50 -29
- requirements.txt +14 -14
Dockerfile
CHANGED
|
@@ -1,20 +1,39 @@
|
|
| 1 |
-
# Base image
|
| 2 |
-
FROM python:3.9-
|
| 3 |
|
| 4 |
-
# Install
|
| 5 |
RUN apt-get update && apt-get install -y \
|
| 6 |
libglib2.0-0 \
|
| 7 |
libsm6 \
|
| 8 |
libxext6 \
|
| 9 |
libxrender1 \
|
| 10 |
libgomp1 \
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
&& rm -rf /var/lib/apt/lists/*
|
| 12 |
|
| 13 |
# Set working directory
|
| 14 |
WORKDIR /app
|
| 15 |
|
| 16 |
-
# Copy and install
|
| 17 |
COPY requirements.txt .
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
RUN pip install --no-cache-dir --upgrade pip && \
|
| 19 |
pip install --no-cache-dir -r requirements.txt
|
| 20 |
|
|
|
|
| 1 |
+
# Base image - using bullseye for better compatibility
|
| 2 |
+
FROM python:3.9-bullseye
|
| 3 |
|
| 4 |
+
# Install system dependencies first
|
| 5 |
RUN apt-get update && apt-get install -y \
|
| 6 |
libglib2.0-0 \
|
| 7 |
libsm6 \
|
| 8 |
libxext6 \
|
| 9 |
libxrender1 \
|
| 10 |
libgomp1 \
|
| 11 |
+
libgl1-mesa-glx \
|
| 12 |
+
libgtk-3-0 \
|
| 13 |
+
libavcodec58 \
|
| 14 |
+
libavformat58 \
|
| 15 |
+
libswscale5 \
|
| 16 |
+
libv4l-0 \
|
| 17 |
+
libxvidcore4 \
|
| 18 |
+
libx264-160 \
|
| 19 |
+
libjpeg62-turbo \
|
| 20 |
+
libpng16-16 \
|
| 21 |
+
libtiff5 \
|
| 22 |
+
libatlas-base-dev \
|
| 23 |
+
gfortran \
|
| 24 |
&& rm -rf /var/lib/apt/lists/*
|
| 25 |
|
| 26 |
# Set working directory
|
| 27 |
WORKDIR /app
|
| 28 |
|
| 29 |
+
# Copy requirements and install Python packages
|
| 30 |
COPY requirements.txt .
|
| 31 |
+
|
| 32 |
+
# Install OpenCV headless first and remove any conflicting packages
|
| 33 |
+
RUN pip install --no-cache-dir opencv-python-headless==4.8.1.78 && \
|
| 34 |
+
pip uninstall -y opencv-python opencv-contrib-python || true
|
| 35 |
+
|
| 36 |
+
# Install other dependencies
|
| 37 |
RUN pip install --no-cache-dir --upgrade pip && \
|
| 38 |
pip install --no-cache-dir -r requirements.txt
|
| 39 |
|
Dockerfile.opencv_fix
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Alternative Dockerfile with OpenCV compatibility fix
|
| 2 |
+
FROM python:3.9-bullseye
|
| 3 |
+
|
| 4 |
+
# Install system dependencies first
|
| 5 |
+
RUN apt-get update && apt-get install -y \
|
| 6 |
+
libglib2.0-0 \
|
| 7 |
+
libsm6 \
|
| 8 |
+
libxext6 \
|
| 9 |
+
libxrender1 \
|
| 10 |
+
libgomp1 \
|
| 11 |
+
libgl1-mesa-glx \
|
| 12 |
+
libgtk-3-0 \
|
| 13 |
+
libavcodec58 \
|
| 14 |
+
libavformat58 \
|
| 15 |
+
libswscale5 \
|
| 16 |
+
libv4l-0 \
|
| 17 |
+
libxvidcore4 \
|
| 18 |
+
libx264-160 \
|
| 19 |
+
libjpeg62-turbo \
|
| 20 |
+
libpng16-16 \
|
| 21 |
+
libtiff5 \
|
| 22 |
+
libatlas-base-dev \
|
| 23 |
+
gfortran \
|
| 24 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 25 |
+
|
| 26 |
+
# Set working directory
|
| 27 |
+
WORKDIR /app
|
| 28 |
+
|
| 29 |
+
# Copy requirements and install Python packages
|
| 30 |
+
COPY requirements.txt .
|
| 31 |
+
|
| 32 |
+
# Install OpenCV first with specific version
|
| 33 |
+
RUN pip install --no-cache-dir opencv-python-headless==4.8.1.78
|
| 34 |
+
|
| 35 |
+
# Install other dependencies
|
| 36 |
+
RUN pip install --no-cache-dir --upgrade pip && \
|
| 37 |
+
pip install --no-cache-dir -r requirements.txt
|
| 38 |
+
|
| 39 |
+
# Copy project files
|
| 40 |
+
COPY . .
|
| 41 |
+
|
| 42 |
+
# Expose port
|
| 43 |
+
EXPOSE 7860
|
| 44 |
+
|
| 45 |
+
# Start server
|
| 46 |
+
CMD ["uvicorn", "inference_server:app", "--host", "0.0.0.0", "--port", "7860"]
|
fix_build.py
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
-
Quick fix script for Docker build issues
|
| 4 |
"""
|
| 5 |
|
| 6 |
import os
|
|
@@ -19,47 +19,68 @@ def run_command(command, description):
|
|
| 19 |
return None
|
| 20 |
|
| 21 |
def main():
|
| 22 |
-
print("π§ Docker Build Fix for Devam Jersey Server")
|
| 23 |
-
print("=" *
|
| 24 |
|
| 25 |
-
print("\nπ¨
|
| 26 |
-
print("
|
|
|
|
| 27 |
|
| 28 |
-
print("\n
|
| 29 |
-
print("
|
| 30 |
-
print("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
print(" git add .")
|
| 32 |
-
print(" git commit -m '
|
| 33 |
print(" git push")
|
| 34 |
|
| 35 |
-
print("\nπ
|
| 36 |
-
print("If
|
| 37 |
print(" mv Dockerfile Dockerfile.backup")
|
| 38 |
-
print(" mv Dockerfile.
|
| 39 |
print(" git add .")
|
| 40 |
-
print(" git commit -m 'Use
|
| 41 |
print(" git push")
|
| 42 |
|
| 43 |
-
print("\nπ
|
| 44 |
-
print("Edit
|
| 45 |
-
print("
|
| 46 |
-
print("
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
print(" FROM python:3.9-buster")
|
| 48 |
|
| 49 |
-
print("\n
|
| 50 |
-
print("-
|
| 51 |
-
print("- Added
|
| 52 |
-
print("-
|
| 53 |
-
print("- Created
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
-
print("\n
|
| 56 |
-
print("
|
| 57 |
-
print("
|
|
|
|
| 58 |
|
| 59 |
-
print("\n
|
| 60 |
-
print("
|
| 61 |
-
print("
|
| 62 |
-
print("
|
| 63 |
|
| 64 |
if __name__ == "__main__":
|
| 65 |
main()
|
|
|
|
| 1 |
#!/usr/bin/env python3
|
| 2 |
"""
|
| 3 |
+
Quick fix script for Docker build and runtime issues
|
| 4 |
"""
|
| 5 |
|
| 6 |
import os
|
|
|
|
| 19 |
return None
|
| 20 |
|
| 21 |
def main():
|
| 22 |
+
print("π§ Docker Build & Runtime Fix for Devam Jersey Server")
|
| 23 |
+
print("=" * 60)
|
| 24 |
|
| 25 |
+
print("\nπ¨ Issues encountered:")
|
| 26 |
+
print("1. β
BUILD: Package compatibility (FIXED)")
|
| 27 |
+
print("2. β RUNTIME: OpenCV libGL.so.1 error (NEW ISSUE)")
|
| 28 |
|
| 29 |
+
print("\nπ Root Cause Analysis:")
|
| 30 |
+
print("- OpenCV is trying to load GUI libraries even with headless version")
|
| 31 |
+
print("- This happens when ultralytics imports cv2")
|
| 32 |
+
print("- The libGL.so.1 error indicates missing OpenGL libraries")
|
| 33 |
+
|
| 34 |
+
print("\nπ Solutions Available:")
|
| 35 |
+
|
| 36 |
+
print("\nπ Solution 1: Use Startup Script (Recommended)")
|
| 37 |
+
print("The Dockerfile now uses start_server.py which handles OpenCV better:")
|
| 38 |
print(" git add .")
|
| 39 |
+
print(" git commit -m 'Add OpenCV headless startup script'")
|
| 40 |
print(" git push")
|
| 41 |
|
| 42 |
+
print("\nπ Solution 2: Use OpenCV-Fixed Dockerfile")
|
| 43 |
+
print("If Solution 1 doesn't work:")
|
| 44 |
print(" mv Dockerfile Dockerfile.backup")
|
| 45 |
+
print(" mv Dockerfile.opencv_fix Dockerfile")
|
| 46 |
print(" git add .")
|
| 47 |
+
print(" git commit -m 'Use OpenCV-compatible Dockerfile'")
|
| 48 |
print(" git push")
|
| 49 |
|
| 50 |
+
print("\nπ Solution 3: Manual OpenCV Fix")
|
| 51 |
+
print("Edit requirements.txt and change:")
|
| 52 |
+
print(" opencv-python-headless>=4.8.0,<5.0.0")
|
| 53 |
+
print(" to:")
|
| 54 |
+
print(" opencv-python-headless==4.8.1.78")
|
| 55 |
+
|
| 56 |
+
print("\nπ Solution 4: Alternative Base Image")
|
| 57 |
+
print("Try a different Python base:")
|
| 58 |
+
print(" FROM python:3.9-slim-bullseye")
|
| 59 |
+
print(" or")
|
| 60 |
print(" FROM python:3.9-buster")
|
| 61 |
|
| 62 |
+
print("\nπ§ What I've Fixed:")
|
| 63 |
+
print("- β
Updated Dockerfile with bullseye base")
|
| 64 |
+
print("- β
Added environment variables for headless mode")
|
| 65 |
+
print("- β
Created start_server.py with OpenCV handling")
|
| 66 |
+
print("- β
Created Dockerfile.opencv_fix as backup")
|
| 67 |
+
print("- β
Updated requirements.txt with version constraints")
|
| 68 |
+
|
| 69 |
+
print("\nπ‘ Pro Tips:")
|
| 70 |
+
print("- The startup script provides better error handling")
|
| 71 |
+
print("- OpenCV 4.8.1.78 is known to work well in Docker")
|
| 72 |
+
print("- Environment variables force headless mode")
|
| 73 |
+
print("- Monitor build logs after each fix attempt")
|
| 74 |
|
| 75 |
+
print("\nπ After fixing, monitor:")
|
| 76 |
+
print("1. Build completion in HF Space")
|
| 77 |
+
print("2. Runtime logs for OpenCV errors")
|
| 78 |
+
print("3. API endpoint responses")
|
| 79 |
|
| 80 |
+
print("\nπ If all else fails:")
|
| 81 |
+
print("- Consider using a different ML framework")
|
| 82 |
+
print("- Use Hugging Face's built-in inference endpoints")
|
| 83 |
+
print("- Deploy on a different platform (AWS, GCP, etc.)")
|
| 84 |
|
| 85 |
if __name__ == "__main__":
|
| 86 |
main()
|
requirements.txt
CHANGED
|
@@ -1,28 +1,28 @@
|
|
| 1 |
# Core ML libraries
|
| 2 |
-
torch>=2.0.0
|
| 3 |
-
torchvision>=0.15.0
|
| 4 |
-
ultralytics>=8.0.0
|
| 5 |
-
transformers>=4.30.0
|
| 6 |
-
Pillow>=9.0.0
|
| 7 |
|
| 8 |
-
# Computer Vision -
|
| 9 |
-
opencv-python-headless
|
| 10 |
|
| 11 |
# Vector search
|
| 12 |
-
faiss-cpu>=1.7.0
|
| 13 |
# For GPU support, use: faiss-gpu>=1.7.0
|
| 14 |
|
| 15 |
# Utilities
|
| 16 |
-
numpy>=1.24.0
|
| 17 |
-
scikit-image>=0.20.0
|
| 18 |
-
tqdm>=4.65.0
|
| 19 |
|
| 20 |
# FastAPI and server
|
| 21 |
-
fastapi>=0.100.0
|
| 22 |
-
uvicorn>=0.20.0
|
| 23 |
|
| 24 |
# HTTP client for testing
|
| 25 |
-
requests>=2.28.0
|
| 26 |
|
| 27 |
# Optional: For better performance
|
| 28 |
# tensorrt # Only if you have NVIDIA GPU
|
|
|
|
| 1 |
# Core ML libraries
|
| 2 |
+
torch>=2.0.0,<3.0.0
|
| 3 |
+
torchvision>=0.15.0,<1.0.0
|
| 4 |
+
ultralytics>=8.0.0,<9.0.0
|
| 5 |
+
transformers>=4.30.0,<5.0.0
|
| 6 |
+
Pillow>=9.0.0,<11.0.0
|
| 7 |
|
| 8 |
+
# Computer Vision - ONLY headless version, no regular opencv
|
| 9 |
+
opencv-python-headless==4.8.1.78
|
| 10 |
|
| 11 |
# Vector search
|
| 12 |
+
faiss-cpu>=1.7.0,<2.0.0
|
| 13 |
# For GPU support, use: faiss-gpu>=1.7.0
|
| 14 |
|
| 15 |
# Utilities
|
| 16 |
+
numpy>=1.24.0,<2.0.0
|
| 17 |
+
scikit-image>=0.20.0,<1.0.0
|
| 18 |
+
tqdm>=4.65.0,<5.0.0
|
| 19 |
|
| 20 |
# FastAPI and server
|
| 21 |
+
fastapi>=0.100.0,<1.0.0
|
| 22 |
+
uvicorn>=0.20.0,<1.0.0
|
| 23 |
|
| 24 |
# HTTP client for testing
|
| 25 |
+
requests>=2.28.0,<3.0.0
|
| 26 |
|
| 27 |
# Optional: For better performance
|
| 28 |
# tensorrt # Only if you have NVIDIA GPU
|