diff --git a/app/core/config.py b/app/core/config.py index 5701e92..48af4a1 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -117,10 +117,10 @@ GEN_SINGLE_LOGO_RABBITMQ_QUEUES = os.getenv("GEN_SINGLE_LOGO_RABBITMQ_QUEUES", f # Generate Product service config GPI_RABBITMQ_QUEUES = os.getenv("GEN_PRODUCT_IMAGE_RABBITMQ_QUEUES", f"ToProductImage{RABBITMQ_ENV}") -GPI_MODEL_NAME_OVERALL = 'diffusion_ensemble_all' -GPI_MODEL_NAME_SINGLE = 'stable_diffusion_1_5_cnet' +GPI_MODEL_NAME_OVERALL = 'stable_diffusion_xl_cnet' +GPI_MODEL_NAME_SINGLE = 'stable_diffusion_xl_cnet' -GPI_MODEL_URL = '10.1.1.240:10041' +GPI_MODEL_URL = '10.1.1.243:10051' # Generate Single Logo service config GRI_RABBITMQ_QUEUES = os.getenv("GEN_RELIGHT_IMAGE_RABBITMQ_QUEUES", f"Relight{RABBITMQ_ENV}") diff --git a/app/service/generate_image/service_generate_product_image.py b/app/service/generate_image/service_generate_product_image.py index 5ea6f83..da4bb4b 100644 --- a/app/service/generate_image/service_generate_product_image.py +++ b/app/service/generate_image/service_generate_product_image.py @@ -15,7 +15,7 @@ import cv2 import numpy as np import redis import tritonclient.grpc as grpcclient -from PIL import Image, ImageOps +from PIL import Image from tritonclient.utils import np_to_triton_dtype from app.core.config import * @@ -41,7 +41,7 @@ class GenerateProductImage: self.batch_size = 1 self.product_type = request_data.product_type self.prompt = request_data.prompt - self.image, self.image_size = pre_processing_image(request_data.image_url) + self.image, self.image_size, self.left, self.top = pre_processing_image(request_data.image_url) self.tasks_id = request_data.tasks_id self.user_id = self.tasks_id[self.tasks_id.rfind('-') + 1:] self.gen_product_data = {'tasks_id': self.tasks_id, 'status': 'PENDING', 'message': "pending", 'image_url': ''} @@ -58,9 +58,10 @@ class GenerateProductImage: if self.product_type == "single": image = result.as_numpy("generated_cnet_image") else: - image = result.as_numpy("generated_inpaint_image") + image = result.as_numpy("generated_cnet_image") image_result = Image.fromarray(np.squeeze(image.astype(np.uint8))).resize(self.image_size) - image_url = upload_SDXL_image(image_result, user_id=self.user_id, category=f"{self.category}", file_name=f"{self.tasks_id}.png") + cropped_image = post_processing_image(image_result, self.left, self.top) + image_url = upload_SDXL_image(cropped_image, user_id=self.user_id, category=f"{self.category}", file_name=f"{self.tasks_id}.png") self.gen_product_data['status'] = "SUCCESS" self.gen_product_data['message'] = "success" self.gen_product_data['image_url'] = str(image_url) @@ -74,7 +75,7 @@ class GenerateProductImage: try: prompts = [self.prompt] * self.batch_size self.image = cv2.cvtColor(self.image, cv2.COLOR_BGR2RGB) - self.image = cv2.resize(self.image, (512, 768)) + self.image = cv2.resize(self.image, (1024, 1024)) images = [self.image.astype(np.uint8)] * self.batch_size if self.product_type == "single": @@ -82,9 +83,9 @@ class GenerateProductImage: image_obj = np.array(images, dtype=np.uint8).reshape((-1, 768, 512, 3)) image_strength_obj = np.array(self.image_strength, dtype=np.float32).reshape(-1, 1) else: - text_obj = np.array(prompts, dtype="object").reshape(1) - image_obj = np.array(images, dtype=np.uint8).reshape((768, 512, 3)) - image_strength_obj = np.array(self.image_strength, dtype=np.float32).reshape((1)) + text_obj = np.array(prompts, dtype="object").reshape((-1, 1)) + image_obj = np.array(images, dtype=np.uint8).reshape((-1, 1024, 1024, 3)) + image_strength_obj = np.array(self.image_strength, dtype=np.float32).reshape((-1, 1)) # 假设 prompts、images 和 self.image_strength 已经定义 @@ -136,22 +137,13 @@ def infer_cancel(tasks_id): def pre_processing_image(image_url): image = oss_get_image(bucket=image_url.split('/')[0], object_name=image_url[image_url.find('/') + 1:], data_type="PIL") + # resize 原图至1024*1024 + image = image.resize((int(1024 / image.height * image.width), 1024)) + # 原始图片的尺寸 width, height = image.size - # 计算长宽比为 3:2 的新尺寸 - desired_ratio = 2 / 3 - current_ratio = width / height - - if current_ratio > desired_ratio: - # 原始图片更宽,需要在上下添加 padding - new_width = width - new_height = int(width / desired_ratio) - else: - # 原始图片更高或者长宽比已经为 3:2 - new_height = height - new_width = int(height * desired_ratio) - + new_height, new_width = 1024, 1024 # 创建一个新的画布,大小为添加 padding 后的尺寸,并设置为白色背景 pad_image = Image.new('RGBA', (new_width, new_height), (0, 0, 0, 0)) @@ -160,9 +152,9 @@ def pre_processing_image(image_url): top = (new_height - height) // 2 pad_image.paste(image, (left, top)) - # 将画布 resize 成宽度 500,长度 750 - resized_image = pad_image.resize((500, 750)) - image_size = (512, 768) + # 将画布 resize 成宽度 1024,长度 1024 + resized_image = pad_image.resize((1024, 1024)) + image_size = (1024, 1024) if resized_image.mode in ('RGBA', 'LA') or (resized_image.mode == 'P' and 'transparency' in resized_image.info): # 创建白色背景 @@ -171,15 +163,28 @@ def pre_processing_image(image_url): background.paste(resized_image, mask=resized_image.split()[3]) image = np.array(background) image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) - return image, image_size + return image, image_size, left, top + + +def post_processing_image(image, left, top): + width, height = image.size + # 计算裁剪后的宽度和坐标 + new_width = width - 2 * left + right = left + new_width + + # 进行裁剪操作 + cropped_image = image.crop((left, 0, right, height)) + + # 保存裁剪后的图像,将此处的 'cropped_image.jpg' 替换为你想要保存的文件名 + return cropped_image if __name__ == '__main__': rd = GenerateProductImageModel( tasks_id="123-89", # prompt="", - image_strength=0.9, - prompt=" the best quality, masterpiece. detailed, high-res, simple background, studio photography, extremely detailed, updo, detailed face, face, close-up, HDR, UHD, 8K realistic, Highly detailed, simple background, Studio lighting", + image_strength=0.65, + prompt="The best quality, masterpiece, real image. A handsome man wearing blouse, outwear, trousers, 8K realistic, HUD", image_url="aida-results/result_00097282-ebb2-11ee-a822-b48351119060.png", product_type="overall" ) diff --git a/app/service/utils/new_oss_client.py b/app/service/utils/new_oss_client.py index 8dbf2fc..f402a14 100644 --- a/app/service/utils/new_oss_client.py +++ b/app/service/utils/new_oss_client.py @@ -82,7 +82,7 @@ if __name__ == '__main__': # url = "aida-users/89/sketchboard/female/Dress/e6724ab7-8d3f-4677-abe0-c3e42ab7af85.jpeg" # url = "aida-users/87/print/956614a2-7e75-4fbe-9ed0-c1831e37a2c9-4-87.png" # url = "aida-users/89/single_logo/123-89.png" - url ="aida-results/result_91559b60-a61c-11ef-af8e-0242ac150002.png" + url ="aida-results/result_27915298-a656-11ef-b4f3-0242ac150002.png" # url = "aida-collection-element/12148/Sketchboard/95ea577b-305b-4a62-b30a-39c0dd3ddb3f.png" read_type = "2"