Spaces:
Running
Running
File size: 3,181 Bytes
eb5980a 1bf41f9 eb5980a 1bf41f9 eb5980a 1bf41f9 eb5980a 1bf41f9 a8e6217 1bf41f9 eb5980a 1bf41f9 eb5980a 1bf41f9 eb5980a 1bf41f9 eb5980a 1bf41f9 a8e6217 eb5980a 71f9f81 a8e6217 71f9f81 eb5980a a8e6217 eb5980a 1bf41f9 eb5980a 1bf41f9 eb5980a 1bf41f9 71f9f81 |
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 |
# -*- coding: utf-8 -*-
"""
SaliencyMapDemo
"""
import argparse
from datetime import datetime
import sys
import cv2
import gradio as gr
import numpy as np
import utils
PROGRAM_NAME = 'SaliencyMapDemo'
__version__ = utils.get_package_version()
def compute_saliency(image: np.ndarray):
"""
入力画像から顕著性マップを作成しJET画像を返します。
Parameters
----------
param1 : np.ndarray
入力画像
Returns
-------
np.ndarray
カラーマップのJET画像
"""
# OpenCVのsaliencyを作成
saliency = cv2.saliency.StaticSaliencySpectralResidual_create()
# 画像の顕著性を計算
success, saliencyMap = saliency.computeSaliency(image)
if success:
# 顕著性マップをカラーマップに変換
saliencyMap = (saliencyMap * 255).astype("uint8")
saliencyMap = cv2.applyColorMap(saliencyMap, cv2.COLORMAP_JET)
#overlay = saliencyMap
# 元の画像とカラーマップを重ね合わせ
overlay = cv2.addWeighted(image, 0.5, saliencyMap, 0.5, 0)
return overlay
else:
return image # エラーが発生した場合は元の画像を返す
def run(args: argparse.Namespace, watch: utils.Stopwatch) -> None:
"""
アプリの画面を作成し、Gradioサービスを起動します。
----------
param1 : argparse.Namespace
コマンドライン引数
param2 : utils.Stopwatch
起動したスタート時間
"""
# 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.
1. inputタブで画像を選択します。
2. Submitボタンを押します。
※画像は外部送信していません。ローカルで処理が完結します。
3. 結果は、overlayタブに表示します。
""")
submit_button = gr.Button("submit")
with gr.Row():
with gr.Tab("input"):
image_input = gr.Image()
with gr.Tab("overlay"):
image_overlay = gr.Image(interactive=False)
submit_button.click(compute_saliency, inputs=image_input, outputs=image_overlay)
gr.Markdown(
f"""
Python {sys.version}
App {__version__}
""")
demo.queue(default_concurrency_limit=5)
print(f"{datetime.now()}:アプリ起動完了({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)
|