Skip to content

Instantly share code, notes, and snippets.

@icebeam7
Last active October 21, 2021 16:29
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save icebeam7/66dee3a4ef04ba2e64437f4ee65d40f6 to your computer and use it in GitHub Desktop.
import logging
import onnxruntime as nxrun
import numpy as np
import PIL
from PIL import Image
import requests
from io import BytesIO
import azure.functions as func
def main(req: func.HttpRequest) -> func.HttpResponse:
## Obtén la imagen y cambia su tamaño a 244x244 píxeles
url = req.params.get('url')
response = requests.get(url)
image = PIL.Image.open(BytesIO(response.content)).resize([224,224])
## Carga el modelo y puntúa la imagen
model_path = "model/model.onnx"
sess = nxrun.InferenceSession(model_path)
input_array = np.array(image, dtype=np.float32)[np.newaxis, :, :, :]
input_array = input_array.transpose((0, 3, 1, 2))[:, (2, 1, 0), :, :]
input_name = sess.get_inputs()[0].name
outputs = sess.run(None, {input_name: input_array.astype(np.float32)})
## Encuentra la etiqueta con la puntuación más alta
label = outputs[0][0][0]
score = (outputs[1][0][outputs[0][0][0]]*100)
## Devuelve y registra la respuesta
response = f"I'm {score:.2f}% sure I see: {label}"
logging.info(response)
return func.HttpResponse(response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment