Update app.py
Browse files
app.py
CHANGED
|
@@ -2,24 +2,50 @@ from huggingface_hub import from_pretrained_fastai
|
|
| 2 |
import gradio as gr
|
| 3 |
from fastai.vision.all import *
|
| 4 |
from icevision.all import *
|
| 5 |
-
from icevision.models.checkpoint import *
|
| 6 |
import PIL
|
| 7 |
|
| 8 |
-
|
| 9 |
-
model = models.torchvision.faster_rcnn.model(backbone=models.torchvision.faster_rcnn.backbones.resnet18_fpn(pretrained=True),
|
| 10 |
-
num_classes=2)
|
| 11 |
|
| 12 |
-
|
| 13 |
-
|
| 14 |
|
| 15 |
-
|
|
|
|
|
|
|
|
|
|
| 16 |
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
from fastai.vision.all import *
|
| 4 |
from icevision.all import *
|
|
|
|
| 5 |
import PIL
|
| 6 |
|
| 7 |
+
class_map = ['raccoon']
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
presize = 512
|
| 10 |
+
size = 384
|
| 11 |
|
| 12 |
+
train_tfms = tfms.A.Adapter(
|
| 13 |
+
[*tfms.A.aug_tfms(size=size, presize=presize), tfms.A.Normalize()]
|
| 14 |
+
)
|
| 15 |
+
valid_tfms = tfms.A.Adapter([*tfms.A.resize_and_pad(size), tfms.A.Normalize()])
|
| 16 |
|
| 17 |
+
model1 = models.torchvision.faster_rcnn.model(backbone=models.torchvision.faster_rcnn.backbones.resnet18_fpn(pretrained=True), num_classes=len(class_map))
|
| 18 |
+
state_dict = torch.load('fasterRCNN_resnet18_Raccoons.pth', map_location=torch.device('cpu'))
|
| 19 |
+
model1.load_state_dict(state_dict)
|
| 20 |
+
|
| 21 |
+
def show_preds(input_image, display_label, display_bbox, detection_threshold):
|
| 22 |
+
|
| 23 |
+
if detection_threshold==0: detection_threshold=0.5
|
| 24 |
+
|
| 25 |
+
img = PIL.Image.fromarray(input_image, 'RGB')
|
| 26 |
+
|
| 27 |
+
pred_dict = models.torchvision.faster_rcnn.end2end_detect(img, valid_tfms, model1, class_map=class_map, detection_threshold=detection_threshold,
|
| 28 |
+
display_label=display_label, display_bbox=display_bbox, return_img=True,
|
| 29 |
+
font_size=16, label_color="#FF59D6")
|
| 30 |
+
|
| 31 |
+
return pred_dict['img']
|
| 32 |
+
|
| 33 |
+
# display_chkbox = gr.inputs.CheckboxGroup(["Label", "BBox"], label="Display", default=True)
|
| 34 |
+
display_chkbox_label = gr.inputs.Checkbox(label="Label", default=True)
|
| 35 |
+
display_chkbox_box = gr.inputs.Checkbox(label="Box", default=True)
|
| 36 |
+
|
| 37 |
+
detection_threshold_slider = gr.inputs.Slider(minimum=0, maximum=1, step=0.1, default=0.5, label="Detection Threshold")
|
| 38 |
+
|
| 39 |
+
outputs = gr.outputs.Image(type="pil")
|
| 40 |
+
|
| 41 |
+
# Option 1: Get an image from local drive
|
| 42 |
+
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'])
|
| 43 |
+
|
| 44 |
+
# # Option 2: Grab an image from a webcam
|
| 45 |
+
# 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)
|
| 46 |
+
|
| 47 |
+
# # Option 3: Continuous image stream from the webcam
|
| 48 |
+
# 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)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
gr_interface.launch(inline=False, share=False, debug=True)
|