{ "cells": [ { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "Running on local URL: http://127.0.0.1:7863\n", "Running on public URL: https://106ff53aeff217d73d.gradio.live\n", "\n", "This share link expires in 72 hours. For free permanent hosting and GPU upgrades, run `gradio deploy` from Terminal to deploy to Spaces (https://huggingface.co/spaces)\n" ] }, { "data": { "text/html": [ "
" ], "text/plain": [ "" ] }, "metadata": {}, "output_type": "display_data" }, { "data": { "text/plain": [] }, "execution_count": 3, "metadata": {}, "output_type": "execute_result" } ], "source": [ "import os\n", "import random\n", "import cv2\n", "import gradio as gr\n", "from PIL import Image\n", "import numpy as np\n", "\n", "def generate_and_delete_word_image(word):\n", " \"\"\"Generates a combined image, displays it, and deletes it afterward.\n", "\n", " Args:\n", " word: The user-provided word (uppercase).\n", "\n", " Returns:\n", " The image data for displaying.\n", " \"\"\"\n", "\n", " dataset_path = \"C:\\DYPIU\\___________________CODE___________________\\Random Word Genrator\\dataset\" # Adjust for your dataset path\n", "\n", " resized_images = []\n", " for letter in word:\n", " letter_folder_path = os.path.join(dataset_path, letter)\n", "\n", " # Check if the folder exists\n", " if not os.path.isdir(letter_folder_path):\n", " print(\"Folder not found for the letter:\", letter)\n", " continue\n", "\n", " images_in_folder = [img for img in os.listdir(letter_folder_path)\n", " if img.endswith(\".jpg\") or img.endswith(\".png\")]\n", "\n", " if not images_in_folder:\n", " print(\"No images found for the letter:\", letter)\n", " continue\n", "\n", " # Randomly select an image from the folder\n", " random_image_name = random.choice(images_in_folder)\n", " image_path = os.path.join(letter_folder_path, random_image_name)\n", "\n", " try:\n", " # Load image using PIL, ensuring RGB mode\n", " image = Image.open(image_path).convert(\"RGB\")\n", " # Convert to NumPy array for OpenCV\n", " resized_image = np.array(image.resize((250, 250)))\n", " except Exception as e:\n", " print(f\"Error loading image: {e}\")\n", " continue\n", "\n", " resized_images.append(resized_image)\n", "\n", " if not resized_images:\n", " print(\"No images found for the input word.\")\n", " return None\n", "\n", " concatenated_image = cv2.hconcat(resized_images)\n", "\n", " # Display the concatenated image\n", " # cv2.imshow(\"Concatenated Image\", concatenated_image)\n", " # cv2.waitKey(0)\n", " # cv2.destroyAllWindows()\n", "\n", " # Return the image data\n", " return concatenated_image\n", "\n", "# Gradio interface with clear explanations\n", "interface = gr.Interface(\n", " fn=generate_and_delete_word_image,\n", " inputs=\"text\", # User input for the word\n", " outputs=\"image\", # Output as an image\n", " title=\"Random Word Image Generator\",\n", " description=\"Enter a word (uppercase letters only) to generate a combined image using random letters from the dataset. The image will be displayed below.\",\n", " \n", " \n", " css=\"\"\"\n", " body {\n", " background-color: white;\n", " font-family: Arial, sans-serif;\n", " }\n", " .gr-interactive-component h1 {\n", " font-size: 20px;\n", " margin-bottom: 10px;\n", " }\n", " \"\"\",\n", " \n", ")\n", "\n", "\n", "interface.launch(share=True) # Launch the Gradio interface and share it publicly\n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.1" } }, "nbformat": 4, "nbformat_minor": 2 }