feat product 后处理图片size改为320*700
fix
This commit is contained in:
@@ -15,7 +15,7 @@ import cv2
|
|||||||
import numpy as np
|
import numpy as np
|
||||||
import redis
|
import redis
|
||||||
import tritonclient.grpc as grpcclient
|
import tritonclient.grpc as grpcclient
|
||||||
from PIL import Image, ImageOps
|
from PIL import Image
|
||||||
from tritonclient.utils import np_to_triton_dtype
|
from tritonclient.utils import np_to_triton_dtype
|
||||||
|
|
||||||
from app.core.config import *
|
from app.core.config import *
|
||||||
@@ -41,7 +41,7 @@ class GenerateProductImage:
|
|||||||
self.batch_size = 1
|
self.batch_size = 1
|
||||||
self.product_type = request_data.product_type
|
self.product_type = request_data.product_type
|
||||||
self.prompt = request_data.prompt
|
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.tasks_id = request_data.tasks_id
|
||||||
self.user_id = self.tasks_id[self.tasks_id.rfind('-') + 1:]
|
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.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))
|
self.redis_client.set(self.tasks_id, json.dumps(self.gen_product_data))
|
||||||
else:
|
else:
|
||||||
# pil图像转成numpy数组
|
# pil图像转成numpy数组
|
||||||
if self.product_type == "single":
|
image = result.as_numpy("generated_inpaint_image")
|
||||||
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))).resize(self.image_size)
|
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['status'] = "SUCCESS"
|
||||||
self.gen_product_data['message'] = "success"
|
self.gen_product_data['message'] = "success"
|
||||||
self.gen_product_data['image_url'] = str(image_url)
|
self.gen_product_data['image_url'] = str(image_url)
|
||||||
@@ -74,16 +72,16 @@ class GenerateProductImage:
|
|||||||
try:
|
try:
|
||||||
prompts = [self.prompt] * self.batch_size
|
prompts = [self.prompt] * self.batch_size
|
||||||
self.image = cv2.cvtColor(self.image, cv2.COLOR_BGR2RGB)
|
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
|
images = [self.image.astype(np.uint8)] * self.batch_size
|
||||||
|
|
||||||
if self.product_type == "single":
|
if self.product_type == "single":
|
||||||
text_obj = np.array(prompts, dtype="object").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_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)
|
image_strength_obj = np.array(self.image_strength, dtype=np.float32).reshape(-1, 1)
|
||||||
else:
|
else:
|
||||||
text_obj = np.array(prompts, dtype="object").reshape(1)
|
text_obj = np.array(prompts, dtype="object").reshape((1))
|
||||||
image_obj = np.array(images, dtype=np.uint8).reshape((768, 512, 3))
|
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))
|
image_strength_obj = np.array(self.image_strength, dtype=np.float32).reshape((1))
|
||||||
|
|
||||||
# 假设 prompts、images 和 self.image_strength 已经定义
|
# 假设 prompts、images 和 self.image_strength 已经定义
|
||||||
@@ -94,11 +92,12 @@ class GenerateProductImage:
|
|||||||
|
|
||||||
input_text.set_data_from_numpy(text_obj)
|
input_text.set_data_from_numpy(text_obj)
|
||||||
input_image.set_data_from_numpy(image_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)
|
input_image_strength.set_data_from_numpy(image_strength_obj)
|
||||||
|
|
||||||
|
inputs = [input_text, input_image, input_image_strength]
|
||||||
|
|
||||||
if self.product_type == "single":
|
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:
|
else:
|
||||||
ctx = self.grpc_client.async_infer(model_name=GPI_MODEL_NAME_OVERALL, inputs=inputs, callback=self.callback)
|
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):
|
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")
|
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
|
width, height = image.size
|
||||||
|
|
||||||
# 计算长宽比为 3:2 的新尺寸
|
new_height, new_width = 1024, 1024
|
||||||
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)
|
|
||||||
|
|
||||||
# 创建一个新的画布,大小为添加 padding 后的尺寸,并设置为白色背景
|
# 创建一个新的画布,大小为添加 padding 后的尺寸,并设置为白色背景
|
||||||
pad_image = Image.new('RGBA', (new_width, new_height), (0, 0, 0, 0))
|
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
|
top = (new_height - height) // 2
|
||||||
pad_image.paste(image, (left, top))
|
pad_image.paste(image, (left, top))
|
||||||
|
|
||||||
# 将画布 resize 成宽度 500,长度 750
|
# 将画布 resize 成宽度 1024,长度 1024
|
||||||
resized_image = pad_image.resize((500, 750))
|
resized_image = pad_image.resize((1024, 1024))
|
||||||
image_size = (512, 768)
|
image_size = (1024, 1024)
|
||||||
|
|
||||||
if resized_image.mode in ('RGBA', 'LA') or (resized_image.mode == 'P' and 'transparency' in resized_image.info):
|
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])
|
background.paste(resized_image, mask=resized_image.split()[3])
|
||||||
image = np.array(background)
|
image = np.array(background)
|
||||||
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
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__':
|
if __name__ == '__main__':
|
||||||
rd = GenerateProductImageModel(
|
rd = GenerateProductImageModel(
|
||||||
tasks_id="123-89",
|
tasks_id="123-89",
|
||||||
# prompt="",
|
# prompt="",
|
||||||
image_strength=0.9,
|
image_strength=0.7,
|
||||||
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",
|
prompt="The best quality, masterpiece,outwear, 8K realistic, HUD",
|
||||||
image_url="aida-results/result_00097282-ebb2-11ee-a822-b48351119060.png",
|
image_url="aida-results/result_53381ada-ac64-11ef-ae9d-0242ac150002.png",
|
||||||
product_type="overall"
|
product_type="overall"
|
||||||
)
|
)
|
||||||
server = GenerateProductImage(rd)
|
server = GenerateProductImage(rd)
|
||||||
|
|||||||
Reference in New Issue
Block a user