File size: 4,524 Bytes
9becae6 4e3cfa9 9becae6 a71ecc9 9becae6 a71ecc9 9becae6 4e3cfa9 9becae6 a71ecc9 9becae6 a71ecc9 9becae6 a71ecc9 9becae6 a71ecc9 9becae6 a71ecc9 9becae6 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 |
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"import gradio as gr\n",
"import numpy as np\n",
"from os import environ\n",
"from PIL import Image as PImage\n",
"from torchvision import transforms as T\n",
"from transformers import MaskFormerForInstanceSegmentation, MaskFormerImageProcessor"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ade_mean=[0.485, 0.456, 0.406]\n",
"ade_std=[0.229, 0.224, 0.225]\n",
"\n",
"palette = [\n",
" [120, 120, 120], [4, 200, 4], [4, 4, 250], [6, 230, 230],\n",
" [80, 50, 50], [120, 120, 80], [140, 140, 140], [204, 5, 255]\n",
"]\n",
"\n",
"model_id = f\"thiagohersan/maskformer-satellite-trees\"\n",
"\n",
"vegetation_labels = [\"vegetation\"]"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"def visualize_instance_seg_mask(img_in, mask, id2label, included_labels):\n",
" img_out = np.zeros((mask.shape[0], mask.shape[1], 3))\n",
" image_total_pixels = mask.shape[0] * mask.shape[1]\n",
" label_ids = np.unique(mask)\n",
"\n",
" id2color = {id: palette[id] for id in label_ids}\n",
" id2count = {id: 0 for id in label_ids}\n",
"\n",
" for i in range(img_out.shape[0]):\n",
" for j in range(img_out.shape[1]):\n",
" img_out[i, j, :] = id2color[mask[i, j]]\n",
" id2count[mask[i, j]] = id2count[mask[i, j]] + 1\n",
"\n",
" image_res = (0.5 * img_in + 0.5 * img_out).astype(np.uint8)\n",
"\n",
" dataframe = [[\n",
" f\"{id2label[id]}\",\n",
" f\"{(100 * id2count[id] / image_total_pixels):.2f} %\",\n",
" f\"{np.sqrt(id2count[id] / image_total_pixels):.2f} m\"\n",
" ] for id in label_ids if id2label[id] in included_labels]\n",
"\n",
" if len(dataframe) < 1:\n",
" dataframe = [[\n",
" f\"\",\n",
" f\"{(0):.2f} %\",\n",
" f\"{(0):.2f} m\"\n",
" ]]\n",
"\n",
" return image_res, dataframe\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# preprocessor = MaskFormerImageProcessor.from_pretrained(model_id)\n",
"preprocessor = MaskFormerImageProcessor(\n",
" do_resize=False,\n",
" do_normalize=False,\n",
" do_rescale=False,\n",
" ignore_index=255,\n",
" do_reduce_labels=False\n",
")\n",
"\n",
"hf_token = environ.get('HFTOKEN') or True\n",
"model = MaskFormerForInstanceSegmentation.from_pretrained(model_id, use_auth_token=hf_token)\n",
"\n",
"test_transform = T.Compose([\n",
" T.ToTensor(),\n",
" T.Normalize(mean=ade_mean, std=ade_std)\n",
"])\n",
"\n",
"with PImage.open(\"../color-filter-calculator/assets/Artshack_screen.jpg\") as img:\n",
" img_size = (img.height, img.width)\n",
" norm_image = test_transform(np.array(img))\n",
" inputs = preprocessor(images=norm_image, return_tensors=\"pt\")\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"outputs = model(**inputs)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"results = preprocessor.post_process_semantic_segmentation(outputs=outputs, target_sizes=[img_size])[0]\n",
"mask_img, dataframe = visualize_instance_seg_mask(np.array(img), results.numpy(), model.config.id2label, vegetation_labels)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"dataframe"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3.8.15 ('gradio2023')",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.9.17"
},
"orig_nbformat": 4,
"vscode": {
"interpreter": {
"hash": "311e94dbd43374307e33a15d3b7324b15a4f7b1d7ecfe8226f18075b87b9fae7"
}
}
},
"nbformat": 4,
"nbformat_minor": 2
}
|