Practica2 / app.py
igmarco's picture
Update app.py
5edad8a
raw
history blame
1.92 kB
from huggingface_hub import from_pretrained_fastai
import gradio as gr
from fastai.vision.all import *
from icevision.all import *
import PIL
class_map = ['raccoon']
presize = 512
size = 384
train_tfms = tfms.A.Adapter(
[*tfms.A.aug_tfms(size=size, presize=presize), tfms.A.Normalize()]
)
valid_tfms = tfms.A.Adapter([*tfms.A.resize_and_pad(size), tfms.A.Normalize()])
model1 = models.torchvision.faster_rcnn.model(backbone=models.torchvision.faster_rcnn.backbones.resnet18_fpn(pretrained=True), num_classes=len(class_map)+1)
state_dict = torch.load('fasterRCNN_resnet18_Raccoons.pth', map_location=torch.device('cpu'))
model1.load_state_dict(state_dict)
def show_preds(input_image, display_label, display_bbox, detection_threshold):
if detection_threshold==0: detection_threshold=0.5
img = PIL.Image.fromarray(input_image, 'RGB')
pred_dict = models.torchvision.faster_rcnn.end2end_detect(img, valid_tfms, model1, class_map=ClassMap(class_map), detection_threshold=detection_threshold,
display_label=display_label, display_bbox=display_bbox, return_img=True,
font_size=16, label_color="#FF59D6")
return pred_dict['img']
# display_chkbox = gr.inputs.CheckboxGroup(["Label", "BBox"], label="Display", default=True)
display_chkbox_label = gr.inputs.Checkbox(label="Label", default=True)
display_chkbox_box = gr.inputs.Checkbox(label="Box", default=True)
detection_threshold_slider = gr.inputs.Slider(minimum=0, maximum=1, step=0.1, default=0.5, label="Detection Threshold")
outputs = gr.outputs.Image(type="pil")
gr_interface = gr.Interface(fn=show_preds, inputs=["image", display_chkbox_label, display_chkbox_box, detection_threshold_slider], outputs=outputs, examples=[['raccoon1.jpg', True, True, 0.5], ['raccoon2.jpg', True, True, 0.5]])
gr_interface.launch(inline=False, share=False, debug=True)