Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -2,7 +2,8 @@
|
|
2 |
building-segmentation
|
3 |
Proof of concept showing effectiveness of a fine tuned instance segmentation model for deteting buildings.
|
4 |
"""
|
5 |
-
|
|
|
6 |
from transformers import DetrFeatureExtractor, DetrForSegmentation
|
7 |
from PIL import Image
|
8 |
import gradio as gr
|
@@ -11,36 +12,46 @@ import torch
|
|
11 |
import torchvision
|
12 |
import detectron2
|
13 |
|
|
|
14 |
import itertools
|
15 |
import seaborn as sns
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
cfg = get_cfg()
|
|
|
|
|
|
|
18 |
|
19 |
-
def segment_buildings(input_image
|
20 |
-
|
21 |
-
cfg.MODEL.WEIGHTS = "model_weights/chatswood_buildings_poc.pth"
|
22 |
-
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.7 # set a custom testing threshold
|
23 |
-
predictor = DefaultPredictor(cfg)
|
24 |
|
|
|
25 |
outputs = predictor(im)
|
26 |
-
|
27 |
v = Visualizer(im[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2)
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
return(output_image)
|
32 |
|
33 |
# gradio components -inputs
|
34 |
-
gr_image_input = gr.inputs.Image()
|
|
|
35 |
gr_slider_confidence = gr.inputs.Slider(0,1,.1,.7,
|
36 |
label='Set confidence threshold % for masks')
|
|
|
37 |
# gradio outputs
|
38 |
-
gr_image_output = gr.outputs.Image()
|
|
|
|
|
|
|
39 |
|
40 |
# Create user interface and launch
|
41 |
gr.Interface(predict_building_mask,
|
42 |
-
inputs =
|
43 |
outputs = gr_image_output,
|
44 |
-
title =
|
45 |
-
|
|
|
46 |
|
|
|
2 |
building-segmentation
|
3 |
Proof of concept showing effectiveness of a fine tuned instance segmentation model for deteting buildings.
|
4 |
"""
|
5 |
+
import os
|
6 |
+
os.system("pip install 'git+https://github.com/facebookresearch/detectron2.git'")
|
7 |
from transformers import DetrFeatureExtractor, DetrForSegmentation
|
8 |
from PIL import Image
|
9 |
import gradio as gr
|
|
|
12 |
import torchvision
|
13 |
import detectron2
|
14 |
|
15 |
+
# import some common detectron2 utilities
|
16 |
import itertools
|
17 |
import seaborn as sns
|
18 |
+
from detectron2 import model_zoo
|
19 |
+
from detectron2.engine import DefaultPredictor
|
20 |
+
from detectron2.config import get_cfg
|
21 |
+
from detectron2.utils.visualizer import Visualizer
|
22 |
+
from detectron2.data import MetadataCatalog, DatasetCatalog
|
23 |
+
from PIL import Image
|
24 |
|
25 |
cfg = get_cfg()
|
26 |
+
cfg.MODEL.DEVICE='cpu'
|
27 |
+
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
|
28 |
+
cfg.MODEL.WEIGHTS = "model_weights/chatswood_buildings_poc.pth"
|
29 |
|
30 |
+
def segment_buildings(input_image):
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
im = cv2.imread(input_image.name)
|
33 |
outputs = predictor(im)
|
|
|
34 |
v = Visualizer(im[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.2)
|
35 |
+
out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
|
36 |
+
return Image.fromarray(np.uint8(out.get_image())).convert('RGB')
|
|
|
|
|
37 |
|
38 |
# gradio components -inputs
|
39 |
+
gr_image_input = gr.inputs.Image(type="file")
|
40 |
+
"""
|
41 |
gr_slider_confidence = gr.inputs.Slider(0,1,.1,.7,
|
42 |
label='Set confidence threshold % for masks')
|
43 |
+
"""
|
44 |
# gradio outputs
|
45 |
+
gr_image_output = gr.outputs.Image(type="pil")
|
46 |
+
|
47 |
+
title = "Building Segmentation"
|
48 |
+
description = "An instance segmentation demo for identifying boundaries of buildings in aerial images using DETR (End-to-End Object Detection) model with MaskRCNN-101 backbone"
|
49 |
|
50 |
# Create user interface and launch
|
51 |
gr.Interface(predict_building_mask,
|
52 |
+
inputs = gr_image_input,
|
53 |
outputs = gr_image_output,
|
54 |
+
title = title,
|
55 |
+
enable_queue = True,
|
56 |
+
description = description).launch()
|
57 |
|