Files
AiDA_Python/app/api/api_design.py

455 lines
18 KiB
Python
Raw Normal View History

2024-06-13 14:31:14 +08:00
import json
2024-05-28 15:22:11 +08:00
import logging
2024-09-12 10:06:25 +08:00
import os
2024-05-28 15:22:11 +08:00
from fastapi import APIRouter, HTTPException, UploadFile, File, Form, BackgroundTasks
2024-05-28 15:22:11 +08:00
2024-09-12 10:06:25 +08:00
from app.schemas.design import DesignModel, DesignProgressModel, ModelProgressModel, DBGConfigModel
2024-06-13 14:31:14 +08:00
from app.schemas.response_template import ResponseModel
2024-09-26 14:19:32 +08:00
from app.service.design.model_process_service import model_transpose
2024-09-26 06:09:05 +00:00
from app.service.design_batch.service import start_design_batch_generate
from app.service.design_fast.design_generate import design_generate, design_generate_v2
2024-09-25 11:40:11 +08:00
from app.service.design_fast.utils.redis_utils import Redis
2024-05-28 15:22:11 +08:00
router = APIRouter()
logger = logging.getLogger()
@router.post("/design")
def design(request_data: DesignModel, background_tasks: BackgroundTasks):
"""
创建一个具有以下参数的请求体:
示例参数
{
"objects": [
{
"basic": {
"body_point_test": {
"waistband_right": [
2024-08-28 11:47:00 +08:00
200,
241
],
"hand_point_right": [
2024-08-28 11:47:00 +08:00
223,
297
],
"waistband_left": [
2024-08-28 11:47:00 +08:00
112,
241
],
"hand_point_left": [
2024-08-28 11:47:00 +08:00
92,
305
],
"shoulder_left": [
2024-08-28 11:47:00 +08:00
99,
116
],
"shoulder_right": [
2024-08-28 11:47:00 +08:00
215,
116
]
},
"layer_order": true,
"scale_bag": 0.7,
"scale_earrings": 0.16,
"self_template": true,
"single_overall": "overall",
"switch_category": ""
},
"items": [
{
2024-08-28 11:47:00 +08:00
"businessId": 270372,
"color": "30 28 28",
"image_id": 69780,
"offset": [
0,
0
],
2024-08-28 11:47:00 +08:00
"path": "aida-sys-image/images/female/trousers/0825000630.jpg",
"print": {
2024-08-28 11:47:00 +08:00
"element": {
"element_angle_list": [],
"element_path_list": [],
"element_scale_list": [],
"location": []
},
"overall": {
"location": [],
"print_angle_list": [],
"print_path_list": [],
"print_scale_list": []
},
"single": {
2024-08-28 11:47:00 +08:00
"location": [],
"print_angle_list": [],
"print_path_list": [],
"print_scale_list": []
}
},
"priority": 10,
"resize_scale": [
1.0,
1.0
],
"type": "Trousers"
},
{
"businessId": 270373,
"color": "30 28 28",
"image_id": 98243,
"offset": [
0,
0
],
"path": "aida-sys-image/images/female/blouse/0902003811.jpg",
"print": {
"element": {
"element_angle_list": [],
"element_path_list": [],
"element_scale_list": [],
"location": []
},
"overall": {
2024-08-28 11:47:00 +08:00
"location": [],
"print_angle_list": [],
"print_path_list": [],
"print_scale_list": []
},
2024-08-28 11:47:00 +08:00
"single": {
"location": [],
"print_angle_list": [],
"print_path_list": [],
"print_scale_list": []
}
},
"priority": 11,
"resize_scale": [
1.0,
1.0
],
"type": "Blouse"
},
{
"businessId": 270374,
"color": "172 68 68",
"image_id": 98244,
"offset": [
0,
0
],
"path": "aida-sys-image/images/female/outwear/0825000410.jpg",
"print": {
"element": {
2024-08-28 11:47:00 +08:00
"element_angle_list": [],
"element_path_list": [],
"element_scale_list": [],
"location": []
},
"overall": {
"location": [],
"print_angle_list": [],
"print_path_list": [],
"print_scale_list": []
},
"single": {
"location": [],
"print_angle_list": [],
"print_path_list": [],
"print_scale_list": []
}
},
2024-08-28 11:47:00 +08:00
"priority": 12,
"resize_scale": [
1.0,
1.0
],
2024-11-19 10:20:25 +08:00
"transparent":{
"mask_url":"test/transparent_test/transparent_mask.png",
"scale":0.1
},
2024-08-28 11:47:00 +08:00
"type": "Outwear"
},
{
2024-08-28 11:47:00 +08:00
"body_path": "aida-sys-image/models/female/5bdfe7ca-64eb-44e4-b03d-8e517520c795.png",
"image_id": 96090,
"type": "Body"
}
]
}
],
2024-08-28 11:47:00 +08:00
"process_id": "83"
}
"""
2024-09-19 14:20:56 +08:00
# logger.info(f"design request item is : @@@@@@:{json.dumps(request_data.dict())}")
# data = generate(request_data=request_data)
# logger.info(f"design response @@@@@@:{json.dumps(data)}")
2024-09-19 15:10:50 +08:00
#
2024-09-19 14:20:56 +08:00
2024-09-25 11:13:25 +08:00
try:
logger.info(f"design request item is : @@@@@@:{json.dumps(request_data.dict())}")
data = design_generate(request_data=request_data)
logger.info(f"design response @@@@@@:{json.dumps(data)}")
except Exception as e:
logger.warning(f"design Run Exception @@@@@@:{e}")
raise HTTPException(status_code=404, detail=str(e))
2024-06-13 14:31:14 +08:00
return ResponseModel(data=data)
2024-06-18 10:50:15 +08:00
@router.post("/design_v2")
async def design_v2(request_data: DesignModel, background_tasks: BackgroundTasks):
"""
创建一个具有以下参数的请求体:
示例参数
{
"objects": [
{
"basic": {
"body_point_test": {
"waistband_right": [
200,
241
],
"hand_point_right": [
223,
297
],
"waistband_left": [
112,
241
],
"hand_point_left": [
92,
305
],
"shoulder_left": [
99,
116
],
"shoulder_right": [
215,
116
]
},
"layer_order": true,
"scale_bag": 0.7,
"scale_earrings": 0.16,
"self_template": true,
"single_overall": "overall",
"switch_category": ""
},
"items": [
{
"businessId": 270372,
"color": "30 28 28",
"image_id": 69780,
"offset": [
0,
0
],
"path": "aida-sys-image/images/female/trousers/0825000630.jpg",
"print": {
"element": {
"element_angle_list": [],
"element_path_list": [],
"element_scale_list": [],
"location": []
},
"overall": {
"location": [],
"print_angle_list": [],
"print_path_list": [],
"print_scale_list": []
},
"single": {
"location": [],
"print_angle_list": [],
"print_path_list": [],
"print_scale_list": []
}
},
"priority": 10,
"resize_scale": [
1.0,
1.0
],
"type": "Trousers"
},
{
"businessId": 270373,
"color": "30 28 28",
"image_id": 98243,
"offset": [
0,
0
],
"path": "aida-sys-image/images/female/blouse/0902003811.jpg",
"print": {
"element": {
"element_angle_list": [],
"element_path_list": [],
"element_scale_list": [],
"location": []
},
"overall": {
"location": [],
"print_angle_list": [],
"print_path_list": [],
"print_scale_list": []
},
"single": {
"location": [],
"print_angle_list": [],
"print_path_list": [],
"print_scale_list": []
}
},
"priority": 11,
"resize_scale": [
1.0,
1.0
],
"type": "Blouse"
},
{
"businessId": 270374,
"color": "172 68 68",
"image_id": 98244,
"offset": [
0,
0
],
"path": "aida-sys-image/images/female/outwear/0825000410.jpg",
"print": {
"element": {
"element_angle_list": [],
"element_path_list": [],
"element_scale_list": [],
"location": []
},
"overall": {
"location": [],
"print_angle_list": [],
"print_path_list": [],
"print_scale_list": []
},
"single": {
"location": [],
"print_angle_list": [],
"print_path_list": [],
"print_scale_list": []
}
},
"priority": 12,
"resize_scale": [
1.0,
1.0
],
"transparent":{
"mask_url":"test/transparent_test/transparent_mask.png",
"scale":0.1
},
"type": "Outwear"
},
{
"body_path": "aida-sys-image/models/female/5bdfe7ca-64eb-44e4-b03d-8e517520c795.png",
"image_id": 96090,
"type": "Body"
}
]
}
],
"process_id": "83"
}
"""
try:
# 异步
logger.info(f"generate_image request item is : @@@@@@:{json.dumps(request_data.dict())}")
background_tasks.add_task(design_generate_v2, request_data)
except Exception as e:
logger.warning(f"design Run Exception @@@@@@:{e}")
raise HTTPException(status_code=404, detail=str(e))
return ResponseModel()
2024-06-18 10:50:15 +08:00
@router.post('/get_progress')
def get_progress(request_data: DesignProgressModel):
"""
获取design 进度
创建一个具有以下参数的请求体:
- **process_id**: 进度id
示例参数
{
"process_id": "6878547032381675"
}
"""
2024-06-18 10:50:15 +08:00
try:
logger.info(f"get_progress request item is : @@@@@@:{json.dumps(request_data.dict())}")
2024-06-18 10:50:15 +08:00
process_id = request_data.process_id
r = Redis()
data = r.read(key=process_id)
2024-06-19 10:53:11 +08:00
if data is None:
raise ValueError(f"No progress ID: {process_id}")
logging.info(f"get_progress process_id @@@@@@ : {process_id} , progress : {json.dumps(data)}")
2024-06-18 10:50:15 +08:00
except Exception as e:
2024-06-19 10:53:11 +08:00
logger.warning(f"get_progress Run Exception @@@@@@:{e}")
2024-06-18 10:50:15 +08:00
raise HTTPException(status_code=404, detail=str(e))
return ResponseModel(data=data)
2024-06-24 16:35:34 +08:00
@router.post('/model_process')
def model_process(request_data: ModelProgressModel):
"""
获取模特图片预处理
创建一个具有以下参数的请求体:
- **model_path**: 模特图片的minio或s3 url地址
示例参数
{
"model_path": "aida-users/10/models/female/9c788f5b-b8c7-424c-b149-025aeb0bda51model.jpg"
}
"""
2024-06-24 16:35:34 +08:00
try:
logger.info(f"model_process request item is : @@@@@@:{json.dumps(request_data.dict())}")
2024-06-24 16:35:34 +08:00
2024-09-26 14:19:32 +08:00
data = model_transpose(image_path=request_data.model_path)
logger.info(f"model_process response @@@@@@:{json.dumps(data)}")
2024-06-24 16:35:34 +08:00
except Exception as e:
logger.warning(f"model_process Run Exception @@@@@@:{e}")
raise HTTPException(status_code=404, detail=str(e))
return ResponseModel(data=data)
2024-09-12 10:06:25 +08:00
# ##############################################################
@router.post("/design_batch_generate")
async def design(file: UploadFile = File(...),
tasks_id: str = Form(...),
user_id: str = Form(...),
2024-09-26 06:09:05 +00:00
file_name: str = Form(...),
2024-09-12 10:06:25 +08:00
total: int = Form(...)
):
dbg_config = DBGConfigModel(
tasks_id=tasks_id,
user_id=user_id,
2024-09-26 06:09:05 +00:00
file_name=file_name,
2024-09-12 10:06:25 +08:00
total=total
)
contents = await file.read()
file_name = file.filename
await save_request_file(contents, file_name)
return await start_design_batch_generate(dbg_config, contents)
async def save_request_file(contents, file_name):
# 创建保存文件的目录(如果不存在)
2024-12-11 11:16:41 +08:00
save_dir = os.path.join(os.getcwd(), "service/design_batch", "request_data")
2024-09-12 10:06:25 +08:00
if not os.path.exists(save_dir):
os.makedirs(save_dir)
# 处理文件
file_path = os.path.join(save_dir, file_name)
with open(file_path, "wb") as f:
f.write(contents)