|
import gradio as gr |
|
from PIL import Image |
|
import numpy as np |
|
from diffusers import AutoPipelineForText2Image |
|
import torch |
|
import os |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
output_dir = "./saved_images" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def latents_to_rgb(latents): |
|
weights = ( |
|
(60, -60, 25, -70), |
|
(60, -5, 15, -50), |
|
(60, 10, -5, -35), |
|
) |
|
weights_tensor = torch.t(torch.tensor(weights, dtype=latents.dtype).to(latents.device)) |
|
biases_tensor = torch.tensor((150, 140, 130), dtype=latents.dtype).to(latents.device) |
|
rgb_tensor = torch.einsum("...lxy,lr -> ...rxy", latents, weights_tensor) + biases_tensor.unsqueeze(-1).unsqueeze(-1) |
|
image_array = rgb_tensor.clamp(0, 255).byte().cpu().numpy().transpose(1, 2, 0) |
|
return Image.fromarray(image_array) |
|
|
|
|
|
def decode_tensors(pipe, step, timestep, callback_kwargs): |
|
latents = callback_kwargs["latents"] |
|
image = latents_to_rgb(latents[0]) |
|
image.save(f"./output_images/{step}.png") |
|
return callback_kwargs |
|
|
|
pipeline = AutoPipelineForText2Image.from_pretrained("bguisard/stable-diffusion-nano-2-1", torch_dtype=torch.float16).to("cuda") |
|
pipeline.load_lora_weights("/root/autodl-tmp/Proj/city_demo/checkpoint-15000",weight_name="pytorch_lora_weights.safetensors") |
|
|
|
def generate_image(text,option): |
|
num_steps = 50 |
|
interval = num_steps // 10 |
|
output_dir = "./intermediate_images" |
|
|
|
|
|
|
|
|
|
while True: |
|
image = pipeline(text, num_inference_steps=num_steps) |
|
final_image = image.images[0] |
|
if option == "Ratio < 5": |
|
if calculate_building_ratio(final_image) < 5: |
|
final_pil_image = final_image.convert('L') |
|
return final_pil_image |
|
else: |
|
if calculate_building_ratio(final_image) >= 5: |
|
final_pil_image = final_image.convert('L') |
|
return final_pil_image |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return final_pil_image |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def check_requirements(image, requirement): |
|
|
|
|
|
if requirement == "Option 1": |
|
|
|
pass |
|
elif requirement == "Option 2": |
|
|
|
pass |
|
return True |
|
|
|
def generate_compliant_image(text, requirements): |
|
while True: |
|
image = generate_image(text) |
|
if check_requirements(image, requirements): |
|
break |
|
return image |
|
def calculate_building_ratio(image): |
|
|
|
|
|
img_array = np.array(image.convert('L')) |
|
|
|
|
|
building_area = np.count_nonzero(img_array != 255) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
height = np.sum(img_array[img_array != 255]) |
|
print(height) |
|
print(img_array[img_array != 255]) |
|
|
|
floors = height / 3 |
|
|
|
|
|
total_area = img_array.size |
|
|
|
|
|
ratio = (floors) / total_area |
|
print(ratio) |
|
return ratio/10 |
|
|
|
|
|
|
|
iface = gr.Interface( |
|
fn=generate_image, |
|
inputs=[ |
|
gr.Textbox(label="Prompt"), |
|
gr.Dropdown(choices=["Ratio < 5", "Ratio >= 5"], label="Select Ratio Requirement") |
|
], |
|
outputs="image", |
|
title="Image of Buildings Generation", |
|
description="Enter text and specify requirements for the generated image. The image will be regenerated until it meets the requirements." |
|
) |
|
|
|
iface.launch(share=True) |
|
|