From f2bb7a11f90da505ed44d8e55cadffc7418193f8 Mon Sep 17 00:00:00 2001 From: zhouchengrong Date: Mon, 17 Jun 2024 13:10:46 +0800 Subject: [PATCH] =?UTF-8?q?feat=20=20=E6=9B=B4=E6=96=B0=E5=93=8D=E5=BA=94?= =?UTF-8?q?=E6=A8=A1=E6=9D=BF=20fix?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/service/design/service.py | 3 + app/service/design/utils/synthesis_item.py | 2 +- app/service/design/utils/upload_image.py | 213 +++++++++++---------- logging_env.py | 2 +- 4 files changed, 112 insertions(+), 108 deletions(-) diff --git a/app/service/design/service.py b/app/service/design/service.py index 372456f..0ba5e72 100644 --- a/app/service/design/service.py +++ b/app/service/design/service.py @@ -5,6 +5,8 @@ from app.service.design.utils.redis_utils import Redis from app.service.design.utils.synthesis_item import synthesis, synthesis_single import concurrent.futures +from app.service.utils.decorator import RunTime + def process_item(item, layers): # logging.info("process running.........") @@ -38,6 +40,7 @@ def final_progress(process_id): return progress +@RunTime def generate(request_data): return_response = {} request_data = request_data.dict() diff --git a/app/service/design/utils/synthesis_item.py b/app/service/design/utils/synthesis_item.py index e6a2f25..caf3fcb 100644 --- a/app/service/design/utils/synthesis_item.py +++ b/app/service/design/utils/synthesis_item.py @@ -75,7 +75,7 @@ def positioning(all_mask_shape, mask_shape, offset): return all_start, all_end, mask_start, mask_end -@RunTime +# @RunTime def synthesis(data, size): # 创建底图 base_image = Image.new('RGBA', size, (0, 0, 0, 0)) diff --git a/app/service/design/utils/upload_image.py b/app/service/design/utils/upload_image.py index 2142126..a4195f7 100644 --- a/app/service/design/utils/upload_image.py +++ b/app/service/design/utils/upload_image.py @@ -28,128 +28,129 @@ minio_client = Minio( # 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) - -@RunTime -def upload_png_mask(front_image, object_name, mask=None): - mask_url = None - if mask is not None: - # 反转掩模 - mask_inverted = cv2.bitwise_not(mask) - # 将掩模转换为 RGBA 格式 - rgba_image = cv2.cvtColor(mask_inverted, cv2.COLOR_BGR2BGRA) - rgba_image[rgba_image[:, :, 0] == 0] = [0, 0, 0, 0] - # 将图像数据保存到内存中的 BytesIO 对象中 - image_bytes = io.BytesIO() - image_bytes.write(cv2.imencode('.png', rgba_image)[1].tobytes()) - image_bytes.seek(0) - try: - key = f"mask/mask_{object_name}.png" - mask_url = f"{AIDA_CLOTHING}/{key}" - s3.put_object(Bucket=AIDA_CLOTHING, Key=key, Body=image_bytes, ContentType='image/png') - except Exception as e: - print(f'上传到 S3 失败: {e}') - with io.BytesIO() as output: - front_image.save(output, format='PNG') - data = output.getvalue() - # 创建一个 S3 客户端 - try: - key = f"image/image_{object_name}.png" - image_url = f"{AIDA_CLOTHING}/{key}" - s3.put_object(Bucket=AIDA_CLOTHING, Key=key, Body=data, ContentType='image/png') - return front_image, image_url, mask_url - except Exception as e: - print(f'上传到 S3 失败: {e}') - - -@RunTime -def upload_layer_image(image, object_name): - with io.BytesIO() as output: - image.save(output, format='PNG') - data = output.getvalue() - # 创建一个 S3 客户端 - try: - key = f"image/image_{object_name}.png" - image_url = f"{AIDA_CLOTHING}/{key}" - s3.put_object(Bucket=AIDA_CLOTHING, Key=key, Body=data, ContentType='image/png') - return image_url - except Exception as e: - print(f'上传到 S3 失败: {e}') - - -@RunTime -def upload_mask_image(mask, object_name): - # 反转掩模 - mask_inverted = cv2.bitwise_not(mask) - # 将掩模转换为 RGBA 格式 - rgba_image = cv2.cvtColor(mask_inverted, cv2.COLOR_BGR2BGRA) - rgba_image[rgba_image[:, :, 0] == 0] = [0, 0, 0, 0] - # 将图像数据保存到内存中的 BytesIO 对象中 - image_bytes = io.BytesIO() - image_bytes.write(cv2.imencode('.png', rgba_image)[1].tobytes()) - image_bytes.seek(0) - try: - key = f"mask/mask_{object_name}.png" - mask_url = f"{AIDA_CLOTHING}/{key}" - s3.put_object(Bucket=AIDA_CLOTHING, Key=key, Body=image_bytes, ContentType='image/png') - return mask_url - except Exception as e: - print(f'上传到 S3 失败: {e}') - - -"""minio 上传""" - +# # @RunTime # def upload_png_mask(front_image, object_name, mask=None): -# start_time = time.time() +# mask_url = None +# if mask is not None: +# # 反转掩模 +# mask_inverted = cv2.bitwise_not(mask) +# # 将掩模转换为 RGBA 格式 +# rgba_image = cv2.cvtColor(mask_inverted, cv2.COLOR_BGR2BGRA) +# rgba_image[rgba_image[:, :, 0] == 0] = [0, 0, 0, 0] +# # 将图像数据保存到内存中的 BytesIO 对象中 +# image_bytes = io.BytesIO() +# image_bytes.write(cv2.imencode('.png', rgba_image)[1].tobytes()) +# image_bytes.seek(0) +# try: +# key = f"mask/mask_{object_name}.png" +# mask_url = f"{AIDA_CLOTHING}/{key}" +# s3.put_object(Bucket=AIDA_CLOTHING, Key=key, Body=image_bytes, ContentType='image/png') +# except Exception as e: +# print(f'上传到 S3 失败: {e}') +# with io.BytesIO() as output: +# front_image.save(output, format='PNG') +# data = output.getvalue() +# # 创建一个 S3 客户端 # try: -# mask_url = None -# if mask is not None: -# mask_inverted = cv2.bitwise_not(mask) -# # 将掩模的3通道转换为4通道,白色部分不透明,黑色部分透明 -# rgba_image = cv2.cvtColor(mask_inverted, cv2.COLOR_BGR2BGRA) -# rgba_image[rgba_image[:, :, 0] == 0] = [0, 0, 0, 0] -# image_bytes = io.BytesIO() -# image_bytes.write(cv2.imencode('.png', rgba_image)[1].tobytes()) -# -# image_bytes.seek(0) -# mask_url = f"{AIDA_CLOTHING}/{minio_client.put_object('aida-clothing', f'mask/mask_{object_name}.png', image_bytes, len(image_bytes.getvalue()), content_type='image/png').object_name}" -# -# image_data = io.BytesIO() -# front_image.save(image_data, format='PNG') -# image_data.seek(0) -# image_bytes = image_data.read() -# image_url = f"{AIDA_CLOTHING}/{minio_client.put_object('aida-clothing', f'image/image_{object_name}.png', io.BytesIO(image_bytes), len(image_bytes), content_type='image/png').object_name}" -# # print(f"upload_png_mask {object_name} = {time.time() - start_time}") +# key = f"image/image_{object_name}.png" +# image_url = f"{AIDA_CLOTHING}/{key}" +# s3.put_object(Bucket=AIDA_CLOTHING, Key=key, Body=data, ContentType='image/png') # return front_image, image_url, mask_url # except Exception as e: -# logging.warning(f"upload_png_mask runtime exception : {e}") +# print(f'上传到 S3 失败: {e}') # # # @RunTime # def upload_layer_image(image, object_name): +# with io.BytesIO() as output: +# image.save(output, format='PNG') +# data = output.getvalue() +# # 创建一个 S3 客户端 # try: -# image_data = io.BytesIO() -# image.save(image_data, format='PNG') -# image_data.seek(0) -# image_bytes = image_data.read() -# image_url = f"{AIDA_CLOTHING}/{minio_client.put_object('aida-clothing', f'image/image_{object_name}.png', io.BytesIO(image_bytes), len(image_bytes), content_type='image/png').object_name}" +# key = f"image/image_{object_name}.png" +# image_url = f"{AIDA_CLOTHING}/{key}" +# s3.put_object(Bucket=AIDA_CLOTHING, Key=key, Body=data, ContentType='image/png') # return image_url # except Exception as e: -# logging.warning(f"upload_png_mask runtime exception : {e}") +# print(f'上传到 S3 失败: {e}') # # # @RunTime # def upload_mask_image(mask, object_name): +# # 反转掩模 +# mask_inverted = cv2.bitwise_not(mask) +# # 将掩模转换为 RGBA 格式 +# rgba_image = cv2.cvtColor(mask_inverted, cv2.COLOR_BGR2BGRA) +# rgba_image[rgba_image[:, :, 0] == 0] = [0, 0, 0, 0] +# # 将图像数据保存到内存中的 BytesIO 对象中 +# image_bytes = io.BytesIO() +# image_bytes.write(cv2.imencode('.png', rgba_image)[1].tobytes()) +# image_bytes.seek(0) # try: -# mask_inverted = cv2.bitwise_not(mask) -# # 将掩模的3通道转换为4通道,白色部分不透明,黑色部分透明 -# rgba_image = cv2.cvtColor(mask_inverted, cv2.COLOR_BGR2BGRA) -# rgba_image[rgba_image[:, :, 0] == 0] = [0, 0, 0, 0] -# image_bytes = io.BytesIO() -# image_bytes.write(cv2.imencode('.png', rgba_image)[1].tobytes()) -# -# image_bytes.seek(0) -# mask_url = f"{AIDA_CLOTHING}/{minio_client.put_object('aida-clothing', f'mask/mask_{object_name}.png', image_bytes, len(image_bytes.getvalue()), content_type='image/png').object_name}" +# key = f"mask/mask_{object_name}.png" +# mask_url = f"{AIDA_CLOTHING}/{key}" +# s3.put_object(Bucket=AIDA_CLOTHING, Key=key, Body=image_bytes, ContentType='image/png') # return mask_url # except Exception as e: -# logging.warning(f"upload_png_mask runtime exception : {e}") +# print(f'上传到 S3 失败: {e}') + + +"""minio 上传""" + + +# @RunTime +def upload_png_mask(front_image, object_name, mask=None): + start_time = time.time() + try: + mask_url = None + if mask is not None: + mask_inverted = cv2.bitwise_not(mask) + # 将掩模的3通道转换为4通道,白色部分不透明,黑色部分透明 + rgba_image = cv2.cvtColor(mask_inverted, cv2.COLOR_BGR2BGRA) + rgba_image[rgba_image[:, :, 0] == 0] = [0, 0, 0, 0] + image_bytes = io.BytesIO() + image_bytes.write(cv2.imencode('.png', rgba_image)[1].tobytes()) + + image_bytes.seek(0) + mask_url = f"{AIDA_CLOTHING}/{minio_client.put_object('aida-clothing', f'mask/mask_{object_name}.png', image_bytes, len(image_bytes.getvalue()), content_type='image/png').object_name}" + + image_data = io.BytesIO() + front_image.save(image_data, format='PNG') + image_data.seek(0) + image_bytes = image_data.read() + image_url = f"{AIDA_CLOTHING}/{minio_client.put_object('aida-clothing', f'image/image_{object_name}.png', io.BytesIO(image_bytes), len(image_bytes), content_type='image/png').object_name}" + # print(f"upload_png_mask {object_name} = {time.time() - start_time}") + return front_image, image_url, mask_url + except Exception as e: + logging.warning(f"upload_png_mask runtime exception : {e}") + + +@RunTime +def upload_layer_image(image, object_name): + try: + image_data = io.BytesIO() + image.save(image_data, format='PNG') + image_data.seek(0) + image_bytes = image_data.read() + image_url = f"{AIDA_CLOTHING}/{minio_client.put_object('aida-clothing', f'image/image_{object_name}.png', io.BytesIO(image_bytes), len(image_bytes), content_type='image/png').object_name}" + return image_url + except Exception as e: + logging.warning(f"upload_png_mask runtime exception : {e}") + + +@RunTime +def upload_mask_image(mask, object_name): + try: + mask_inverted = cv2.bitwise_not(mask) + # 将掩模的3通道转换为4通道,白色部分不透明,黑色部分透明 + rgba_image = cv2.cvtColor(mask_inverted, cv2.COLOR_BGR2BGRA) + rgba_image[rgba_image[:, :, 0] == 0] = [0, 0, 0, 0] + image_bytes = io.BytesIO() + image_bytes.write(cv2.imencode('.png', rgba_image)[1].tobytes()) + + image_bytes.seek(0) + mask_url = f"{AIDA_CLOTHING}/{minio_client.put_object('aida-clothing', f'mask/mask_{object_name}.png', image_bytes, len(image_bytes.getvalue()), content_type='image/png').object_name}" + return mask_url + except Exception as e: + logging.warning(f"upload_png_mask runtime exception : {e}") diff --git a/logging_env.py b/logging_env.py index d1ac9bc..d618e37 100644 --- a/logging_env.py +++ b/logging_env.py @@ -9,7 +9,7 @@ LOGGER_CONFIG_DICT = { "handlers": { "console": { "class": "logging.StreamHandler", - "level": "DEBUG", + "level": "INFO", "formatter": "simple", "stream": "ext://sys.stdout", },