Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -21,86 +21,54 @@ from detectron2.config import get_cfg
|
|
| 21 |
from detectron2.utils.visualizer import Visualizer
|
| 22 |
from detectron2.data import MetadataCatalog
|
| 23 |
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
"cfg": None,
|
| 31 |
-
"metadata": None
|
| 32 |
-
},
|
| 33 |
-
]
|
| 34 |
-
|
| 35 |
-
model_name_to_id = {model["name"] : id_ for id_, model in enumerate(models)}
|
| 36 |
-
|
| 37 |
-
for model in models:
|
| 38 |
-
|
| 39 |
-
model["cfg"] = get_cfg()
|
| 40 |
-
# model["cfg"].merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_X_101_32x8d_FPN_3x.yaml"))
|
| 41 |
-
model["cfg"].MODEL.ROI_HEADS.NUM_CLASSES = len(model["classes"])
|
| 42 |
-
model["cfg"].MODEL.WEIGHTS = model["model_path"]
|
| 43 |
-
|
| 44 |
-
model["metadata"] = MetadataCatalog.get(model["name"])
|
| 45 |
-
model["metadata"].thing_classes = model["classes"]
|
| 46 |
-
|
| 47 |
-
if not torch.cuda.is_available():
|
| 48 |
-
model["cfg"].MODEL.DEVICE = "cpu"
|
| 49 |
-
|
| 50 |
|
| 51 |
-
|
| 52 |
-
if image_url:
|
| 53 |
-
r = requests.get(image_url)
|
| 54 |
-
if r:
|
| 55 |
-
im = np.frombuffer(r.content, dtype="uint8")
|
| 56 |
-
im = cv2.imdecode(im, cv2.IMREAD_COLOR)
|
| 57 |
-
else:
|
| 58 |
-
# Model expect BGR!
|
| 59 |
-
im = image[:,:,::-1]
|
| 60 |
|
| 61 |
-
|
| 62 |
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
|
| 72 |
-
|
| 73 |
-
return out.get_image()
|
| 74 |
|
| 75 |
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
Currently, a model is supported that was trained on manually annotated segments from petri dish images:
|
| 80 |
-
* [Version 1 (1-class)](https://huggingface.co/dbmdz/detectron2-model): This model can detect *Object* segments on a given petri dish.
|
| 81 |
-
"""
|
| 82 |
-
footer = "Made by eyepop.ai with ❤️."
|
| 83 |
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
gr.Markdown(description)
|
| 87 |
-
|
| 88 |
-
with gr.Tab("From URL"):
|
| 89 |
-
url_input = gr.Textbox(label="Image URL", placeholder="https://huggingface.co/spaces/eyepop-ai/segmentation/blob/main/image_10_jpg.rf.46f8c223849190c943d24da8555d62fe.jpg")
|
| 90 |
|
| 91 |
-
|
| 92 |
-
image_input = gr.Image(type="numpy", label="Input Image")
|
| 93 |
-
|
| 94 |
-
min_score = gr.Slider(minimum=0.0, maximum=1.0, value=0.5, label="Minimum score")
|
| 95 |
-
|
| 96 |
-
model_name = gr.Radio(choices=[model["name"] for model in models], value=models[0]["name"], label="Select Detectron2 model")
|
| 97 |
|
| 98 |
-
output_image = gr.Image(type="pil", label="Output")
|
| 99 |
|
| 100 |
-
inference_button = gr.Button("Submit")
|
| 101 |
-
|
| 102 |
-
inference_button.click(fn=inference, inputs=[url_input, image_input, min_score, model_name], outputs=output_image)
|
| 103 |
|
| 104 |
-
|
|
|
|
|
|
|
|
|
|
| 105 |
|
| 106 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 21 |
from detectron2.utils.visualizer import Visualizer
|
| 22 |
from detectron2.data import MetadataCatalog
|
| 23 |
|
| 24 |
+
url1 = 'https://images.unsplash.com/photo-1503463168353-9d883c7f5255?q=80&w=1748&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D'
|
| 25 |
+
r = requests.get(url1, allow_redirects=True)
|
| 26 |
+
open("balloon.jpg", 'wb').write(r.content)
|
| 27 |
+
url2 = 'https://images.unsplash.com/photo-1599158150601-1417ebbaafdd?q=80&w=1636&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D'
|
| 28 |
+
r = requests.get(url2, allow_redirects=True)
|
| 29 |
+
open("football.jpg", 'wb').write(r.content)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 30 |
|
| 31 |
+
model_name='COCO-InstanceSegmentation/mask_rcnn_X_101_32x8d_FPN_3x.yaml'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
|
| 33 |
+
# model = model_zoo.get(model_name, trained=True)
|
| 34 |
|
| 35 |
+
cfg = get_cfg()
|
| 36 |
+
# add project-specific config (e.g., TensorMask) here if you're not running a model in detectron2's core library
|
| 37 |
+
cfg.merge_from_file(model_zoo.get_config_file(model_name))
|
| 38 |
+
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 # set threshold for this model
|
| 39 |
+
# Find a model from detectron2's model zoo. You can use the https://dl.fbaipublicfiles... url as w ell
|
| 40 |
+
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(model_name)
|
| 41 |
|
| 42 |
+
if not torch.cuda.is_available():
|
| 43 |
+
cfg.MODEL.DEVICE='cpu'
|
| 44 |
|
| 45 |
+
predictor = DefaultPredictor(cfg)
|
|
|
|
|
|
|
|
|
|
| 46 |
|
| 47 |
|
| 48 |
+
def inference(image):
|
| 49 |
+
img = np.array(image.resize((1024,1024)))
|
| 50 |
+
outputs = predictor(img)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
+
v = Visualizer(img, MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2)
|
| 53 |
+
out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
|
| 55 |
+
return out.get_image()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
|
|
|
| 57 |
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
+
title = "Detectron2-MaskRCNN X101"
|
| 60 |
+
description = "demo for Detectron2. To use it, simply upload your image, or click one of the examples to load them. Read more at the links below.\
|
| 61 |
+
</br><b>Model: COCO-InstanceSegmentation/mask_rcnn_X_101_32x8d_FPN_3x.yaml</b>"
|
| 62 |
+
article = "<p style='text-align: center'><a href='https://arxiv.org/abs/2012.07177'>Simple Copy-Paste is a Strong Data Augmentation Method for Instance Segmentation</a> | <a href='https://github.com/facebookresearch/detectron2/blob/main/MODEL_ZOO.md'>Detectron model ZOO</a></p>"
|
| 63 |
|
| 64 |
+
gr.Interface(
|
| 65 |
+
inference,
|
| 66 |
+
[gr.inputs.Image(type="pil", label="Input")],
|
| 67 |
+
gr.outputs.Image(type="numpy", label="Output"),
|
| 68 |
+
title=title,
|
| 69 |
+
description=description,
|
| 70 |
+
article=article,
|
| 71 |
+
examples=[
|
| 72 |
+
["balloon.jpg"],
|
| 73 |
+
["football.jpg"]
|
| 74 |
+
]).launch()
|