File size: 4,011 Bytes
ec054fc
 
 
 
 
 
 
 
 
1e56791
ec054fc
 
 
 
 
 
 
 
 
 
 
 
88e2ebd
ec054fc
 
88e2ebd
ec054fc
88e2ebd
ec054fc
 
 
 
 
 
 
 
56e36e2
 
 
 
 
695ab3c
56e36e2
c4810ac
19eac47
56e36e2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4810ac
 
56e36e2
ec054fc
 
c4810ac
ec054fc
c4810ac
 
 
 
 
 
 
 
 
 
 
ec054fc
c4810ac
ec054fc
d5127f7
c4810ac
 
ae2743c
 
 
 
3d87618
ae2743c
88e2ebd
ae2743c
 
 
 
 
d5127f7
c4810ac
ae2743c
ec054fc
 
d5127f7
 
ec054fc
 
d5127f7
ec054fc
 
 
 
 
 
2e79720
4fc2fae
ec054fc
 
 
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

from controlnet_aux import OpenposeDetector
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
from diffusers import UniPCMultistepScheduler
import gradio as gr
import torch
from PIL import Image, ImageDraw, ImageFont
import os 
import cv2
import glob
from PIL import Image
import numpy as np
from diffusers.utils import load_image
import random

# Constants
low_threshold = 100
high_threshold = 200

# Models
pose_model = OpenposeDetector.from_pretrained("lllyasviel/ControlNet")
controlnet = ControlNetModel.from_pretrained(
    "lllyasviel/sd-controlnet-openpose"
)
pipe = StableDiffusionControlNetPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5", controlnet=controlnet, safety_checker=None
)
pipe = pipe.to("cpu")


def get_pose(image):
    return pose_model(image) 



def generate_an_image_from_text(text, text_size_, width, lenght):
    # Create a blank image
    image = Image.new('RGB', (width, lenght), color = (255, 255, 255))
    # Create a drawing object
    draw = ImageDraw.Draw(image)
    # font def 
    dir_path = ''
    # Get a list of all the font files in the directory
    print("start generation")
    font_files = glob.glob(os.path.join(dir_path, '*.ttf'))
    # Get a list of font paths
    font_paths = []
    for font_file in font_files:
        font_paths.append(font_file)
    # Select a random font
    font_path = random.choice(font_paths)
    #print(font_path)
    font = ImageFont.truetype(font_path, text_size_)
    # Get the text size
    text_size = draw.textsize(text, font)
    # Calculate the x and y positions for the text
    x = (image.width - text_size[0]) / 2
    y = (image.height - text_size[1]) / 2
    # Draw the text on the image
    draw.text((x, y), text, fill=(0, 0, 0), font=font)
    print("end generation")

    return image

def to_Canny(image):
    print("start canny")

    # Let's load the popular vermeer image
    image = np.array(image)
    
    low_threshold = 100
    high_threshold = 200
    
    image = cv2.Canny(image, low_threshold, high_threshold)
    image = image[:, :, None]
    image = np.concatenate([image, image, image], axis=2)
    canny_image = Image.fromarray(image)
    print("end canny")

    return canny_image

def inference(prompt,canny_image,number,seed, steps ):
    print("start inference")

    
    pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
    # This command loads the individual model components on GPU on-demand. So, we don't
    # need to explicitly call pipe.to("cuda").
    #pipe.enable_model_cpu_offload()
    # xformers
    #pipe.enable_xformers_memory_efficient_attention()
    # Generator seed,
    generator = torch.manual_seed(seed)
    image_ = canny_image
    prompt = prompt
    out_image = pipe(
      prompt, num_inference_steps=steps, generator=generator, image=image_, num_images_per_prompt=number)
    print('end inference')
    return out_image
    


def generation(prompt,text,seed,police_size, lenght, width,number,num_inference_steps):
    img = generate_an_image_from_text(text,police_size,lenght,width)
    img = to_Canny(img)
    output = inference(prompt,img, number,seed,num_inference_steps)
    all_outputs = []
    for image in output.images:
        all_outputs.append(image)
    return all_outputs

gr.Interface(fn=generation, 
             inputs=[gr.Textbox(value="A steampunk Alphabetic Logo, steampunk style, with glowing mecha parts, mecha alphabets, high quality, high res, ultra HD"), gr.Textbox(), gr.Slider(0, 200,value=60), gr.Slider(0, 200, value=90), gr.Slider(0, 1024, value=512), gr.Slider(0, 1024, value=512),
                     gr.Slider(0, 7,value=2, step=1),gr.Slider(0, 20,value=5, step=1)], outputs=gr.Gallery().style(grid=[2], height="auto"), title="Generate a logo using Text ",cache_examples=True, examples=[["A steampunk Alphabetic Logo, steampunk style, with glowing mecha parts, mecha alphabets, high quality, high res, ultra HD", "Logo",60,90,512,512,2,5]]).launch(enable_queue=True)