CoolFace
Apppublic

johnwesley756/instance-segmentation

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
api.py66 linesDownload Raw Back to root
1import os2import sys3 4# ✅ ABSOLUTE PATH FIX (THIS IS THE KEY)5BASE_DIR = os.path.dirname(os.path.abspath(__file__))6sys.path.insert(0, BASE_DIR)7 8from fastapi import FastAPI, UploadFile, File, HTTPException9from fastapi.middleware.cors import CORSMiddleware10import numpy as np11import cv212import base6413 14from ui import run_inference15 16app = FastAPI(17    title="Tooth Decay Detection API",18    version="1.0.0"19)20 21app.add_middleware(22    CORSMiddleware,23    allow_origins=["*"],24    allow_methods=["*"],25    allow_headers=["*"],26)27 28 29@app.get("/")30def root():31    return {"message": "Tooth Decay Detection API running"}32 33 34@app.get("/health")35def health():36    return {"status": "healthy"}37 38 39@app.post("/predict")40async def predict(file: UploadFile = File(...)):41    if not file.content_type.startswith("image/"):42        raise HTTPException(400, "File must be an image")43 44    image_bytes = await file.read()45    image = cv2.imdecode(46        np.frombuffer(image_bytes, np.uint8),47        cv2.IMREAD_COLOR48    )49 50    if image is None:51        raise HTTPException(400, "Invalid image file")52 53    severity, summary, detections, annotated = run_inference(image)54 55    _, buffer = cv2.imencode(".jpg", annotated)56    encoded_image = base64.b64encode(buffer).decode()57 58    return {59        "success": True,60        "severity": severity,61        "summary": summary,62        "detections": detections,63        "total_detections": len(detections),64        "annotated_image": encoded_image65    }66