Mohamed-salah-dev's picture
Create app.py
08301c4 verified
Raw
History Blame Contribute Delete
1.49 kB
from fastapi import FastAPI, File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from ultralytics import YOLO
import cv2
import numpy as np
from PIL import Image
import io
app = FastAPI(title="Pothole Detection API")
# السماح للـ Flutter بالاتصال بالـ API
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
# تحميل الموديل الجاهز من الـ Hugging Face أوتوماتيك
model = YOLO('Harisanth/Pothole-Finetuned-YOLOv8')
@app.get("/")
def home():
return {"message": "Pothole Detection API is running بنجاح!"}
@app.post("/predict")
async def predict_pothole(file: UploadFile = File(...)):
# قراءة الصورة
request_object_content = await file.read()
image = Image.open(io.BytesIO(request_object_content)).convert("RGB")
# تحويل الصورة لصيغة OpenCV
open_cv_image = np.array(image)
open_cv_image = open_cv_image[:, :, ::-1].copy()
# تشغيل الموديل
results = model(open_cv_image)
boxes_count = 0
pothole_detected = False
for result in results:
boxes_count = len(result.boxes)
if boxes_count > 0:
pothole_detected = True
# الرد لزميلك بتاع الفلتر
return {
"pothole_detected": pothole_detected,
"number_of_potholes": boxes_count,
"status": "success"
}