amobionovo commited on
Commit
6516104
·
verified ·
1 Parent(s): 41477c1

Upload 3 files

Browse files
Files changed (3) hide show
  1. best.pt +3 -0
  2. handler.py +65 -0
  3. requirements.txt +6 -0
best.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:295dee8c572bf2d3e530f8bdb09537ecc4806894fb20fd5eacc034a00a387956
3
+ size 6248810
handler.py ADDED
@@ -0,0 +1,65 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, Any
2
+ import base64
3
+ import io
4
+ from PIL import Image
5
+ import torch
6
+
7
+ class EndpointHandler:
8
+ """
9
+ Hugging Face Inference Endpoint handler for LocustGuard YOLO model.
10
+
11
+ Expected JSON:
12
+ {
13
+ "image": "<base64 encoded image>",
14
+ "conf": 0.25,
15
+ "iou": 0.45
16
+ }
17
+
18
+ Returns:
19
+ {
20
+ "detections": [
21
+ {
22
+ "label": str,
23
+ "confidence": float,
24
+ "coordinates": [xmin, ymin, xmax, ymax]
25
+ }
26
+ ]
27
+ }
28
+ """
29
+
30
+ def __init__(self, path: str = "."):
31
+ self.model = torch.hub.load(
32
+ "ultralytics/yolov5",
33
+ "custom",
34
+ path=f"{path}/best.pt",
35
+ force_reload=False
36
+ )
37
+
38
+ def __call__(self, data: Dict[str, Any]) -> Dict[str, Any]:
39
+ image_b64 = data["image"]
40
+ conf = float(data.get("conf", 0.25))
41
+ iou = float(data.get("iou", 0.45))
42
+
43
+ image_bytes = base64.b64decode(image_b64)
44
+ image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
45
+
46
+ self.model.conf = conf
47
+ self.model.iou = iou
48
+
49
+ results = self.model(image, size=640)
50
+ r = results.pandas().xyxy[0]
51
+
52
+ detections = []
53
+ for _, row in r.iterrows():
54
+ detections.append({
55
+ "label": row["name"],
56
+ "confidence": float(row["confidence"]),
57
+ "coordinates": [
58
+ round(float(row["xmin"]), 2),
59
+ round(float(row["ymin"]), 2),
60
+ round(float(row["xmax"]), 2),
61
+ round(float(row["ymax"]), 2),
62
+ ]
63
+ })
64
+
65
+ return {"detections": detections}
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ torch==2.1.2
2
+ torchvision==0.16.2
3
+ ultralytics==8.1.0
4
+ pillow==10.3.0
5
+ pandas==2.2.2
6
+ numpy==1.26.4