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 22503193122345767891110111213141516131718192021181920212223222324252627282930262731323334353629303132333738394041424344364538394046474849505152535455565742585960446162634748646566515253545556575867686970716061626364727374757677787980818283848586876869707172737488767778from typing import Dict, List, Anyimport supervision as svimport urllib.requestimport numpy as npimport cv2import base64from inference_sdk import InferenceHTTPClientfrom ultralytics import YOLO
class EndpointHandler: def __init__(self, key): # pass api key to model def __init__(self): #pass api key to model # self.CLIENT = InferenceHTTPClient( # api_url="https://detect.roboflow.com", # api_key=key # ) # print("checkpoint 1") pass
def __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 = 1 model = YOLO("./best.pt")########################### Load Image ################################# if(isurl): # for url set isurl = 1 print("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 file img = cv2.imdecode(arr, -1) # 'Load it as it is' else: # for image file img = cv2.imread(path) print("checkpoint 2")########################################################################### ###########################################################################
########################### Model Detection ################################# # change model_id to use a different model # can try: ########################### 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 # clothing-detection-s4ioc/6 # mainmodel/2 print("start model detection") result = CLIENT.infer(path, model_id="clothing-detection-s4ioc/6") detections = sv.Detections.from_inference(result) #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 ############################################################ Data proccessing ################################# # only pass the first detection # change 1 -> to len(detections.xyxy) to pass all photos print("Data processing") if detections.confidence.size == 0: if(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) 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 object jpg_as_text = base64.b64encode(buffer).decode("utf-8") # Decode bytes to string ########################################################################### return jpg_as_text
jpg_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))
# data = {# "inputs": {# "isurl": True,# "path": "http://192.168.10.20/cam-hi.jpg",# "key": "iJuYzEzNEFSaQq4e0hfE",# }# }
# # test run# Model = EndpointHandler()# print(Model(data))11243supervisionnumpyopencv-pythoninference_sdkultralyticsbest.ptAdded+3
handler.py+58-48
requirements.txt+1-2