dnth commited on
Commit
4fe0d2e
·
1 Parent(s): 614d39d

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +60 -0
app.py ADDED
@@ -0,0 +1,60 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #from icevision.models import *
2
+ from icevision.models.checkpoint import *
3
+ #from icevision.all import *
4
+ from icevision.models import mmdet
5
+ import icedata
6
+ import PIL
7
+ import requests
8
+ import torch
9
+ from torchvision import transforms
10
+ import cv2
11
+ import gradio as gr
12
+
13
+ classes = ['Army_navy', 'Bulldog', 'Castroviejo', 'Forceps', 'Frazier', 'Hemostat', 'Iris',
14
+ 'Mayo_metz', 'Needle', 'Potts', 'Richardson', 'Scalpel', 'Towel_clip', 'Weitlaner', 'Yankauer']
15
+ class_map = ClassMap(classes)
16
+
17
+
18
+ metrics = [COCOMetric(metric_type=COCOMetricType.bbox)]
19
+
20
+
21
+ model_type = models.mmdet.vfnet
22
+ backbone = model_type.backbones.resnet50_fpn_mstrain_2x
23
+
24
+ checkpoint_path = 'VFNet_teacher_nov29_mAP82.6.pth'
25
+
26
+ checkpoint_and_model = model_from_checkpoint(checkpoint_path)
27
+
28
+ model_loaded = checkpoint_and_model["model"]
29
+ img_size = checkpoint_and_model["img_size"]
30
+
31
+ valid_tfms = tfms.A.Adapter(
32
+ [*tfms.A.resize_and_pad(img_size), tfms.A.Normalize()])
33
+
34
+ def show_preds_gradio(input_image, display_label, display_bbox, detection_threshold):
35
+
36
+ if detection_threshold == 0:
37
+ detection_threshold = 0.5
38
+
39
+ img = PIL.Image.fromarray(input_image, 'RGB')
40
+
41
+ pred_dict = model_type.end2end_detect(img, valid_tfms, model_loaded, class_map=class_map, detection_threshold=detection_threshold,
42
+ display_label=display_label, display_bbox=display_bbox, return_img=True,
43
+ font_size=16, label_color="#FF59D6")
44
+
45
+ return pred_dict['img']
46
+
47
+
48
+ display_chkbox_label = gr.inputs.Checkbox(label="Label", default=True)
49
+ display_chkbox_box = gr.inputs.Checkbox(label="Box", default=True)
50
+
51
+ detection_threshold_slider = gr.inputs.Slider(
52
+ minimum=0, maximum=1, step=0.1, default=0.5, label="Detection Threshold")
53
+
54
+ outputs = gr.outputs.Image(type="pil")
55
+
56
+ gr_interface = gr.Interface(fn=show_preds_gradio, inputs=["image", display_chkbox_label, display_chkbox_box, detection_threshold_slider],
57
+ outputs=outputs, title='Surgical Instrument Detection and Identification Tool') # , article=article, examples=examples)
58
+
59
+
60
+ gr_interface.launch(inline=False, share=True, debug=True)