{ "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 }