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

Update handler.py

Browse files
Files changed (1) hide show
  1. handler.py +12 -8
handler.py CHANGED
@@ -2,8 +2,8 @@ from huggingface_hub import hf_hub_download
2
  from typing import Dict, List, Any
3
  from ultralytics import YOLO
4
  import json
5
- from urllib.request import urlopen
6
- from PIL import Image
7
  from io import BytesIO
8
 
9
  class EndpointHandler():
@@ -42,12 +42,16 @@ class EndpointHandler():
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),
 
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
 
9
  class EndpointHandler():
 
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
+ image_size = image.shape
49
+ if image.shape[0]>image.shape[0]:
50
+ x, y = 1280, 960
51
+ else:
52
+ y, x = 1280, 960
53
+ image = cv2.resize(image, (x, y))
54
+ predictions = predict_objects(self, image, [image_size[0]/x,image_size[1]/y])
55
  return {
56
  "statusCode": 200,
57
  "body": json.dumps(predictions),