Update handler.py
Browse files- 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 |
-
|
| 6 |
-
|
| 7 |
from io import BytesIO
|
| 8 |
|
| 9 |
class EndpointHandler():
|
|
@@ -42,12 +42,16 @@ class EndpointHandler():
|
|
| 42 |
image_path = event["inputs"]
|
| 43 |
|
| 44 |
try:
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 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),
|