Repository navigation
Expand file tree
/
Copy pathmain.py
More file actions
377 lines (341 loc) · 11.5 KB
/
Copy pathmain.py
File metadata and controls
377 lines (341 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import os
from pathlib import Path
from fastapi import (
BackgroundTasks,
FastAPI,
File,
Form,
HTTPException,
Response,
UploadFile,
Request,
)
from starlette.background import BackgroundTask
from fastapi.responses import FileResponse
from fastapi.middleware.cors import CORSMiddleware
from loguru import logger
import asyncio
import tempfile
import gen
import utils
OUTPUT_DIR = Path(__file__).parent / "output"
INPUT_DIR = Path(__file__).parent / "input"
if not OUTPUT_DIR.exists():
OUTPUT_DIR.mkdir(parents=True, exist_ok=True)
if not INPUT_DIR.exists():
INPUT_DIR.mkdir(parents=True, exist_ok=True)
app = FastAPI(title="Cut Avatar API")
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.middleware("http")
async def auth_middleware(request: Request, call_next):
auth_header = request.headers.get("Authorization")
api_key = os.getenv("API_KEY", "")
if api_key:
if not auth_header or auth_header != f"Bearer {api_key}":
return Response(
"Unauthorized",
status_code=401,
headers={"WWW-Authenticate": "Bearer"},
)
response = await call_next(request)
return response
@app.get("/result/{id}")
async def download_result(id: str):
result_file = OUTPUT_DIR / f"{id}.png"
if not result_file.exists():
raise HTTPException(status_code=404, detail="文件未找到")
return FileResponse(
result_file,
filename=f"{id}.png",
background=BackgroundTasks(
[BackgroundTask(utils.cleanup_temp_file, [result_file])]
),
)
@app.post("/cutone", description="从单个上传图像中生成指定对象的第一个正方形图片")
async def cut_image(
type: gen.GenSquareType = Form(
gen.GenSquareType.HEAD,
description="要生成的对象类型",
),
file: UploadFile = File(
...,
description="上传的图片文件",
),
size: int = Form(640, description="目标正方形边长 (像素)", ge=32),
padding: float = Form(
0.3,
description="对象周围的扩展比例 (0.3表示在对象基础上向外扩展30%)",
ge=0.0,
le=1.0,
),
):
if not file.content_type or not file.content_type.startswith("image/"):
raise HTTPException(status_code=400, detail="文件类型不支持")
img_path = Path(tempfile.mktemp(suffix=".png", dir=INPUT_DIR))
try:
content = await file.read()
if not content:
raise HTTPException(status_code=400, detail="文件内容为空")
if not img_path.parent.exists():
img_path.parent.mkdir(parents=True, exist_ok=True)
with open(img_path, "wb") as f:
f.write(content)
avatars = await asyncio.to_thread(
gen.square,
type=gen.GenSquareType(type),
img_path=img_path,
output_dir=OUTPUT_DIR,
target_size=size,
padding_ratio=padding,
)
if not avatars:
raise HTTPException(status_code=500, detail="未检测到对象")
avatar = avatars[0]
return FileResponse(
avatar,
filename=f"{type}.png",
background=BackgroundTasks(
[BackgroundTask(utils.cleanup_temp_file, [img_path, *avatars])],
),
)
except Exception as e:
logger.error(f"处理图片时发生错误: {str(e)}")
raise HTTPException(status_code=500, detail=f"图片处理错误: {str(e)}")
finally:
if img_path.exists():
img_path.unlink(missing_ok=True)
@app.post("/cutall", description="从单个上传图像中生成指定对象的所有正方形图片")
async def cut_all_images(
req: Request,
type: gen.GenSquareType = Form(
gen.GenSquareType.HEAD, description="要生成的对象类型"
),
file: UploadFile = File(
...,
description="上传的图片文件",
),
size: int = Form(640, description="目标正方形边长 (像素)", ge=32),
padding: float = Form(
0.3,
description="对象周围的扩展比例 (0.3表示在对象基础上向外扩展30%)",
ge=0.0,
le=1.0,
),
):
if not file.content_type or not file.content_type.startswith("image/"):
raise HTTPException(status_code=400, detail="文件类型不支持")
img_path = Path(tempfile.mktemp(suffix=".png", dir=INPUT_DIR))
try:
content = await file.read()
if not content:
raise HTTPException(status_code=400, detail="文件内容为空")
if not img_path.parent.exists():
img_path.parent.mkdir(parents=True, exist_ok=True)
with open(img_path, "wb") as f:
f.write(content)
avatars = await asyncio.to_thread(
gen.square,
type=gen.GenSquareType(type),
img_path=img_path,
output_dir=OUTPUT_DIR,
target_size=size,
padding_ratio=padding,
)
if not avatars:
raise HTTPException(status_code=500, detail="未检测到对象")
return {
"message": "生成成功",
"count": len(avatars),
"urls": [
f"{str(req.base_url).removesuffix('/')}/result/{avatar.stem}"
for avatar in avatars
],
}
except Exception as e:
logger.error(f"处理图片时发生错误: {str(e)}")
raise HTTPException(status_code=500, detail=f"图片处理错误: {str(e)}")
finally:
if img_path.exists():
img_path.unlink(missing_ok=True)
@app.post("/mask", description="标记识别到的对象区域")
async def mask_image(
file: UploadFile = File(
...,
description="上传的图片文件",
),
type: gen.GenSquareType = Form(
gen.GenSquareType.HEAD,
description="要标记的对象类型",
),
padding: float = Form(
0.2,
description="对象周围的扩展比例 (0.3表示在对象基础上向外扩展30%)",
ge=0.0,
le=1.0,
),
color: str = Form(
"red",
description="标记颜色",
),
width: int = Form(
8,
description="标记线条宽度 (像素)",
ge=1,
le=32,
),
):
if not file.content_type or not file.content_type.startswith("image/"):
raise HTTPException(status_code=400, detail="文件类型不支持")
try:
content = await file.read()
if not content:
raise HTTPException(status_code=400, detail="文件内容为空")
img_path = Path(tempfile.mktemp(suffix=".png", dir=INPUT_DIR))
with open(img_path, "wb") as f:
f.write(content)
result = await asyncio.to_thread(
gen.mask,
type=gen.GenSquareType(type),
img_path=img_path,
output_dir=OUTPUT_DIR,
padding_ratio=padding,
color=color,
width=width,
)
if not result:
raise HTTPException(status_code=500, detail="标记生成失败")
return FileResponse(
result,
filename=f"{type}_mask.png",
background=BackgroundTasks(
[BackgroundTask(utils.cleanup_temp_file, [img_path, result])],
),
)
except Exception as e:
logger.error(f"处理图片时发生错误: {str(e)}")
raise HTTPException(status_code=500, detail=f"图片处理错误: {str(e)}")
@app.post("/highlight", description="高亮识别到的对象区域")
async def highlight_image(
file: UploadFile = File(
...,
description="上传的图片文件",
),
type: gen.GenSquareType = Form(
gen.GenSquareType.HEAD,
description="要标记的对象类型",
),
padding: float = Form(
0.2,
description="对象周围的扩展比例 (0.3表示在对象基础上向外扩展30%)",
ge=0.0,
le=1.0,
),
with_mask: bool = Form(
False,
description="是否同时生成遮罩",
),
blur_radius: float = Form(
15.0,
description="高亮模糊半径 (像素)",
ge=0.0,
le=100.0,
),
mask_color: str = Form(
"red",
description="标记颜色",
),
mask_width: int = Form(
8,
description="标记线条宽度 (像素)",
ge=1,
le=32,
),
):
if not file.content_type or not file.content_type.startswith("image/"):
raise HTTPException(status_code=400, detail="文件类型不支持")
try:
content = await file.read()
if not content:
raise HTTPException(status_code=400, detail="文件内容为空")
img_path = Path(tempfile.mktemp(suffix=".png", dir=INPUT_DIR))
with open(img_path, "wb") as f:
f.write(content)
result = await asyncio.to_thread(
gen.highlight,
type=gen.GenSquareType(type),
img_path=img_path,
output_dir=OUTPUT_DIR,
padding_ratio=padding,
blur_radius=blur_radius,
with_mask=with_mask,
mask_color=mask_color,
mask_width=mask_width,
)
if not result:
raise HTTPException(status_code=500, detail="高亮生成失败")
return FileResponse(
result,
filename=f"{type}_highlight.png",
background=BackgroundTasks(
[BackgroundTask(utils.cleanup_temp_file, [img_path, result])],
),
)
except Exception as e:
logger.error(f"处理图片时发生错误: {str(e)}")
raise HTTPException(status_code=500, detail=f"图片处理错误: {str(e)}")
# for compatibility
@app.post("/cut/avatar")
async def cut_avatar(
file: UploadFile = File(
...,
description="上传的图片文件",
),
size: int = Form(640, description="目标正方形边长 (像素)", ge=32),
padding: float = Form(
0.3,
description="头像周围的扩展比例 (0.3表示在头部基础上向外扩展30%)",
ge=0.0,
le=1.0,
),
):
if not file.content_type or not file.content_type.startswith("image/"):
raise HTTPException(status_code=400, detail="文件类型不支持")
try:
content = await file.read()
if not content:
raise HTTPException(status_code=400, detail="文件内容为空")
img_path = Path(tempfile.mktemp(suffix=".png", dir=INPUT_DIR))
with open(img_path, "wb") as f:
f.write(content)
avatars = await asyncio.to_thread(
gen.square,
type=gen.GenSquareType.HEAD,
img_path=img_path,
output_dir=OUTPUT_DIR,
target_size=size,
padding_ratio=padding,
)
if not avatars:
raise HTTPException(status_code=500, detail="头像生成失败")
avatar = avatars[0]
return FileResponse(
avatar,
filename="avatar.png",
background=BackgroundTasks(
[BackgroundTask(utils.cleanup_temp_file, [img_path, *avatars])],
),
)
except Exception as e:
logger.error(f"处理图片时发生错误: {str(e)}")
raise HTTPException(status_code=500, detail=f"图片处理错误: {str(e)}")
if __name__ == "__main__":
import uvicorn
host = os.getenv("HOST", "0.0.0.0")
port = int(os.getenv("PORT", 39728))
uvicorn.run(app, host=host, port=port)