NikhilJoson commited on
Commit
f5e157a
·
verified ·
1 Parent(s): 952541f

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +48 -77
app.py CHANGED
@@ -1,96 +1,67 @@
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
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
- # 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, current_pixel_size
45
 
46
- def next_image(pixel_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 = pixel_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, current_pixel_size
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, current_pixel_size
74
 
75
- # Gradio App Layout
76
- MARKDOWN = "## Guess the Celebrity before the Image Clears Up!"
 
 
 
77
  with gr.Blocks(theme=gr.themes.Ocean()) 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, gr.State(current_pixel_size)])
93
- Clear_button.click(fn=progressive_clear, inputs=[gr.State(current_pixel_size)], outputs=[pixelated_image, answer, gr.State(current_pixel_size)])
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)