Spaces:
Sleeping
Sleeping
NikhilJoson
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,96 +1,67 @@
|
|
1 |
# Importing required libraries
|
2 |
import gradio as gr
|
3 |
from PIL import Image
|
4 |
-
import
|
5 |
|
6 |
-
# Define
|
7 |
-
|
8 |
|
9 |
-
#
|
10 |
-
|
11 |
-
# Reduce the image size
|
12 |
-
small = image.resize((image.size[0] // pixel_size, image.size[1] // pixel_size), Image.Resampling.NEAREST)
|
13 |
-
# Scale back to original size
|
14 |
-
return small.resize(image.size, Image.Resampling.NEAREST)
|
15 |
|
16 |
-
#
|
17 |
-
|
18 |
-
celeb_folder = {
|
19 |
-
"Tom Cruise": "./Celebs/TomCruise.jpeg",
|
20 |
-
"Jake Gyllenhal": "./Celebs/JakeGyllenhal.jpg",
|
21 |
-
"Natalie Portman": "./Celebs/NataliePortman.png",
|
22 |
-
"Aubrey Plaza": "./Celebs/AubreyPlaza.jpg",
|
23 |
-
"Oscar Isaac": "./Celebs/OscarIsaac.jpg",
|
24 |
-
"Kate Winslet": "./Celebs/KateWinslet.jpg",
|
25 |
-
"Ellen DeGeneres": "./Celebs/EllenDeGeneres.jpg"
|
26 |
-
}
|
27 |
|
28 |
# Initialize global variables
|
29 |
-
|
|
|
30 |
current_pixel_size = 256
|
31 |
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
celebrity = celeb_list[current_index]
|
39 |
-
image_path = celeb_folder[celebrity]
|
40 |
-
|
41 |
-
# Open and pixelate the image
|
42 |
-
img = Image.open(image_path)
|
43 |
-
result_img = pixelate(img, current_pixel_size)
|
44 |
-
return result_img, celebrity, current_pixel_size
|
45 |
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
image_path = celeb_folder[celebrity]
|
55 |
-
|
56 |
-
# Open and pixelate the image
|
57 |
-
img = Image.open(image_path)
|
58 |
-
result_img = pixelate(img, current_pixel_size)
|
59 |
-
return result_img, celebrity, current_pixel_size
|
60 |
|
61 |
-
|
62 |
-
|
63 |
-
Progressively clears the pixelation of the current image.
|
64 |
-
"""
|
65 |
global current_pixel_size
|
66 |
-
current_pixel_size
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
# Open and pixelate the image
|
71 |
-
img = Image.open(image_path)
|
72 |
-
result_img = pixelate(img, current_pixel_size)
|
73 |
-
return result_img, celebrity, current_pixel_size
|
74 |
|
75 |
-
#
|
76 |
-
|
|
|
|
|
|
|
77 |
with gr.Blocks(theme=gr.themes.Ocean()) as demo:
|
78 |
-
gr.Markdown(
|
79 |
-
|
|
|
80 |
with gr.Row():
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
answer = gr.Textbox(label="Current Celebrity")
|
85 |
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
Next_button.click(fn=next_image, inputs=[gr.State(current_pixel_size)], outputs=[pixelated_image, answer, gr.State(current_pixel_size)])
|
95 |
|
96 |
-
demo.launch(debug=True, show_error=True)
|
|
|
1 |
# Importing required libraries
|
2 |
import gradio as gr
|
3 |
from PIL import Image
|
4 |
+
import os
|
5 |
|
6 |
+
# Define the folder containing images
|
7 |
+
CELEB_FOLDER = "./Celebs"
|
8 |
|
9 |
+
# Get the list of celebrity images from the folder
|
10 |
+
celebs = {os.path.splitext(file)[0]: os.path.join(CELEB_FOLDER, file) for file in os.listdir(CELEB_FOLDER)}
|
|
|
|
|
|
|
|
|
11 |
|
12 |
+
# Define pixel sizes for different levels of clarity
|
13 |
+
PIXEL_SIZES = [128, 96, 64, 48, 32, 24, 16, 12, 8, 4, 2]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
14 |
|
15 |
# Initialize global variables
|
16 |
+
current_image = None
|
17 |
+
current_name = None
|
18 |
current_pixel_size = 256
|
19 |
|
20 |
+
# Function to pixelate an image
|
21 |
+
def pixelate(image, pixel_size):
|
22 |
+
# Reduce the image size
|
23 |
+
small = image.resize((image.size[0] // pixel_size, image.size[1] // pixel_size), Image.Resampling.NEAREST)
|
24 |
+
# Scale back to the original size
|
25 |
+
return small.resize(image.size, Image.Resampling.NEAREST)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
26 |
|
27 |
+
# Start a new game
|
28 |
+
def start_game():
|
29 |
+
global current_image, current_name, current_pixel_size
|
30 |
+
current_name, image_path = random.choice(list(celebs.items()))
|
31 |
+
current_image = Image.open(image_path)
|
32 |
+
current_pixel_size = PIXEL_SIZES[0]
|
33 |
+
pixelated_image = pixelate(current_image, current_pixel_size)
|
34 |
+
return pixelated_image, "Image Loaded. Guess the Celebrity!"
|
|
|
|
|
|
|
|
|
|
|
|
|
35 |
|
36 |
+
# Reveal more of the image
|
37 |
+
def clear_more():
|
|
|
|
|
38 |
global current_pixel_size
|
39 |
+
if current_pixel_size > PIXEL_SIZES[-1]:
|
40 |
+
current_pixel_size = PIXEL_SIZES[PIXEL_SIZES.index(current_pixel_size) + 1]
|
41 |
+
pixelated_image = pixelate(current_image, current_pixel_size)
|
42 |
+
return pixelated_image, "Getting clearer! Keep guessing!"
|
|
|
|
|
|
|
|
|
43 |
|
44 |
+
# Reveal the answer
|
45 |
+
def reveal_answer():
|
46 |
+
return f"The correct answer is: {current_name}!"
|
47 |
+
|
48 |
+
# Create the Gradio interface
|
49 |
with gr.Blocks(theme=gr.themes.Ocean()) as demo:
|
50 |
+
gr.Markdown("# 🎭 Guess the Celebrity Game!")
|
51 |
+
gr.Markdown("Can you identify the celebrity as the image becomes clearer?")
|
52 |
+
|
53 |
with gr.Row():
|
54 |
+
pixelated_image = gr.Image(type="pil", label="Pixelated Image")
|
55 |
+
answer = gr.Textbox(label="Your Guess", placeholder="Type your guess here...")
|
56 |
+
result = gr.Label(label="Game Status")
|
|
|
57 |
|
58 |
+
with gr.Row():
|
59 |
+
start_button = gr.Button("Start Game", variant='primary')
|
60 |
+
clear_button = gr.Button("Clear More", variant='secondary')
|
61 |
+
reveal_button = gr.Button("Reveal Answer", variant='success')
|
62 |
|
63 |
+
start_button.click(start_game, inputs=[], outputs=[pixelated_image, result])
|
64 |
+
clear_button.click(clear_more, inputs=[], outputs=[pixelated_image, result])
|
65 |
+
reveal_button.click(reveal_answer, inputs=[], outputs=result)
|
|
|
66 |
|
67 |
+
demo.launch(debug=True, show_error=True)
|