Drazcat-AI commited on
Commit
dabbccf
·
verified ·
1 Parent(s): 9966503

Upload 3 files

Browse files
cervezas_v2-12/runs/detect/train/weights/best.pt ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:2c3ac5e75e25800ba22b48c1174c3ecffb62404f7b93c58c232fb291f2910e9c
3
+ size 87836713
handler.py CHANGED
@@ -1,52 +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
-
6
- class EndpointHandler():
7
- def __init__(self, path=""):
8
- hf_hub_download(repo_id="Drazcat-AI/cervezas", filename="yolov8_cervezas/runs/detect/train/weights/best.pt")
9
- self.model = YOLO(hf_hub_download(repo_id="Drazcat-AI/cervezas", filename="yolov8_cervezas/runs/detect/train/weights/best.pt", local_files_only=True))
10
-
11
- def predict_objects(self, image_path):
12
- results = self.model(image_path, imgsz=800)
13
- predictions = []
14
- for box in results[0].boxes:
15
- class_id = results[0].names[box.cls[0].item()]
16
- cords = box.xywh[0].tolist()
17
- cords = [round(x) for x in cords]
18
- conf = round(box.conf[0].item(), 2)
19
- prediction = {
20
- "label": class_id,
21
- "score": conf,
22
- "box":{
23
- "x": cords[0],
24
- "y": cords[1],
25
- "width": cords[2],
26
- "height": cords[3]}
27
- }
28
- predictions.append(prediction)
29
- predictions_array = {"predictions": predictions}
30
-
31
- return predictions_array
32
-
33
- def __call__(self, event):
34
- if "inputs" not in event:
35
- return {
36
- "statusCode": 400,
37
- "body": json.dumps("Error: Please provide an 'inputs' parameter."),
38
- }
39
-
40
- image_path = event["inputs"]
41
-
42
- try:
43
- predictions = self.predict_objects(image_path)
44
- return {
45
- "statusCode": 200,
46
- "body": json.dumps(predictions),
47
- }
48
- except Exception as e:
49
- return {
50
- "statusCode": 500,
51
- "body": json.dumps(f"Error: {str(e)}"),
 
 
 
 
 
 
52
  }
 
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/cervezas", filename="cervezas_v2-12/runs/detect/train/weights/best.pt")
12
+ self.model = YOLO(hf_hub_download(repo_id="Drazcat-AI/cervezas", filename="cervezas_v2-12/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 CHANGED
@@ -1,2 +1,3 @@
1
- ultralytics
2
- torchvision==0.12.0
 
 
1
+ ultralytics==8.3.61
2
+ opencv-python
3
+ numpy