Update app.py
Browse files
app.py
CHANGED
@@ -2,50 +2,24 @@ from huggingface_hub import from_pretrained_fastai
|
|
2 |
import gradio as gr
|
3 |
from fastai.vision.all import *
|
4 |
from icevision.all import *
|
|
|
5 |
import PIL
|
6 |
|
7 |
-
|
|
|
|
|
8 |
|
9 |
-
|
10 |
-
|
11 |
|
12 |
-
|
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 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
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)
|
|
|
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 |
+
checkpoint_path = "fasterRCNN_resnet18_Raccoons.pth"
|
9 |
+
model = models.torchvision.faster_rcnn.model(backbone=models.torchvision.faster_rcnn.backbones.resnet18_fpn(pretrained=True),
|
10 |
+
num_classes=1)
|
11 |
|
12 |
+
state_dict = torch.load(checkpoint_path, map_location=torch.device('cpu'))
|
13 |
+
model.load_state_dict(state_dict)
|
14 |
|
15 |
+
infer_tfms = tfms.A.Adapter([*tfms.A.resize_and_pad(384),tfms.A.Normalize()])
|
|
|
|
|
|
|
16 |
|
17 |
+
# Definimos una función que se encarga de llevar a cabo las predicciones
|
18 |
+
def predict(img):
|
19 |
+
img = PIL.Image.fromarray(img, "RGB")
|
20 |
+
pred_dict = models.torchvision.faster_rcnn.end2end_detect(img, infer_tfms, model.to("cpu"), class_map=ClassMap(['raccoon']), detection_threshold=0.5)
|
21 |
+
return pred_dict["img"]
|
22 |
+
|
23 |
+
# Creamos la interfaz y la lanzamos.
|
24 |
+
gr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(128, 128)), outputs=[gr.outputs.Image(type="pil", label="VFNet Inference")],
|
25 |
+
examples=['raccoon1.jpg','raccoon2.jpg']).launch(share=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|