Skip to content

Instantly share code, notes, and snippets.

@pietz
Created June 4, 2023 15:52
Show Gist options
  • Save pietz/43033547368ec801a151e803dadb1410 to your computer and use it in GitHub Desktop.
Save pietz/43033547368ec801a151e803dadb1410 to your computer and use it in GitHub Desktop.
Azure Function + FastAPI + ONNX
# __init__.py
# import logging
# import azure.functions as func
# from main.app import app
# def main(req: func.HttpRequest, context: func.Context) -> func.HttpResponse:
# logging.info("Python HTTP trigger function processed a request.")
# return func.AsgiMiddleware(app).handle(req, context)
import os
import logging
import nest_asyncio
import onnxruntime
from fastapi import FastAPI, UploadFile
max_size = 1152
root = os.path.dirname(os.path.abspath(__file__))
model_path = os.path.join(root, "GL_VGG19_QNRF.onnx")
nest_asyncio.apply()
app = FastAPI()
@app.get("/health")
async def health():
return {"info": "Up and running!"}
@app.post("/count")
async def count(file: UploadFile):
logging.info("Converting to PIL...")
img = Image.open(io.BytesIO(await file.read())).convert("RGB")
img = ImageOps.exif_transpose(img)
logging.info("Image size: "+str(img.size))
if max_size is not None and max(img.size) > max_size:
logging.info("Resizing image...")
img.thumbnail((max_size, max_size))
logging.info("Converting to Numpy...")
img = np.array(img, dtype=np.float32) / 255.0
logging.info("Normalizing image...")
img -= np.array([[[0.485, 0.456, 0.406]]])
img /= np.array([[[0.229, 0.224, 0.225]]])
logging.info("Arranging to BCHW...")
img = img.transpose((2, 0, 1))[None]
logging.info("Running inference...")
rt = onnxruntime.InferenceSession(model_path, None)
mask = rt.run(None, {'input': img})[0][0,0]
count = int(round(mask.sum()))
logging.info("Counted {} objects.".format(count))
return {"count": count}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment