File size: 5,692 Bytes
0a96ac9 |
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 |
import gradio as gr
from pathlib import Path
import secrets
import shutil
from inference import detector, detector_processor, segment_predictor, ModelInference
current_dir = Path(__file__).parent
def process_images(image_path, files, slider_value, request: gr.Request):
user_dir: Path = current_dir / str(request.session_hash)
user_dir.mkdir(exist_ok=True)
annotation_path = user_dir / f"{secrets.token_hex(nbytes=8)}_annotations.coco.json"
class_names = list(inferencer.id2label.values())
if image_path:
print(f"Processing image: {image_path}")
seg_detections, annotated_frame = inferencer.predict_one(image_path)
inferencer.save_annotations([image_path], [seg_detections], class_names, annotation_path)
elif files:
print(f"Processing files: {files}")
print(f"Batch size: {slider_value}")
all_image_paths, all_results, annotated_frame, detector_failed_list, segmentor_failed_list = inferencer.predict_folder(files, slider_value)
print(f"Detector failed list: {detector_failed_list}")
print(f"Segmentor failed list: {segmentor_failed_list}")
inferencer.save_annotations(all_image_paths, all_results, class_names, annotation_path)
return [
gr.UploadButton(visible=False),
gr.Button("Run", visible=False),
gr.DownloadButton("Download annotation results", value=annotation_path, label="Download", visible=True),
gr.Image(value=annotated_frame, label="Annotated Image", visible=True),
]
def upload_file():
return [
None,
gr.UploadButton(visible=False),
gr.Slider(1, 6, step=1, label="Batch size", interactive=True, value=4, visible=True),
gr.Button("Run", visible=True),
gr.DownloadButton(visible=False),
gr.Image(value=None, label="Annotated Image", visible=True),
]
def upload_image(imge_path):
return [
gr.UploadButton(visible=False),
gr.Slider(1, 6, step=1, label="Batch size", interactive=True, value=4, visible=False),
gr.Button("Run", visible=True),
gr.DownloadButton(visible=False),
gr.Image(value=None, label="Annotated Image", visible=True),
]
def download_file():
return [
gr.Image(value=None),
gr.UploadButton(visible=True),
gr.Slider(1, 6, step=1, label="Batch size", interactive=True, value=4, visible=False),
gr.Button("Run", visible=False),
gr.DownloadButton(visible=True),
gr.Image(value=None, visible=False),
]
def delete_directory(request: gr.Request):
"""Delete the user-specific directory when the user's session ends."""
user_dir = current_dir / str(request.session_hash)
if user_dir.exists():
shutil.rmtree(user_dir)
def create_gradio_interface():
with gr.Blocks(theme=gr.themes.Monochrome(), delete_cache=(60, 3600)) as demo:
gr.HTML("""
<div style="text-align: center;">
<h1>Satellite Image Roofs Auto Annotation</h1>
<p>Powered by a <a href="https://huggingface.co/Yifeng-Liu/rt-detr-finetuned-for-satellite-image-roofs-detection" target="_blank">fine-tuned RT-DETR model</a> and Fast-SAM model.</p>
<p>📤 Upload an image or a folder containing images.</p>
<p>🖼️ Images are saved in a user-specific directory and deleted when the user closes the page.</p>
<p>⚙️ Each user can upload files with a maximum file size of 200 MB.</p>
</div>
""")
with gr.Row():
with gr.Column(scale=1):
img_input = gr.Image(
interactive=True,
sources=["upload", "clipboard"],
show_share_button=True,
type='filepath',
label="Upload a single image",
)
upload_button = gr.UploadButton("Upload a folder", file_count="directory")
batch_slider = gr.Slider(1, 6, step=1, label="Batch size", interactive=True, value=4, visible=False)
run_button = gr.Button("Run", visible=False)
with gr.Column(scale=1):
img_output = gr.Image(label="Annotated Image", visible=False)
download_button = gr.DownloadButton("Download annotation results", label="Download", visible=False)
with gr.Row():
examples = gr.Examples(
examples=[["./img/example.jpg"]],
inputs=[img_input],
outputs=[upload_button, batch_slider, run_button, download_button, img_output],
fn=upload_image,
run_on_click=True,
)
upload_button.upload(upload_file, None, [img_input, upload_button, batch_slider, run_button, download_button, img_output])
download_button.click(download_file, None, [img_input, upload_button, batch_slider, run_button, download_button, img_output])
run_button.click(process_images,
[img_input, upload_button, batch_slider],
[upload_button, run_button, download_button, img_output])
img_input.upload(upload_image, img_input, [upload_button, batch_slider, run_button, download_button, img_output])
demo.unload(delete_directory)
return demo
def inferencer_init():
id2label = {0: 'building'}
CONFIDENCE_TRESHOLD = 0.5
return ModelInference(detector, detector_processor, segment_predictor, id2label, CONFIDENCE_TRESHOLD)
inferencer = inferencer_init()
if __name__ == "__main__":
demo = create_gradio_interface()
demo.launch(max_file_size=200 * gr.FileSize.MB)
|