Practica2 / app.py
el-filatova's picture
Update app.py
31ec1f8 verified
raw
history blame
1.07 kB
from huggingface_hub import from_pretrained_fastai
import gradio as gr
from fastai.vision.all import *
from PIL import ImageFile
from icevision.all import *
# repo_id = "YOUR_USERNAME/YOUR_LEARNER_NAME"
repo_id = "el-filatova/Practica2"
learner = from_pretrained_fastai(repo_id)
labels = learner.dls.vocab
class_map = ClassMap(['kangaroo'])
state_dict = torch.load('fasterRCNNFkangaroo.pth')
model = models.torchvision.faster_rcnn.model(num_classes=len(class_map))
model.load_state_dict(state_dict)
# Definimos una función que se encarga de llevar a cabo las predicciones
def predict(img):
img = PILImage.create(img)
infer_tfms = tfms.A.Adapter([*tfms.A.resize_and_pad(size),tfms.A.Normalize()])
pred_dict = models.torchvision.faster_rcnn.end2end_detect(img, infer_tfms, model.to("cpu"), class_map=class_map, detection_threshold=0.5)
return pred_dict
# Creamos la interfaz y la lanzamos.
gr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(128, 128)), outputs=gr.outputs.Label(num_top_classes=3),examples=['image.jpg']).launch(share=False)