Drazcat-AI commited on
Commit
32c088f
·
verified ·
1 Parent(s): 2d78362

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +13 -15
handler.py CHANGED
@@ -11,21 +11,19 @@ class EndpointHandler():
11
  hf_hub_download(repo_id="Drazcat-AI/flejes", filename="yolov8_flejes/runs/detect/train/weights/best.pt")
12
  self.model = YOLO(hf_hub_download(repo_id="Drazcat-AI/flejes", filename="yolov8_flejes/runs/detect/train/weights/best.pt", local_files_only=True))
13
 
14
- def predict_objects(self, image_path):
15
- results = self.model(image_path, imgsz=1280)
16
- #results = self.model(image_path, imgsz=800)
17
- #results = self.model(image_path)
18
  predictions = []
19
  for box in results[0].boxes:
20
  class_id = results[0].names[box.cls[0].item()]
21
  cords = box.xywh[0].tolist()
22
- cords = [round(x) for x in cords]
23
  conf = box.conf[0].item()
24
  prediction = {
25
- "x": cords[0],
26
- "y": cords[1],
27
- "width": cords[2],
28
- "height": cords[3],
29
  "confidence": conf,
30
  "class": class_id
31
  }
@@ -44,12 +42,12 @@ class EndpointHandler():
44
  image_path = event["inputs"]
45
 
46
  try:
47
- #url image
48
- #data=urlopen(image_path).read()
49
- #image = Image.open(BytesIO(data))
50
- #bytes image
51
- image=image_path
52
- predictions = self.predict_objects(image)
53
  return {
54
  "statusCode": 200,
55
  "body": json.dumps(predictions),
 
11
  hf_hub_download(repo_id="Drazcat-AI/flejes", filename="yolov8_flejes/runs/detect/train/weights/best.pt")
12
  self.model = YOLO(hf_hub_download(repo_id="Drazcat-AI/flejes", filename="yolov8_flejes/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)
 
 
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
+ #cords = [round(x) for x in cords]
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
  }
 
42
  image_path = event["inputs"]
43
 
44
  try:
45
+ data=urlopen(image_path).read()
46
+ image = Image.open(BytesIO(data))
47
+ image_size = image.size
48
+ image = image.resize([1280,960])
49
+ predictions = self.predict_objects(image, [image_size[0]/1280,image_size[1]/960])
50
+
51
  return {
52
  "statusCode": 200,
53
  "body": json.dumps(predictions),