|
from fastapi import FastAPI, File, UploadFile |
|
from fastapi.responses import JSONResponse |
|
from fastapi.middleware.cors import CORSMiddleware |
|
from PIL import Image |
|
import io |
|
|
|
from pathlib import Path |
|
from model import YOLOModel |
|
import shutil |
|
|
|
yolo = YOLOModel() |
|
|
|
UPLOAD_FOLDER = Path("./uploads") |
|
UPLOAD_FOLDER.mkdir(exist_ok=True) |
|
|
|
app = FastAPI() |
|
|
|
@app.post("/upload") |
|
async def upload_image(image: UploadFile = File(...)): |
|
|
|
try: |
|
file_path = UPLOAD_FOLDER / image.filename |
|
with file_path.open("wb") as buffer: |
|
shutil.copyfileobj(image.file, buffer) |
|
|
|
|
|
predictions = yolo.predict(str(file_path)) |
|
print(f'\n\n\n{predictions}\n\n\ \n\t\t\t\tare predictions') |
|
|
|
file_path.unlink() |
|
return JSONResponse(content={"items": predictions}) |
|
|
|
|
|
except Exception as e: |
|
return JSONResponse(content={"error": str(e)}, status_code=500) |
|
|
|
|
|
app.add_middleware( |
|
CORSMiddleware, |
|
allow_origins=["http://192.168.56.1:3000", "http://192.168.56.1:3001"], |
|
allow_methods=["*"], |
|
allow_headers=["*"], |
|
) |
|
|
|
if __name__ == "__main__": |
|
import uvicorn |
|
uvicorn.run("app:app", host="0.0.0.0", port=7860, reload=True) |
|
|