Spaces:
Running
Running
File size: 5,855 Bytes
c9baa67 ebf34ca c9baa67 952a171 1a1fa99 04c23df 1a1fa99 c9baa67 e9d36f9 c9baa67 69f4b00 1a1fa99 e298838 c9baa67 2791be8 c9baa67 1a1fa99 c9baa67 cb6a4a8 c9baa67 d144755 1a1fa99 c9baa67 1a1fa99 c9baa67 cb6a4a8 e9d36f9 c9baa67 1a1fa99 c9baa67 0fa21cf c9baa67 1a1fa99 2e394ee 0fa21cf 2e394ee 1a1fa99 c9baa67 1a1fa99 c9baa67 1a1fa99 c9baa67 |
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 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 |
from typing import Tuple
import gradio as gr
import numpy as np
import cv2
import SaRa.saraRC1 as sara
import warnings
warnings.filterwarnings("ignore")
ALPHA = 0.4
GENERATORS = ['itti', 'deepgaze']
MARKDOWN = """
<h1 style='text-align: center'>Saliency Ranking π</h1>
Saliency Ranking is a fundamental π **Computer Vision** π process aimed at discerning the most visually significant features within an image πΌοΈ.
π This demo showcases the **SaRa (Saliency-Driven Object Ranking)** model for Saliency Ranking π―, which can efficiently rank the visual saliency of an image without requiring any training. πΌοΈ
This technique is configured on the Saliency Map generator model by Itti, which works based on the primate visual cortex π§ , and can work with or without depth information π.
<div style="display: flex; align-items: center;">
<a href="https://github.com/dylanseychell/SaliencyRanking" style="margin-right: 10px;">
<img src="https://badges.aleen42.com/src/github.svg">
</a>
<a href="https://github.com/mbar0075/SaRa" style="margin-right: 10px;">
<img src="https://badges.aleen42.com/src/github.svg">
</a>
<a href="https://github.com/matthewkenely/ICT3909" style="margin-right: 10px;">
<img src="https://badges.aleen42.com/src/github.svg">
</a>
</div>
"""
IMAGE_EXAMPLES = [
['https://media.roboflow.com/supervision/image-examples/people-walking.png', 32],
['https://media.roboflow.com/supervision/image-examples/vehicles.png', 32],
['https://media.roboflow.com/supervision/image-examples/basketball-1.png', 32],
]
def detect_and_annotate(image,
GRID_SIZE,
generator,
ALPHA=ALPHA,
mode=1)-> np.ndarray:
# Converting from PIL to OpenCV
image = np.array(image)
# Convert image from BGR to RGB
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
# Copy and convert the image for sara processing
sara_image = image.copy()
# sara_image = cv2.cvtColor(sara_image, cv2.COLOR_RGB2BGR)
# Resetting sara
sara.reset()
# Running sara (Original implementation on itti)
sara_info = sara.return_sara(sara_image, GRID_SIZE, generator, mode=mode)
# Generate saliency map
saliency_map = sara.return_saliency(image, generator=generator)
# Resize saliency map to match the image size
saliency_map = cv2.resize(saliency_map, (image.shape[1], image.shape[0]))
# Apply color map and convert to RGB
saliency_map = cv2.applyColorMap(saliency_map, cv2.COLORMAP_JET)
saliency_map = cv2.cvtColor(saliency_map, cv2.COLOR_BGR2RGB)
# Overlay the saliency map on the original image
saliency_map = cv2.addWeighted(saliency_map, ALPHA, image, 1-ALPHA, 0)
# Extract and convert heatmap to RGB
heatmap = sara_info[0]
heatmap = cv2.cvtColor(heatmap, cv2.COLOR_BGR2RGB)
return saliency_map, heatmap
def process_image(
input_image: np.ndarray,
GRIDSIZE: int,
) -> Tuple[np.ndarray, np.ndarray, np.ndarray, np.ndarray]:
# Validate GRID_SIZE
if GRIDSIZE is None or GRIDSIZE < 3:
GRIDSIZE = 9
itti_saliency_map, itti_heatmap = detect_and_annotate(
input_image, GRIDSIZE, 'itti')
_, itti_heatmap2 = detect_and_annotate(
input_image, GRIDSIZE, 'itti', mode=2)
# deepgaze_saliency_map, deepgaze_heatmap = detect_and_annotate(
# input_image, GRIDSIZE, 'deepgaze')
return (
itti_saliency_map,
itti_heatmap,
itti_heatmap2,
# deepgaze_saliency_map,
# deepgaze_heatmap,
)
grid_size_Component = gr.Slider(
minimum=3,
maximum=100,
value=32,
step=1,
label="Grid Size",
info=(
"The grid size for the Saliency Ranking (SaRa) model. The grid size determines "
"the number of regions the image is divided into. A higher grid size results in "
"more regions and a lower grid size results in fewer regions. The default grid "
"size is 9."
))
with gr.Blocks() as demo:
gr.Markdown(MARKDOWN)
with gr.Accordion("Configuration", open=False):
with gr.Row():
grid_size_Component.render()
with gr.Row():
input_image_component = gr.Image(
type='pil',
label='Input'
)
itti_saliency_map = gr.Image(
type='pil',
label='Itti Saliency Map'
)
with gr.Row():
itti_heatmap = gr.Image(
type='pil',
label='Saliency Ranking Heatmap 1'
)
itti_heatmap2 = gr.Image(
type='pil',
label='Saliency Ranking Heatmap 2'
)
# with gr.Row():
# deepgaze_saliency_map = gr.Image(
# type='pil',
# label='DeepGaze Saliency Map'
# )
# deepgaze_heatmap = gr.Image(
# type='pil',
# label='DeepGaze Saliency Ranking Heatmap'
# )
submit_button_component = gr.Button(
value='Submit',
scale=1,
variant='primary'
)
gr.Examples(
fn=process_image,
examples=IMAGE_EXAMPLES,
inputs=[
input_image_component,
grid_size_Component,
],
outputs=[
itti_saliency_map,
itti_heatmap,
itti_heatmap2,
# deepgaze_saliency_map,
# deepgaze_heatmap,
]
)
submit_button_component.click(
fn=process_image,
inputs=[
input_image_component,
grid_size_Component,
],
outputs=[
itti_saliency_map,
itti_heatmap,
itti_heatmap2,
# deepgaze_saliency_map,
# deepgaze_heatmap,
]
)
demo.launch(debug=False, show_error=True, max_threads=1) |