File size: 4,430 Bytes
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
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.3
GENERATORS = ['itti', 'deepgaze']

MARKDOWN = """
<h1 style='text-align: center'>Saliency Ranking: Itti vs. Deepgaze</h1>
"""

IMAGE_EXAMPLES = [
    ['https://media.roboflow.com/supervision/image-examples/people-walking.png', 9],
    ['https://media.roboflow.com/supervision/image-examples/vehicles.png', 9],
    ['https://media.roboflow.com/supervision/image-examples/basketball-1.png', 9],
]

def detect_and_annotate(image: np.ndarray, 
                        GRID_SIZE: int, 
                        generator: str, 
                        ALPHA:float =ALPHA)-> np.ndarray:
    # 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=1)

    # 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 and GRIDSIZE < 4:
        GRIDSIZE = 9

    itti_saliency_map, itti_heatmap = detect_and_annotate(
        input_image, sara, GRIDSIZE, 'itti')
    deepgaze_saliency_map, deepgaze_heatmap = detect_and_annotate(
        input_image, sara, GRIDSIZE, 'deepgaze')

    return (
        itti_saliency_map,
        itti_heatmap,
        deepgaze_saliency_map,
        deepgaze_heatmap,
    )

grid_size_Component = gr.Slider(
    minimum=4,
    maximum=70,
    value=9,
    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'
        )
    with gr.Row():
        itti_saliency_map = gr.Image(
            type='pil',
            label='Itti Saliency Map'
        )
        itti_heatmap = gr.Image(
            type='pil',
            label='Itti Saliency Ranking Heatmap'
        )
    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,
            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,
            deepgaze_saliency_map,
            deepgaze_heatmap,
        ]
    )

demo.launch(debug=False, show_error=True, max_threads=1)