feat 4090 triton server 测试
fix
This commit is contained in:
@@ -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"
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user