代码结构化 优化

This commit is contained in:
zcr
2026-06-04 11:35:59 +08:00
parent cc2404831d
commit de349a6d20
6 changed files with 1952 additions and 2239 deletions

View File

@@ -11,7 +11,12 @@ from minio import Minio
from app.core.config import settings
from app.service.utils.decorator import RunTime
minio_client = Minio(settings.MINIO_URL, access_key=settings.MINIO_ACCESS, secret_key=settings.MINIO_SECRET, secure=settings.MINIO_SECURE)
minio_client = Minio(
settings.MINIO_URL,
access_key=settings.MINIO_ACCESS,
secret_key=settings.MINIO_SECRET,
secure=settings.MINIO_SECURE,
)
# 自定义 Retry 类
@@ -30,7 +35,7 @@ http_client = urllib3.PoolManager(
num_pools=10, # 设置连接池大小
maxsize=10,
timeout=timeout,
cert_reqs='CERT_REQUIRED', # 需要证书验证
cert_reqs="CERT_REQUIRED", # 需要证书验证
retries=CustomRetry(
total=5,
backoff_factor=0.2,
@@ -51,7 +56,7 @@ def oss_get_image(oss_client, bucket, object_name, data_type):
image_array = np.frombuffer(image_bytes, np.uint8) # 转成8位无符号整型
image_object = cv2.imdecode(image_array, cv2.IMREAD_UNCHANGED)
if image_object.dtype == np.uint16:
image_object = (image_object / 256).astype('uint8')
image_object = (image_object / 256).astype("uint8")
else:
data_bytes = BytesIO(image_data.read())
image_object = Image.open(data_bytes)
@@ -63,13 +68,19 @@ def oss_get_image(oss_client, bucket, object_name, data_type):
def oss_upload_image(oss_client, bucket, object_name, image_bytes):
req = None
try:
req = oss_client.put_object(bucket_name=bucket, object_name=object_name, data=io.BytesIO(image_bytes), length=len(image_bytes), content_type='image/png')
req = oss_client.put_object(
bucket_name=bucket,
object_name=object_name,
data=io.BytesIO(image_bytes),
length=len(image_bytes),
content_type="image/png",
)
except Exception as e:
logger.warning(f" | 上传图片出现异常 ######: {e}")
return req
if __name__ == '__main__':
if __name__ == "__main__":
# url = "aida-results/result_0002186a-e631-11ee-86a6-b48351119060.png"
# url = "aida-collection-element/11523/Moodboard/f60af0d2-94c2-48f9-90ff-74b8e8a481b5.jpg"
# url = "aida-sys-image/images/female/outwear/0628000054.jpg"
@@ -81,16 +92,26 @@ if __name__ == '__main__':
# url = "aida-users/89/sketchboard/female/Dress/e6724ab7-8d3f-4677-abe0-c3e42ab7af85.jpeg"
# url = "aida-users/87/print/956614a2-7e75-4fbe-9ed0-c1831e37a2c9-4-87.png"
# url = "aida-users/89/single_logo/123-89.png"
url = "aida-results/result_a7adcbd8-ef8d-11f0-8c92-0966ede33ab5.png"
url = "aida-collection-element/26293/Sketchboard/b503d482-3334-46e7-9dee-44e380fb4294.png"
# url = "aida-collection-element/12148/Sketchboard/95ea577b-305b-4a62-b30a-39c0dd3ddb3f.png"
read_type = "2"
if read_type == "cv2":
img = oss_get_image(oss_client=minio_client, bucket=url.split('/')[0], object_name=url[url.find('/') + 1:], data_type=read_type)
img = oss_get_image(
oss_client=minio_client,
bucket=url.split("/")[0],
object_name=url[url.find("/") + 1 :],
data_type=read_type,
)
cv2.imshow("", img)
cv2.waitKey(0)
else:
img = oss_get_image(oss_client=minio_client, bucket=url.split('/')[0], object_name=url[url.find('/') + 1:], data_type=read_type)
img = oss_get_image(
oss_client=minio_client,
bucket=url.split("/")[0],
object_name=url[url.find("/") + 1 :],
data_type=read_type,
)
draw = ImageDraw.Draw(img)
# 获取图片尺寸
width, height = img.size
@@ -103,7 +124,7 @@ if __name__ == '__main__':
draw.line(
[(center_x, 0), (center_x, height)], # 从顶部到底部的垂直线
fill=(255, 0, 0), # 红色 (R, G, B)
width=2 # 线宽
width=2, # 线宽
)
img.show()