File size: 1,291 Bytes
a0a2522
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
import os
os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE"
from fastapi import FastAPI,UploadFile,File
from alpr_module import detect_license_plate, extract_plate_from_image, enhance_image_for_ocr, extract_license_number
import tempfile




app = FastAPI()





@app.post("/")
async def recognize_plate(file: UploadFile = File(...)):
    try:
        # حفظ الصورة مؤقتاً
        with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as tmp:
            tmp.write(await file.read())
            tmp_path = tmp.name
        # الخطوات
        predictions = detect_license_plate(tmp_path)
        plate_img = extract_plate_from_image(tmp_path, predictions)
        if plate_img is None:
            os.remove(tmp_path)
            return JSONResponse(status_code=404, content={"error": "لم يتم اكتشاف أي لوحة"})
        enhanced_img = enhance_image_for_ocr(plate_img)
        numbers = extract_license_number(enhanced_img)
        os.remove(tmp_path)
        if numbers:
            longest = max(numbers, key=len)
            return {"plate_number": longest}
        else:
            return {"plate_number": None}
    except Exception as e:
        return JSONResponse(status_code=500, content={"error": str(e)})