From 926593826a32e37fb85d9c7699e0b5ffb4b35c15 Mon Sep 17 00:00:00 2001 From: zhouchengrong Date: Sun, 1 Dec 2024 18:51:53 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=20product=20=E5=90=8E=E5=A4=84=E7=90=86?= =?UTF-8?q?=E5=9B=BE=E7=89=87size=E6=94=B9=E4=B8=BA320*700=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../service_generate_product_image.py | 69 ++++++++++--------- 1 file changed, 36 insertions(+), 33 deletions(-) diff --git a/app/service/generate_image/service_generate_product_image.py b/app/service/generate_image/service_generate_product_image.py index 5ea6f83..1c20a13 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': ''} @@ -55,12 +55,10 @@ class GenerateProductImage: self.redis_client.set(self.tasks_id, json.dumps(self.gen_product_data)) else: # pil图像转成numpy数组 - 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_inpaint_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,16 +72,16 @@ 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": text_obj = np.array(prompts, dtype="object").reshape(-1, 1) - image_obj = np.array(images, dtype=np.uint8).reshape((-1, 768, 512, 3)) + 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) else: - text_obj = np.array(prompts, dtype="object").reshape(1) - image_obj = np.array(images, dtype=np.uint8).reshape((768, 512, 3)) + text_obj = np.array(prompts, dtype="object").reshape((1)) + image_obj = np.array(images, dtype=np.uint8).reshape((1024, 1024, 3)) image_strength_obj = np.array(self.image_strength, dtype=np.float32).reshape((1)) # 假设 prompts、images 和 self.image_strength 已经定义 @@ -94,11 +92,12 @@ class GenerateProductImage: input_text.set_data_from_numpy(text_obj) input_image.set_data_from_numpy(image_obj) - inputs = [input_text, input_image, input_image_strength] input_image_strength.set_data_from_numpy(image_strength_obj) + inputs = [input_text, input_image, input_image_strength] + if self.product_type == "single": - ctx = self.grpc_client.async_infer(model_name=GPI_MODEL_NAME_SINGLE, inputs=inputs, callback=self.callback) + ctx = self.grpc_client.async_infer(model_name="stable_diffusion_xl_cnet_inpaint", inputs=inputs, callback=self.callback) else: ctx = self.grpc_client.async_infer(model_name=GPI_MODEL_NAME_OVERALL, inputs=inputs, callback=self.callback) @@ -136,22 +135,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 +150,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,16 +161,29 @@ 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): + resized_image = image.resize((int(image.width * (768 / image.height)), 768)) + # 计算裁剪的坐标 + left = (resized_image.width - 512) // 2 + upper = 0 + right = left + 512 + lower = 768 + + # 进行裁剪 + cropped_image = resized_image.crop((left, upper, right, lower)) + 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_url="aida-results/result_00097282-ebb2-11ee-a822-b48351119060.png", + image_strength=0.7, + prompt="The best quality, masterpiece,outwear, 8K realistic, HUD", + image_url="aida-results/result_53381ada-ac64-11ef-ae9d-0242ac150002.png", product_type="overall" ) server = GenerateProductImage(rd)