Skip to content

Instantly share code, notes, and snippets.

@icebeam7
Last active Oct 21, 2021
Embed
What would you like to do?
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