Spaces:
Runtime error
Runtime error
Commit
·
72efb9f
1
Parent(s):
bef0f58
Upload run.py
Browse files
run.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import sys
|
| 2 |
+
import os
|
| 3 |
+
import cv2
|
| 4 |
+
import matplotlib
|
| 5 |
+
import matplotlib.pyplot as plt
|
| 6 |
+
import numpy as np
|
| 7 |
+
import torch
|
| 8 |
+
import torchvision
|
| 9 |
+
import glob
|
| 10 |
+
import gradio as gr
|
| 11 |
+
from PIL import Image
|
| 12 |
+
from segment_anything import SamAutomaticMaskGenerator, SamPredictor, sam_model_registry
|
| 13 |
+
import logging
|
| 14 |
+
from huggingface_hub import hf_hub_download
|
| 15 |
+
|
| 16 |
+
token = os.environ['HUB_TOKEN']
|
| 17 |
+
loc =hf_hub_download(repo_id="JunchuanYu/files_for_segmentRS", filename="utils.py",repo_type="dataset",local_dir='.',token=token)
|
| 18 |
+
sys.path.append(loc)
|
| 19 |
+
from utils import *
|
| 20 |
+
|
| 21 |
+
with gr.Blocks(theme='gradio/soft') as demo:
|
| 22 |
+
gr.Markdown(title)
|
| 23 |
+
with gr.Accordion("Instructions For User 👉", open=False):
|
| 24 |
+
gr.Markdown(description)
|
| 25 |
+
x=gr.State(value=[])
|
| 26 |
+
y=gr.State(value=[])
|
| 27 |
+
label=gr.State(value=[])
|
| 28 |
+
with gr.Row():
|
| 29 |
+
with gr.Column(scale=13):
|
| 30 |
+
with gr.Row():
|
| 31 |
+
with gr.Column():
|
| 32 |
+
mode=gr.inputs.Radio(['Positive','Negative'], type="value",default='Positive',label='Types of sampling methods')
|
| 33 |
+
with gr.Column():
|
| 34 |
+
clear_bn=gr.Button("Clear Selection")
|
| 35 |
+
interseg_button = gr.Button("Interactive Segment",variant='primary')
|
| 36 |
+
with gr.Row():
|
| 37 |
+
input_img = gr.Image(label="Input")
|
| 38 |
+
gallery = gr.Image(label="Points")
|
| 39 |
+
|
| 40 |
+
input_img.select(get_select_coords, [input_img, mode,x,y,label], [gallery,x,y,label])
|
| 41 |
+
|
| 42 |
+
with gr.Row():
|
| 43 |
+
output_img = gr.Image(label="Result")
|
| 44 |
+
mask_img = gr.Image(label="Mask")
|
| 45 |
+
with gr.Row():
|
| 46 |
+
with gr.Column():
|
| 47 |
+
thresh = gr.Slider(minimum=0.8, maximum=1, value=0.90, step=0.01, interactive=True, label="Threshhold")
|
| 48 |
+
with gr.Column():
|
| 49 |
+
points = gr.Slider(minimum=16, maximum=96, value=32, step=16, interactive=True, label="Points/Side")
|
| 50 |
+
|
| 51 |
+
with gr.Column(scale=2,min_width=8):
|
| 52 |
+
example = gr.Examples(
|
| 53 |
+
examples=[[s,0.9,32] for s in glob.glob('./images/*')],
|
| 54 |
+
fn=auto_seg,
|
| 55 |
+
inputs=[input_img,thresh,points],
|
| 56 |
+
outputs=[output_img],
|
| 57 |
+
cache_examples=False,examples_per_page=5)
|
| 58 |
+
|
| 59 |
+
autoseg_button = gr.Button("Auto Segment",variant="primary")
|
| 60 |
+
emptyBtn = gr.Button("Restart",variant="secondary")
|
| 61 |
+
|
| 62 |
+
interseg_button.click(interactive_seg, inputs=[input_img,x,y,label], outputs=[output_img,mask_img])
|
| 63 |
+
autoseg_button.click(auto_seg, inputs=[input_img,thresh,points], outputs=[mask_img])
|
| 64 |
+
|
| 65 |
+
clear_bn.click(clear_point,outputs=[gallery,mode,x,y,label],show_progress=True)
|
| 66 |
+
emptyBtn.click(reset_state,outputs=[input_img,gallery,output_img,mask_img,thresh,points,mode,x,y,label],show_progress=True,)
|
| 67 |
+
|
| 68 |
+
gr.Markdown(descriptionend)
|
| 69 |
+
if __name__ == "__main__":
|
| 70 |
+
demo.launch(debug=False,show_api=False)
|