Drazcat-AI commited on
Commit
2cf474c
·
verified ·
1 Parent(s): e4d7728

Upload 3 files

Browse files
cecinas_v3-16/runs/detect/train/weights/best.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:a2152a4cc64980610d5612914c97ec8317bab751fdacab036533c1291ef094b7
3
+ size 351206650
handler.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from huggingface_hub import hf_hub_download
2
+ from typing import Dict, List, Any
3
+ from ultralytics import YOLO
4
+ import json
5
+ import urllib.request
6
+ import cv2
7
+ from io import BytesIO
8
+ import numpy as np
9
+ class EndpointHandler():
10
+ def __init__(self, path=""):
11
+ hf_hub_download(repo_id="Drazcat-AI/cecinas", filename="cecinas_v3-16/runs/detect/train/weights/best.pt")
12
+ self.model = YOLO(hf_hub_download(repo_id="Drazcat-AI/cecinas", filename="cecinas_v3-16/runs/detect/train/weights/best.pt", local_files_only=True))
13
+
14
+ def predict_objects(self, image_path, image_size_m):
15
+ results = self.model(image_path, imgsz=[1280, 960])
16
+ predictions = []
17
+ for box in results[0].boxes:
18
+ class_id = results[0].names[box.cls[0].item()]
19
+ cords = box.xywh[0].tolist()
20
+
21
+ conf = box.conf[0].item()
22
+ prediction = {
23
+ "x": round(cords[0]*image_size_m[0]),
24
+ "y": round(cords[1]*image_size_m[1]),
25
+ "width": round(cords[2]*image_size_m[0]),
26
+ "height": round(cords[3]*image_size_m[1]),
27
+ "confidence": conf,
28
+ "class": class_id
29
+ }
30
+ predictions.append(prediction)
31
+ predictions_array = {"predictions": predictions}
32
+
33
+ return predictions_array
34
+
35
+ def __call__(self, event):
36
+ if "inputs" not in event:
37
+ return {
38
+ "statusCode": 400,
39
+ "body": json.dumps("Error: Please provide an 'inputs' parameter."),
40
+ }
41
+
42
+ image_path = event["inputs"]
43
+
44
+ try:
45
+ with urllib.request.urlopen(image_path) as response:
46
+ image_content = np.asarray(bytearray(response.read()), dtype=np.uint8)
47
+ image = cv2.imdecode(image_content, cv2.IMREAD_COLOR)
48
+
49
+ predictions = self.predict_objects(image, (1,1))
50
+ return {
51
+ "statusCode": 200,
52
+ "body": json.dumps(predictions),
53
+ }
54
+ except Exception as e:
55
+ return {
56
+ "statusCode": 500,
57
+ "body": json.dumps(f"Error: {str(e)}"),
58
+ }
requirements.txt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ ultralytics==8.3.61
2
+ opencv-python
3
+ numpy