File size: 2,748 Bytes
4d2a77d
4dd7634
 
38bc428
4dd7634
 
d09f4a0
4dd7634
 
4d2a77d
4dd7634
 
518144e
4dd7634
 
 
29fdb3c
88a46d3
4dd7634
 
 
 
95c2369
4dd7634
 
 
 
518144e
4dd7634
 
95c2369
4dd7634
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
95c2369
4dd7634
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ed21b04
 
4dd7634
 
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
import os
import random
import cv2
import gradio as gr
from PIL import Image
import numpy as np

def generate_and_delete_word_image(word):
    """Generates a combined image, displays it, and deletes it afterward.

    Args:
        word: The user-provided word (uppercase).

    Returns:
        The image data for displaying.
    """
    word = word.upper()
    dataset_path = "dataset/"  # Adjust for your dataset path
    # sample = "C:\DYPIU\___________________CODE___________________\Random Word Genrator\dataset\samples.jpg"
    resized_images = []
    for letter in word:
        letter_folder_path = os.path.join(dataset_path, letter)

        # Check if the folder exists
        if not os.path.isdir(letter_folder_path):
            print("Folder not found for the letter:", letter)
            continue

        images_in_folder = [img for img in os.listdir(letter_folder_path)
                            if img.endswith(".jpg") or img.endswith(".png")]

        if not images_in_folder:
            print("No images found for the letter:", letter)
            continue

        # Randomly select an image from the folder
        random_image_name = random.choice(images_in_folder)
        image_path = os.path.join(letter_folder_path, random_image_name)

        try:
            # Load image using PIL, ensuring RGB mode
            image = Image.open(image_path).convert("RGB")
            # Convert to NumPy array for OpenCV
            resized_image = np.array(image.resize((250, 250)))
        except Exception as e:
            print(f"Error loading image: {e}")
            continue

        resized_images.append(resized_image)

    if not resized_images:
        print("No images found for the input word.")
        return None

    # sample_img = sample
    concatenated_image = cv2.hconcat(resized_images)

    # Display the concatenated image
    # cv2.imshow("Concatenated Image", concatenated_image)
    # cv2.waitKey(0)
    # cv2.destroyAllWindows()

    # Return the image data
    return concatenated_image

# Gradio interface with clear explanations
interface = gr.Interface(
    fn=generate_and_delete_word_image,
    inputs="text",  # User input for the word
    outputs="image",  # Output as an image
    title="POCKET RANDOM NOTE GENERATOR", # Title
    description="Randsomator allows you to generate random ranson with one click.",
    
    css="""
      botton {
      background: green;
      color: #32B531
      }
      body {
        background-color: black;
        font-family: Arial, sans-serif;
      }
      .gr-interactive-component h1 {
        font-size: 20px;
        margin-bottom: 10px;
      }
    """,
    
)


interface.launch(share=True)  # Launch the Gradio interface and share it publicly