Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,19 +1,18 @@
|
|
| 1 |
-
from gradio.outputs import Label
|
| 2 |
from icevision.all import *
|
| 3 |
import PIL
|
| 4 |
import torch
|
|
|
|
| 5 |
import gradio as gr
|
| 6 |
-
import os
|
| 7 |
|
| 8 |
# Load model
|
| 9 |
class_map = ClassMap(['selected_variant'])
|
| 10 |
backbone = faster_rcnn.backbones.resnet_fpn.resnet18(pretrained=True)
|
| 11 |
model = faster_rcnn.model(backbone=backbone, num_classes=len(class_map))
|
| 12 |
model.load_state_dict(torch.load('object_localization_full-ancestry.model.pth', map_location=torch.device('cpu')))
|
|
|
|
| 13 |
|
| 14 |
-
def predict(
|
| 15 |
-
|
| 16 |
-
):
|
| 17 |
infer_ds = Dataset.from_images([image])
|
| 18 |
|
| 19 |
batch, samples = faster_rcnn.build_infer_batch(infer_ds)
|
|
@@ -24,15 +23,12 @@ def predict(
|
|
| 24 |
)
|
| 25 |
return samples[0]["img"], preds[0]
|
| 26 |
|
| 27 |
-
def show_preds(input_image, display_list, detection_threshold):
|
| 28 |
-
display_label = ("Label" in display_list)
|
| 29 |
-
display_bbox = ("BBox" in display_list)
|
| 30 |
|
| 31 |
-
|
| 32 |
|
| 33 |
-
img, pred = predict(model=model, image=input_image, detection_threshold=
|
| 34 |
# print(pred)
|
| 35 |
-
img = draw_pred(img=img, pred=pred, class_map=class_map, display_label=
|
| 36 |
img = PIL.Image.fromarray(img)
|
| 37 |
# print("Output Image: ", img.size, type(img))
|
| 38 |
return img
|
|
@@ -44,15 +40,11 @@ examples = [
|
|
| 44 |
['3.png']
|
| 45 |
]
|
| 46 |
|
| 47 |
-
display_chkbox = gr.inputs.CheckboxGroup(["Label", "BBox"], label="Display")
|
| 48 |
-
detection_threshold_slider = gr.inputs.Slider(minimum=0, maximum=1, step=0.1, default=0.5, label="Detection Threshold")
|
| 49 |
-
|
| 50 |
-
outputs = gr.outputs.Image(type="pil")
|
| 51 |
-
|
| 52 |
gr_interface = gr.Interface(
|
| 53 |
fn=show_preds,
|
| 54 |
-
inputs=["image"
|
| 55 |
-
outputs=
|
| 56 |
title='Selection Scan - Object Detection',
|
| 57 |
examples=examples)
|
|
|
|
| 58 |
gr_interface.launch(inline=False, share=False, debug=True)
|
|
|
|
|
|
|
| 1 |
from icevision.all import *
|
| 2 |
import PIL
|
| 3 |
import torch
|
| 4 |
+
from torchvision import transforms
|
| 5 |
import gradio as gr
|
|
|
|
| 6 |
|
| 7 |
# Load model
|
| 8 |
class_map = ClassMap(['selected_variant'])
|
| 9 |
backbone = faster_rcnn.backbones.resnet_fpn.resnet18(pretrained=True)
|
| 10 |
model = faster_rcnn.model(backbone=backbone, num_classes=len(class_map))
|
| 11 |
model.load_state_dict(torch.load('object_localization_full-ancestry.model.pth', map_location=torch.device('cpu')))
|
| 12 |
+
#model_type = models.torchvision.faster_rcnn
|
| 13 |
|
| 14 |
+
def predict(model, image, detection_threshold: float = 0.5):
|
| 15 |
+
# Whenever you have images in memory (numpy arrays) you can use `Dataset.from_images`
|
|
|
|
| 16 |
infer_ds = Dataset.from_images([image])
|
| 17 |
|
| 18 |
batch, samples = faster_rcnn.build_infer_batch(infer_ds)
|
|
|
|
| 23 |
)
|
| 24 |
return samples[0]["img"], preds[0]
|
| 25 |
|
|
|
|
|
|
|
|
|
|
| 26 |
|
| 27 |
+
def show_preds(input_image):
|
| 28 |
|
| 29 |
+
img, pred = predict(model=model, image=input_image, detection_threshold=0.5)
|
| 30 |
# print(pred)
|
| 31 |
+
img = draw_pred(img=img, pred=pred, class_map=class_map, display_label=False, display_bbox=True)
|
| 32 |
img = PIL.Image.fromarray(img)
|
| 33 |
# print("Output Image: ", img.size, type(img))
|
| 34 |
return img
|
|
|
|
| 40 |
['3.png']
|
| 41 |
]
|
| 42 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
gr_interface = gr.Interface(
|
| 44 |
fn=show_preds,
|
| 45 |
+
inputs=["image"],
|
| 46 |
+
outputs=gr.Image(type="pil"),
|
| 47 |
title='Selection Scan - Object Detection',
|
| 48 |
examples=examples)
|
| 49 |
+
|
| 50 |
gr_interface.launch(inline=False, share=False, debug=True)
|