Spaces:
Runtime error
Runtime error
Sushan
commited on
Commit
•
a86a345
1
Parent(s):
e5810ee
Required Files
Browse files- Dockerfile +20 -0
- app.py +59 -0
- requirements.txt +0 -0
Dockerfile
ADDED
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Base image
|
2 |
+
FROM python:3.10-slim
|
3 |
+
|
4 |
+
# Set working directory
|
5 |
+
WORKDIR /app
|
6 |
+
|
7 |
+
# Copy necessary files
|
8 |
+
COPY app.py /app/app.py
|
9 |
+
COPY best_yolov8m_model.pt /app/best_yolov8m_model.pt
|
10 |
+
COPY requirements.txt /app/requirements.txt
|
11 |
+
|
12 |
+
# Install required packages
|
13 |
+
RUN pip install --no-cache-dir --upgrade pip && \
|
14 |
+
pip install --no-cache-dir -r requirements.txt
|
15 |
+
|
16 |
+
# Expose the port for the API
|
17 |
+
EXPOSE 8000
|
18 |
+
|
19 |
+
# Run FastAPI
|
20 |
+
CMD ["uvicorn", "app:app", "--host", "0.0.0.0", "--port", "8000"]
|
app.py
ADDED
@@ -0,0 +1,59 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from fastapi import FastAPI, File, UploadFile
|
2 |
+
from PIL import Image
|
3 |
+
import io
|
4 |
+
import cv2
|
5 |
+
import torch
|
6 |
+
from ultralytics import YOLO
|
7 |
+
import numpy as np
|
8 |
+
from typing import List
|
9 |
+
|
10 |
+
# Initialize the FastAPI app
|
11 |
+
app = FastAPI()
|
12 |
+
|
13 |
+
# Load YOLOv8 model
|
14 |
+
model = YOLO("best_yolov8m_model.pt")
|
15 |
+
|
16 |
+
# Class names for algae
|
17 |
+
class_names = ["blue-green-algae", "brown-algae", "red-algae"]
|
18 |
+
|
19 |
+
def predict(image: Image.Image):
|
20 |
+
# Convert image to OpenCV format
|
21 |
+
image_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
|
22 |
+
|
23 |
+
# Perform inference
|
24 |
+
results = model(image_cv)
|
25 |
+
|
26 |
+
# Process the results
|
27 |
+
detected_objects = []
|
28 |
+
for result in results:
|
29 |
+
for box in result.boxes:
|
30 |
+
class_id = int(box.cls)
|
31 |
+
conf = box.conf
|
32 |
+
bbox = box.xyxy
|
33 |
+
detected_objects.append({
|
34 |
+
"class": class_names[class_id],
|
35 |
+
"confidence": float(conf),
|
36 |
+
"bbox": bbox.tolist()
|
37 |
+
})
|
38 |
+
|
39 |
+
return detected_objects, results
|
40 |
+
|
41 |
+
@app.post("/predict/")
|
42 |
+
async def predict_api(file: UploadFile = File(...)):
|
43 |
+
# Read image from the uploaded file
|
44 |
+
image = Image.open(io.BytesIO(await file.read()))
|
45 |
+
|
46 |
+
# Run inference
|
47 |
+
detections, results = predict(image)
|
48 |
+
|
49 |
+
# Annotate the image with bounding boxes
|
50 |
+
result_image = results[0].plot() # Plot the bounding boxes
|
51 |
+
|
52 |
+
# Convert to bytes to return as response
|
53 |
+
_, encoded_image = cv2.imencode('.jpg', result_image)
|
54 |
+
result_image_bytes = encoded_image.tobytes()
|
55 |
+
|
56 |
+
return {
|
57 |
+
"image": result_image_bytes, # Encoded result image with bounding boxes
|
58 |
+
"detections": detections # List of detected algae types and bounding boxes
|
59 |
+
}
|
requirements.txt
ADDED
File without changes
|