Upload 3 files
Browse filesFiles changed (3)
- best.ptAdded+3
- handler.py+58-48
- requirements.txt+1-2
123version https://git-lfs.github.com/spec/v1oid sha256:d025427b1b29ea551bf60c80d148f93dac0d121feaa46bc9d534a781e1c3cffbsize 22503193123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778from typing import Dict, List, Anyimport supervision as svimport urllib.requestimport numpy as npimport cv2import 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 #################################print("load image")CLIENT = InferenceHTTPClient(api_url="https://detect.roboflow.com", api_key=key)print("get client")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# clothing-detection-s4ioc/6# mainmodel/2print("start model detection")result = CLIENT.infer(path, model_id="clothing-detection-s4ioc/6")detections = sv.Detections.from_inference(result)# print(detections)###################################################################################################### Data proccessing ################################## only pass the first detection# change 1 -> to len(detections.xyxy) to pass all photosprint("Data processing")if 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("utf-8") # 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))12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788from typing import Dict, List, Anyimport urllib.requestimport numpy as npimport cv2import base64from ultralytics import YOLOclass EndpointHandler:def __init__(self): #pass api key to model# self.CLIENT = InferenceHTTPClient(# api_url="https://detect.roboflow.com",# api_key=key# )# print("checkpoint 1")passdef __call__(self, data: Dict[str, Any]) -> List[Dict[str, Any]]:inputs = data.get("inputs")isurl = inputs.get("isurl")path = inputs.get("path")model = YOLO("./best.pt")########################### Load Image #################################if(isurl): # for url set isurl = 1print("checkpoint 2-1")req = urllib.request.urlopen(path)print("checkpoint 2-2")arr = np.asarray(bytearray(req.read()), dtype=np.uint8)print("checkpoint 2-3")img = cv2.imdecode(arr, -1) # 'Load it as it is'else: # for image fileimg = cv2.imread(path)print("checkpoint 2")###################################################################################################### Model Detection ################################## change model_id to use a different model# can try:# clothing-detection-s4ioc/6 //good# clothing-segmentation-dataset/1# t-shirts-detector/1# mainmodel/2#result = self.CLIENT.infer(path, model_id="mainmodel/2")result = model(img)#annotated_frame = result[0].plot()detections = result[0].boxes#print(result[0].boxes.xyxy)#cv2.imshow("YOLOv8 Inference", annotated_frame)# print(result)#cv2.waitKey(0)#detections = sv.Detections.from_inference(result)# print(detections)print("checkpoint 3")###################################################################################################### Data proccessing ################################## only pass the first detection# change 1 -> to len(detections.xyxy) to pass all photosif(detections.xyxy.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)cv2.imwrite("result.jpg", clothes)# create base 64 objectjpg_as_text = base64.b64encode(buffer)print("checkpoint 4")###########################################################################return jpg_as_text############################################################################ test run# Model = Image_detect()# test file image# print(Model("test_images/test6.jpg", 0))#test url# print(Model("http://10.10.2.100/cam-lo.jpg", 1))
1234supervisionnumpyopencv-pythoninference_sdk123numpyopencv-pythonultralytics
best.ptAdded+3
handler.py+58-48
requirements.txt+1-2