Upload handler.py
Browse files- handler.py +43 -33
handler.py
CHANGED
|
@@ -6,58 +6,68 @@ import cv2
|
|
| 6 |
import base64
|
| 7 |
from inference_sdk import InferenceHTTPClient
|
| 8 |
|
| 9 |
-
|
| 10 |
-
|
|
|
|
| 11 |
pass
|
| 12 |
-
|
| 13 |
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 14 |
inputs = data.get("inputs")
|
| 15 |
isurl = inputs.get("isurl")
|
| 16 |
path = inputs.get("path")
|
| 17 |
key = inputs.get("key")
|
| 18 |
-
########################### Load Image #################################
|
| 19 |
-
CLIENT = InferenceHTTPClient(
|
| 20 |
-
|
| 21 |
-
api_key=key
|
| 22 |
-
)
|
| 23 |
-
if(isurl): # for url set isurl = 1
|
| 24 |
req = urllib.request.urlopen(path)
|
| 25 |
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
|
| 26 |
-
img = cv2.imdecode(arr, -1)
|
| 27 |
-
else:
|
| 28 |
img = cv2.imread(path)
|
| 29 |
-
###########################################################################
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
#
|
| 34 |
-
# can try:
|
| 35 |
# clothing-segmentation-dataset/1
|
| 36 |
# t-shirts-detector/1
|
| 37 |
# mainmodel/2
|
| 38 |
result = CLIENT.infer(path, model_id="mainmodel/2")
|
| 39 |
detections = sv.Detections.from_inference(result)
|
| 40 |
# print(detections)
|
| 41 |
-
###########################################################################
|
| 42 |
-
|
| 43 |
|
| 44 |
-
########################### Data proccessing #################################
|
| 45 |
# only pass the first detection
|
| 46 |
# change 1 -> to len(detections.xyxy) to pass all photos
|
| 47 |
-
if
|
| 48 |
return "Not Found"
|
| 49 |
else:
|
| 50 |
-
x1, y1, x2, y2 =
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
# create base 64 object
|
| 54 |
-
jpg_as_text = base64.b64encode(buffer)
|
| 55 |
-
###########################################################################
|
| 56 |
return jpg_as_text
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
#
|
| 63 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import base64
|
| 7 |
from inference_sdk import InferenceHTTPClient
|
| 8 |
|
| 9 |
+
|
| 10 |
+
class EndpointHandler:
|
| 11 |
+
def __init__(self): # pass api key to model
|
| 12 |
pass
|
| 13 |
+
|
| 14 |
def __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:
|
| 15 |
inputs = data.get("inputs")
|
| 16 |
isurl = inputs.get("isurl")
|
| 17 |
path = inputs.get("path")
|
| 18 |
key = inputs.get("key")
|
| 19 |
+
########################### Load Image #################################
|
| 20 |
+
CLIENT = InferenceHTTPClient(api_url="https://detect.roboflow.com", api_key=key)
|
| 21 |
+
if isurl: # for url set isurl = 1
|
|
|
|
|
|
|
|
|
|
| 22 |
req = urllib.request.urlopen(path)
|
| 23 |
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
|
| 24 |
+
img = cv2.imdecode(arr, -1) # 'Load it as it is'
|
| 25 |
+
else: # for image file
|
| 26 |
img = cv2.imread(path)
|
| 27 |
+
###########################################################################
|
| 28 |
+
|
| 29 |
+
########################### Model Detection #################################
|
| 30 |
+
# change model_id to use a different model
|
| 31 |
+
# can try:
|
|
|
|
| 32 |
# clothing-segmentation-dataset/1
|
| 33 |
# t-shirts-detector/1
|
| 34 |
# mainmodel/2
|
| 35 |
result = CLIENT.infer(path, model_id="mainmodel/2")
|
| 36 |
detections = sv.Detections.from_inference(result)
|
| 37 |
# print(detections)
|
| 38 |
+
###########################################################################
|
|
|
|
| 39 |
|
| 40 |
+
########################### Data proccessing #################################
|
| 41 |
# only pass the first detection
|
| 42 |
# change 1 -> to len(detections.xyxy) to pass all photos
|
| 43 |
+
if detections.confidence.size == 0:
|
| 44 |
return "Not Found"
|
| 45 |
else:
|
| 46 |
+
x1, y1, x2, y2 = (
|
| 47 |
+
int(detections.xyxy[0][0]),
|
| 48 |
+
int(detections.xyxy[0][1]),
|
| 49 |
+
int(detections.xyxy[0][2]),
|
| 50 |
+
int(detections.xyxy[0][3]),
|
| 51 |
+
)
|
| 52 |
+
clothes = img[y1:y2, x1:x2]
|
| 53 |
+
retval, buffer = cv2.imencode(".jpg", clothes)
|
| 54 |
# create base 64 object
|
| 55 |
+
jpg_as_text = base64.b64encode(buffer) # Decode bytes to string
|
| 56 |
+
###########################################################################
|
| 57 |
return jpg_as_text
|
| 58 |
+
|
| 59 |
+
|
| 60 |
+
###########################################################################
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
# data = {
|
| 64 |
+
# "inputs": {
|
| 65 |
+
# "isurl": True,
|
| 66 |
+
# "path": "http://192.168.10.20/cam-hi.jpg",
|
| 67 |
+
# "key": "iJuYzEzNEFSaQq4e0hfE",
|
| 68 |
+
# }
|
| 69 |
+
# }
|
| 70 |
+
|
| 71 |
+
# # test run
|
| 72 |
+
# Model = EndpointHandler()
|
| 73 |
+
# print(Model(data))
|