diff --git a/app/core/config.py b/app/core/config.py index 7456912..6a4ad23 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -129,8 +129,14 @@ GSL_MODEL_NAME = 'stable_diffusion_xl_transparent' GEN_SINGLE_LOGO_RABBITMQ_QUEUES = os.getenv("GEN_SINGLE_LOGO_RABBITMQ_QUEUES", f"GenSingleLogo{RABBITMQ_ENV}") # Generate Product service config +# GPI_RABBITMQ_QUEUES = os.getenv("GEN_PRODUCT_IMAGE_RABBITMQ_QUEUES", f"ToProductImage{RABBITMQ_ENV}") +# GPI_MODEL_NAME_OVERALL = 'sdxl_ensemble_all' +# GPI_MODEL_URL = '10.1.1.243:10051' + +# Generate Product service config 旧版product img 模型 GPI_RABBITMQ_QUEUES = os.getenv("GEN_PRODUCT_IMAGE_RABBITMQ_QUEUES", f"ToProductImage{RABBITMQ_ENV}") -GPI_MODEL_NAME_OVERALL = 'sdxl_ensemble_all' +GPI_MODEL_NAME_OVERALL = 'diffusion_ensemble_all' +GPI_MODEL_NAME_SINGLE = 'stable_diffusion_1_5_cnet' GPI_MODEL_URL = '10.1.1.243:10051' # Generate Single Logo service config diff --git a/app/service/design_fast/pipeline/print_painting.py b/app/service/design_fast/pipeline/print_painting.py index 6fe40d8..fd5f910 100644 --- a/app/service/design_fast/pipeline/print_painting.py +++ b/app/service/design_fast/pipeline/print_painting.py @@ -17,6 +17,22 @@ class PrintPainting: element_print = result['print']['element'] result['single_image'] = None result['print_image'] = None + # TODO 给result['pattern_image'] resize 到resize_scale的大小 + # TODO 给result['mask'] resize 到resize_scale的大小 + + if result['resize_scale'][0] == 1.0 and result['resize_scale'][1] == 1.0: + pass + else: + height, width = result['pattern_image'].shape[:2] + new_width = int(width * result['resize_scale'][0]) + new_height = int(height * result['resize_scale'][1]) + + result['pattern_image'] = cv2.resize(result['pattern_image'], (new_width, new_height)) + result['final_image'] = cv2.resize(result['final_image'], (new_width, new_height)) + result['mask'] = cv2.resize(result['mask'], (new_width, new_height)) + result['gray'] = cv2.resize(result['gray'], (new_width, new_height)) + + print(1) if overall_print['print_path_list']: painting_dict = {'dim_image_h': result['pattern_image'].shape[0], 'dim_image_w': result['pattern_image'].shape[1]} result['print_image'] = result['pattern_image'] @@ -37,9 +53,12 @@ class PrintPainting: print_background = np.zeros((result['pattern_image'].shape[0], result['pattern_image'].shape[1], 3), dtype=np.uint8) mask_background = np.zeros((result['pattern_image'].shape[0], result['pattern_image'].shape[1], 3), dtype=np.uint8) for i in range(len(single_print['print_path_list'])): + if not (result['resize_scale'][0] == 1.0 and result['resize_scale'][1] == 1.0): + single_print['location'][i] = (int(single_print['location'][i][0] * result['resize_scale'][0]), int(single_print['location'][i][1] * result['resize_scale'][1])) + image, image_mode = self.read_image(single_print['print_path_list'][i]) if image_mode == "RGBA": - new_size = (int(image.width * single_print['print_scale_list'][i]), int(image.height * single_print['print_scale_list'][i])) + new_size = (int(result['pattern_image'].shape[1] * single_print['print_scale_list'][i][0]), int(result['pattern_image'].shape[0] * single_print['print_scale_list'][i][1])) mask = image.split()[3] resized_source = image.resize(new_size) @@ -62,9 +81,12 @@ class PrintPainting: mask = np.expand_dims(mask, axis=2) mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR) mask = cv2.bitwise_not(mask) + + mask = cv2.resize(mask, (int(result['final_image'].shape[0] * single_print['print_scale_list'][i][0]), int(result['final_image'].shape[1] * single_print['print_scale_list'][i][1]))) + image = cv2.resize(image, (int(result['final_image'].shape[0] * single_print['print_scale_list'][i][0]), int(result['final_image'].shape[1] * single_print['print_scale_list'][i][1]))) # 旋转后的坐标需要重新算 - rotate_mask, _ = self.img_rotate(mask, single_print['print_angle_list'][i], single_print['print_scale_list'][i]) - rotate_image, rotated_new_size = self.img_rotate(image, single_print['print_angle_list'][i], single_print['print_scale_list'][i]) + rotate_mask, _ = self.img_rotate(mask, single_print['print_angle_list'][i]) + rotate_image, rotated_new_size = self.img_rotate(image, single_print['print_angle_list'][i]) # x, y = int(result['print']['location'][i][0] - rotated_new_size[0] - (rotate_mask.shape[0] - image.shape[0]) / 2), int(result['print']['location'][i][1] - rotated_new_size[1] - (rotate_mask.shape[1] - image.shape[1]) / 2) x, y = int(single_print['location'][i][0] - rotated_new_size[0]), int(single_print['location'][i][1] - rotated_new_size[1]) @@ -141,9 +163,11 @@ class PrintPainting: print_background = np.zeros((result['final_image'].shape[0], result['final_image'].shape[1], 3), dtype=np.uint8) mask_background = np.zeros((result['final_image'].shape[0], result['final_image'].shape[1], 3), dtype=np.uint8) for i in range(len(element_print['element_path_list'])): + if not (result['resize_scale'][0] == 1.0 and result['resize_scale'][1] == 1.0): + element_print['location'][i] = (int(element_print['location'][i][0] * result['resize_scale'][0]), int(element_print['location'][i][1] * result['resize_scale'][1])) image, image_mode = self.read_image(element_print['element_path_list'][i]) if image_mode == "RGBA": - new_size = (int(image.width * element_print['element_scale_list'][i]), int(image.height * element_print['element_scale_list'][i])) + new_size = (int(result['final_image'].shape[1] * element_print['element_scale_list'][i][0]), int(result['final_image'].shape[0] * element_print['element_scale_list'][i][1])) mask = image.split()[3] resized_source = image.resize(new_size) @@ -165,9 +189,11 @@ class PrintPainting: mask = np.expand_dims(mask, axis=2) mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR) mask = cv2.bitwise_not(mask) + mask = cv2.resize(mask, (int(result['final_image'].shape[0] * single_print['print_scale_list'][i][0]), int(result['final_image'].shape[1] * single_print['print_scale_list'][i][1]))) + image = cv2.resize(image, (int(result['final_image'].shape[0] * single_print['print_scale_list'][i][0]), int(result['final_image'].shape[1] * single_print['print_scale_list'][i][1]))) # 旋转后的坐标需要重新算 - rotate_mask, _ = self.img_rotate(mask, element_print['element_angle_list'][i], element_print['element_scale_list'][i]) - rotate_image, rotated_new_size = self.img_rotate(image, element_print['element_angle_list'][i], element_print['element_scale_list'][i]) + rotate_mask, _ = self.img_rotate(mask, element_print['element_angle_list'][i]) + rotate_image, rotated_new_size = self.img_rotate(image, element_print['element_angle_list'][i]) # x, y = int(result['print']['location'][i][0] - rotated_new_size[0] - (rotate_mask.shape[0] - image.shape[0]) / 2), int(result['print']['location'][i][1] - rotated_new_size[1] - (rotate_mask.shape[1] - image.shape[1]) / 2) x, y = int(element_print['location'][i][0] - rotated_new_size[0]), int(element_print['location'][i][1] - rotated_new_size[1]) @@ -409,7 +435,7 @@ class PrintPainting: return high, low @staticmethod - def img_rotate(image, angel, scale): + def img_rotate(image, angel): """顺时针旋转图像任意角度 Args: @@ -424,7 +450,7 @@ class PrintPainting: center = (w // 2, h // 2) # if type(angel) is not int: # angel = 0 - M = cv2.getRotationMatrix2D(center, -angel, scale) + M = cv2.getRotationMatrix2D(center, -angel, 1) # 调整旋转后的图像长宽 rotated_h = int((w * np.abs(M[0, 1]) + (h * np.abs(M[0, 0])))) rotated_w = int((h * np.abs(M[0, 1]) + (w * np.abs(M[0, 0])))) @@ -433,7 +459,7 @@ class PrintPainting: # 旋转图像 rotated_img = cv2.warpAffine(image, M, (rotated_w, rotated_h)) - return rotated_img, ((rotated_img.shape[1] - image.shape[1] * scale) // 2, (rotated_img.shape[0] - image.shape[0] * scale) // 2) + return rotated_img, ((rotated_img.shape[1] - image.shape[1]) // 2, (rotated_img.shape[0] - image.shape[0]) // 2) # return rotated_img, (0, 0) @staticmethod diff --git a/app/service/design_fast/pipeline/split.py b/app/service/design_fast/pipeline/split.py index 344c5c5..115f814 100644 --- a/app/service/design_fast/pipeline/split.py +++ b/app/service/design_fast/pipeline/split.py @@ -21,11 +21,21 @@ class Split(object): def __call__(self, result): try: - if result['name'] in ('outwear', 'dress', 'blouse', 'skirt', 'trousers', 'tops', 'bottoms','accessories'): - front_mask = result['front_mask'] - back_mask = result['back_mask'] + if result['name'] in ('outwear', 'dress', 'blouse', 'skirt', 'trousers', 'tops', 'bottoms', 'accessories'): + + if result['resize_scale'][0] == 1.0 and result['resize_scale'][1] == 1.0: + front_mask = result['front_mask'] + back_mask = result['back_mask'] + else: + height, width = result['front_mask'].shape[:2] + new_width = int(width * result['resize_scale'][0]) + new_height = int(height * result['resize_scale'][1]) + + front_mask = cv2.resize(result['front_mask'], (new_width, new_height)) + back_mask = cv2.resize(result['back_mask'], (new_width, new_height)) + rgba_image = rgb_to_rgba(result['final_image'], front_mask + back_mask) - new_size = (int(rgba_image.shape[1] * result["scale"] * result["resize_scale"][0]), int(rgba_image.shape[0] * result["scale"] * result["resize_scale"][1])) + new_size = (int(rgba_image.shape[1] * result["scale"]), int(rgba_image.shape[0] * result["scale"])) rgba_image = cv2.resize(rgba_image, new_size) result_front_image = np.zeros_like(rgba_image) front_mask = cv2.resize(front_mask, new_size) diff --git a/app/service/design_fast/utils/design_ensemble.py b/app/service/design_fast/utils/design_ensemble.py index bfc50c6..8eef4f2 100644 --- a/app/service/design_fast/utils/design_ensemble.py +++ b/app/service/design_fast/utils/design_ensemble.py @@ -25,6 +25,7 @@ from app.core.config import * def keypoint_preprocess(img_path): img = mmcv.imread(img_path) + img = cv2.copyMakeBorder(img, 25, 25, 25, 25, cv2.BORDER_CONSTANT, value=[255, 255, 255]) img_scale = (256, 256) h, w = img.shape[:2] img = cv2.resize(img, img_scale) @@ -62,7 +63,11 @@ def keypoint_postprocess(output, scale_factor): scale_matrix = np.diag(scale_factor) nan = np.isinf(scale_matrix) scale_matrix[nan] = 0 - return np.ceil(np.dot(segment_result, scale_matrix) * 4) + # 应用缩放因子 + scaled_result = np.ceil(np.dot(segment_result, scale_matrix) * 4) + # 补偿边框偏移 + compensated_result = scaled_result - 25 + return compensated_result """ diff --git a/app/service/generate_image/service_generate_product_image.py b/app/service/generate_image/service_generate_product_image.py index 1c20a13..a575f07 100644 --- a/app/service/generate_image/service_generate_product_image.py +++ b/app/service/generate_image/service_generate_product_image.py @@ -1,4 +1,196 @@ -#!/usr/bin/env python +# #!/usr/bin/env python +# # -*- coding: UTF-8 -*- +# """ +# @Project :trinity_client +# @File :service_att_recognition.py +# @Author :周成融 +# @Date :2023/7/26 12:01:05 +# @detail : +# """ +# import json +# import logging +# import time +# +# import cv2 +# import numpy as np +# import redis +# import tritonclient.grpc as grpcclient +# from PIL import Image +# from tritonclient.utils import np_to_triton_dtype +# +# from app.core.config import * +# from app.schemas.generate_image import GenerateProductImageModel +# from app.service.generate_image.utils.upload_sd_image import upload_SDXL_image +# from app.service.utils.oss_client import oss_get_image +# +# logger = logging.getLogger() +# +# +# class GenerateProductImage: +# def __init__(self, request_data): +# if DEBUG is False: +# self.connection = pika.BlockingConnection(pika.ConnectionParameters(**RABBITMQ_PARAMS)) +# self.channel = self.connection.channel() +# # self.connection = pika.BlockingConnection(pika.ConnectionParameters(**RABBITMQ_PARAMS)) +# # self.channel = self.connection.channel() +# # self.minio_client = Minio(MINIO_URL, access_key=MINIO_ACCESS, secret_key=MINIO_SECRET, secure=MINIO_SECURE) +# self.grpc_client = grpcclient.InferenceServerClient(url=GPI_MODEL_URL) +# self.redis_client = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT, db=REDIS_DB, decode_responses=True) +# self.category = "product_image" +# self.image_strength = request_data.image_strength +# self.batch_size = 1 +# self.product_type = request_data.product_type +# self.prompt = request_data.prompt +# 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': ''} +# self.redis_client.set(self.tasks_id, json.dumps(self.gen_product_data)) +# self.redis_client.expire(self.tasks_id, 600) +# +# def callback(self, result, error): +# if error: +# self.gen_product_data['status'] = "FAILURE" +# self.gen_product_data['message'] = str(error) +# self.redis_client.set(self.tasks_id, json.dumps(self.gen_product_data)) +# else: +# # pil图像转成numpy数组 +# image = result.as_numpy("generated_inpaint_image") +# image_result = Image.fromarray(np.squeeze(image.astype(np.uint8))).resize(self.image_size) +# 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) +# self.redis_client.set(self.tasks_id, json.dumps(self.gen_product_data)) +# +# def read_tasks_status(self): +# status_data = self.redis_client.get(self.tasks_id) +# return json.loads(status_data), status_data +# +# def get_result(self): +# try: +# prompts = [self.prompt] * self.batch_size +# self.image = cv2.cvtColor(self.image, cv2.COLOR_BGR2RGB) +# 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, 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((1024, 1024, 3)) +# image_strength_obj = np.array(self.image_strength, dtype=np.float32).reshape((1)) +# +# # 假设 prompts、images 和 self.image_strength 已经定义 +# +# input_text = grpcclient.InferInput("prompt", text_obj.shape, np_to_triton_dtype(text_obj.dtype)) +# input_image = grpcclient.InferInput("input_image", image_obj.shape, "UINT8") +# input_image_strength = grpcclient.InferInput("image_strength", image_strength_obj.shape, np_to_triton_dtype(image_strength_obj.dtype)) +# +# input_text.set_data_from_numpy(text_obj) +# input_image.set_data_from_numpy(image_obj) +# 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="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) +# +# time_out = 600 +# while time_out > 0: +# gen_product_data, _ = self.read_tasks_status() +# if gen_product_data['status'] in ["REVOKED", "FAILURE"]: +# ctx.cancel() +# break +# elif gen_product_data['status'] == "SUCCESS": +# break +# time_out -= 1 +# time.sleep(0.1) +# gen_product_data, _ = self.read_tasks_status() +# return gen_product_data +# except Exception as e: +# self.gen_product_data['status'] = "FAILURE" +# self.gen_product_data['message'] = str(e) +# self.redis_client.set(self.tasks_id, json.dumps(self.gen_product_data)) +# raise Exception(str(e)) +# finally: +# dict_gen_product_data, str_gen_product_data = self.read_tasks_status() +# if DEBUG is False: +# self.channel.basic_publish(exchange='', routing_key=GPI_RABBITMQ_QUEUES, body=str_gen_product_data) +# logger.info(f" [x] Sent to: {GPI_RABBITMQ_QUEUES} data:@@@@ {json.dumps(dict_gen_product_data, indent=4)}") +# +# +# def infer_cancel(tasks_id): +# redis_client = redis.StrictRedis(host=REDIS_HOST, port=REDIS_PORT, db=REDIS_DB, decode_responses=True) +# data = {'tasks_id': tasks_id, 'status': 'REVOKED', 'message': "revoked", 'data': 'revoked'} +# gen_product_data = json.dumps(data) +# redis_client.set(tasks_id, gen_product_data) +# return data +# +# +# 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 +# +# new_height, new_width = 1024, 1024 +# # 创建一个新的画布,大小为添加 padding 后的尺寸,并设置为白色背景 +# pad_image = Image.new('RGBA', (new_width, new_height), (0, 0, 0, 0)) +# +# # 将原始图片粘贴到新的画布中心 +# left = (new_width - width) // 2 +# top = (new_height - height) // 2 +# pad_image.paste(image, (left, top)) +# +# # 将画布 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): +# # 创建白色背景 +# background = Image.new("RGB", image_size, (255, 255, 255)) +# # 将图片粘贴到白色背景上 +# background.paste(resized_image, mask=resized_image.split()[3]) +# image = np.array(background) +# image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) +# 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.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) +# print(server.get_result()) + +# 旧版product +# !/usr/bin/env python # -*- coding: UTF-8 -*- """ @Project :trinity_client @@ -41,7 +233,7 @@ class GenerateProductImage: self.batch_size = 1 self.product_type = request_data.product_type self.prompt = request_data.prompt - self.image, self.image_size, self.left, self.top = pre_processing_image(request_data.image_url) + self.image = 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,10 +247,13 @@ class GenerateProductImage: self.redis_client.set(self.tasks_id, json.dumps(self.gen_product_data)) else: # pil图像转成numpy数组 - image = result.as_numpy("generated_inpaint_image") - image_result = Image.fromarray(np.squeeze(image.astype(np.uint8))).resize(self.image_size) - 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") + if self.product_type == "single": + image = result.as_numpy("generated_cnet_image") + else: + image = result.as_numpy("generated_inpaint_image") + image_result = Image.fromarray(np.squeeze(image.astype(np.uint8))) + # cropped_image = post_processing_image(image_result, self.left, self.top) + image_url = upload_SDXL_image(image_result, 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) @@ -71,17 +266,18 @@ class GenerateProductImage: def get_result(self): try: prompts = [self.prompt] * self.batch_size - self.image = cv2.cvtColor(self.image, cv2.COLOR_BGR2RGB) - self.image = cv2.resize(self.image, (1024, 1024)) + + self.image = cv2.cvtColor(self.image, cv2.COLOR_RGBA2RGB) + # 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, 1024, 1024, 3)) - image_strength_obj = np.array(self.image_strength, dtype=np.float32).reshape(-1, 1) + text_obj = np.array(prompts, dtype="object").reshape((-1, 1)) + 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((1024, 1024, 3)) + 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)) # 假设 prompts、images 和 self.image_strength 已经定义 @@ -97,7 +293,7 @@ class GenerateProductImage: inputs = [input_text, input_image, input_image_strength] if self.product_type == "single": - ctx = self.grpc_client.async_infer(model_name="stable_diffusion_xl_cnet_inpaint", inputs=inputs, callback=self.callback) + ctx = self.grpc_client.async_infer(model_name=GPI_MODEL_NAME_SINGLE, inputs=inputs, callback=self.callback) else: ctx = self.grpc_client.async_infer(model_name=GPI_MODEL_NAME_OVERALL, inputs=inputs, callback=self.callback) @@ -135,33 +331,41 @@ 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)) + # 目标图片的尺寸 + target_width = 512 + target_height = 768 # 原始图片的尺寸 - width, height = image.size + original_width, original_height = image.size - new_height, new_width = 1024, 1024 - # 创建一个新的画布,大小为添加 padding 后的尺寸,并设置为白色背景 - pad_image = Image.new('RGBA', (new_width, new_height), (0, 0, 0, 0)) + # 计算宽度和高度的缩放比例 + width_ratio = target_width / original_width + height_ratio = target_height / original_height - # 将原始图片粘贴到新的画布中心 - left = (new_width - width) // 2 - top = (new_height - height) // 2 - pad_image.paste(image, (left, top)) + # 选择较小的缩放比例,确保图片能完整放入目标图片中 + scale_ratio = min(width_ratio, height_ratio) - # 将画布 resize 成宽度 1024,长度 1024 - resized_image = pad_image.resize((1024, 1024)) - image_size = (1024, 1024) + # 计算调整后的尺寸 + new_width = int(original_width * scale_ratio) + new_height = int(original_height * scale_ratio) - if resized_image.mode in ('RGBA', 'LA') or (resized_image.mode == 'P' and 'transparency' in resized_image.info): - # 创建白色背景 - background = Image.new("RGB", image_size, (255, 255, 255)) - # 将图片粘贴到白色背景上 - background.paste(resized_image, mask=resized_image.split()[3]) - image = np.array(background) - image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) - return image, image_size, left, top + # 调整图片大小 + resized_image = image.resize((new_width, new_height)) + + # 创建一个 512x768 的透明图片 + result_image = Image.new("RGBA", (target_width, target_height), (255, 255, 255, 0)) + + # 计算需要粘贴的位置,使图片居中 + x_offset = (target_width - new_width) // 2 + y_offset = (target_height - new_height) // 2 + + # 将调整大小后的图片粘贴到透明图片上 + result_image.paste(resized_image, (x_offset, y_offset), mask=resized_image.split()[3]) + + image = np.array(result_image) + + # image = cv2.cvtColor(image, cv2.COLOR_BGRA2RGBA) + return image def post_processing_image(image, left, top): @@ -182,9 +386,9 @@ if __name__ == '__main__': tasks_id="123-89", # prompt="", 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" + prompt=" The best quality, masterpiece, real image.Outwear,high quality clothing details,8K realistic,HDR", + image_url="aida-results/result_40b1a2fe-e220-11ef-9bfa-0242ac150003.png", + product_type="single" ) server = GenerateProductImage(rd) print(server.get_result()) diff --git a/app/service/prompt_generation/chatgpt_for_translation.py b/app/service/prompt_generation/chatgpt_for_translation.py index e668500..833eda0 100644 --- a/app/service/prompt_generation/chatgpt_for_translation.py +++ b/app/service/prompt_generation/chatgpt_for_translation.py @@ -95,10 +95,10 @@ def get_translation_from_llama3(text): # prompt = f"System: {prefix_for_llama}\nUser:[{text}]" # 先获取用户输入文本的语言 - language = get_language(text) + # language = get_language(text) - if 'English' in language: - return text + # if 'English' in language: + # return text # 创建请求的负载 translator是自定义的翻译模型 payload = { @@ -106,7 +106,6 @@ def get_translation_from_llama3(text): "prompt": f"[{text}]", "stream": False } - # 将负载转换为 JSON 格式 headers = {'Content-Type': 'application/json'} response = requests.post(url, data=json.dumps(payload), headers=headers) diff --git a/app/service/utils/new_oss_client.py b/app/service/utils/new_oss_client.py index 7939333..92b41fa 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-users/89/123-89.png" + url = "aida-results/result_4185cc1c-e476-11ef-b8e1-0826ae3ad6b3.png" # url = "aida-collection-element/12148/Sketchboard/95ea577b-305b-4a62-b30a-39c0dd3ddb3f.png" read_type = "2"