fix  minio and s3
This commit is contained in:
zhouchengrong
2024-06-21 17:13:39 +08:00
parent d0597f4b4c
commit 2df1518a99
11 changed files with 200 additions and 146 deletions

View File

@@ -1,6 +1,6 @@
import logging import logging
from fastapi import APIRouter from fastapi import APIRouter
from app.core.config import SR_RABBITMQ_QUEUES, GI_RABBITMQ_QUEUES, GPI_RABBITMQ_QUEUES, GRI_RABBITMQ_QUEUES from app.core.config import SR_RABBITMQ_QUEUES, GI_RABBITMQ_QUEUES, GPI_RABBITMQ_QUEUES, GRI_RABBITMQ_QUEUES, OSS
from fastapi import FastAPI, HTTPException from fastapi import FastAPI, HTTPException
from app.schemas.response_template import ResponseModel from app.schemas.response_template import ResponseModel
@@ -16,6 +16,7 @@ def test(id: int):
"GI_RABBITMQ_QUEUES": GI_RABBITMQ_QUEUES, "GI_RABBITMQ_QUEUES": GI_RABBITMQ_QUEUES,
"GPI_RABBITMQ_QUEUES": GPI_RABBITMQ_QUEUES, "GPI_RABBITMQ_QUEUES": GPI_RABBITMQ_QUEUES,
"GRI_RABBITMQ_QUEUES": GRI_RABBITMQ_QUEUES, "GRI_RABBITMQ_QUEUES": GRI_RABBITMQ_QUEUES,
"local_oss_server": OSS
} }
logger.info(data) logger.info(data)
if id == 1: if id == 1:

View File

@@ -11,12 +11,12 @@ from minio import Minio
import tritonclient.http as httpclient import tritonclient.http as httpclient
from app.core.config import * from app.core.config import *
from app.schemas.attribute_retrieve import AttributeRecognitionModel from app.schemas.attribute_retrieve import AttributeRecognitionModel
from app.service.utils.oss_client import oss_get_image
class AttributeRecognition: class AttributeRecognition:
def __init__(self, const, request_data): def __init__(self, const, request_data):
self.minio_client = Minio(MINIO_URL, access_key=MINIO_ACCESS, secret_key=MINIO_SECRET, secure=MINIO_SECURE) # self.minio_client = Minio(MINIO_URL, access_key=MINIO_ACCESS, secret_key=MINIO_SECRET, secure=MINIO_SECURE)
logging.info("实例化完成")
self.request_data = [] self.request_data = []
for i, sketch in enumerate(request_data): for i, sketch in enumerate(request_data):
self.request_data.append( self.request_data.append(
@@ -97,9 +97,10 @@ class AttributeRecognition:
return res return res
def get_image(self, url): def get_image(self, url):
response = self.minio_client.get_object(url.split("/", 1)[0], url.split("/", 1)[1]) # response = self.minio_client.get_object(url.split("/", 1)[0], url.split("/", 1)[1])
img = np.frombuffer(response.data, np.uint8) # 转成8位无符号整型 # img = np.frombuffer(response.data, np.uint8) # 转成8位无符号整型
img = cv2.imdecode(img, cv2.IMREAD_COLOR) # 解码 # img = cv2.imdecode(img, cv2.IMREAD_COLOR) #
img = oss_get_image(bucket=url.split("/", 1)[0], object_name=url.split("/", 1)[1], data_type="cv2")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
return img return img

View File

@@ -18,12 +18,13 @@ import torch
from app.core.config import * from app.core.config import *
from app.schemas.attribute_retrieve import CategoryRecognitionModel from app.schemas.attribute_retrieve import CategoryRecognitionModel
from app.service.utils.oss_client import oss_get_image
class CategoryRecognition: class CategoryRecognition:
def __init__(self, request_data): def __init__(self, request_data):
self.attr_type = pd.read_csv(CATEGORY_PATH) self.attr_type = pd.read_csv(CATEGORY_PATH)
self.minio_client = Minio(MINIO_URL, access_key=MINIO_ACCESS, secret_key=MINIO_SECRET, secure=MINIO_SECURE) # self.minio_client = Minio(MINIO_URL, access_key=MINIO_ACCESS, secret_key=MINIO_SECRET, secure=MINIO_SECURE)
self.request_data = [] self.request_data = []
self.triton_client = httpclient.InferenceServerClient(url=ATT_TRITON_URL) self.triton_client = httpclient.InferenceServerClient(url=ATT_TRITON_URL)
for sketch in request_data: for sketch in request_data:
@@ -51,9 +52,10 @@ class CategoryRecognition:
def get_image(self, url): def get_image(self, url):
# Get data of an object. # Get data of an object.
# Read data from response. # Read data from response.
response = self.minio_client.get_object(url.split("/", 1)[0], url.split("/", 1)[1]) # response = self.minio_client.get_object(url.split("/", 1)[0], url.split("/", 1)[1])
img = np.frombuffer(response.data, np.uint8) # 转成8位无符号整型 # img = np.frombuffer(response.data, np.uint8) # 转成8位无符号整型
img = cv2.imdecode(img, cv2.IMREAD_COLOR) # 解码 # img = cv2.imdecode(img, cv2.IMREAD_COLOR) # 解码
img = oss_get_image(bucket=url.split("/", 1)[0], object_name=url.split("/", 1)[1], data_type="cv2")
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
return img return img

View File

@@ -1,5 +1,6 @@
import logging import logging
import time import time
import numpy as np import numpy as np
from pymilvus import MilvusClient from pymilvus import MilvusClient
@@ -71,11 +72,8 @@ class KeypointDetection(object):
] ]
try: try:
client = MilvusClient(uri=MILVUS_URL, token=MILVUS_TOKEN, db_name=MILVUS_ALIAS) client = MilvusClient(uri=MILVUS_URL, token=MILVUS_TOKEN, db_name=MILVUS_ALIAS)
start_time = time.time() # start_time = time.time()
res = client.upsert( res = client.upsert(collection_name=MILVUS_TABLE_KEYPOINT, data=data)
collection_name=MILVUS_TABLE_KEYPOINT,
data=data,
)
# logging.info(f"save keypoint time : {time.time() - start_time}") # logging.info(f"save keypoint time : {time.time() - start_time}")
client.close() client.close()
return dict(zip(KEYPOINT_RESULT_TABLE_FIELD_SET, result.reshape(12, 2).astype(int).tolist())) return dict(zip(KEYPOINT_RESULT_TABLE_FIELD_SET, result.reshape(12, 2).astype(int).tolist()))

View File

@@ -1,6 +1,5 @@
import io import io
import logging import logging
import time
import cv2 import cv2
import numpy as np import numpy as np
@@ -8,6 +7,7 @@ from PIL import Image
from minio import Minio from minio import Minio
from app.core.config import * from app.core.config import *
from app.service.utils.oss_client import oss_get_image
from ..builder import PIPELINES from ..builder import PIPELINES
@@ -70,11 +70,7 @@ class LoadImageFromFile(object):
class LoadBodyImageFromFile(object): class LoadBodyImageFromFile(object):
def __init__(self, body_path): def __init__(self, body_path):
self.body_path = body_path self.body_path = body_path
self.minioClient = Minio( # self.minioClient = Minio(f"{MINIO_URL}", access_key=MINIO_ACCESS, secret_key=MINIO_SECRET, secure=MINIO_SECURE)
f"{MINIO_URL}",
access_key=MINIO_ACCESS,
secret_key=MINIO_SECRET,
secure=MINIO_SECURE)
# response = self.minioClient.get_object("aida-mannequins", "model_1693218345.2714431.png") # response = self.minioClient.get_object("aida-mannequins", "model_1693218345.2714431.png")
@@ -82,33 +78,33 @@ class LoadBodyImageFromFile(object):
def __call__(self, result): def __call__(self, result):
result["image_url"] = result['body_path'] = self.body_path result["image_url"] = result['body_path'] = self.body_path
result["name"] = "mannequin" result["name"] = "mannequin"
if not result['image_url'].lower().endswith(".png"): # if not result['image_url'].lower().endswith(".png"):
logging.info(1) # bucket = self.body_path.split("/", 1)[0]
bucket = self.body_path.split("/", 1)[0] # object_name = self.body_path.split("/", 1)[1]
object_name = self.body_path.split("/", 1)[1] # new_object_name = f'{object_name[:object_name.rfind(".")]}.png'
new_object_name = f'{object_name[:object_name.rfind(".")]}.png' # image = self.minioClient.get_object(bucket, object_name)
image = self.minioClient.get_object(bucket, object_name) # image = Image.open(io.BytesIO(image.data))
image = Image.open(io.BytesIO(image.data)) # image = image.convert("RGBA")
image = image.convert("RGBA") # data = image.getdata()
data = image.getdata() # #
# # new_data = []
new_data = [] # for item in data:
for item in data: # if item[0] >= 230 and item[1] >= 230 and item[2] >= 230:
if item[0] >= 230 and item[1] >= 230 and item[2] >= 230: # new_data.append((255, 255, 255, 0))
new_data.append((255, 255, 255, 0)) # else:
else: # new_data.append(item)
new_data.append(item) # image.putdata(new_data)
image.putdata(new_data) # image_data = io.BytesIO()
image_data = io.BytesIO() # image.save(image_data, format='PNG')
image.save(image_data, format='PNG') # image_data.seek(0)
image_data.seek(0) # image_bytes = image_data.read()
image_bytes = image_data.read() # image_path = f"{bucket}/{self.minioClient.put_object(bucket, new_object_name, io.BytesIO(image_bytes), len(image_bytes), content_type='image/png').object_name}"
image_path = f"{bucket}/{self.minioClient.put_object(bucket, new_object_name, io.BytesIO(image_bytes), len(image_bytes), content_type='image/png').object_name}" # self.body_path = image_path
self.body_path = image_path # result["image_url"] = result['body_path'] = self.body_path
result["image_url"] = result['body_path'] = self.body_path # response = self.minioClient.get_object(self.body_path.split("/", 1)[0], self.body_path.split("/", 1)[1])
response = self.minioClient.get_object(self.body_path.split("/", 1)[0], self.body_path.split("/", 1)[1])
# put_image_time = time.time() # put_image_time = time.time()
result['body_image'] = Image.open(io.BytesIO(response.read())) # result['body_image'] = Image.open(io.BytesIO(response.read()))
result['body_image'] = oss_get_image(bucket=self.body_path.split("/", 1)[0], object_name=self.body_path.split("/", 1)[1], data_type="PIL")
# logging.info(f"Image.open time is : {time.time() - put_image_time}") # logging.info(f"Image.open time is : {time.time() - put_image_time}")
return result return result

View File

@@ -1,19 +1,16 @@
import random import random
from io import BytesIO
# import boto3 # import boto3
import cv2 import cv2
import numpy as np import numpy as np
from PIL import Image from PIL import Image
from minio import Minio
from app.core.config import * from app.service.utils.oss_client import oss_get_image
from ..builder import PIPELINES from ..builder import PIPELINES
minio_client = Minio(
MINIO_URL, # minio_client = Minio(MINIO_URL, access_key=MINIO_ACCESS, secret_key=MINIO_SECRET, secure=MINIO_SECURE)
access_key=MINIO_ACCESS,
secret_key=MINIO_SECRET,
secure=MINIO_SECURE)
# s3 = boto3.client('s3', aws_access_key_id=S3_ACCESS_KEY, aws_secret_access_key=S3_AWS_SECRET_ACCESS_KEY, region_name=S3_REGION_NAME) # s3 = boto3.client('s3', aws_access_key_id=S3_ACCESS_KEY, aws_secret_access_key=S3_AWS_SECRET_ACCESS_KEY, region_name=S3_REGION_NAME)
@@ -56,17 +53,18 @@ class Painting(object):
@staticmethod @staticmethod
def get_gradient(bucket_name, object_name): def get_gradient(bucket_name, object_name):
image_data = minio_client.get_object(bucket_name, object_name) # image_data = minio_client.get_object(bucket_name, object_name)
# image_data = s3.get_object(Bucket=bucket_name, Key=object_name)['Body'] # image_data = s3.get_object(Bucket=bucket_name, Key=object_name)['Body']
# 从数据流中读取图像 # 从数据流中读取图像
image_bytes = image_data.read() # image_bytes = image_data.read()
# 将图像数据转换为numpy数组 # 将图像数据转换为numpy数组
image_array = np.asarray(bytearray(image_bytes), dtype=np.uint8) # image_array = np.asarray(bytearray(image_bytes), dtype=np.uint8)
# 使用OpenCV解码图像数组 # 使用OpenCV解码图像数组
image = cv2.imdecode(image_array, cv2.IMREAD_COLOR) # image = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
image = oss_get_image(bucket=bucket_name, object_name=object_name, data_type="cv2")
return image return image
@staticmethod @staticmethod
@@ -494,16 +492,20 @@ class PrintPainting(object):
if not 'IfSingle' in print_dict.keys(): if not 'IfSingle' in print_dict.keys():
print_dict['IfSingle'] = False print_dict['IfSingle'] = False
data = minio_client.get_object(print_dict['print_path_list'][0].split("/", 1)[0], print_dict['print_path_list'][0].split("/", 1)[1]) # data = minio_client.get_object(print_dict['print_path_list'][0].split("/", 1)[0], print_dict['print_path_list'][0].split("/", 1)[1])
# data = s3.get_object(Bucket=print_dict['print_path_list'][0].split("/", 1)[0], Key=print_dict['print_path_list'][0].split("/", 1)[1])['Body'] # data_bytes = BytesIO(data.read())
# image = Image.open(data_bytes)
# image_mode = image.mode
data_bytes = BytesIO(data.read()) bucket_name = print_dict['print_path_list'][0].split("/", 1)[0]
image = Image.open(data_bytes) object_name = print_dict['print_path_list'][0].split("/", 1)[1]
image_mode = image.mode image = oss_get_image(bucket=bucket_name, object_name=object_name, data_type="cv2")
# 判断图片格式如果是RGBA 则贴在一张纯白图片上 防止透明转黑 # 判断图片格式如果是RGBA 则贴在一张纯白图片上 防止透明转黑
if image_mode == "RGBA": if image.shape[2] == 4:
new_background = Image.new('RGB', image.size, (255, 255, 255)) image_rgb = cv2.cvtColor(image, cv2.COLOR_BGRA2RGBA)
new_background.paste(image, mask=image.split()[3]) image_pil = Image.fromarray(image_rgb)
new_background = Image.new('RGB', image_pil.size, (255, 255, 255))
new_background.paste(image_pil, mask=image.split()[3])
image = new_background image = new_background
print_dict['image'] = cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2BGR) print_dict['image'] = cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2BGR)
@@ -577,21 +579,30 @@ class PrintPainting(object):
@staticmethod @staticmethod
def read_image(image_url): def read_image(image_url):
data = minio_client.get_object(image_url.split("/", 1)[0], image_url.split("/", 1)[1]) image = oss_get_image(bucket=image_url.split("/", 1)[0], object_name=image_url.split("/", 1)[1], data_type="cv2")
# data = s3.get_object(Bucket=image_url.split("/", 1)[0], Key=image_url.split("/", 1)[1])['Body'] if image.shape[2] == 4:
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGRA2RGBA)
data_bytes = BytesIO(data.read()) image = Image.fromarray(image_rgb)
image = Image.open(data_bytes) image_mode = "RGBA"
image_mode = image.mode else:
# 判断图片格式如果是RGBA 则贴在一张纯白图片上 防止透明转黑 image_mode = "RGB"
if image_mode == "RGBA":
# new_background = Image.new('RGB', image.size, (255, 255, 255))
# new_background.paste(image, mask=image.split()[3])
# image = new_background
return image, image_mode
image = cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2BGR)
return image, image_mode return image, image_mode
# data = minio_client.get_object(image_url.split("/", 1)[0], image_url.split("/", 1)[1])
# # data = s3.get_object(Bucket=image_url.split("/", 1)[0], Key=image_url.split("/", 1)[1])['Body']
#
# data_bytes = BytesIO(data.read())
# image = Image.open(data_bytes)
# image_mode = image.mode
# # 判断图片格式如果是RGBA 则贴在一张纯白图片上 防止透明转黑
# if image_mode == "RGBA":
# # new_background = Image.new('RGB', image.size, (255, 255, 255))
# # new_background.paste(image, mask=image.split()[3])
# # image = new_background
# return image, image_mode
# image = cv2.cvtColor(np.asarray(image), cv2.COLOR_RGB2BGR)
# return image, "RGB"
# @staticmethod # @staticmethod
# def read_image(image_url): # def read_image(image_url):
# response = requests.get(image_url) # response = requests.get(image_url)

View File

@@ -41,7 +41,7 @@ class Split(object):
else: else:
back_mask = result['back_mask'] back_mask = result['back_mask']
rgba_image = rgb_to_rgba((result['final_image'].shape[0], result['final_image'].shape[1]), result['final_image'], result['mask']) rgba_image = rgb_to_rgba((result['final_image'].shape[0], result['final_image'].shape[1]), re4sult['final_image'], result['mask'])
result_front_image = np.zeros_like(rgba_image) result_front_image = np.zeros_like(rgba_image)
result_front_image[front_mask != 0] = rgba_image[front_mask != 0] result_front_image[front_mask != 0] = rgba_image[front_mask != 0]

View File

@@ -13,15 +13,12 @@ import io
from app.core.config import * from app.core.config import *
from app.service.design.utils.design_ensemble import get_keypoint_result from app.service.design.utils.design_ensemble import get_keypoint_result
from app.service.utils.oss_client import oss_get_image, oss_upload_image
class DesignPreprocessing: class DesignPreprocessing:
def __init__(self): # def __init__(self):
self.minio_client = Minio( # self.minio_client = Minio(MINIO_URL, access_key=MINIO_ACCESS, secret_key=MINIO_SECRET, secure=MINIO_SECURE)
MINIO_URL,
access_key=MINIO_ACCESS,
secret_key=MINIO_SECRET,
secure=MINIO_SECURE)
# @ RunTime # @ RunTime
def pipeline(self, image_list): def pipeline(self, image_list):
@@ -51,8 +48,9 @@ class DesignPreprocessing:
def read_image(self, image_list): def read_image(self, image_list):
for obj in image_list: for obj in image_list:
file = self.minio_client.get_object(obj['image_url'].split("/", 1)[0], obj['image_url'].split("/", 1)[1]).data # file = self.minio_client.get_object(obj['image_url'].split("/", 1)[0], obj['image_url'].split("/", 1)[1]).data
image = cv2.imdecode(np.frombuffer(file, np.uint8), 1) # image = cv2.imdecode(np.frombuffer(file, np.uint8), 1)
image = oss_get_image(bucket=obj['image_url'].split("/", 1)[0], object_name=obj['image_url'].split("/", 1)[1], data_type="cv2")
if len(image.shape) == 2: if len(image.shape) == 2:
image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB) image = cv2.cvtColor(image, cv2.COLOR_GRAY2RGB)
elif image.shape[2] == 4: # 如果是四通道 mask elif image.shape[2] == 4: # 如果是四通道 mask
@@ -125,7 +123,10 @@ class DesignPreprocessing:
try: try:
# 覆盖到minio # 覆盖到minio
image_bytes = cv2.imencode(".jpg", item['obj'])[1].tobytes() image_bytes = cv2.imencode(".jpg", item['obj'])[1].tobytes()
self.minio_client.put_object(item['image_url'].split("/", 1)[0], item['image_url'].split("/", 1)[1], io.BytesIO(image_bytes), len(image_bytes), content_type="image/jpeg", ) # self.minio_client.put_object(item['image_url'].split("/", 1)[0], item['image_url'].split("/", 1)[1], io.BytesIO(image_bytes), len(image_bytes), content_type="image/jpeg", )
bucket_name = item['image_url'].split("/", 1)[0]
object_name = item['image_url'].split("/", 1)[1]
oss_upload_image(bucket=bucket_name, object_name=object_name, image_bytes=image_bytes)
print(f"Object '{item['image_url'].split('/', 1)[1]}' overwritten successfully.") print(f"Object '{item['image_url'].split('/', 1)[1]}' overwritten successfully.")
except ResponseError as err: except ResponseError as err:
print(f"Error: {err}") print(f"Error: {err}")
@@ -165,36 +166,76 @@ class DesignPreprocessing:
# @ RunTime # @ RunTime
def composing_image(self, image_list): def composing_image(self, image_list):
for image in image_list: for image in image_list:
if image['site'] == 'down': ''' 比例相同 整合上下装代码'''
image_width = image['obj'].shape[1] image_width = image['obj'].shape[1]
waist_width = image['keypoint_result']['waistband_right'][1] - image['keypoint_result']['waistband_left'][1] waist_width = image['keypoint_result']['waistband_right'][1] - image['keypoint_result']['waistband_left'][1]
scale = 0.4 scale = 0.4
if waist_width / scale >= image['obj'].shape[1]: if waist_width / scale >= image_width:
add_width = int((waist_width / scale - image_width) / 2) add_width = int((waist_width / scale - image_width) / 2)
ret = cv2.copyMakeBorder(image['obj'], 0, 0, add_width, add_width, cv2.BORDER_CONSTANT, value=(256, 256, 256)) ret = cv2.copyMakeBorder(image['obj'], 0, 0, add_width, add_width, cv2.BORDER_CONSTANT, value=(256, 256, 256))
if IF_DEBUG_SHOW: if IF_DEBUG_SHOW:
cv2.imshow("composing_image", ret) cv2.imshow("composing_image", ret)
cv2.waitKey(0) cv2.waitKey(0)
image_bytes = cv2.imencode(".jpg", ret)[1].tobytes() image_bytes = cv2.imencode(".jpg", ret)[1].tobytes()
image['show_image_url'] = f"{image['image_url'].split('/', 1)[0]}/{self.minio_client.put_object(image['image_url'].split('/', 1)[0], image['image_url'].split('/', 1)[1].replace('.', '-show.'), io.BytesIO(image_bytes), len(image_bytes), content_type='image/jpeg').object_name}" # image['show_image_url'] = f"{image['image_url'].split('/', 1)[0]}/{self.minio_client.put_object(image['image_url'].split('/', 1)[0], image['image_url'].split('/', 1)[1].replace('.', '-show.'), io.BytesIO(image_bytes), len(image_bytes), content_type='image/jpeg').object_name}"
else: bucket_name = image['image_url'].split('/', 1)[0]
image_bytes = cv2.imencode(".jpg", image['obj'])[1].tobytes() object_name = image['image_url'].split('/', 1)[1]
image['show_image_url'] = f"{image['image_url'].split('/', 1)[0]}/{self.minio_client.put_object(image['image_url'].split('/', 1)[0], image['image_url'].split('/', 1)[1].replace('.', '-show.'), io.BytesIO(image_bytes), len(image_bytes), content_type='image/jpeg').object_name}" oss_upload_image(bucket=bucket_name, object_name=object_name, image_bytes=image_bytes)
image['show_image_url'] = f"{bucket_name}/{object_name}"
else: else:
scale = 0.4 image_bytes = cv2.imencode(".jpg", image['obj'])[1].tobytes()
image_width = image['obj'].shape[1] # image['show_image_url'] = f"{image['image_url'].split('/', 1)[0]}/{self.minio_client.put_object(image['image_url'].split('/', 1)[0], image['image_url'].split('/', 1)[1].replace('.', '-show.'), io.BytesIO(image_bytes), len(image_bytes), content_type='image/jpeg').object_name}"
waist_width = image['keypoint_result']['armpit_right'][1] - image['keypoint_result']['armpit_left'][1] bucket_name = image['image_url'].split('/', 1)[0]
if waist_width / scale >= image_width: object_name = image['image_url'].split('/', 1)[1]
add_width = int((waist_width / scale - image_width) / 2) oss_upload_image(bucket=bucket_name, object_name=object_name, image_bytes=image_bytes)
ret = cv2.copyMakeBorder(image['obj'], 0, 0, add_width, add_width, cv2.BORDER_CONSTANT, value=(256, 256, 256)) image['show_image_url'] = f"{bucket_name}/{object_name}"
if IF_DEBUG_SHOW:
cv2.imshow("composing_image", ret) # if image['site'] == 'down':
cv2.waitKey(0) # image_width = image['obj'].shape[1]
image_bytes = cv2.imencode(".jpg", ret)[1].tobytes() # waist_width = image['keypoint_result']['waistband_right'][1] - image['keypoint_result']['waistband_left'][1]
image['show_image_url'] = f"{image['image_url'].split('/', 1)[0]}/{self.minio_client.put_object(image['image_url'].split('/', 1)[0], image['image_url'].split('/', 1)[1].replace('.', '-show.'), io.BytesIO(image_bytes), len(image_bytes), content_type='image/jpeg').object_name}" # scale = 0.4
else: # if waist_width / scale >= image_width:
image_bytes = cv2.imencode(".jpg", image['obj'])[1].tobytes() # add_width = int((waist_width / scale - image_width) / 2)
image['show_image_url'] = f"{image['image_url'].split('/', 1)[0]}/{self.minio_client.put_object(image['image_url'].split('/', 1)[0], image['image_url'].split('/', 1)[1].replace('.', '-show.'), io.BytesIO(image_bytes), len(image_bytes), content_type='image/jpeg').object_name}" # ret = cv2.copyMakeBorder(image['obj'], 0, 0, add_width, add_width, cv2.BORDER_CONSTANT, value=(256, 256, 256))
# if IF_DEBUG_SHOW:
# cv2.imshow("composing_image", ret)
# cv2.waitKey(0)
# image_bytes = cv2.imencode(".jpg", ret)[1].tobytes()
# # image['show_image_url'] = f"{image['image_url'].split('/', 1)[0]}/{self.minio_client.put_object(image['image_url'].split('/', 1)[0], image['image_url'].split('/', 1)[1].replace('.', '-show.'), io.BytesIO(image_bytes), len(image_bytes), content_type='image/jpeg').object_name}"
# bucket_name = image['image_url'].split('/', 1)[0]
# object_name = image['image_url'].split('/', 1)[1]
# oss_upload_image(bucket=bucket_name, object_name=object_name, image_bytes=image_bytes)
# image['show_image_url'] = f"{bucket_name}/{object_name}"
# else:
# image_bytes = cv2.imencode(".jpg", image['obj'])[1].tobytes()
# # image['show_image_url'] = f"{image['image_url'].split('/', 1)[0]}/{self.minio_client.put_object(image['image_url'].split('/', 1)[0], image['image_url'].split('/', 1)[1].replace('.', '-show.'), io.BytesIO(image_bytes), len(image_bytes), content_type='image/jpeg').object_name}"
# bucket_name = image['image_url'].split('/', 1)[0]
# object_name = image['image_url'].split('/', 1)[1]
# oss_upload_image(bucket=bucket_name, object_name=object_name, image_bytes=image_bytes)
# image['show_image_url'] = f"{bucket_name}/{object_name}"
# else:
# image_width = image['obj'].shape[1]
# waist_width = image['keypoint_result']['waistband_right'][1] - image['keypoint_result']['waistband_left'][1]
# scale = 0.4
# if waist_width / scale >= image_width:
# add_width = int((waist_width / scale - image_width) / 2)
# ret = cv2.copyMakeBorder(image['obj'], 0, 0, add_width, add_width, cv2.BORDER_CONSTANT, value=(256, 256, 256))
# if IF_DEBUG_SHOW:
# cv2.imshow("composing_image", ret)
# cv2.waitKey(0)
# image_bytes = cv2.imencode(".jpg", ret)[1].tobytes()
# # image['show_image_url'] = f"{image['image_url'].split('/', 1)[0]}/{self.minio_client.put_object(image['image_url'].split('/', 1)[0], image['image_url'].split('/', 1)[1].replace('.', '-show.'), io.BytesIO(image_bytes), len(image_bytes), content_type='image/jpeg').object_name}"
# bucket_name = image['image_url'].split('/', 1)[0]
# object_name = image['image_url'].split('/', 1)[1]
# oss_upload_image(bucket=bucket_name, object_name=object_name, image_bytes=image_bytes)
# image['show_image_url'] = f"{bucket_name}/{object_name}"
# else:
# image_bytes = cv2.imencode(".jpg", image['obj'])[1].tobytes()
# # image['show_image_url'] = f"{image['image_url'].split('/', 1)[0]}/{self.minio_client.put_object(image['image_url'].split('/', 1)[0], image['image_url'].split('/', 1)[1].replace('.', '-show.'), io.BytesIO(image_bytes), len(image_bytes), content_type='image/jpeg').object_name}"
# bucket_name = image['image_url'].split('/', 1)[0]
# object_name = image['image_url'].split('/', 1)[1]
# oss_upload_image(bucket=bucket_name, object_name=object_name, image_bytes=image_bytes)
# image['show_image_url'] = f"{bucket_name}/{object_name}"
return image_list return image_list
@staticmethod @staticmethod

View File

@@ -10,22 +10,17 @@
import json import json
import logging import logging
import time import time
from io import BytesIO
import cv2 import cv2
import minio import minio
import redis import redis
import tritonclient.grpc as grpcclient import tritonclient.grpc as grpcclient
import numpy as np import numpy as np
from minio import Minio
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 *
from app.schemas.generate_image import GenerateImageModel from app.schemas.generate_image import GenerateImageModel
from app.service.generate_image.utils.adjust_contrast import adjust_contrast
from app.service.generate_image.utils.image_processing import remove_background, stain_detection, generate_category_recognition, autoLevels, luminance_adjust, face_detect_pic from app.service.generate_image.utils.image_processing import remove_background, stain_detection, generate_category_recognition, autoLevels, luminance_adjust, face_detect_pic
from app.service.generate_image.utils.upload_sd_image import upload_png_sd, upload_stain_png_sd from app.service.generate_image.utils.upload_sd_image import upload_png_sd
from app.service.utils.oss_client import get_image from app.service.utils.oss_client import oss_get_image
logger = logging.getLogger() logger = logging.getLogger()
@@ -70,7 +65,7 @@ class GenerateImage:
# image_cv2 = cv2.imdecode(image_array, cv2.IMREAD_COLOR) # image_cv2 = cv2.imdecode(image_array, cv2.IMREAD_COLOR)
# image_rbg = cv2.cvtColor(image_cv2, cv2.COLOR_BGR2RGB) # image_rbg = cv2.cvtColor(image_cv2, cv2.COLOR_BGR2RGB)
image_cv2 = get_image(object_name=image_url, data_type="cv2") image_cv2 = oss_get_image(bucket=image_url.split('/')[0], object_name=image_url, data_type="cv2")
image_rbg = cv2.cvtColor(image_cv2, cv2.COLOR_BGR2RGB) image_rbg = cv2.cvtColor(image_cv2, cv2.COLOR_BGR2RGB)
image = cv2.resize(image_rbg, (1024, 1024)) image = cv2.resize(image_rbg, (1024, 1024))
except minio.error.S3Error: except minio.error.S3Error:

View File

@@ -1,17 +1,15 @@
import io import json
import logging import logging
import time import time
import minio.error
import redis
import json
import cv2 import cv2
import minio.error
import numpy as np import numpy as np
import redis
import torch import torch
import tritonclient.grpc as grpcclient import tritonclient.grpc as grpcclient
from minio import Minio
from app.core.config import * from app.core.config import *
from app.schemas.super_resolution import SuperResolutionModel from app.schemas.super_resolution import SuperResolutionModel
from app.service.utils.decorator import RunTime from app.service.utils.oss_client import oss_get_image, oss_upload_image
logger = logging.getLogger() logger = logging.getLogger()
@@ -24,7 +22,7 @@ class SuperResolution:
self.user_id = self.tasks_id[self.tasks_id.rfind('-') + 1:] self.user_id = self.tasks_id[self.tasks_id.rfind('-') + 1:]
self.sr_image_url = data.sr_image_url self.sr_image_url = data.sr_image_url
self.sr_xn = data.sr_xn self.sr_xn = data.sr_xn
self.minio_client = Minio(MINIO_URL, access_key=MINIO_ACCESS, secret_key=MINIO_SECRET, secure=MINIO_SECURE) # self.minio_client = Minio(MINIO_URL, access_key=MINIO_ACCESS, secret_key=MINIO_SECRET, secure=MINIO_SECURE)
self.redis_client.set(self.tasks_id, json.dumps({'status': 'PENDING', 'message': "pending", 'data': ''})) self.redis_client.set(self.tasks_id, json.dumps({'status': 'PENDING', 'message': "pending", 'data': ''}))
self.redis_client.expire(self.tasks_id, 600) self.redis_client.expire(self.tasks_id, 600)
self.connection = pika.BlockingConnection(pika.ConnectionParameters(**RABBITMQ_PARAMS)) self.connection = pika.BlockingConnection(pika.ConnectionParameters(**RABBITMQ_PARAMS))
@@ -33,16 +31,25 @@ class SuperResolution:
# @RunTime # @RunTime
def read_image(self): def read_image(self):
try: try:
image_data = self.minio_client.get_object(self.sr_image_url.split("/", 1)[0], self.sr_image_url.split("/", 1)[1]) img = oss_get_image(bucket=self.sr_image_url.split("/", 1)[0], object_name=self.sr_image_url.split("/", 1)[1], data_type="cv2")
except minio.error.S3Error as e: except minio.error.S3Error as e:
sr_data = json.dumps({'tasks_id': self.tasks_id, 'status': 'ERROR', 'message': f'{e}'}) sr_data = json.dumps({'tasks_id': self.tasks_id, 'status': 'ERROR', 'message': f'{e}'})
self.channel.basic_publish(exchange='', routing_key=SR_RABBITMQ_QUEUES, body=sr_data) self.channel.basic_publish(exchange='', routing_key=SR_RABBITMQ_QUEUES, body=sr_data)
logger.info(f" [x] Sent {sr_data}") logger.info(f" [x] Sent {sr_data}")
raise FileNotFoundError(f"Image '{self.sr_image_url.split('/', 1)[1]}' not found in bucket '{self.sr_image_url.split('/', 1)[0]}'") raise FileNotFoundError(f"Image '{self.sr_image_url.split('/', 1)[1]}' not found in bucket '{self.sr_image_url.split('/', 1)[0]}'")
img = np.frombuffer(image_data.data, np.uint8) # 转成8位无符号整型
img = cv2.imdecode(img, cv2.IMREAD_COLOR).astype(np.float32) / 255. # 解码
return img return img
# try:
# image_data = self.minio_client.get_object(self.sr_image_url.split("/", 1)[0], self.sr_image_url.split("/", 1)[1])
# except minio.error.S3Error as e:
# sr_data = json.dumps({'tasks_id': self.tasks_id, 'status': 'ERROR', 'message': f'{e}'})
# self.channel.basic_publish(exchange='', routing_key=SR_RABBITMQ_QUEUES, body=sr_data)
# logger.info(f" [x] Sent {sr_data}")
# raise FileNotFoundError(f"Image '{self.sr_image_url.split('/', 1)[1]}' not found in bucket '{self.sr_image_url.split('/', 1)[0]}'")
# img = np.frombuffer(image_data.data, np.uint8) # 转成8位无符号整型
# img = cv2.imdecode(img, cv2.IMREAD_COLOR).astype(np.float32) / 255. # 解码
# return img
def read_tasks_status(self): def read_tasks_status(self):
status_data = json.loads(self.redis_client.get(self.tasks_id)) status_data = json.loads(self.redis_client.get(self.tasks_id))
logging.info(f"{self.tasks_id} ===> {status_data}") logging.info(f"{self.tasks_id} ===> {status_data}")
@@ -101,8 +108,10 @@ class SuperResolution:
def upload_img_sr(self, image): def upload_img_sr(self, image):
try: try:
image_bytes = cv2.imencode('.jpg', image)[1].tobytes() image_bytes = cv2.imencode('.jpg', image)[1].tobytes()
res = self.minio_client.put_object(f'{SR_MINIO_BUCKET}', f'{self.user_id}/sr/output/{self.tasks_id}.jpg', io.BytesIO(image_bytes), len(image_bytes), content_type='image/png') # res = self.minio_client.put_object(f'{SR_MINIO_BUCKET}', f'{self.user_id}/sr/output/{self.tasks_id}.jpg', io.BytesIO(image_bytes), len(image_bytes), content_type='image/png')
image_url = f"aida-users/{res.object_name}" object_name = f'{self.user_id}/sr/output/{self.tasks_id}.jpg'
oss_upload_image(bucket=SR_MINIO_BUCKET, object_name=object_name, image_bytes=image_bytes)
image_url = f"{SR_MINIO_BUCKET}/{object_name}"
return image_url return image_url
except Exception as e: except Exception as e:
logger.warning(f"upload_png_mask runtime exception : {e}") logger.warning(f"upload_png_mask runtime exception : {e}")

View File

@@ -15,8 +15,8 @@ logger = logging.getLogger()
# 获取图片 # 获取图片
def oss_get_image(bucket, object_name, data_type): def oss_get_image(bucket, object_name, data_type):
# cv2 默认全通道读取
image_object = None image_object = None
try: try:
if OSS == "minio": if OSS == "minio":
oss_client = Minio(MINIO_URL, access_key=MINIO_ACCESS, secret_key=MINIO_SECRET, secure=MINIO_SECURE) oss_client = Minio(MINIO_URL, access_key=MINIO_ACCESS, secret_key=MINIO_SECRET, secure=MINIO_SECURE)
@@ -24,11 +24,10 @@ def oss_get_image(bucket, object_name, data_type):
else: else:
oss_client = boto3.client('s3', aws_access_key_id=S3_ACCESS_KEY, aws_secret_access_key=S3_AWS_SECRET_ACCESS_KEY, region_name=S3_REGION_NAME) oss_client = boto3.client('s3', aws_access_key_id=S3_ACCESS_KEY, aws_secret_access_key=S3_AWS_SECRET_ACCESS_KEY, region_name=S3_REGION_NAME)
image_data = oss_client.get_object(Bucket=bucket, Key=object_name)['Body'] image_data = oss_client.get_object(Bucket=bucket, Key=object_name)['Body']
if data_type == "cv2": if data_type == "cv2":
image_bytes = image_data.read() image_bytes = image_data.read()
image_array = np.frombuffer(image_bytes, np.uint8) # 转成8位无符号整型 image_array = np.frombuffer(image_bytes, np.uint8) # 转成8位无符号整型
image_object = cv2.imdecode(image_array, cv2.IMREAD_COLOR) image_object = cv2.imdecode(image_array, cv2.IMREAD_UNCHANGED)
else: else:
data_bytes = BytesIO(image_data.read()) data_bytes = BytesIO(image_data.read())
image_object = Image.open(data_bytes) image_object = Image.open(data_bytes)
@@ -56,11 +55,12 @@ if __name__ == '__main__':
# url = "aida-collection-element/11523/Moodboard/f60af0d2-94c2-48f9-90ff-74b8e8a481b5.jpg" # url = "aida-collection-element/11523/Moodboard/f60af0d2-94c2-48f9-90ff-74b8e8a481b5.jpg"
# url = "aida-sys-image/images/female/outwear/0628000054.jpg" # url = "aida-sys-image/images/female/outwear/0628000054.jpg"
# url = "aida-users/89/product_image/string-89.png" # url = "aida-users/89/product_image/string-89.png"
# url = "aida-users/89/single_logo/123-89.png" url = "aida-users/89/single_logo/123-89.png"
# url = 'aida-users/89/relight_image/123-89.png' # url = 'aida-users/89/relight_image/123-89.png'
# url = 'aida-users/89/relight_image/123-89.png' # url = 'aida-users/89/relight_image/123-89.png'
url = 'aida-users/89/relight_image/123-89.png' # url = 'aida-users/89/relight_image/123-89.png'
read_type = "PIL" # url = "aida-users/89/sketchboard/female/Dress/e6724ab7-8d3f-4677-abe0-c3e42ab7af85.jpeg"
read_type = "cv2"
if read_type == "cv2": if read_type == "cv2":
img = oss_get_image(bucket=url.split('/')[0], object_name=url[url.find('/') + 1:], data_type=read_type) img = oss_get_image(bucket=url.split('/')[0], object_name=url[url.find('/') + 1:], data_type=read_type)
cv2.imshow("", img) cv2.imshow("", img)