upload image preprocessor
Browse files- clothes_detect.py +57 -0
- requirements.txt +6 -0
clothes_detect.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import supervision as sv
|
| 2 |
+
import urllib.request
|
| 3 |
+
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
+
import base64
|
| 6 |
+
from inference_sdk import InferenceHTTPClient
|
| 7 |
+
|
| 8 |
+
class Image_detect:
|
| 9 |
+
def __init__(self, key): #pass api key to model
|
| 10 |
+
self.CLIENT = InferenceHTTPClient(
|
| 11 |
+
api_url="https://detect.roboflow.com",
|
| 12 |
+
api_key=key
|
| 13 |
+
)
|
| 14 |
+
|
| 15 |
+
def __call__(self, path , isurl):
|
| 16 |
+
########################### Load Image #################################
|
| 17 |
+
if(isurl): # for url set isurl = 1
|
| 18 |
+
req = urllib.request.urlopen(path)
|
| 19 |
+
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
|
| 20 |
+
img = cv2.imdecode(arr, -1) # 'Load it as it is'
|
| 21 |
+
else: # for image file
|
| 22 |
+
img = cv2.imread(path)
|
| 23 |
+
###########################################################################
|
| 24 |
+
|
| 25 |
+
|
| 26 |
+
########################### Model Detection #################################
|
| 27 |
+
# change model_id to use a different model
|
| 28 |
+
# can try:
|
| 29 |
+
# clothing-segmentation-dataset/1
|
| 30 |
+
# t-shirts-detector/1
|
| 31 |
+
# mainmodel/2
|
| 32 |
+
result = self.CLIENT.infer(path, model_id="mainmodel/2")
|
| 33 |
+
detections = sv.Detections.from_inference(result)
|
| 34 |
+
# print(detections)
|
| 35 |
+
###########################################################################
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
########################### Data proccessing #################################
|
| 39 |
+
# only pass the first detection
|
| 40 |
+
# change 1 -> to len(detections.xyxy) to pass all photos
|
| 41 |
+
if(detections.confidence.size == 0):
|
| 42 |
+
return "Not Found"
|
| 43 |
+
else:
|
| 44 |
+
x1, y1, x2, y2 = int(detections.xyxy[0][0]), int(detections.xyxy[0][1]), int(detections.xyxy[0][2]), int(detections.xyxy[0][3])
|
| 45 |
+
clothes = img[y1: y2, x1: x2]
|
| 46 |
+
retval , buffer = cv2.imencode('.jpg', clothes)
|
| 47 |
+
# create base 64 object
|
| 48 |
+
jpg_as_text = base64.b64encode(buffer)
|
| 49 |
+
###########################################################################
|
| 50 |
+
return jpg_as_text
|
| 51 |
+
###########################################################################
|
| 52 |
+
|
| 53 |
+
|
| 54 |
+
|
| 55 |
+
# test run
|
| 56 |
+
# Model = Image_detect("api key")
|
| 57 |
+
# print(Model("test_images/test5.jpg", 0))
|
requirements.txt
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
supervision
|
| 2 |
+
urllib.request
|
| 3 |
+
numpy
|
| 4 |
+
cv2
|
| 5 |
+
base64
|
| 6 |
+
inference_sdk
|