Spaces:
Running
Running
Update app.py
#2
by
thompson1234
- opened
app.py
CHANGED
|
@@ -1,8 +1,6 @@
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from ultralytics import YOLO
|
| 4 |
-
from huggingface_hub import hf_hub_download
|
| 5 |
-
|
| 6 |
import numpy as np
|
| 7 |
from PIL import Image
|
| 8 |
import io
|
|
@@ -10,7 +8,7 @@ import uvicorn
|
|
| 10 |
|
| 11 |
app = FastAPI()
|
| 12 |
|
| 13 |
-
# CORS
|
| 14 |
app.add_middleware(
|
| 15 |
CORSMiddleware,
|
| 16 |
allow_origins=["*"],
|
|
@@ -19,16 +17,20 @@ app.add_middleware(
|
|
| 19 |
allow_headers=["*"],
|
| 20 |
)
|
| 21 |
|
| 22 |
-
#
|
| 23 |
-
# DOWNLOAD CRACK MODEL FROM HF
|
| 24 |
-
# ===========================================
|
| 25 |
-
|
| 26 |
print("π΅ Loading local crack model...")
|
| 27 |
model = YOLO("best.pt")
|
| 28 |
print("β
Crack Model Loaded Successfully")
|
| 29 |
|
| 30 |
# ===========================================
|
| 31 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
# ===========================================
|
| 33 |
@app.post("/predict")
|
| 34 |
async def predict(img: UploadFile = File(...)):
|
|
@@ -36,10 +38,9 @@ async def predict(img: UploadFile = File(...)):
|
|
| 36 |
bytes_data = await img.read()
|
| 37 |
image = Image.open(io.BytesIO(bytes_data)).convert("RGB")
|
| 38 |
np_img = np.array(image)
|
| 39 |
-
|
| 40 |
results = model(np_img)
|
| 41 |
result = results[0]
|
| 42 |
-
|
| 43 |
# crack detection: check boxes
|
| 44 |
if result.boxes is None or len(result.boxes) == 0:
|
| 45 |
return {
|
|
@@ -47,10 +48,9 @@ async def predict(img: UploadFile = File(...)):
|
|
| 47 |
{"label": "normal", "confidence": 1.0}
|
| 48 |
]
|
| 49 |
}
|
| 50 |
-
|
| 51 |
# There are crack boxes
|
| 52 |
conf = float(result.boxes.conf.max().item())
|
| 53 |
-
|
| 54 |
return {
|
| 55 |
"data": [
|
| 56 |
{
|
|
@@ -59,7 +59,6 @@ async def predict(img: UploadFile = File(...)):
|
|
| 59 |
}
|
| 60 |
]
|
| 61 |
}
|
| 62 |
-
|
| 63 |
except Exception as e:
|
| 64 |
print("β Prediction error:", e)
|
| 65 |
return {
|
|
@@ -67,6 +66,5 @@ async def predict(img: UploadFile = File(...)):
|
|
| 67 |
"error": str(e)
|
| 68 |
}
|
| 69 |
|
| 70 |
-
|
| 71 |
if __name__ == "__main__":
|
| 72 |
-
uvicorn.run(app, host="0.0.0.0", port=7860)
|
|
|
|
| 1 |
from fastapi import FastAPI, UploadFile, File
|
| 2 |
from fastapi.middleware.cors import CORSMiddleware
|
| 3 |
from ultralytics import YOLO
|
|
|
|
|
|
|
| 4 |
import numpy as np
|
| 5 |
from PIL import Image
|
| 6 |
import io
|
|
|
|
| 8 |
|
| 9 |
app = FastAPI()
|
| 10 |
|
| 11 |
+
# CORS μ€μ
|
| 12 |
app.add_middleware(
|
| 13 |
CORSMiddleware,
|
| 14 |
allow_origins=["*"],
|
|
|
|
| 17 |
allow_headers=["*"],
|
| 18 |
)
|
| 19 |
|
| 20 |
+
# λͺ¨λΈ λ‘λ
|
|
|
|
|
|
|
|
|
|
| 21 |
print("π΅ Loading local crack model...")
|
| 22 |
model = YOLO("best.pt")
|
| 23 |
print("β
Crack Model Loaded Successfully")
|
| 24 |
|
| 25 |
# ===========================================
|
| 26 |
+
# 1. (μ€μ) μ΄ λΆλΆμ΄ μμ΄μΌ "Not Found"κ° μ λΉλλ€!
|
| 27 |
+
# ===========================================
|
| 28 |
+
@app.get("/")
|
| 29 |
+
def read_root():
|
| 30 |
+
return {"message": "ConcreteAI Crack Detection API is running!", "status": "OK"}
|
| 31 |
+
|
| 32 |
+
# ===========================================
|
| 33 |
+
# 2. μμΈ‘ API
|
| 34 |
# ===========================================
|
| 35 |
@app.post("/predict")
|
| 36 |
async def predict(img: UploadFile = File(...)):
|
|
|
|
| 38 |
bytes_data = await img.read()
|
| 39 |
image = Image.open(io.BytesIO(bytes_data)).convert("RGB")
|
| 40 |
np_img = np.array(image)
|
|
|
|
| 41 |
results = model(np_img)
|
| 42 |
result = results[0]
|
| 43 |
+
|
| 44 |
# crack detection: check boxes
|
| 45 |
if result.boxes is None or len(result.boxes) == 0:
|
| 46 |
return {
|
|
|
|
| 48 |
{"label": "normal", "confidence": 1.0}
|
| 49 |
]
|
| 50 |
}
|
| 51 |
+
|
| 52 |
# There are crack boxes
|
| 53 |
conf = float(result.boxes.conf.max().item())
|
|
|
|
| 54 |
return {
|
| 55 |
"data": [
|
| 56 |
{
|
|
|
|
| 59 |
}
|
| 60 |
]
|
| 61 |
}
|
|
|
|
| 62 |
except Exception as e:
|
| 63 |
print("β Prediction error:", e)
|
| 64 |
return {
|
|
|
|
| 66 |
"error": str(e)
|
| 67 |
}
|
| 68 |
|
|
|
|
| 69 |
if __name__ == "__main__":
|
| 70 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|