Spaces:
Sleeping

yaseen113 commited on
Commit
d04eeeb
·
verified ·
1 Parent(s): 3fac790
Files changed (3) hide show
  1. Dockerfile +18 -0
  2. app.py +90 -0
  3. requirements.txt +6 -0
Dockerfile ADDED
@@ -0,0 +1,18 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ FROM python:3.11
2
+
3
+ WORKDIR /app
4
+
5
+ # Install OpenCV dependencies
6
+ RUN apt-get update && apt-get install -y \
7
+ libgl1 \
8
+ libglib2.0-0 \
9
+ && rm -rf /var/lib/apt/lists/*
10
+
11
+ COPY requirements.txt .
12
+ RUN pip install --no-cache-dir -r requirements.txt
13
+
14
+ COPY . .
15
+
16
+ EXPOSE 7860
17
+
18
+ CMD ["python", "app.py"]
app.py ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify, send_from_directory
2
+ from flask_cors import CORS
3
+ from ultralytics import YOLO
4
+ from PIL import Image, ImageDraw
5
+ import io
6
+ import base64
7
+ import os
8
+ import requests
9
+
10
+ MODEL_URL = "https://huggingface.co/yaseen113/microplastic-model/resolve/main/last.pt"
11
+ MODEL_PATH = "last.pt"
12
+
13
+ app = Flask(__name__, static_folder="static", template_folder="templates")
14
+ CORS(app)
15
+
16
+ # Download model first
17
+ if not os.path.exists(MODEL_PATH):
18
+ print("Downloading model...")
19
+ r = requests.get(MODEL_URL)
20
+ with open(MODEL_PATH, "wb") as f:
21
+ f.write(r.content)
22
+
23
+ print("Loading model...")
24
+ model = YOLO(MODEL_PATH)
25
+ print("Model loaded")
26
+
27
+ @app.route("/")
28
+ def index():
29
+ return send_from_directory("templates", "index.html")
30
+
31
+ @app.route("/detect", methods=["POST"])
32
+ def detect():
33
+ if "image" not in request.files:
34
+ return jsonify({"error": "No image uploaded"}), 400
35
+
36
+ file = request.files["image"]
37
+ img_bytes = file.read()
38
+ image = Image.open(io.BytesIO(img_bytes)).convert("RGB")
39
+
40
+
41
+ results = model(image, conf=0.25)
42
+ result = results[0]
43
+
44
+ detections = []
45
+ annotated_image = image.copy()
46
+ draw = ImageDraw.Draw(annotated_image)
47
+
48
+ boxes = result.boxes
49
+ if boxes is not None and len(boxes) > 0:
50
+ for box in boxes:
51
+ x1, y1, x2, y2 = box.xyxy[0].tolist()
52
+ conf = float(box.conf[0])
53
+ cls_id = int(box.cls[0])
54
+ label = model.names[cls_id]
55
+
56
+
57
+ draw.rectangle([x1, y1, x2, y2], outline="#00FFCC", width=3)
58
+
59
+
60
+ text = f"{label} {conf:.0%}"
61
+ text_bbox = draw.textbbox((x1, y1 - 22), text)
62
+ draw.rectangle(text_bbox, fill="#00FFCC")
63
+ draw.text((x1, y1 - 22), text, fill="#000000")
64
+
65
+ detections.append({
66
+ "label": label,
67
+ "confidence": round(conf, 4),
68
+ "bbox": [round(x1), round(y1), round(x2), round(y2)],
69
+ "width_px": round(x2 - x1),
70
+ "height_px": round(y2 - y1),
71
+ })
72
+
73
+
74
+ buf = io.BytesIO()
75
+ annotated_image.save(buf, format="PNG")
76
+ encoded = base64.b64encode(buf.getvalue()).decode("utf-8")
77
+
78
+ total = len(detections)
79
+ avg_conf = round(sum(d["confidence"] for d in detections) / total, 4) if total else 0
80
+
81
+ return jsonify({
82
+ "total_detections": total,
83
+ "average_confidence": avg_conf,
84
+ "detections": detections,
85
+ "annotated_image": f"data:image/png;base64,{encoded}",
86
+ "image_size": {"width": image.width, "height": image.height},
87
+ })
88
+
89
+ if __name__ == "__main__":
90
+ app.run(host="0.0.0.0", port=7860)
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ flask
2
+ flask-cors
3
+ ultralytics
4
+ pillow
5
+ requests
6
+ gunicorn