Spaces:
Sleeping
Sleeping
NikhilJoson
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,11 +1,11 @@
|
|
1 |
-
#Importing required libraries
|
2 |
-
import spaces
|
3 |
import gradio as gr
|
4 |
-
import random
|
5 |
from PIL import Image
|
|
|
6 |
|
7 |
# Define pixel sizes for different levels
|
8 |
pixel_sizes = [128, 96, 64, 32, 24, 16, 12, 8, 4, 2]
|
|
|
9 |
# Function to pixelate an image
|
10 |
def pixelate(image, pixel_size):
|
11 |
# Reduce the image size
|
@@ -13,67 +13,84 @@ def pixelate(image, pixel_size):
|
|
13 |
# Scale back to original size
|
14 |
return small.resize(image.size, Image.Resampling.NEAREST)
|
15 |
|
16 |
-
#
|
17 |
celeb_list = ["Tom Cruise", "Jake Gyllenhal", "Natalie Portman", "Aubrey Plaza", "Oscar Isaac", "Kate Winslet", "Ellen DeGeneres"]
|
18 |
celeb_folder = {
|
19 |
-
"Tom Cruise"
|
20 |
-
"Jake Gyllenhal"
|
21 |
-
"Natalie Portman"
|
22 |
-
"
|
23 |
-
"Oscar Isaac"
|
24 |
-
"
|
25 |
-
"
|
26 |
}
|
27 |
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
global curr_size
|
32 |
-
|
33 |
-
if next:
|
34 |
-
index = celeb_list.index(photo) + 1
|
35 |
-
photo = celeb_list[index]
|
36 |
-
|
37 |
-
print(f"Processing {photo}")
|
38 |
-
image_path = folder[photo]
|
39 |
-
img = Image.open(BytesIO(image_path))
|
40 |
-
|
41 |
-
for curr_size in pixel_sizes:
|
42 |
-
if curr_size<prev_size:
|
43 |
-
result_img = pixelate(img, curr_size)
|
44 |
-
photo_answer = photo
|
45 |
-
return result_img, photo
|
46 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
47 |
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
55 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
56 |
|
57 |
-
|
58 |
-
|
|
|
59 |
gr.Markdown(MARKDOWN)
|
60 |
-
|
61 |
-
Old_size = curr_size
|
62 |
-
Actual_Answer = photo_answer
|
63 |
-
if_next=True
|
64 |
-
|
65 |
with gr.Row():
|
66 |
with gr.Column(scale=1):
|
67 |
-
pixelated_image = gr.Image(type='pil',
|
68 |
-
with gr.Accordion("Answer", open=False):
|
69 |
-
answer = gr.Textbox()
|
70 |
|
71 |
-
|
72 |
-
|
73 |
-
|
|
|
74 |
|
75 |
-
|
76 |
-
|
77 |
-
|
|
|
78 |
|
79 |
-
demo.launch(debug=
|
|
|
1 |
+
# Importing required libraries
|
|
|
2 |
import gradio as gr
|
|
|
3 |
from PIL import Image
|
4 |
+
import random
|
5 |
|
6 |
# Define pixel sizes for different levels
|
7 |
pixel_sizes = [128, 96, 64, 32, 24, 16, 12, 8, 4, 2]
|
8 |
+
|
9 |
# Function to pixelate an image
|
10 |
def pixelate(image, pixel_size):
|
11 |
# Reduce the image size
|
|
|
13 |
# Scale back to original size
|
14 |
return small.resize(image.size, Image.Resampling.NEAREST)
|
15 |
|
16 |
+
# List of celebrities and folder paths
|
17 |
celeb_list = ["Tom Cruise", "Jake Gyllenhal", "Natalie Portman", "Aubrey Plaza", "Oscar Isaac", "Kate Winslet", "Ellen DeGeneres"]
|
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 |
+
current_index = 0
|
30 |
+
current_pixel_size = 256
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
|
32 |
+
def clear_and_start(prev_size=256):
|
33 |
+
"""
|
34 |
+
Resets the current image and returns the first level of pixelation.
|
35 |
+
"""
|
36 |
+
global current_index, current_pixel_size
|
37 |
+
current_pixel_size = prev_size
|
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
|
45 |
|
46 |
+
def next_image(prev_size):
|
47 |
+
"""
|
48 |
+
Moves to the next celebrity and pixelates the image.
|
49 |
+
"""
|
50 |
+
global current_index, current_pixel_size
|
51 |
+
current_index = (current_index + 1) % len(celeb_list) # Loop through the list
|
52 |
+
current_pixel_size = prev_size
|
53 |
+
celebrity = celeb_list[current_index]
|
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
|
60 |
|
61 |
+
def progressive_clear(pixel_size):
|
62 |
+
"""
|
63 |
+
Progressively clears the pixelation of the current image.
|
64 |
+
"""
|
65 |
+
global current_pixel_size
|
66 |
+
current_pixel_size = max(pixel_size - 32, 2) # Decrease pixel size for better clarity
|
67 |
+
celebrity = celeb_list[current_index]
|
68 |
+
image_path = celeb_folder[celebrity]
|
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
|
74 |
|
75 |
+
# Gradio App Layout
|
76 |
+
MARKDOWN = "## Guess the Celebrity before the Image Clears Up!"
|
77 |
+
with gr.Blocks() as demo:
|
78 |
gr.Markdown(MARKDOWN)
|
79 |
+
|
|
|
|
|
|
|
|
|
80 |
with gr.Row():
|
81 |
with gr.Column(scale=1):
|
82 |
+
pixelated_image = gr.Image(type='pil', label='Pixelated Image')
|
83 |
+
with gr.Accordion("Reveal Answer", open=False):
|
84 |
+
answer = gr.Textbox(label="Current Celebrity")
|
85 |
|
86 |
+
with gr.Column(scale=1):
|
87 |
+
Start_button = gr.Button(value='Start', variant='primary')
|
88 |
+
Clear_button = gr.Button(value='Clear More', variant='secondary')
|
89 |
+
Next_button = gr.Button(value='Next Image', variant='success')
|
90 |
|
91 |
+
# Define button actions
|
92 |
+
Start_button.click(fn=clear_and_start, inputs=[], outputs=[pixelated_image, answer])
|
93 |
+
Clear_button.click(fn=progressive_clear, inputs=[gr.Number(value=current_pixel_size)], outputs=[pixelated_image, answer])
|
94 |
+
Next_button.click(fn=next_image, inputs=[gr.Number(value=current_pixel_size)], outputs=[pixelated_image, answer])
|
95 |
|
96 |
+
demo.launch(debug=True, show_error=True)
|