muddasser commited on
Commit
827d115
·
verified ·
1 Parent(s): d56c49a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -9
app.py CHANGED
@@ -7,26 +7,34 @@ from PIL import Image
7
  from io import BytesIO
8
  import easyocr
9
  from ultralytics import YOLO
 
 
 
 
 
 
10
 
11
  app = FastAPI()
12
 
13
- # ✅ Root endpoint so "/" returns something
14
- @app.get("/")
15
- async def root():
16
- return {"message": "YOLO + EasyOCR API is running!"}
17
 
18
- # Initialize OCR and YOLO
19
- reader = easyocr.Reader(['en'])
20
- model = YOLO("yolov8n.pt")
21
- print("✅ YOLO model loaded")
22
 
23
- # CORS settings
24
  app.add_middleware(
25
  CORSMiddleware,
26
  allow_origins=["*"], allow_credentials=True,
27
  allow_methods=["*"], allow_headers=["*"],
28
  )
29
 
 
 
 
 
30
  @app.post("/detect")
31
  async def detect_plate(file: UploadFile = File(...)):
32
  try:
 
7
  from io import BytesIO
8
  import easyocr
9
  from ultralytics import YOLO
10
+ import os
11
+
12
+ # ✅ Force YOLO & EasyOCR cache dirs
13
+ os.environ["EASYOCR_MODULE_PATH"] = "/app/.EasyOCR"
14
+ os.environ["EASYOCR_CACHE_DIR"] = "/app/.EasyOCR"
15
+ os.environ["YOLO_CONFIG_DIR"] = "/app/.yolo"
16
 
17
  app = FastAPI()
18
 
19
+ # ✅ Load EasyOCR from pre-downloaded cache
20
+ reader = easyocr.Reader(['en'], model_storage_directory="/app/.EasyOCR")
 
 
21
 
22
+ # Load YOLO from pre-downloaded weights
23
+ yolo_weights_path = "/root/.cache/torch/hub/checkpoints/yolov8n.pt"
24
+ model = YOLO(yolo_weights_path)
25
+ print("✅ YOLO model loaded from cache:", yolo_weights_path)
26
 
27
+ # CORS settings
28
  app.add_middleware(
29
  CORSMiddleware,
30
  allow_origins=["*"], allow_credentials=True,
31
  allow_methods=["*"], allow_headers=["*"],
32
  )
33
 
34
+ @app.get("/")
35
+ async def root():
36
+ return {"message": "YOLO Plate Detector API is running."}
37
+
38
  @app.post("/detect")
39
  async def detect_plate(file: UploadFile = File(...)):
40
  try: