magarcd commited on
Commit
732ad15
·
verified ·
1 Parent(s): 0b47711

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +43 -0
app.py ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ 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
+ class_map = ClassMap(['apple','banana','orange'])
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('fasterRCNNFruits.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=ClassMap(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
+ gr_interface = gr.Interface(fn=show_preds, inputs=["image", display_chkbox_label, display_chkbox_box, detection_threshold_slider], outputs=outputs, examples=[['mixed_24.jpg', True, True, 0.5], ['mixed_24.jpg', True, True, 0.5]])
42
+
43
+ gr_interface.launch(inline=False, share=False, debug=True)