Spaces:
Sleeping
Sleeping
Upload 4 files
Browse files- Dockerfile +31 -0
- app.py +86 -0
- best.pt +3 -0
- requirements.txt +7 -0
Dockerfile
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
FROM python:3.11-slim
|
| 2 |
+
|
| 3 |
+
# Create a non-root user
|
| 4 |
+
RUN useradd -m -u 1000 user
|
| 5 |
+
USER user
|
| 6 |
+
ENV PATH="/home/user/.local/bin:$PATH"
|
| 7 |
+
|
| 8 |
+
WORKDIR /app
|
| 9 |
+
|
| 10 |
+
# Install system dependencies (must be root)
|
| 11 |
+
USER root
|
| 12 |
+
RUN apt-get update && apt-get install -y \
|
| 13 |
+
libgl1-mesa-glx \
|
| 14 |
+
libglib2.0-0 \
|
| 15 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 16 |
+
USER user
|
| 17 |
+
|
| 18 |
+
# Copy requirements
|
| 19 |
+
COPY --chown=user requirements.txt .
|
| 20 |
+
|
| 21 |
+
# Install dependencies
|
| 22 |
+
RUN pip install --no-cache-dir --upgrade -r requirements.txt
|
| 23 |
+
|
| 24 |
+
# Copy the rest of the code
|
| 25 |
+
COPY --chown=user . .
|
| 26 |
+
|
| 27 |
+
# Expose the port FastAPI runs on
|
| 28 |
+
EXPOSE 7860
|
| 29 |
+
|
| 30 |
+
# Command to run the application
|
| 31 |
+
CMD ["python", "app.py"]
|
app.py
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI, File, UploadFile
|
| 2 |
+
from fastapi.responses import HTMLResponse
|
| 3 |
+
from ultralytics import YOLO
|
| 4 |
+
import uvicorn
|
| 5 |
+
import io
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import numpy as np
|
| 8 |
+
|
| 9 |
+
app = FastAPI(title="Fire Detection API", description="API for detecting fire and smoke using YOLOv26")
|
| 10 |
+
|
| 11 |
+
# Load model
|
| 12 |
+
model = YOLO("best.pt")
|
| 13 |
+
|
| 14 |
+
@app.get("/", response_class=HTMLResponse)
|
| 15 |
+
def read_root():
|
| 16 |
+
return """
|
| 17 |
+
<!DOCTYPE html>
|
| 18 |
+
<html>
|
| 19 |
+
<head>
|
| 20 |
+
<title>Fire Detection API</title>
|
| 21 |
+
<style>
|
| 22 |
+
body { font-family: sans-serif; text-align: center; padding: 50px; background: #f4f4f9; }
|
| 23 |
+
.container { background: white; padding: 20px; border-radius: 10px; display: inline-block; box-shadow: 0 4px 6px rgba(0,0,0,0.1); }
|
| 24 |
+
h1 { color: #d9534f; }
|
| 25 |
+
input { margin: 20px 0; }
|
| 26 |
+
button { background: #d9534f; color: white; border: none; padding: 10px 20px; border-radius: 5px; cursor: pointer; }
|
| 27 |
+
#result { margin-top: 20px; text-align: left; }
|
| 28 |
+
</style>
|
| 29 |
+
</head>
|
| 30 |
+
<body>
|
| 31 |
+
<div class="container">
|
| 32 |
+
<h1>🔥 Fire Detection API</h1>
|
| 33 |
+
<p>Upload an image to detect fire or smoke</p>
|
| 34 |
+
<input type="file" id="imageInput" accept="image/*">
|
| 35 |
+
<br>
|
| 36 |
+
<button onclick="uploadImage()">Detect</button>
|
| 37 |
+
<div id="result"></div>
|
| 38 |
+
</div>
|
| 39 |
+
|
| 40 |
+
<script>
|
| 41 |
+
async function uploadImage() {
|
| 42 |
+
const input = document.getElementById('imageInput');
|
| 43 |
+
if (!input.files[0]) return alert('Please select an image');
|
| 44 |
+
|
| 45 |
+
const formData = new FormData();
|
| 46 |
+
formData.append('file', input.files[0]);
|
| 47 |
+
|
| 48 |
+
const resultDiv = document.getElementById('result');
|
| 49 |
+
resultDiv.innerHTML = 'Detecting...';
|
| 50 |
+
|
| 51 |
+
try {
|
| 52 |
+
const response = await fetch('/predict', { method: 'POST', body: formData });
|
| 53 |
+
const data = await response.json();
|
| 54 |
+
resultDiv.innerHTML = '<pre>' + JSON.stringify(data, null, 2) + '</pre>';
|
| 55 |
+
} catch (e) {
|
| 56 |
+
resultDiv.innerHTML = 'Error: ' + e.message;
|
| 57 |
+
}
|
| 58 |
+
}
|
| 59 |
+
</script>
|
| 60 |
+
</body>
|
| 61 |
+
</html>
|
| 62 |
+
"""
|
| 63 |
+
|
| 64 |
+
@app.post("/predict")
|
| 65 |
+
async def predict(file: UploadFile = File(...)):
|
| 66 |
+
# Read image
|
| 67 |
+
contents = await file.read()
|
| 68 |
+
image = Image.open(io.BytesIO(contents)).convert("RGB")
|
| 69 |
+
|
| 70 |
+
# Run inference
|
| 71 |
+
results = model.predict(image, conf=0.25)
|
| 72 |
+
|
| 73 |
+
detections = []
|
| 74 |
+
for result in results:
|
| 75 |
+
for box in result.boxes:
|
| 76 |
+
detection = {
|
| 77 |
+
"class": model.names[int(box.cls[0])],
|
| 78 |
+
"confidence": float(box.conf[0]),
|
| 79 |
+
"bbox": [float(x) for x in box.xyxy[0]] # [x1, y1, x2, y2]
|
| 80 |
+
}
|
| 81 |
+
detections.append(detection)
|
| 82 |
+
|
| 83 |
+
return {"detections": detections}
|
| 84 |
+
|
| 85 |
+
if __name__ == "__main__":
|
| 86 |
+
uvicorn.run(app, host="0.0.0.0", port=7860)
|
best.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:52684ddffd4797f1bc5ff1bd2b72af3ec34eabf0c1646c37f518bb766b27b79f
|
| 3 |
+
size 20301317
|
requirements.txt
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
ultralytics
|
| 2 |
+
fastapi
|
| 3 |
+
uvicorn
|
| 4 |
+
python-multipart
|
| 5 |
+
huggingface_hub
|
| 6 |
+
opencv-python
|
| 7 |
+
pillow
|