muddasser commited on
Commit
2a5aabf
·
verified ·
1 Parent(s): 5d11207

Update Dockerfile

Browse files
Files changed (1) hide show
  1. Dockerfile +6 -58
Dockerfile CHANGED
@@ -2,14 +2,12 @@
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
@@ -19,10 +17,6 @@ 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
-
26
  # Stage 2 - Final Image
27
  FROM python:3.10-slim
28
  ENV DEBIAN_FRONTEND=noninteractive
@@ -38,61 +32,15 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
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"]
 
2
  FROM python:3.10-slim AS builder
3
  ENV DEBIAN_FRONTEND=noninteractive
4
 
 
5
  RUN apt-get update && apt-get install -y --no-install-recommends \
6
+ build-essential cmake git curl \
7
  && rm -rf /var/lib/apt/lists/*
8
 
9
  WORKDIR /app
10
  COPY requirements.txt .
 
11
  RUN python -m venv /opt/venv && \
12
  /opt/venv/bin/pip install --upgrade pip && \
13
  /opt/venv/bin/pip install --no-cache-dir -r requirements.txt
 
17
  curl -L -o /root/.cache/torch/hub/checkpoints/yolov8n.pt \
18
  https://github.com/ultralytics/assets/releases/download/v8.3.0/yolov8n.pt
19
 
 
 
 
 
20
  # Stage 2 - Final Image
21
  FROM python:3.10-slim
22
  ENV DEBIAN_FRONTEND=noninteractive
 
32
  && rm -rf /var/lib/apt/lists/*
33
 
34
  COPY --from=builder /opt/venv /opt/venv
35
+ COPY --from=builder /root/.cache/torch/hub/checkpoints/yolov8n.pt /app/yolov8n.pt
 
36
 
37
  WORKDIR /app
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
38
 
39
+ # Create required cache folders and fix permissions
40
+ RUN mkdir -p /app/.EasyOCR /app/.yolo && \
41
+ chmod -R 777 /app
42
 
43
+ COPY . .
 
 
44
 
45
  EXPOSE 7860
46
  CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "7860"]