import sys import os import cv2 import matplotlib import matplotlib.pyplot as plt import numpy as np import torch import torchvision import glob import gradio as gr from PIL import Image from segment_anything import SamAutomaticMaskGenerator, SamPredictor, sam_model_registry import logging from huggingface_hub import hf_hub_download token = os.environ['HUB_TOKEN'] loc =hf_hub_download(repo_id="JunchuanYu/files_for_segmentRS", filename="utils.py",repo_type="dataset",local_dir='.',token=token) sys.path.append(loc) from utils import * sam_checkpoint ="sam_vit_b_01ec64.pth" # sam_checkpoint = "sam_vit_h_4b8939.pth" device = torch.device('cuda' if torch.cuda.is_available() else 'cpu') # use GPU if available model_type = "vit_b" sam = sam_model_registry[model_type](checkpoint=sam_checkpoint) sam.to(device=device) predictor = SamPredictor(sam) logging.basicConfig(filename="app.log", level=logging.INFO) title=( """ #
Segment-RS ๐ฐ๏ธ ##
A remote sensing interactive interpretation tools based on segment-anything (SAM ๐) ###
YJC (yujunchuan@mail.cgs.gov.cn) ๐ง
"""
)
description =(
"""
Segment-RS is an interactive remote sensing interpretation tool that has been developed based on [SAM](https://github.com/facebookresearch/segment-anything). It allows for the real-time extraction of various remote sensing targets through interaction. Segment-RS is equipped with two interpretation models, namely, interactive extraction and automatic extraction.
* Interactive extraction involves manually selecting samples (positive and negative) from the image to extract obvious targets. It should be emphasized that this manual interaction method is suitable for extracting an independent target in the scene and not suitable for extracting multiple targets of the same type at once as it is still under development.
* Automatic extraction does not require any interaction, one can simply click the "Auto Segment" button to get the segmentation result. Additionally, the accuracy and granularity of segmentation can be adjusted through "Prediction Thresh" and "Points Per Side".
"""
)
descriptionend=(
"""