Upload 3 files
Browse files- cervezas_v2-12/runs/detect/train/weights/best.pt +3 -0
- handler.py +57 -51
- requirements.txt +3 -2
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 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
|
|
| 1 |
+
ultralytics==8.3.61
|
| 2 |
+
opencv-python
|
| 3 |
+
numpy
|