Spaces:
Runtime error
Runtime error
Download app.py from yaseen113/microplastic-detector: direct link, hf CLI and curl.
- Browser
- Download file 2.75 kB
-
https://huggingface.co/spaces/yaseen113/microplastic-detector/resolve/main/app.py
- Command line
-
hf download hf://spaces/yaseen113/microplastic-detector/app.py
-
curl -L -o app.py https://huggingface.co/spaces/yaseen113/microplastic-detector/resolve/main/app.py
2.75 kB
| from flask import Flask, request, jsonify, send_from_directory | |
| from flask_cors import CORS | |
| from ultralytics import YOLO | |
| from PIL import Image, ImageDraw | |
| import io | |
| import base64 | |
| import os | |
| import requests | |
| MODEL_URL = "https://huggingface.co/yaseen113/microplastic-model/resolve/main/last.pt" | |
| MODEL_PATH = "last.pt" | |
| app = Flask(__name__, static_folder="static", template_folder="templates") | |
| CORS(app) | |
| # Download model first | |
| if not os.path.exists(MODEL_PATH): | |
| print("Downloading model...") | |
| r = requests.get(MODEL_URL) | |
| with open(MODEL_PATH, "wb") as f: | |
| f.write(r.content) | |
| print("Loading model...") | |
| model = YOLO(MODEL_PATH) | |
| print("Model loaded") | |
| def index(): | |
| return send_from_directory("templates", "index.html") | |
| def detect(): | |
| if "image" not in request.files: | |
| return jsonify({"error": "No image uploaded"}), 400 | |
| file = request.files["image"] | |
| img_bytes = file.read() | |
| image = Image.open(io.BytesIO(img_bytes)).convert("RGB") | |
| results = model(image, conf=0.25) | |
| result = results[0] | |
| detections = [] | |
| annotated_image = image.copy() | |
| draw = ImageDraw.Draw(annotated_image) | |
| boxes = result.boxes | |
| if boxes is not None and len(boxes) > 0: | |
| for box in boxes: | |
| x1, y1, x2, y2 = box.xyxy[0].tolist() | |
| conf = float(box.conf[0]) | |
| cls_id = int(box.cls[0]) | |
| label = model.names[cls_id] | |
| draw.rectangle([x1, y1, x2, y2], outline="#00FFCC", width=3) | |
| text = f"{label} {conf:.0%}" | |
| text_bbox = draw.textbbox((x1, y1 - 22), text) | |
| draw.rectangle(text_bbox, fill="#00FFCC") | |
| draw.text((x1, y1 - 22), text, fill="#000000") | |
| detections.append({ | |
| "label": label, | |
| "confidence": round(conf, 4), | |
| "bbox": [round(x1), round(y1), round(x2), round(y2)], | |
| "width_px": round(x2 - x1), | |
| "height_px": round(y2 - y1), | |
| }) | |
| buf = io.BytesIO() | |
| annotated_image.save(buf, format="PNG") | |
| encoded = base64.b64encode(buf.getvalue()).decode("utf-8") | |
| total = len(detections) | |
| avg_conf = round(sum(d["confidence"] for d in detections) / total, 4) if total else 0 | |
| return jsonify({ | |
| "total_detections": total, | |
| "average_confidence": avg_conf, | |
| "detections": detections, | |
| "annotated_image": f"data:image/png;base64,{encoded}", | |
| "image_size": {"width": image.width, "height": image.height}, | |
| }) | |
| if __name__ == "__main__": | |
| app.run(host="0.0.0.0", port=7860) | |