Skip to content

Commit

Permalink
fixes #51: Change model_utilitary_function to model_request module an…
Browse files Browse the repository at this point in the history
…d update app.py with new imports and function calls
  • Loading branch information
Maxence Guindon committed Feb 14, 2024
1 parent 560e50f commit 559c66b
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 102 deletions.
10 changes: 5 additions & 5 deletions app.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import time
import azure_storage.azure_storage_api as azure_storage_api
import model_inference.inference as inference
import model_utilitary_functions.model_UTILS as utils
import model_request.model_request as req
from custom_exceptions import (
DeleteDirectoryRequestError,
ListDirectoriesRequestError,
Expand Down Expand Up @@ -37,8 +37,8 @@
# The following tuples will be used to store the endpoints and their respective utilitary functions
tuple_endpoints = (
((endpoint_url, endpoint_api_key, "m-14of15seeds-6seedsmag", None),)
,((sd_endpoint, sd_api_key, "seed-detector-1", utils.image_slicing),
(swin_endpoint, swin_api_key, "swinv1-base-dataaugv2-1", utils.swin_result_parser))
,((sd_endpoint, sd_api_key, "seed-detector-1", inference.image_slicing),
(swin_endpoint, swin_api_key, "swinv1-base-dataaugv2-1", inference.swin_result_parser))
)

CACHE = {
Expand Down Expand Up @@ -201,13 +201,13 @@ async def inference_request():
if isinstance(image_bytes, list):
result_json = []
for img in image_bytes:
req = await utils.request_factory(img, endpoint_url, endpoint_api_key, model_name)
req = await req.request_factory(img, endpoint_url, endpoint_api_key, model_name)
response = urllib.request.urlopen(req)
result = response.read()
result_json.append(json.loads(result.decode("utf-8")))

elif isinstance(image_bytes, str):
req = await utils.request_factory(image_bytes, endpoint_url, endpoint_api_key, model_name)
req = await req.request_factory(image_bytes, endpoint_url, endpoint_api_key, model_name)
response = urllib.request.urlopen(req)
result = response.read()
result_json = json.loads(result.decode("utf-8"))
Expand Down
62 changes: 62 additions & 0 deletions model_inference/inference.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,68 @@
import numpy as np
import io
import base64
from PIL import Image
from custom_exceptions import ProcessInferenceResultError

async def image_slicing(image_bytes: bytes, result_json: dict) -> list:
"""
This function takes the image bytes and the result_json from the model and
returns a list of cropped images.
The result_json is expected to be in the following format:
{
"boxes": [
{
"box": {
"topX": 0.0,
"topY": 0.0,
"bottomX": 0.0,
"bottomY": 0.0
},
"label": "string",
"score": 0.0
}
],
}
"""
boxes = result_json[0]['boxes']
image_io_byte = io.BytesIO(base64.b64decode(image_bytes))
image_io_byte.seek(0)
image = Image.open(image_io_byte)

format = image.format

cropped_images = [bytes(0) for _ in boxes]

for i, box in enumerate(boxes):
topX = int(box['box']['topX'] * image.width)
topY = int(box['box']['topY'] * image.height)
bottomX = int(box['box']['bottomX'] * image.width)
bottomY = int(box['box']['bottomY'] * image.height)

img = image.crop((topX, topY, bottomX, bottomY))

buffered = io.BytesIO()
img.save(buffered, format)

cropped_images[i] = base64.b64encode(buffered.getvalue()) #.decode("utf8")

return cropped_images

async def swin_result_parser(img_box:dict, results: dict) -> list:
"""
Args:
img_box (dict): The image box containing the bounding boxes and labels.
results (dict): The results from the model containing the detected seeds.
Returns:
list: The updated image box with modified labels and scores.
"""
for i, result in enumerate(results):
img_box[0]['boxes'][i]['label'] = result[0].get("label")
img_box[0]['boxes'][i]['score'] = result[0].get("score")
img_box[0]['boxes'][i]["all_result"] = [d for d in result]

return img_box

async def process_inference_results(data, imageDims):
"""
Expand Down
Empty file added model_request/__init__.py
Empty file.
32 changes: 32 additions & 0 deletions model_request/model_request.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import json
from urllib.request import Request

async def request_factory(img_bytes: str | bytes, endpoint_url: str, api_key: str, model_name: str) -> Request:
"""
Args:
img_bytes (str | bytes): The image data as either a string or bytes.
endpoint_url (str): The URL of the AI model endpoint.
api_key (str): The API key for accessing the AI model.
Returns:
Request: The request object for calling the AI model.
"""
headers = {
"Content-Type": "application/json",
"Authorization": ("Bearer " + api_key),
"azureml-model-deployment": model_name,
}

if isinstance(img_bytes, str):
data = {
"input_data": {
"columns": ["image"],
"index": [0],
"data": [img_bytes],
}
}
body = str.encode(json.dumps(data))
elif isinstance(img_bytes, bytes):
body = img_bytes

return Request(endpoint_url, body, headers)
97 changes: 0 additions & 97 deletions model_utilitary_functions/model_UTILS.py

This file was deleted.

0 comments on commit 559c66b

Please sign in to comment.