Spaces:
Runtime error
Runtime error
Thiago Hersan
commited on
Commit
•
a67cc84
1
Parent(s):
eb2f4fb
add tree+grass for vegetation count
Browse files
app.py
CHANGED
@@ -3,19 +3,20 @@ import numpy as np
|
|
3 |
from transformers import MaskFormerFeatureExtractor, MaskFormerForInstanceSegmentation
|
4 |
|
5 |
|
6 |
-
feature_extractor = MaskFormerFeatureExtractor.from_pretrained("facebook/maskformer-swin-tiny-coco")
|
7 |
-
model = MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-swin-tiny-coco")
|
8 |
-
|
9 |
-
|
10 |
|
11 |
def visualize_instance_seg_mask(img_in, mask, id2label):
|
12 |
img_out = np.zeros((mask.shape[0], mask.shape[1], 3))
|
13 |
image_total_pixels = mask.shape[0] * mask.shape[1]
|
14 |
label_ids = np.unique(mask)
|
|
|
15 |
|
16 |
def get_color(id):
|
17 |
id_color = (np.random.randint(0, 2), np.random.randint(0, 4), np.random.randint(0, 256))
|
18 |
-
if
|
19 |
id_color = (0, 140, 0)
|
20 |
return id_color
|
21 |
|
@@ -29,13 +30,24 @@ def visualize_instance_seg_mask(img_in, mask, id2label):
|
|
29 |
|
30 |
image_res = (0.5 * img_in + 0.5 * img_out) / 255
|
31 |
|
32 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
33 |
f"{id2label[id]}",
|
34 |
f"{(100 * id2count[id] / image_total_pixels):.2f} %",
|
35 |
f"{np.sqrt(id2count[id] / image_total_pixels):.2f} m"
|
36 |
-
] for id in label_ids
|
|
|
|
|
|
|
|
|
37 |
|
38 |
-
return image_res,
|
39 |
|
40 |
|
41 |
def query_image(img):
|
@@ -50,7 +62,7 @@ def query_image(img):
|
|
50 |
demo = gr.Interface(
|
51 |
query_image,
|
52 |
inputs=[gr.Image(label="Input Image")],
|
53 |
-
outputs=[gr.Image(label="
|
54 |
title="maskformer-swin-large-coco",
|
55 |
allow_flagging="never",
|
56 |
analytics_enabled=None
|
|
|
3 |
from transformers import MaskFormerFeatureExtractor, MaskFormerForInstanceSegmentation
|
4 |
|
5 |
|
6 |
+
# feature_extractor = MaskFormerFeatureExtractor.from_pretrained("facebook/maskformer-swin-tiny-coco")
|
7 |
+
# model = MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-swin-tiny-coco")
|
8 |
+
feature_extractor = MaskFormerFeatureExtractor.from_pretrained("facebook/maskformer-swin-large-coco")
|
9 |
+
model = MaskFormerForInstanceSegmentation.from_pretrained("facebook/maskformer-swin-large-coco")
|
10 |
|
11 |
def visualize_instance_seg_mask(img_in, mask, id2label):
|
12 |
img_out = np.zeros((mask.shape[0], mask.shape[1], 3))
|
13 |
image_total_pixels = mask.shape[0] * mask.shape[1]
|
14 |
label_ids = np.unique(mask)
|
15 |
+
vegetation_labels = ["tree-merged", "grass-merged"]
|
16 |
|
17 |
def get_color(id):
|
18 |
id_color = (np.random.randint(0, 2), np.random.randint(0, 4), np.random.randint(0, 256))
|
19 |
+
if id2label[id] in vegetation_labels:
|
20 |
id_color = (0, 140, 0)
|
21 |
return id_color
|
22 |
|
|
|
30 |
|
31 |
image_res = (0.5 * img_in + 0.5 * img_out) / 255
|
32 |
|
33 |
+
vegetation_count = sum([id2count[id] for id in label_ids if id2label[id] in vegetation_labels])
|
34 |
+
|
35 |
+
dataframe_vegetation_items = [[
|
36 |
+
f"{id2label[id]}",
|
37 |
+
f"{(100 * id2count[id] / image_total_pixels):.2f} %",
|
38 |
+
f"{np.sqrt(id2count[id] / image_total_pixels):.2f} m"
|
39 |
+
] for id in label_ids if id2label[id] in vegetation_labels]
|
40 |
+
dataframe_all_items = [[
|
41 |
f"{id2label[id]}",
|
42 |
f"{(100 * id2count[id] / image_total_pixels):.2f} %",
|
43 |
f"{np.sqrt(id2count[id] / image_total_pixels):.2f} m"
|
44 |
+
] for id in label_ids]
|
45 |
+
dataframe_vegetation_total = [[
|
46 |
+
f"vegetation",
|
47 |
+
f"{(100 * vegetation_count / image_total_pixels):.2f} %",
|
48 |
+
f"{np.sqrt(vegetation_count / image_total_pixels):.2f} m"]]
|
49 |
|
50 |
+
return image_res, dataframe_vegetation_total
|
51 |
|
52 |
|
53 |
def query_image(img):
|
|
|
62 |
demo = gr.Interface(
|
63 |
query_image,
|
64 |
inputs=[gr.Image(label="Input Image")],
|
65 |
+
outputs=[gr.Image(label="Vegetation"), gr.DataFrame(headers=None, label="Area Info")],
|
66 |
title="maskformer-swin-large-coco",
|
67 |
allow_flagging="never",
|
68 |
analytics_enabled=None
|