Spaces:
Sleeping
Sleeping
Update app/routes.py
Browse files- app/routes.py +58 -58
app/routes.py
CHANGED
|
@@ -1,58 +1,58 @@
|
|
| 1 |
-
from app import app
|
| 2 |
-
from fastapi.responses import JSONResponse
|
| 3 |
-
from fastapi import UploadFile,File
|
| 4 |
-
from app.process import detect,predict
|
| 5 |
-
|
| 6 |
-
SUPPORTED_FILES =['png','jpeg','jpg']
|
| 7 |
-
CLASSES = {0:'no rust',1:'rust'}
|
| 8 |
-
|
| 9 |
-
@app.get("/")
|
| 10 |
-
def index():
|
| 11 |
-
return JSONResponse({
|
| 12 |
-
"app":"Rust detection",
|
| 13 |
-
"version":"1.0.0",
|
| 14 |
-
"models":[{
|
| 15 |
-
"Mobilenet":{
|
| 16 |
-
"size":"Large",
|
| 17 |
-
"version":3,
|
| 18 |
-
"accuracy":92.0
|
| 19 |
-
},
|
| 20 |
-
"Yolo":{
|
| 21 |
-
"size":"Medium",
|
| 22 |
-
"version":9,
|
| 23 |
-
},
|
| 24 |
-
|
| 25 |
-
}]
|
| 26 |
-
})
|
| 27 |
-
|
| 28 |
-
@app.post("predict",response_class=JSONResponse)
|
| 29 |
-
def upload(image:UploadFile=File(...)):
|
| 30 |
-
extension = image.filename.split(".")[-1]
|
| 31 |
-
if extension not in SUPPORTED_FILES:
|
| 32 |
-
return JSONResponse(content={"error": "Unsupported file type"},status_code=400)
|
| 33 |
-
|
| 34 |
-
image_bytes = image.file.read()
|
| 35 |
-
try:
|
| 36 |
-
class_number,probability =predict(image_bytes)
|
| 37 |
-
uri =None
|
| 38 |
-
if class_number ==1 :
|
| 39 |
-
uri =detect(image_bytes)
|
| 40 |
-
response ={
|
| 41 |
-
"prediction":CLASSES[class_number],
|
| 42 |
-
"probability":round(probability,4)*100,
|
| 43 |
-
"uri":uri
|
| 44 |
-
}
|
| 45 |
-
return JSONResponse(content=response,status_code=200)
|
| 46 |
-
except Exception as exp:
|
| 47 |
-
print(f"[ERROR] {str(exp)}")
|
| 48 |
-
return JSONResponse(content={
|
| 49 |
-
"error":"internal Server error"
|
| 50 |
-
},status_code=500)
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
|
|
|
| 1 |
+
from app import app
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
from fastapi import UploadFile,File
|
| 4 |
+
from app.process import detect,predict
|
| 5 |
+
|
| 6 |
+
SUPPORTED_FILES =['png','jpeg','jpg']
|
| 7 |
+
CLASSES = {0:'no rust',1:'rust'}
|
| 8 |
+
|
| 9 |
+
@app.get("/")
|
| 10 |
+
def index():
|
| 11 |
+
return JSONResponse({
|
| 12 |
+
"app":"Rust detection",
|
| 13 |
+
"version":"1.0.0",
|
| 14 |
+
"models":[{
|
| 15 |
+
"Mobilenet":{
|
| 16 |
+
"size":"Large",
|
| 17 |
+
"version":3,
|
| 18 |
+
"accuracy":92.0
|
| 19 |
+
},
|
| 20 |
+
"Yolo":{
|
| 21 |
+
"size":"Medium",
|
| 22 |
+
"version":9,
|
| 23 |
+
},
|
| 24 |
+
|
| 25 |
+
}]
|
| 26 |
+
})
|
| 27 |
+
|
| 28 |
+
@app.post("/predict",response_class=JSONResponse)
|
| 29 |
+
def upload(image:UploadFile=File(...)):
|
| 30 |
+
extension = image.filename.split(".")[-1]
|
| 31 |
+
if extension not in SUPPORTED_FILES:
|
| 32 |
+
return JSONResponse(content={"error": "Unsupported file type"},status_code=400)
|
| 33 |
+
|
| 34 |
+
image_bytes = image.file.read()
|
| 35 |
+
try:
|
| 36 |
+
class_number,probability =predict(image_bytes)
|
| 37 |
+
uri =None
|
| 38 |
+
if class_number ==1 :
|
| 39 |
+
uri =detect(image_bytes)
|
| 40 |
+
response ={
|
| 41 |
+
"prediction":CLASSES[class_number],
|
| 42 |
+
"probability":round(probability,4)*100,
|
| 43 |
+
"uri":uri
|
| 44 |
+
}
|
| 45 |
+
return JSONResponse(content=response,status_code=200)
|
| 46 |
+
except Exception as exp:
|
| 47 |
+
print(f"[ERROR] {str(exp)}")
|
| 48 |
+
return JSONResponse(content={
|
| 49 |
+
"error":"internal Server error"
|
| 50 |
+
},status_code=500)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
|
| 58 |
+
|