feat
fix design 多进程处理
This commit is contained in:
@@ -1,5 +1,6 @@
|
|||||||
import concurrent.futures
|
import concurrent.futures
|
||||||
import io
|
import io
|
||||||
|
from concurrent.futures import ProcessPoolExecutor
|
||||||
|
|
||||||
import cv2
|
import cv2
|
||||||
|
|
||||||
@@ -62,7 +63,7 @@ def generate(request_data):
|
|||||||
obj = futures[future]
|
obj = futures[future]
|
||||||
return_response[obj] = future.result()[0]
|
return_response[obj] = future.result()[0]
|
||||||
return_png_mask.extend(future.result()[1])
|
return_png_mask.extend(future.result()[1])
|
||||||
# upload_results = process_images(return_png_mask)
|
upload_results = process_images(return_png_mask)
|
||||||
final_progress(process_id)
|
final_progress(process_id)
|
||||||
return return_response
|
return return_response
|
||||||
|
|
||||||
@@ -151,7 +152,7 @@ def process_object(cfg, process_id, total):
|
|||||||
|
|
||||||
@RunTime
|
@RunTime
|
||||||
def process_images(images):
|
def process_images(images):
|
||||||
with concurrent.futures.ThreadPoolExecutor() as executor:
|
with ProcessPoolExecutor() as executor:
|
||||||
results = list(executor.map(upload_images, images))
|
results = list(executor.map(upload_images, images))
|
||||||
# results = []
|
# results = []
|
||||||
# for image in images:
|
# for image in images:
|
||||||
|
|||||||
@@ -17,39 +17,39 @@ from app.service.utils.decorator import RunTime
|
|||||||
from app.service.utils.oss_client import oss_upload_image
|
from app.service.utils.oss_client import oss_upload_image
|
||||||
|
|
||||||
|
|
||||||
@RunTime
|
|
||||||
def upload_png_mask(front_image, object_name, mask=None):
|
|
||||||
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}"
|
|
||||||
# oss upload ####################
|
|
||||||
req = oss_upload_image(bucket=AIDA_CLOTHING, object_name=f"mask/mask_{object_name}.png", image_bytes=cv2.imencode('.png', rgba_image)[1])
|
|
||||||
mask_url = f"{AIDA_CLOTHING}/mask/mask_{object_name}.png"
|
|
||||||
|
|
||||||
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}"
|
|
||||||
req = oss_upload_image(bucket=AIDA_CLOTHING, object_name=f"image/image_{object_name}.png", image_bytes=image_bytes)
|
|
||||||
image_url = f"{AIDA_CLOTHING}/image/image_{object_name}.png"
|
|
||||||
return front_image, image_url, mask_url
|
|
||||||
except Exception as e:
|
|
||||||
logging.warning(f"upload_png_mask runtime exception : {e}")
|
|
||||||
|
|
||||||
|
|
||||||
# @RunTime
|
# @RunTime
|
||||||
# def upload_png_mask(front_image, object_name, mask=None):
|
# def upload_png_mask(front_image, object_name, mask=None):
|
||||||
# mask_url = None
|
# try:
|
||||||
# if mask is not None:
|
# mask_url = None
|
||||||
# mask_url = f"{AIDA_CLOTHING}/mask/mask_{object_name}.png"
|
# if mask is not None:
|
||||||
# image_url = f"{AIDA_CLOTHING}/image/image_{object_name}.png"
|
# mask_inverted = cv2.bitwise_not(mask)
|
||||||
# return front_image, image_url, mask_url
|
# # 将掩模的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}"
|
||||||
|
# # oss upload ####################
|
||||||
|
# req = oss_upload_image(bucket=AIDA_CLOTHING, object_name=f"mask/mask_{object_name}.png", image_bytes=cv2.imencode('.png', rgba_image)[1])
|
||||||
|
# mask_url = f"{AIDA_CLOTHING}/mask/mask_{object_name}.png"
|
||||||
|
#
|
||||||
|
# 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}"
|
||||||
|
# req = oss_upload_image(bucket=AIDA_CLOTHING, object_name=f"image/image_{object_name}.png", image_bytes=image_bytes)
|
||||||
|
# image_url = f"{AIDA_CLOTHING}/image/image_{object_name}.png"
|
||||||
|
# return front_image, image_url, mask_url
|
||||||
|
# except Exception as e:
|
||||||
|
# logging.warning(f"upload_png_mask runtime exception : {e}")
|
||||||
|
|
||||||
|
|
||||||
|
@RunTime
|
||||||
|
def upload_png_mask(front_image, object_name, mask=None):
|
||||||
|
mask_url = None
|
||||||
|
if mask is not None:
|
||||||
|
mask_url = f"{AIDA_CLOTHING}/mask/mask_{object_name}.png"
|
||||||
|
image_url = f"{AIDA_CLOTHING}/image/image_{object_name}.png"
|
||||||
|
return front_image, image_url, mask_url
|
||||||
|
|||||||
Reference in New Issue
Block a user