from huggingface_hub import from_pretrained_fastai import gradio as gr from fastai.vision.all import * from icevision.all import * model1 = models.torchvision.faster_rcnn.model(backbone=models.torchvision.faster_rcnn.backbones.resnet18_fpn, num_classes=len(class_map)) state_dict = torch.load('fasterRCNN_resnet18_Raccoons.pth') 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=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") # Option 1: Get an image from local drive gr_interface = gr.Interface(fn=show_preds, inputs=["image", display_chkbox_label, display_chkbox_box, detection_threshold_slider], outputs=outputs, examples=['raccoon1.jpg','raccoon2.jpg']) # # Option 2: Grab an image from a webcam # gr_interface = gr.Interface(fn=show_preds, inputs=["webcam", display_chkbox_label, display_chkbox_box, detection_threshold_slider], outputs=outputs, title='IceApp - COCO', live=False) # # Option 3: Continuous image stream from the webcam # gr_interface = gr.Interface(fn=show_preds, inputs=["webcam", display_chkbox_label, display_chkbox_box, detection_threshold_slider], outputs=outputs, title='IceApp - COCO', live=True) gr_interface.launch(inline=False, share=False, debug=True)