Skip to content

Commit

Permalink
added roboflow example
Browse files Browse the repository at this point in the history
  • Loading branch information
robertgshaw committed Jan 7, 2023
1 parent 886e27d commit f37178b
Show file tree
Hide file tree
Showing 20 changed files with 2,046 additions and 0 deletions.
510 changes: 510 additions & 0 deletions examples/roboflow-logging/README.md

Large diffs are not rendered by default.

42 changes: 42 additions & 0 deletions examples/roboflow-logging/client-multiple-endpoints.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import cv2, requests, json, argparse
import matplotlib.pyplot as plt

parser = argparse.ArgumentParser(description="Pass a name to a file to be annotated")
parser.add_argument('--image_path', default="test/images/a9f16c_2_9_png.rf.c048a60764e56735d7465cdec974d102.jpg")

MODEL_IMG_SIZE = 640
ENDPOINT_URL_FINETUNED = "http://localhost:5543/yolov5-s-finetuned/predict/from_files"
ENDPOINT_URL_COCO = "http://localhost:5543/yolov5-s-coco/predict/from_files"

def main(image_path):
for endpoint_url in [ENDPOINT_URL_FINETUNED, ENDPOINT_URL_COCO]:
im = cv2.imread(image_path)
im_size = im.shape[:2]
assert im_size[0] == im_size[1]
scale_ratio = im_size[0] / MODEL_IMG_SIZE

resp = requests.post(
url=endpoint_url,
files=[('request', open(image_path, 'rb'))]
)

boxes = json.loads(resp.text)['boxes'][0]
for xmin, ymin, xmax, ymax in boxes:
start_point = (int(xmin * scale_ratio), int(ymin * scale_ratio))
end_point = (int(xmax * scale_ratio), int(ymax * scale_ratio))
color = (0, 255, 0)
thickness = 2
im = cv2.rectangle(im, start_point, end_point, color, thickness)

plt.figure(figsize=(15, 15))
plt.axis("off")
plt.imshow(im)

if endpoint_url == ENDPOINT_URL_FINETUNED:
plt.savefig("annotated-finetuned.png")
else:
plt.savefig("annotated-coco.png")

if __name__ == '__main__':
args = parser.parse_args()
main(args.image_path)
27 changes: 27 additions & 0 deletions examples/roboflow-logging/client-send-all.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import requests, os, time

# the dataset we downloaded had 3 subsets in 3 folders
paths = []
for folder_name in ['test', 'valid', 'train']:
path = f"{folder_name}/images/"
paths += [path + img_name for img_name in os.listdir(path)]


# same URL for the endpoint as before
ENDPOINT_URL = "http://localhost:5543/yolov5-s-coco/predict/from_files"


# send each image to the endpoint
i = 0
print(f"Sending {len(paths)} images to the server")
for image_path in paths:
if i % 60 == 0:
print(i)
i+=1

resp = requests.post(
url=ENDPOINT_URL,
files=[('request', open(image_path, 'rb'))]
)
print(resp)
time.sleep(1)
11 changes: 11 additions & 0 deletions examples/roboflow-logging/client-send-one.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import requests, json

ENDPOINT_URL = "http://localhost:5543/yolov5-s-coco/predict/from_files"
IMAGE_PATH = "test/images/4b770a_3_6_png.rf.f5d975605c1f73e1a95a1d8edc4ce5b1.jpg"

resp = requests.post(
url=ENDPOINT_URL,
files=[('request', open(IMAGE_PATH, 'rb'))]
)

print(json.loads(resp.text))
1,399 changes: 1,399 additions & 0 deletions examples/roboflow-logging/example-client.ipynb

Large diffs are not rendered by default.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/roboflow-logging/images/labeling.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions examples/roboflow-logging/server/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
deepsparse[server,yolo]
roboflow
28 changes: 28 additions & 0 deletions examples/roboflow-logging/server/roboflow-logger.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from deepsparse.loggers import BaseLogger, MetricCategories
import typing, PIL, io, requests, datetime
from requests_toolbelt.multipart.encoder import MultipartEncoder

class RoboflowLogger(BaseLogger):

# the arguments to the construction will be defined in the config file
def __init__(self, dataset_name: str, api_key: str):
# per Roboflow docs
self.upload_url = f"https://api.roboflow.com/dataset/{dataset_name}/upload?api_key={api_key}"
super(RoboflowLogger, self).__init__()

# this function will be called from DeepSparse Server, based on the config
def log(self, identifier: str, value: typing.Any, category: typing.Optional[str]=None):
if category == MetricCategories.DATA:
# unpacks value and converts to image in a buffer
img = PIL.Image.fromarray(value.images[0], mode="RGB")
buffered = io.BytesIO()
img.save(buffered, quality=90, format="JPEG")

# packs as multipart
img_name = f"production-image-{datetime.datetime.now()}.jpg"
m = MultipartEncoder(fields={'file': (img_name, buffered.getvalue(), "image/jpeg")})

# uploads to roboflow
r = requests.post(self.upload_url, data=m, headers={'Content-Type': m.content_type})

print("request_complete")
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## server-config-no-roboflow-logging.yaml
loggers:
python:

endpoints:
- task: yolo
model: zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/pruned_quant-aggressive_94
name: yolov5-s-coco
route: /yolov5-s-coco/predict
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# server-config-roboflow-logging.yaml
loggers:
roboflow_logger:
path: server/roboflow-logger.py:RoboflowLogger
api_key: YOUR_API_KEY
dataset_name: YOUR_DATASET_NAME

endpoints:
- task: yolo
model: zoo:cv/detection/yolov5-s/pytorch/ultralytics/coco/pruned_quant-aggressive_94
name: yolov5-s-coco
route: /yolov5-s-coco/predict
data_logging:
pipeline_inputs:
- func: identity
frequency: 1
target_loggers:
- roboflow_logger

0 comments on commit f37178b

Please sign in to comment.