Spaces:
Sleeping
Sleeping
File size: 4,558 Bytes
eb5980a 1bf41f9 eb5980a 1bf41f9 da8bdb9 eb5980a 1bf41f9 da8bdb9 eb5980a da8bdb9 1bf41f9 da8bdb9 1bf41f9 da8bdb9 eb5980a da8bdb9 eb5980a da8bdb9 eb5980a 1bf41f9 eb5980a da8bdb9 eb5980a 71f9f81 a8e6217 71f9f81 eb5980a da8bdb9 eb5980a da8bdb9 eb5980a da8bdb9 eb5980a da8bdb9 eb5980a da8bdb9 1bf41f9 eb5980a 1bf41f9 da8bdb9 1bf41f9 71f9f81 da8bdb9 |
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 |
# -*- coding: utf-8 -*-
"""
SaliencyMapDemo
"""
import argparse
from datetime import datetime
import sys
import gradio as gr
import numpy as np
import utils
from saliency import SaliencyMap, convertColorMap
from reporter import get_current_reporter
PROGRAM_NAME = 'SaliencyMapDemo'
__version__ = utils.get_package_version()
log = get_current_reporter()
def jetTab_Selected(image: np.ndarray):
#print(f"{datetime.now()}#jet")
saliency = SaliencyMap("SpectralResidual")
success, saliencyMap = saliency.computeSaliency(image)
retval = convertColorMap(image, saliencyMap, "jet")
#print(f"{datetime.now()}#jet")
return retval
def hotTab_Selected(image: np.ndarray):
#print(f"{datetime.now()}#hot")
saliency = SaliencyMap("SpectralResidual")
success, saliencyMap = saliency.computeSaliency(image)
retval = convertColorMap(image, saliencyMap, "hot")
#print(f"{datetime.now()}#hot")
return retval
def submit_Clicked(image: np.ndarray, algorithm: str):
"""
入力画像を元に顕著マップを計算します。
Parameters:
image: 入力画像
str: 顕著性マップのアルゴリズム
Returns:
np.ndarray: JET画像
np.ndarray: HOT画像
"""
log.info(f"#submit_Clicked")
watch = utils.Stopwatch.startNew()
saliency = SaliencyMap(algorithm)
success, saliencyMap = saliency.computeSaliency(image)
log.info(f"#SaliencyMap computeSaliency()")
if not success:
return image, image # エラーが発生した場合は入力画像を返します。
log.info(f"#jet")
jet = convertColorMap(image, saliencyMap, "jet")
#jet = None
log.info(f"#hot")
hot = convertColorMap(image, saliencyMap, "hot")
saliency = None
log.info(f"#submit_Clicked End{watch.stop():.3f}")
return jet, hot
def run(args: argparse.Namespace, watch: utils.Stopwatch) -> None:
"""
アプリの画面を作成し、Gradioサービスを起動します。
Parameters:
args: コマンドライン引数
watch: 起動したスタート時間
"""
# analytics_enabled=False
# https://github.com/gradio-app/gradio/issues/4226
with gr.Blocks(analytics_enabled=False, \
title=f"{PROGRAM_NAME} {__version__}", \
head="""
<meta name="format-detection" content="telephone=no">
<meta name="robots" content="noindex, nofollow, noarchive">
<meta name="referrer" content="no-referrer" />
""") as demo:
gr.Markdown(
"""
# Saliency Map demo.
""")
with gr.Accordion("取り扱い説明書", open=False):
gr.Markdown(
"""
1. inputタブで画像を選択します。
2. Submitボタンを押します。
※画像は外部送信していません。ローカルで処理が完結します。
3. 結果は、JETタブとHOTタブに表示します。
""")
algorithmType = gr.Radio(["SpectralResidual", "FineGrained"], label="Saliency", value="SpectralResidual", interactive=True)
submit_button = gr.Button("submit")
with gr.Row():
with gr.Tab("input", id="input"):
image_input = gr.Image(sources = ["upload", "clipboard"], interactive=True)
with gr.Tab("overlay(JET)"):
image_overlay_jet = gr.Image(interactive=False)
#tab_jet.select(jetTab_Selected, inputs=[image_input], outputs=image_overlay_jet)
with gr.Tab("overlay(HOT)"):
image_overlay_hot = gr.Image(interactive=False)
#tab_hot.select(hotTab_Selected, inputs=[image_input], outputs=image_overlay_hot, api_name=False)
submit_button.click(submit_Clicked, inputs=[image_input, algorithmType], outputs=[image_overlay_jet, image_overlay_hot])
gr.Markdown(
f"""
Python {sys.version}
App {__version__}
""")
demo.queue(default_concurrency_limit=5)
log.info(f"#アプリ起動完了({watch.stop():.3f}s)")
# https://www.gradio.app/docs/gradio/blocks#blocks-launch
demo.launch(
max_file_size=args.max_file_size,
server_port=args.server_port,
inbrowser=True,
share=False,
)
|