ujoy007 commited on
Commit
5b33654
Β·
1 Parent(s): 7322f55

another changes in names

Browse files
Files changed (4) hide show
  1. Dockerfile +3 -1
  2. README.md +4 -3
  3. main.py +26 -40
  4. requirements.txt +2 -2
Dockerfile CHANGED
@@ -5,10 +5,12 @@ WORKDIR /code
5
  # Install system dependencies
6
  RUN apt-get update && apt-get install -y libgl1 libglib2.0-0
7
 
 
8
  COPY requirements.txt .
9
  RUN pip install --no-cache-dir -r requirements.txt
10
 
 
11
  COPY . .
12
 
13
- # Run FastAPI with Uvicorn on HF's expected port
14
  CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
 
5
  # Install system dependencies
6
  RUN apt-get update && apt-get install -y libgl1 libglib2.0-0
7
 
8
+ # Copy dependencies
9
  COPY requirements.txt .
10
  RUN pip install --no-cache-dir -r requirements.txt
11
 
12
+ # Copy app code
13
  COPY . .
14
 
15
+ # Run FastAPI with Uvicorn on the correct file and port
16
  CMD ["uvicorn", "main:app", "--host", "0.0.0.0", "--port", "7860"]
README.md CHANGED
@@ -1,5 +1,5 @@
1
  ---
2
- title: YOLO FastAPI App
3
  emoji: πŸ€–
4
  colorFrom: blue
5
  colorTo: green
@@ -8,6 +8,7 @@ app_file: main.py
8
  pinned: false
9
  ---
10
 
11
- # YOLO Object Detection API πŸš€
12
 
13
- This runs a FastAPI app serving a pretrained YOLOv8 model.
 
 
1
  ---
2
+ title: YOLO FastAPI Deploy
3
  emoji: πŸ€–
4
  colorFrom: blue
5
  colorTo: green
 
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.
main.py CHANGED
@@ -1,46 +1,32 @@
1
- from fastapi import FastAPI, File, UploadFile
2
  from ultralytics import YOLO
3
- import uvicorn
4
- import cv2
5
- import numpy as np
6
- from io import BytesIO
7
- from PIL import Image
8
- import base64
9
-
10
- # Load pretrained YOLOv11 model
11
- model = YOLO("yolo11s.pt")
12
 
 
13
  app = FastAPI()
14
 
 
 
 
 
 
 
 
15
  @app.post("/detect")
16
  async def detect(file: UploadFile = File(...)):
17
- # Read image from user
18
- contents = await file.read()
19
- image = Image.open(BytesIO(contents)).convert("RGB")
20
-
21
- # Run YOLO inference
22
- results = model.predict(image)
23
-
24
- # Extract detections
25
- detections = []
26
- for r in results:
27
- for box in r.boxes:
28
- cls = int(box.cls[0].item())
29
- conf = float(box.conf[0].item())
30
- detections.append({
31
- "class": model.names[cls],
32
- "confidence": conf
33
- })
34
-
35
- # Optionally return annotated image
36
- annotated_frame = results[0].plot() # numpy array (BGR)
37
- _, buffer = cv2.imencode(".jpg", annotated_frame)
38
- img_base64 = base64.b64encode(buffer).decode("utf-8")
39
-
40
- return {
41
- "detections": detections,
42
- "annotated_image": f"data:image/jpeg;base64,{img_base64}"
43
- }
44
-
45
- # if __name__ == "__main__":
46
- # uvicorn.run(app, host="0.0.0.0", port=8000)
 
1
+ from fastapi import FastAPI, UploadFile, File
2
  from ultralytics import YOLO
3
+ import shutil
4
+ import uuid
5
+ import os
 
 
 
 
 
 
6
 
7
+ # Initialize FastAPI
8
  app = FastAPI()
9
 
10
+ # Load YOLO model (small version for speed)
11
+ model = YOLO("yolo11s.pt") # or yolov8n.pt if you prefer
12
+
13
+ UPLOAD_DIR = "uploads"
14
+ os.makedirs(UPLOAD_DIR, exist_ok=True)
15
+
16
+
17
  @app.post("/detect")
18
  async def detect(file: UploadFile = File(...)):
19
+ # Save the uploaded file
20
+ file_id = str(uuid.uuid4())
21
+ file_path = os.path.join(UPLOAD_DIR, f"{file_id}_{file.filename}")
22
+ with open(file_path, "wb") as buffer:
23
+ shutil.copyfileobj(file.file, buffer)
24
+
25
+ # Run YOLO detection
26
+ results = model(file_path)
27
+
28
+ # Save result image
29
+ result_path = f"{file_path}_result.jpg"
30
+ results[0].save(filename=result_path)
31
+
32
+ return {"message": "Detection complete", "result_file": result_path}
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
requirements.txt CHANGED
@@ -1,6 +1,6 @@
1
- ultralytics
2
  fastapi
3
  uvicorn
 
4
  python-multipart
5
  pillow
6
- opencv-python-headless
 
 
1
  fastapi
2
  uvicorn
3
+ ultralytics
4
  python-multipart
5
  pillow
6
+ opencv-python-headless