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