Spaces:
Runtime error
Runtime error
File size: 5,141 Bytes
d09f4a0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 | {
"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": [
"<div><iframe src=\"https://106ff53aeff217d73d.gradio.live\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
],
"text/plain": [
"<IPython.core.display.HTML object>"
]
},
"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
}
|