Upload handler.py
Browse files5 unmodified lines67891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162635 unmodified linesimport base64from inference_sdk import InferenceHTTPClientclass EndpointHandler():def __init__(self, key): #pass api key to modelpassdef __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:inputs = data.get("inputs")isurl = inputs.get("isurl")path = inputs.get("path")key = inputs.get("key")########################### Load Image #################################CLIENT = InferenceHTTPClient(api_url="https://detect.roboflow.com",api_key=key)if(isurl): # for url set isurl = 1req = urllib.request.urlopen(path)arr = np.asarray(bytearray(req.read()), dtype=np.uint8)img = cv2.imdecode(arr, -1) # 'Load it as it is'else: # for image fileimg = cv2.imread(path)###################################################################################################### Model Detection ################################## change model_id to use a different model# can try:# clothing-segmentation-dataset/1# t-shirts-detector/1# mainmodel/2result = CLIENT.infer(path, model_id="mainmodel/2")detections = sv.Detections.from_inference(result)# print(detections)###################################################################################################### Data proccessing ################################## only pass the first detection# change 1 -> to len(detections.xyxy) to pass all photosif(detections.confidence.size == 0):return "Not Found"else:x1, y1, x2, y2 = int(detections.xyxy[0][0]), int(detections.xyxy[0][1]), int(detections.xyxy[0][2]), int(detections.xyxy[0][3])clothes = img[y1: y2, x1: x2]retval , buffer = cv2.imencode('.jpg', clothes)# create base 64 objectjpg_as_text = base64.b64encode(buffer)###########################################################################return jpg_as_text############################################################################ test run# Model = Image_detect("api key")# print(Model("test_images/test5.jpg", 0))5 unmodified lines6789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172735 unmodified linesimport base64from inference_sdk import InferenceHTTPClientclass EndpointHandler:def __init__(self): # pass api key to modelpassdef __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:inputs = data.get("inputs")isurl = inputs.get("isurl")path = inputs.get("path")key = inputs.get("key")########################### Load Image #################################CLIENT = InferenceHTTPClient(api_url="https://detect.roboflow.com", api_key=key)if isurl: # for url set isurl = 1req = urllib.request.urlopen(path)arr = np.asarray(bytearray(req.read()), dtype=np.uint8)img = cv2.imdecode(arr, -1) # 'Load it as it is'else: # for image fileimg = cv2.imread(path)###################################################################################################### Model Detection ################################## change model_id to use a different model# can try:# clothing-segmentation-dataset/1# t-shirts-detector/1# mainmodel/2result = CLIENT.infer(path, model_id="mainmodel/2")detections = sv.Detections.from_inference(result)# print(detections)###################################################################################################### Data proccessing ################################## only pass the first detection# change 1 -> to len(detections.xyxy) to pass all photosif detections.confidence.size == 0:return "Not Found"else:x1, y1, x2, y2 = (int(detections.xyxy[0][0]),int(detections.xyxy[0][1]),int(detections.xyxy[0][2]),int(detections.xyxy[0][3]),)clothes = img[y1:y2, x1:x2]retval, buffer = cv2.imencode(".jpg", clothes)# create base 64 objectjpg_as_text = base64.b64encode(buffer) # Decode bytes to string###########################################################################return jpg_as_text############################################################################ data = {# "inputs": {# "isurl": True,# "path": "http://192.168.10.20/cam-hi.jpg",# "key": "iJuYzEzNEFSaQq4e0hfE",# }# }# # test run# Model = EndpointHandler()# print(Model(data))
handler.py+43-33