Limit image size & Modify Interface
Browse files
app.py
CHANGED
@@ -7,8 +7,8 @@ from skimage.feature import graycomatrix, graycoprops
|
|
7 |
from torchvision import transforms
|
8 |
import os
|
9 |
|
10 |
-
NUM_ROUNDS = 10
|
11 |
-
PROB_THRESHOLD = 0.3
|
12 |
|
13 |
# Load the model
|
14 |
model = torch.jit.load("SuSy.pt")
|
@@ -34,7 +34,7 @@ def process_image(image):
|
|
34 |
patch = image.crop((x, y, x + patch_size, y + patch_size))
|
35 |
patches[i * num_patches_y + j] = np.array(patch)
|
36 |
|
37 |
-
# Compute the most relevant patches
|
38 |
dissimilarity_scores = []
|
39 |
for patch in patches:
|
40 |
transform_patch = transforms.Compose([transforms.PILToTensor(), transforms.Grayscale()])
|
@@ -66,6 +66,20 @@ def process_image(image):
|
|
66 |
|
67 |
return sorted_probs
|
68 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
69 |
|
70 |
class GameState:
|
71 |
def __init__(self):
|
@@ -77,6 +91,7 @@ class GameState:
|
|
77 |
self.is_game_active = False
|
78 |
self.last_results = None
|
79 |
self.waiting_for_input = True
|
|
|
80 |
|
81 |
def reset(self):
|
82 |
self.__init__()
|
@@ -131,23 +146,25 @@ def start_game():
|
|
131 |
game_state.game_images = load_images()
|
132 |
game_state.is_game_active = True
|
133 |
game_state.waiting_for_input = True
|
134 |
-
|
|
|
|
|
|
|
135 |
|
136 |
return (
|
137 |
-
gr.update(value=
|
138 |
gr.update(visible=False), # Hide start button
|
139 |
-
gr.update(
|
140 |
-
gr.update(visible=True, interactive=True), # Show
|
141 |
create_score_html(),
|
142 |
gr.update(visible=False) # Hide feedback
|
143 |
)
|
144 |
|
145 |
def submit_guess(user_guess):
|
146 |
-
if not game_state.is_game_active or not game_state.waiting_for_input
|
147 |
-
return [gr.update()] * 6
|
148 |
-
|
149 |
-
|
150 |
-
model_prediction = process_image(current_image)
|
151 |
correct_answer = "Real" if "real_images" in game_state.game_images[game_state.current_round] else "Fake"
|
152 |
|
153 |
# Determine model's guess based on probabilities
|
@@ -173,23 +190,25 @@ def submit_guess(user_guess):
|
|
173 |
if game_state.current_round >= game_state.total_rounds:
|
174 |
game_state.is_game_active = False
|
175 |
return (
|
176 |
-
gr.update(value=None, visible=False),
|
177 |
-
gr.update(visible=True),
|
178 |
-
gr.update(
|
179 |
-
gr.update(visible=False),
|
180 |
create_score_html(),
|
181 |
gr.update(visible=True, value="<div style='text-align: center; margin-top: 20px; font-size: 1.2em;'>Game Over! Click 'Start New Game' to play again.</div>")
|
182 |
)
|
183 |
-
|
184 |
# Continue to next round
|
185 |
-
|
|
|
|
|
186 |
return (
|
187 |
-
gr.update(value=
|
188 |
-
gr.update(visible=False),
|
189 |
-
gr.update(
|
190 |
-
gr.update(visible=True, interactive=True),
|
191 |
create_score_html(),
|
192 |
-
gr.update(visible=False)
|
193 |
)
|
194 |
|
195 |
# Custom CSS
|
@@ -203,6 +222,15 @@ custom_css = """
|
|
203 |
max-width: 200px;
|
204 |
margin: 0 auto;
|
205 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
206 |
"""
|
207 |
|
208 |
# Define Gradio interface
|
@@ -236,21 +264,23 @@ with gr.Blocks(css=custom_css) as iface:
|
|
236 |
interactive=False,
|
237 |
visible=False
|
238 |
)
|
239 |
-
guess_input = gr.Radio(
|
240 |
-
choices=["Real", "Fake"],
|
241 |
-
label="Your Guess",
|
242 |
-
interactive=False,
|
243 |
-
visible=False
|
244 |
-
)
|
245 |
-
submit_button = gr.Button(
|
246 |
-
"Submit Guess",
|
247 |
-
visible=False,
|
248 |
-
variant="primary"
|
249 |
-
)
|
250 |
|
251 |
with gr.Column(scale=1):
|
252 |
score_display = gr.HTML()
|
253 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
254 |
with gr.Row():
|
255 |
with gr.Column(elem_id="start-button"):
|
256 |
start_button = gr.Button("Start New Game", variant="primary", size="sm")
|
@@ -263,21 +293,32 @@ with gr.Blocks(css=custom_css) as iface:
|
|
263 |
outputs=[
|
264 |
image_display,
|
265 |
start_button,
|
266 |
-
|
267 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
268 |
score_display,
|
269 |
feedback_display
|
270 |
]
|
271 |
)
|
272 |
|
273 |
-
|
274 |
-
fn=submit_guess,
|
275 |
-
inputs=[guess_input],
|
276 |
outputs=[
|
277 |
image_display,
|
278 |
start_button,
|
279 |
-
|
280 |
-
|
281 |
score_display,
|
282 |
feedback_display
|
283 |
]
|
|
|
7 |
from torchvision import transforms
|
8 |
import os
|
9 |
|
10 |
+
NUM_ROUNDS = 10
|
11 |
+
PROB_THRESHOLD = 0.3
|
12 |
|
13 |
# Load the model
|
14 |
model = torch.jit.load("SuSy.pt")
|
|
|
34 |
patch = image.crop((x, y, x + patch_size, y + patch_size))
|
35 |
patches[i * num_patches_y + j] = np.array(patch)
|
36 |
|
37 |
+
# Compute the most relevant patches
|
38 |
dissimilarity_scores = []
|
39 |
for patch in patches:
|
40 |
transform_patch = transforms.Compose([transforms.PILToTensor(), transforms.Grayscale()])
|
|
|
66 |
|
67 |
return sorted_probs
|
68 |
|
69 |
+
def resize_image(image, max_size=512):
|
70 |
+
"""Resize image to have a maximum dimension of max_size while preserving aspect ratio"""
|
71 |
+
width, height = image.size
|
72 |
+
if width > height:
|
73 |
+
if width > max_size:
|
74 |
+
new_width = max_size
|
75 |
+
new_height = int(height * (max_size / width))
|
76 |
+
else:
|
77 |
+
if height > max_size:
|
78 |
+
new_height = max_size
|
79 |
+
new_width = int(width * (max_size / height))
|
80 |
+
else:
|
81 |
+
return image
|
82 |
+
return image.resize((new_width, new_height), Image.Resampling.LANCZOS)
|
83 |
|
84 |
class GameState:
|
85 |
def __init__(self):
|
|
|
91 |
self.is_game_active = False
|
92 |
self.last_results = None
|
93 |
self.waiting_for_input = True
|
94 |
+
self.original_image = None
|
95 |
|
96 |
def reset(self):
|
97 |
self.__init__()
|
|
|
146 |
game_state.game_images = load_images()
|
147 |
game_state.is_game_active = True
|
148 |
game_state.waiting_for_input = True
|
149 |
+
|
150 |
+
# Store original image and create resized version for display
|
151 |
+
game_state.original_image = Image.open(game_state.game_images[0])
|
152 |
+
display_image = resize_image(game_state.original_image)
|
153 |
|
154 |
return (
|
155 |
+
gr.update(value=display_image, visible=True), # Show resized image
|
156 |
gr.update(visible=False), # Hide start button
|
157 |
+
gr.update(visible=True, interactive=True), # Show Real button
|
158 |
+
gr.update(visible=True, interactive=True), # Show Fake button
|
159 |
create_score_html(),
|
160 |
gr.update(visible=False) # Hide feedback
|
161 |
)
|
162 |
|
163 |
def submit_guess(user_guess):
|
164 |
+
if not game_state.is_game_active or not game_state.waiting_for_input:
|
165 |
+
return [gr.update()] * 6
|
166 |
+
|
167 |
+
model_prediction = process_image(game_state.original_image)
|
|
|
168 |
correct_answer = "Real" if "real_images" in game_state.game_images[game_state.current_round] else "Fake"
|
169 |
|
170 |
# Determine model's guess based on probabilities
|
|
|
190 |
if game_state.current_round >= game_state.total_rounds:
|
191 |
game_state.is_game_active = False
|
192 |
return (
|
193 |
+
gr.update(value=None, visible=False),
|
194 |
+
gr.update(visible=True),
|
195 |
+
gr.update(visible=False),
|
196 |
+
gr.update(visible=False),
|
197 |
create_score_html(),
|
198 |
gr.update(visible=True, value="<div style='text-align: center; margin-top: 20px; font-size: 1.2em;'>Game Over! Click 'Start New Game' to play again.</div>")
|
199 |
)
|
200 |
+
|
201 |
# Continue to next round
|
202 |
+
game_state.original_image = Image.open(game_state.game_images[game_state.current_round])
|
203 |
+
display_image = resize_image(game_state.original_image)
|
204 |
+
|
205 |
return (
|
206 |
+
gr.update(value=display_image, visible=True),
|
207 |
+
gr.update(visible=False),
|
208 |
+
gr.update(visible=True, interactive=True),
|
209 |
+
gr.update(visible=True, interactive=True),
|
210 |
create_score_html(),
|
211 |
+
gr.update(visible=False)
|
212 |
)
|
213 |
|
214 |
# Custom CSS
|
|
|
222 |
max-width: 200px;
|
223 |
margin: 0 auto;
|
224 |
}
|
225 |
+
#guess-buttons {
|
226 |
+
display: flex;
|
227 |
+
gap: 10px;
|
228 |
+
justify-content: center;
|
229 |
+
margin-top: 20px;
|
230 |
+
}
|
231 |
+
.guess-button {
|
232 |
+
min-width: 120px;
|
233 |
+
}
|
234 |
"""
|
235 |
|
236 |
# Define Gradio interface
|
|
|
264 |
interactive=False,
|
265 |
visible=False
|
266 |
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
267 |
|
268 |
with gr.Column(scale=1):
|
269 |
score_display = gr.HTML()
|
270 |
+
with gr.Row(elem_id="guess-buttons"):
|
271 |
+
real_button = gr.Button(
|
272 |
+
"Real",
|
273 |
+
visible=False,
|
274 |
+
variant="primary",
|
275 |
+
elem_classes=["guess-button"]
|
276 |
+
)
|
277 |
+
fake_button = gr.Button(
|
278 |
+
"Fake",
|
279 |
+
visible=False,
|
280 |
+
variant="secondary",
|
281 |
+
elem_classes=["guess-button"]
|
282 |
+
)
|
283 |
+
|
284 |
with gr.Row():
|
285 |
with gr.Column(elem_id="start-button"):
|
286 |
start_button = gr.Button("Start New Game", variant="primary", size="sm")
|
|
|
293 |
outputs=[
|
294 |
image_display,
|
295 |
start_button,
|
296 |
+
real_button,
|
297 |
+
fake_button,
|
298 |
+
score_display,
|
299 |
+
feedback_display
|
300 |
+
]
|
301 |
+
)
|
302 |
+
|
303 |
+
real_button.click(
|
304 |
+
fn=lambda: submit_guess("Real"),
|
305 |
+
outputs=[
|
306 |
+
image_display,
|
307 |
+
start_button,
|
308 |
+
real_button,
|
309 |
+
fake_button,
|
310 |
score_display,
|
311 |
feedback_display
|
312 |
]
|
313 |
)
|
314 |
|
315 |
+
fake_button.click(
|
316 |
+
fn=lambda: submit_guess("Fake"),
|
|
|
317 |
outputs=[
|
318 |
image_display,
|
319 |
start_button,
|
320 |
+
real_button,
|
321 |
+
fake_button,
|
322 |
score_display,
|
323 |
feedback_display
|
324 |
]
|