ujoy007 commited on
Commit
eece6a4
Β·
1 Parent(s): 60e7e74

all problems are now fixed. \detect should return predictions

Browse files
Files changed (2) hide show
  1. README.md +16 -11
  2. main.py +17 -22
README.md CHANGED
@@ -1,14 +1,19 @@
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
- title: YOLO FastAPI Deploy
3
- emoji: πŸ€–
4
- colorFrom: blue
5
- colorTo: green
6
- sdk: docker
7
- app_file: main.py
8
- pinned: false
9
- ---
10
 
11
- # YOLO FastAPI Deployment πŸš€
 
 
12
 
13
- This Space deploys a YOLO model with FastAPI.
14
- Use `/docs` to test endpoints, or send requests from Postman.
 
1
+ # YOLO FastAPI Deploy πŸš€
2
+
3
+ This Space deploys a YOLO object detection model with **FastAPI** on Hugging Face Spaces.
4
+
5
+ ## πŸ”— Live URL
6
+
7
+ πŸ‘‰ [https://ujoy-yolo-fast-api-deploy.hf.space](https://ujoy-yolo-fast-api-deploy.hf.space)
8
+
9
+ - Root health: `/`
10
+ - API docs (Swagger UI): `/docs`
11
+ - Detect endpoint: `/detect`
12
+
13
  ---
 
 
 
 
 
 
 
 
14
 
15
+ ## πŸ“Œ Usage
16
+
17
+ ### 1. Upload an image for detection
18
 
19
+ **Endpoint**:
 
main.py CHANGED
@@ -1,32 +1,33 @@
1
  from fastapi import FastAPI, UploadFile, File
2
- from fastapi.responses import JSONResponse, FileResponse
3
  from ultralytics import YOLO
4
  import shutil
5
  import uuid
6
  import os
7
- import base64
8
 
9
  app = FastAPI()
10
 
11
  # Load YOLO model
12
  model = YOLO("yolo11s.pt")
13
 
14
- # Use /tmp for writable storage in Hugging Face Spaces
15
  UPLOAD_DIR = "/tmp/uploads"
16
  os.makedirs(UPLOAD_DIR, exist_ok=True)
17
 
18
 
19
  @app.get("/")
20
  def home():
21
- return {"message": "YOLO FastAPI is running! πŸš€ Go to /docs for the API."}
22
 
23
 
24
  @app.post("/detect")
25
- async def detect(file: UploadFile = File(...), return_type: str = "json"):
26
  """
27
  Run YOLO detection.
28
- - return_type=json β†’ return base64 encoded image inside JSON
29
- - return_type=file β†’ return annotated image as downloadable file
 
 
30
  """
31
  # Save uploaded file
32
  file_id = str(uuid.uuid4())
@@ -37,19 +38,13 @@ async def detect(file: UploadFile = File(...), return_type: str = "json"):
37
  # Run YOLO detection
38
  results = model(file_path)
39
 
40
- # Save annotated result image
41
- result_path = f"{file_path}_result.jpg"
42
- results[0].save(filename=result_path)
43
-
44
- if return_type == "file":
45
- # Return file directly (downloadable)
46
- return FileResponse(result_path, media_type="image/jpeg", filename="result.jpg")
47
- else:
48
- # Encode image as base64 string
49
- with open(result_path, "rb") as img_file:
50
- encoded = base64.b64encode(img_file.read()).decode("utf-8")
51
-
52
- return JSONResponse(content={
53
- "message": "Detection complete",
54
- "image_base64": encoded
55
  })
 
 
 
1
  from fastapi import FastAPI, UploadFile, File
2
+ from fastapi.responses import JSONResponse
3
  from ultralytics import YOLO
4
  import shutil
5
  import uuid
6
  import os
 
7
 
8
  app = FastAPI()
9
 
10
  # Load YOLO model
11
  model = YOLO("yolo11s.pt")
12
 
13
+ # Writable directory on Hugging Face
14
  UPLOAD_DIR = "/tmp/uploads"
15
  os.makedirs(UPLOAD_DIR, exist_ok=True)
16
 
17
 
18
  @app.get("/")
19
  def home():
20
+ return {"status": "running", "docs": "/docs"}
21
 
22
 
23
  @app.post("/detect")
24
+ async def detect(file: UploadFile = File(...)):
25
  """
26
  Run YOLO detection.
27
+ Returns:
28
+ - bounding boxes
29
+ - class labels
30
+ - confidence scores
31
  """
32
  # Save uploaded file
33
  file_id = str(uuid.uuid4())
 
38
  # Run YOLO detection
39
  results = model(file_path)
40
 
41
+ # Extract predictions
42
+ predictions = []
43
+ for box in results[0].boxes:
44
+ predictions.append({
45
+ "class": results[0].names[int(box.cls)],
46
+ "confidence": float(box.conf),
47
+ "bbox": [float(x) for x in box.xyxy[0].tolist()] # [x1, y1, x2, y2]
 
 
 
 
 
 
 
 
48
  })
49
+
50
+ return JSONResponse(content={"predictions": predictions})