Spaces:
Running
Running
# Importing required libraries | |
import gradio as gr | |
from PIL import Image | |
import random | |
import os | |
# Define the folder containing images | |
CHRS_FOLDER = "./Cartoon_Characters" | |
# Get the list of celebrity images from the folder | |
celebs = {os.path.splitext(file)[0]: os.path.join(CHRS_FOLDER, file) for file in os.listdir(CHRS_FOLDER)} | |
# Define pixel sizes for different levels of clarity | |
PIXEL_SIZES = [64, 48, 32, 24, 16, 12, 8, 4, 2] | |
# Initialize global variables | |
current_image = None | |
current_name = None | |
current_pixel_size = 96 | |
used_images = set() # Track used images | |
# Function to pixelate an image | |
def pixelate(image, pixel_size): | |
# Reduce the image size | |
small = image.resize((image.size[0] // pixel_size, image.size[1] // pixel_size), Image.Resampling.NEAREST) | |
# Scale back to the original size | |
return small.resize(image.size, Image.Resampling.NEAREST) | |
# Start a new game | |
def start_game(): | |
global current_image, current_name, current_pixel_size, used_images | |
# Check if all images have been used; if so, reset | |
if len(used_images) == len(celebs): | |
used_images.clear() | |
# Select a random image that has not been used | |
available_images = list(set(celebs.keys()) - used_images) | |
current_name = random.choice(available_images) | |
used_images.add(current_name) | |
# Load the selected image | |
image_path = celebs[current_name] | |
current_image = Image.open(image_path) | |
current_pixel_size = PIXEL_SIZES[0] | |
pixelated_image = pixelate(current_image, current_pixel_size) | |
return pixelated_image, "Image Loaded. Guess the Cartoon Character!" | |
# Reveal more of the image | |
def clear_more(): | |
global current_pixel_size | |
if current_pixel_size > PIXEL_SIZES[-1]: | |
current_pixel_size = PIXEL_SIZES[PIXEL_SIZES.index(current_pixel_size) + 1] | |
pixelated_image = pixelate(current_image, current_pixel_size) | |
return pixelated_image, "Getting clearer! Keep guessing!" | |
# Check the user's guess | |
def check_guess(user_guess): | |
if user_guess.strip().lower() == current_name.strip().lower(): | |
return f"Correct! π You guessed it right! This character's name is {current_name}" | |
else: | |
return "Incorrect. Keep trying!" | |
# Reveal the answer and original image | |
def reveal_answer(): | |
return current_image, f"The correct answer is: {current_name}! π" | |
# Create the Gradio interface | |
with gr.Blocks(theme=gr.themes.Ocean()) as demo: | |
gr.Markdown("# π Guess the Cartoon Characters Game!") | |
gr.Markdown("Can you identify the Cartoon Characters as the image becomes clearer?") | |
with gr.Row(): | |
pixelated_image = gr.Image(type="pil", label="Pixelated Image") | |
answer_input = gr.Textbox(label="Your Guess", placeholder="Type your guess here...") | |
result = gr.Label(label="Game Status") | |
with gr.Row(): | |
start_button = gr.Button("Start Game", variant='huggingface') | |
clear_button = gr.Button("Clear More", variant='secondary') | |
guess_button = gr.Button("Submit Guess", variant='primary') | |
reveal_button = gr.Button("Reveal Answer", variant='success') | |
start_button.click(start_game, inputs=[], outputs=[pixelated_image, result]) | |
clear_button.click(clear_more, inputs=[], outputs=[pixelated_image, result]) | |
guess_button.click(check_guess, inputs=answer_input, outputs=result) | |
reveal_button.click(reveal_answer, inputs=[], outputs=[pixelated_image, result]) | |
demo.launch(debug=True, show_error=True) | |