ogamaniuk commited on
Commit
6322921
·
verified ·
1 Parent(s): 9d865de

Create handler.py

Browse files
Files changed (1) hide show
  1. handler.py +71 -0
handler.py ADDED
@@ -0,0 +1,71 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from typing import Dict, List, Any
2
+ from ultralytics import YOLO
3
+ import base64
4
+ from io import BytesIO
5
+ from PIL import Image
6
+
7
+ class EndpointHandler:
8
+ def __init__(self, path=""):
9
+ # Load the YOLO model
10
+ self.model = YOLO(f"{path}/FFDNet-L.pt")
11
+ self.id_to_cls = {0: "TextBox", 1: "ChoiceButton", 2: "Signature"}
12
+
13
+ def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
14
+ """
15
+ Args:
16
+ data: A dictionary containing:
17
+ - "inputs": base64 encoded image or image URL
18
+ - "parameters": optional dict with confidence, iou, imgsz
19
+ Returns:
20
+ List of predictions with bounding boxes and classes
21
+ """
22
+ # Extract image from request
23
+ inputs = data.pop("inputs", data)
24
+ parameters = data.pop("parameters", {})
25
+
26
+ # Handle image input (base64 or URL)
27
+ if isinstance(inputs, str):
28
+ if inputs.startswith("http"):
29
+ image = inputs
30
+ else:
31
+ # Decode base64
32
+ image_data = base64.b64decode(inputs)
33
+ image = Image.open(BytesIO(image_data))
34
+ else:
35
+ image = inputs
36
+
37
+ # Get parameters with defaults
38
+ confidence = parameters.get("conf", 0.3)
39
+ iou = parameters.get("iou", 0.1)
40
+ imgsz = parameters.get("imgsz", 1600)
41
+ augment = parameters.get("augment", True)
42
+
43
+ # Run inference
44
+ results = self.model.predict(
45
+ image,
46
+ conf=confidence,
47
+ iou=iou,
48
+ imgsz=imgsz,
49
+ augment=augment
50
+ )
51
+
52
+ # Format results
53
+ predictions = []
54
+ for result in results:
55
+ if result.boxes is not None:
56
+ for box in result.boxes.cpu().numpy():
57
+ x, y, w, h = box.xywhn[0]
58
+ cls_id = int(box.cls.item())
59
+
60
+ predictions.append({
61
+ "widget_type": self.id_to_cls[cls_id],
62
+ "confidence": float(box.conf[0]),
63
+ "bounding_box": {
64
+ "cx": float(x),
65
+ "cy": float(y),
66
+ "w": float(w),
67
+ "h": float(h)
68
+ }
69
+ })
70
+
71
+ return predictions