Spaces:
Sleeping
Sleeping
# -*- coding: utf-8 -*- | |
""" | |
SaliencyMapDemo | |
""" | |
#from datetime import datetime | |
import sys | |
from typing import Literal | |
import gradio as gr | |
import numpy as np | |
from src import PROGRAM_NAME, get_package_version | |
from src.args_parser import parse_args | |
from src.reporter import log | |
from src.saliency import SaliencyMap, convert_colormap | |
from src.utils import Stopwatch | |
__version__ = get_package_version() | |
log.info("#ใขใใช่ตทๅไธญ") | |
watch = Stopwatch.start_new() | |
def jet_tab_selected(image: np.ndarray): | |
""" | |
JETใฟใใ้ธๆๆ | |
""" | |
sw = Stopwatch.start_new() | |
log.info(f"#jet_tab_selected({sw.elapsed:.3f}s)") | |
saliency = SaliencyMap("SpectralResidual") | |
success, saliency_map = saliency.compute(image) | |
if not success: | |
return image # ใจใฉใผใ็บ็ใใๅ ดๅใฏๅ ฅๅ็ปๅใ่ฟใใพใใ | |
retval = convert_colormap(image, saliency_map, "jet") | |
log.info(f"#jet_tab_selected({sw.elapsed:.3f}s)") | |
return retval | |
def hot_tab_selected(image: np.ndarray): | |
""" | |
HOTใฟใใ้ธๆๆ | |
""" | |
sw = Stopwatch.start_new() | |
log.info(f"#hot_tab_selected({sw.elapsed:.3f}s)") | |
saliency = SaliencyMap("SpectralResidual") | |
success, saliency_map = saliency.compute(image) | |
if not success: | |
return image # ใจใฉใผใ็บ็ใใๅ ดๅใฏๅ ฅๅ็ปๅใ่ฟใใพใใ | |
retval = convert_colormap(image, saliency_map, "turbo") | |
log.info(f"#hot_tab_selected({sw.elapsed:.3f}s)") | |
return retval | |
def submit_clicked(image: np.ndarray, algorithm: Literal["SpectralResidual", "FineGrained"]): | |
""" | |
ๅ ฅๅ็ปๅใๅ ใซ้ก่ใใใใ่จ็ฎใใพใใ | |
Parameters: | |
image: ๅ ฅๅ็ปๅ | |
str: ้ก่ๆงใใใใฎใขใซใดใชใบใ | |
Returns: | |
np.ndarray: JET็ปๅ | |
np.ndarray: HOT็ปๅ | |
""" | |
sw = Stopwatch.start_new() | |
log.info(f"#submit_clicked({sw.elapsed:.3f}s)") | |
# | |
saliency = SaliencyMap(algorithm) | |
log.debug(f"#SaliencyMap({sw.elapsed:.3f}s)") | |
success, saliency_map = saliency.compute(image) | |
log.debug(f"#compute({sw.elapsed:.3f}s)") | |
if not success: | |
return image, image # ใจใฉใผใ็บ็ใใๅ ดๅใฏๅ ฅๅ็ปๅใ่ฟใใพใใ | |
log.debug(f"#jet({sw.elapsed:.3f}s)") | |
jet = convert_colormap(image, saliency_map, "jet") | |
# jet = None | |
log.debug(f"#hot({sw.elapsed:.3f}s)") | |
hot = convert_colormap(image, saliency_map, "hot") | |
saliency = None | |
log.info(f"#submit_clicked({sw.elapsed:.3f}s)") | |
return jet, hot | |
def gallery_selected(_, evt: gr.SelectData): | |
""" | |
ใฎใฃใฉใชใผใฎ็ปๅใ้ธๆใใใใจใใซๅผใณๅบใใใใณใผใซใใใฏ้ขๆฐใ | |
Parameters: | |
_ (Unused): ไฝฟ็จใใใชใๅผๆฐใ | |
evt (gr.SelectData): Gradioใฎใฎใฃใฉใชใผ้ธๆใคใใณใใใผใฟใ | |
Returns: | |
str: ้ธๆใใใใฎใฃใฉใชใผ็ปๅใฎใในใ | |
""" | |
image_path = evt.value['image']['path'] | |
return image_path | |
args = parse_args() | |
""" | |
ใขใใชใฎ็ป้ขใไฝๆใใGradioใตใผใในใ่ตทๅใใพใใ | |
ใใใใชใญใผใๅฏพๅฟใจใใฆใtopใฌใใซใฎใคใณใใณใใซใ | |
https://www.gradio.app/guides/developing-faster-with-reload-mode | |
""" | |
with gr.Blocks( | |
title=f"{PROGRAM_NAME} {get_package_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ใฟใใซ่กจ็คบใใใพใใ | |
### ๆดป็จใขใคใใข๐จ | |
ใใฎใใขใฏใๅตไฝๆดปๅใฎ้ใซๆณจ็ฎใใใใคใณใใ่ฆ่ฆๅใใใใใซๅฝน็ซใกใพใใ่ฆ่ฆๅใใใ็ตๆใๅบใซใใฉใฎ้จๅใซๅ ็ญใๅฟ ่ฆใใๅคๆญใใใใจใใงใใพใใ | |
ใใจใใฐใ้กใฎ็ฎใซๆณจ็ฎใใคใณใใๅฐใชใๅ ดๅใใใฎ้จๅใ้็น็ใซๅ ็ญใใใใจใงใไฝๅๅ จไฝใฎ้ญ ๅใ้ซใใใใจใใงใใใใใใใพใใใ | |
ใๅฉ็จใใใ ใใใใใใจใใใใใพใใ | |
""") | |
with gr.Accordion("Saliency Map User Guide", open=False): | |
gr.Markdown(""" | |
## Learn about saliency maps: | |
A saliency map visually highlights important areas in an image, mimicking how humans focus on key information. | |
Each pixel is assigned a value representing attention level, highlighting regions of interest. | |
### Try the demo: | |
To use the saliency map demo, follow two steps | |
1. Upload an image or paste it from the clipboard. | |
2. Click "Submit" to generate and view the saliency map on separate tabs. | |
### Application Ideas๐จ. | |
Useful for creative projects to identify points of interest and enhance appeal. | |
For instance, if eyes are focal points, focus enhancements there. | |
Thank you for your interest! | |
""") | |
algorithm_type = gr.Radio( | |
["SpectralResidual", "FineGrained"], | |
label="Saliency", | |
value="SpectralResidual", | |
interactive=True | |
) | |
submit_button = gr.Button("submit", variant="primary") | |
with gr.Tab("input", elem_id="input_tab"): | |
image_input = gr.Image(label="input", show_label=True, sources=["upload", "clipboard"]) | |
with gr.Tab("overlay(JET)", elem_id="jet_tab"): | |
image_overlay_jet = gr.Image(label="jet", show_label=True, interactive=False) | |
with gr.Tab("overlay(HOT)", elem_id="hot_tab"): | |
image_overlay_hot = gr.Image(label="hot", show_label=True, interactive=False) | |
# | |
with gr.Accordion("Sample Image Gallery", open=False): | |
gr.Markdown(""" | |
### ็ปๅใฎใฉใคใปใณใน่กจ็คบ | |
็ปๅใฎใฉใคใปใณในใฏใในใฆCC0(ใใใชใใฏใใกใคใณ)ใงใใ | |
""") | |
gallery = gr.Gallery(type="filepath", | |
value=["assets/black_256x256.webp", | |
"assets/grayscale_256x256.webp", | |
"assets/DSC_0108.webp", | |
"assets/DSC_0297.webp"], | |
label="Sample Gallery", | |
interactive=False, | |
#height=156, | |
columns=5, | |
allow_preview=False, | |
selected_index=0, | |
preview=False, | |
show_download_button=False, | |
show_share_button=False | |
) | |
# ใฎใฃใฉใชใผๅ ใฎ็ปๅใ้ธๆๆ | |
gallery.select(gallery_selected, | |
inputs=[gallery], | |
outputs=[image_input], | |
show_api=False | |
) | |
submit_button.click( | |
submit_clicked, | |
inputs=[image_input, algorithm_type], | |
outputs=[image_overlay_jet, image_overlay_hot] | |
) | |
gr.Markdown(f""" | |
Python {sys.version} | |
App {get_package_version()} | |
""") | |
demo.queue(default_concurrency_limit=5) | |
log.info(f"#ใขใใช่ตทๅๅฎไบ({watch.elapsed:.3f}s)ใขใใชใ็ตไบใใใซใฏCtrl+Cใญใผใๅ ฅๅใใฆใใ ใใใ") | |
log.debug("reload") | |
if __name__ == "__main__": | |
# ใขใใชใ่ตทๅใใพใใ | |
# https://www.gradio.app/docs/gradio/blocks#blocks-launch | |
demo.launch( | |
inbrowser=args.inbrowser, | |
share=args.share, | |
server_port=args.server_port, | |
max_file_size=args.max_file_size, | |
) | |