From b5d9f0cd41d3b82f0b71e301591192983a620ff3 Mon Sep 17 00:00:00 2001 From: smthemex <138738845+smthemex@users.noreply.github.com> Date: Fri, 31 May 2024 15:59:29 +0800 Subject: [PATCH] Update utils.py delete gradio code --- utils/utils.py | 88 ++------------------------------------------------ 1 file changed, 2 insertions(+), 86 deletions(-) diff --git a/utils/utils.py b/utils/utils.py index a9a9de2..94b903b 100644 --- a/utils/utils.py +++ b/utils/utils.py @@ -1,11 +1,7 @@ -from email.mime import image + import torch -import base64 -import gradio as gr import numpy as np -from PIL import Image,ImageOps,ImageDraw, ImageFont -from io import BytesIO -import random +from PIL import Image,ImageOps,ImageDraw MAX_COLORS = 12 import os dir_path = os.path.dirname(os.path.abspath(__file__)) @@ -350,83 +346,3 @@ def preprocess_mask(mask_, h, w, device): mask = torch.nn.functional.interpolate(mask, size=(h, w), mode='nearest') return mask -def process_sketch(canvas_data): - binary_matrixes = [] - base64_img = canvas_data['image'] - image_data = base64.b64decode(base64_img.split(',')[1]) - image = Image.open(BytesIO(image_data)).convert("RGB") - im2arr = np.array(image) - colors = [tuple(map(int, rgb[4:-1].split(','))) for rgb in canvas_data['colors']] - colors_fixed = [] - - r, g, b = 255, 255, 255 - binary_matrix = create_binary_matrix(im2arr, (r,g,b)) - binary_matrixes.append(binary_matrix) - binary_matrix_ = np.repeat(np.expand_dims(binary_matrix, axis=(-1)), 3, axis=(-1)) - colored_map = binary_matrix_*(r,g,b) + (1-binary_matrix_)*(50,50,50) - colors_fixed.append(gr.update(value=colored_map.astype(np.uint8))) - - for color in colors: - r, g, b = color - if any(c != 255 for c in (r, g, b)): - binary_matrix = create_binary_matrix(im2arr, (r,g,b)) - binary_matrixes.append(binary_matrix) - binary_matrix_ = np.repeat(np.expand_dims(binary_matrix, axis=(-1)), 3, axis=(-1)) - colored_map = binary_matrix_*(r,g,b) + (1-binary_matrix_)*(50,50,50) - colors_fixed.append(gr.update(value=colored_map.astype(np.uint8))) - - visibilities = [] - colors = [] - for n in range(MAX_COLORS): - visibilities.append(gr.update(visible=False)) - colors.append(gr.update()) - for n in range(len(colors_fixed)): - visibilities[n] = gr.update(visible=True) - colors[n] = colors_fixed[n] - - return [gr.update(visible=True), binary_matrixes, *visibilities, *colors] - -def process_prompts(binary_matrixes, *seg_prompts): - return [gr.update(visible=True), gr.update(value=' , '.join(seg_prompts[:len(binary_matrixes)]))] - -def process_example(layout_path, all_prompts, seed_): - - all_prompts = all_prompts.split('***') - - binary_matrixes = [] - colors_fixed = [] - - im2arr = np.array(Image.open(layout_path))[:,:,:3] - unique, counts = np.unique(np.reshape(im2arr,(-1,3)), axis=0, return_counts=True) - sorted_idx = np.argsort(-counts) - - binary_matrix = create_binary_matrix(im2arr, (0,0,0)) - binary_matrixes.append(binary_matrix) - binary_matrix_ = np.repeat(np.expand_dims(binary_matrix, axis=(-1)), 3, axis=(-1)) - colored_map = binary_matrix_*(255,255,255) + (1-binary_matrix_)*(50,50,50) - colors_fixed.append(gr.update(value=colored_map.astype(np.uint8))) - - for i in range(len(all_prompts)-1): - r, g, b = unique[sorted_idx[i]] - if any(c != 255 for c in (r, g, b)) and any(c != 0 for c in (r, g, b)): - binary_matrix = create_binary_matrix(im2arr, (r,g,b)) - binary_matrixes.append(binary_matrix) - binary_matrix_ = np.repeat(np.expand_dims(binary_matrix, axis=(-1)), 3, axis=(-1)) - colored_map = binary_matrix_*(r,g,b) + (1-binary_matrix_)*(50,50,50) - colors_fixed.append(gr.update(value=colored_map.astype(np.uint8))) - - visibilities = [] - colors = [] - prompts = [] - for n in range(MAX_COLORS): - visibilities.append(gr.update(visible=False)) - colors.append(gr.update()) - prompts.append(gr.update()) - - for n in range(len(colors_fixed)): - visibilities[n] = gr.update(visible=True) - colors[n] = colors_fixed[n] - prompts[n] = all_prompts[n+1] - - return [gr.update(visible=True), binary_matrixes, *visibilities, *colors, *prompts, - gr.update(visible=True), gr.update(value=all_prompts[0]), int(seed_)]