Spaces:
Sleeping
Sleeping
Update Dockerfile
Browse files- Dockerfile +55 -14
Dockerfile
CHANGED
|
@@ -2,25 +2,24 @@
|
|
| 2 |
FROM python:3.10-slim AS builder
|
| 3 |
ENV DEBIAN_FRONTEND=noninteractive
|
| 4 |
|
| 5 |
-
# Install build
|
| 6 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 7 |
-
build-essential cmake git curl \
|
| 8 |
&& rm -rf /var/lib/apt/lists/*
|
| 9 |
|
| 10 |
WORKDIR /app
|
| 11 |
-
|
| 12 |
-
# Install Python dependencies in a venv
|
| 13 |
COPY requirements.txt .
|
|
|
|
| 14 |
RUN python -m venv /opt/venv && \
|
| 15 |
/opt/venv/bin/pip install --upgrade pip && \
|
| 16 |
/opt/venv/bin/pip install --no-cache-dir -r requirements.txt
|
| 17 |
|
| 18 |
-
# β
Pre-download YOLOv8n weights
|
| 19 |
RUN mkdir -p /root/.cache/torch/hub/checkpoints && \
|
| 20 |
curl -L -o /root/.cache/torch/hub/checkpoints/yolov8n.pt \
|
| 21 |
https://github.com/ultralytics/assets/releases/download/v8.3.0/yolov8n.pt
|
| 22 |
|
| 23 |
-
# β
Pre-
|
| 24 |
RUN mkdir -p /app/.EasyOCR && \
|
| 25 |
/opt/venv/bin/python -c "import easyocr; easyocr.Reader(['en'], model_storage_directory='/app/.EasyOCR')"
|
| 26 |
|
|
@@ -29,29 +28,71 @@ FROM python:3.10-slim
|
|
| 29 |
ENV DEBIAN_FRONTEND=noninteractive
|
| 30 |
ENV PATH="/opt/venv/bin:$PATH"
|
| 31 |
|
| 32 |
-
# β
|
| 33 |
ENV EASYOCR_MODULE_PATH=/app/.EasyOCR
|
| 34 |
ENV EASYOCR_CACHE_DIR=/app/.EasyOCR
|
| 35 |
ENV YOLO_CONFIG_DIR=/app/.yolo
|
| 36 |
|
| 37 |
-
# Runtime deps (OpenCV etc.)
|
| 38 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 39 |
libgl1 libglib2.0-0 \
|
| 40 |
&& rm -rf /var/lib/apt/lists/*
|
| 41 |
|
| 42 |
-
# Copy Python env + caches from builder
|
| 43 |
COPY --from=builder /opt/venv /opt/venv
|
| 44 |
COPY --from=builder /root/.cache /root/.cache
|
| 45 |
COPY --from=builder /app/.EasyOCR /app/.EasyOCR
|
| 46 |
|
| 47 |
WORKDIR /app
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
|
| 49 |
-
|
| 50 |
-
RUN mkdir -p /app/.yolo && \
|
| 51 |
-
chmod -R 777 /app && \
|
| 52 |
-
chmod -R 777 /root/.cache
|
| 53 |
|
| 54 |
-
|
|
|
|
|
|
|
| 55 |
|
| 56 |
EXPOSE 7860
|
| 57 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|
|
|
|
| 2 |
FROM python:3.10-slim AS builder
|
| 3 |
ENV DEBIAN_FRONTEND=noninteractive
|
| 4 |
|
| 5 |
+
# Install build deps + OpenCV runtime libs
|
| 6 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 7 |
+
build-essential cmake git curl libgl1 libglib2.0-0 \
|
| 8 |
&& rm -rf /var/lib/apt/lists/*
|
| 9 |
|
| 10 |
WORKDIR /app
|
|
|
|
|
|
|
| 11 |
COPY requirements.txt .
|
| 12 |
+
|
| 13 |
RUN python -m venv /opt/venv && \
|
| 14 |
/opt/venv/bin/pip install --upgrade pip && \
|
| 15 |
/opt/venv/bin/pip install --no-cache-dir -r requirements.txt
|
| 16 |
|
| 17 |
+
# β
Pre-download YOLOv8n weights
|
| 18 |
RUN mkdir -p /root/.cache/torch/hub/checkpoints && \
|
| 19 |
curl -L -o /root/.cache/torch/hub/checkpoints/yolov8n.pt \
|
| 20 |
https://github.com/ultralytics/assets/releases/download/v8.3.0/yolov8n.pt
|
| 21 |
|
| 22 |
+
# β
Pre-cache EasyOCR models into /app/.EasyOCR
|
| 23 |
RUN mkdir -p /app/.EasyOCR && \
|
| 24 |
/opt/venv/bin/python -c "import easyocr; easyocr.Reader(['en'], model_storage_directory='/app/.EasyOCR')"
|
| 25 |
|
|
|
|
| 28 |
ENV DEBIAN_FRONTEND=noninteractive
|
| 29 |
ENV PATH="/opt/venv/bin:$PATH"
|
| 30 |
|
| 31 |
+
# β
Fix EasyOCR cache location
|
| 32 |
ENV EASYOCR_MODULE_PATH=/app/.EasyOCR
|
| 33 |
ENV EASYOCR_CACHE_DIR=/app/.EasyOCR
|
| 34 |
ENV YOLO_CONFIG_DIR=/app/.yolo
|
| 35 |
|
|
|
|
| 36 |
RUN apt-get update && apt-get install -y --no-install-recommends \
|
| 37 |
libgl1 libglib2.0-0 \
|
| 38 |
&& rm -rf /var/lib/apt/lists/*
|
| 39 |
|
|
|
|
| 40 |
COPY --from=builder /opt/venv /opt/venv
|
| 41 |
COPY --from=builder /root/.cache /root/.cache
|
| 42 |
COPY --from=builder /app/.EasyOCR /app/.EasyOCR
|
| 43 |
|
| 44 |
WORKDIR /app
|
| 45 |
+
RUN mkdir -p /app/.yolo && chmod -R 777 /app && chmod -R 777 /root/.cache
|
| 46 |
+
|
| 47 |
+
# β
app.py baked in
|
| 48 |
+
COPY <<EOF /app/app.py
|
| 49 |
+
from fastapi import FastAPI, File, UploadFile
|
| 50 |
+
from fastapi.responses import JSONResponse
|
| 51 |
+
from fastapi.middleware.cors import CORSMiddleware
|
| 52 |
+
import numpy as np
|
| 53 |
+
from PIL import Image
|
| 54 |
+
from io import BytesIO
|
| 55 |
+
import easyocr
|
| 56 |
+
from ultralytics import YOLO
|
| 57 |
+
|
| 58 |
+
app = FastAPI()
|
| 59 |
+
|
| 60 |
+
@app.get("/")
|
| 61 |
+
async def root():
|
| 62 |
+
return {"message": "YOLO + EasyOCR API is running!"}
|
| 63 |
+
|
| 64 |
+
reader = easyocr.Reader(['en'], model_storage_directory='/app/.EasyOCR')
|
| 65 |
+
model = YOLO("yolov8n.pt")
|
| 66 |
+
print("β
YOLO model loaded")
|
| 67 |
+
|
| 68 |
+
app.add_middleware(
|
| 69 |
+
CORSMiddleware,
|
| 70 |
+
allow_origins=["*"], allow_credentials=True,
|
| 71 |
+
allow_methods=["*"], allow_headers=["*"],
|
| 72 |
+
)
|
| 73 |
+
|
| 74 |
+
@app.post("/detect")
|
| 75 |
+
async def detect_plate(file: UploadFile = File(...)):
|
| 76 |
+
try:
|
| 77 |
+
contents = await file.read()
|
| 78 |
+
image = Image.open(BytesIO(contents)).convert("RGB")
|
| 79 |
+
image_np = np.array(image)
|
| 80 |
+
|
| 81 |
+
results = model(image_np)[0]
|
| 82 |
+
plate_texts = []
|
| 83 |
+
|
| 84 |
+
for box in results.boxes:
|
| 85 |
+
x1, y1, x2, y2 = map(int, box.xyxy[0])
|
| 86 |
+
plate_crop = image_np[y1:y2, x1:x2]
|
| 87 |
+
text = reader.readtext(plate_crop, detail=0)
|
| 88 |
+
if text:
|
| 89 |
+
plate_texts.extend(text)
|
| 90 |
|
| 91 |
+
return JSONResponse(content={"plates": plate_texts})
|
|
|
|
|
|
|
|
|
|
| 92 |
|
| 93 |
+
except Exception as e:
|
| 94 |
+
return JSONResponse(content={"error": str(e)}, status_code=500)
|
| 95 |
+
EOF
|
| 96 |
|
| 97 |
EXPOSE 7860
|
| 98 |
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]
|