File size: 1,487 Bytes
ea32d4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5920752
ea32d4d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
from app import app
from fastapi.responses import JSONResponse
from fastapi import UploadFile,File
from app.process import detect,predict

SUPPORTED_FILES =['png','jpeg','jpg']
CLASSES = {0:'no rust',1:'rust'}

@app.get("/")
def index():
    return JSONResponse({
        "app":"Rust detection",
        "version":"1.0.0",
        "models":[{
            "Mobilenet":{
                "size":"Large",
                "version":3,
                "accuracy":92.0
            },
            "Yolo":{
                "size":"Medium",
                "version":9,      
            },
            
        }]
    })

@app.post("/predict",response_class=JSONResponse)
def upload(image:UploadFile=File(...)):
    extension = image.filename.split(".")[-1]
    if extension not in SUPPORTED_FILES:
        return JSONResponse(content={"error": "Unsupported file type"},status_code=400)
    
    image_bytes = image.file.read()
    try:
        class_number,probability =predict(image_bytes)
        uri =None
        if class_number ==1 :
            uri =detect(image_bytes)
        response ={
            "prediction":CLASSES[class_number],
            "probability":str(round(probability,4)*100),
            "uri":uri
        }
        return JSONResponse(content=response,status_code=200)
    except Exception as exp:
        print(f"[ERROR] {str(exp)}")
        return JSONResponse(content={
            "error":"internal Server error"
            },status_code=500)