main.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. from fastapi import FastAPI, File, UploadFile
  2. from ultralytics import YOLO
  3. import io
  4. import torch
  5. from PIL import Image
  6. app = FastAPI()
  7. # Load your custom trained model
  8. model = YOLO('best.pt')
  9. @app.post("/detect")
  10. async def detect_ripeness(file: UploadFile = File(...)):
  11. image_bytes = await file.read()
  12. img = Image.open(io.BytesIO(image_bytes))
  13. # 1. Run YOLO detection
  14. results = model(img)
  15. # 2. Extract Detections and the 'Embedding'
  16. # We use the feature map from the model as a vector
  17. detections = []
  18. # Using the last hidden layer or a flattened feature map as a 'pseudo-vector'
  19. # For a true vector, we'd usually use a CLIP model, but for now, we'll return detection data
  20. for r in results:
  21. for box in r.boxes:
  22. detections.append({
  23. "class": model.names[int(box.cls)],
  24. "confidence": round(float(box.conf), 2),
  25. "box": box.xyxy.tolist()[0]
  26. })
  27. return {
  28. "status": "success",
  29. "data": detections,
  30. "message": "Model processed palm oil FFB successfully"
  31. }
  32. if __name__ == "__main__":
  33. import uvicorn
  34. uvicorn.run(app, host="0.0.0.0", port=8000)