fix: cross-domain issue (#110)

* fix:解决前后端调用时的跨域问题

* Update requirements.txt

---------

Co-authored-by: Ze-Yi LIN <58305964+Zeyi-Lin@users.noreply.github.com>
This commit is contained in:
Soulerror
2024-09-12 11:53:12 +08:00
committed by GitHub
parent 9d3a678342
commit d4210909e8
2 changed files with 50 additions and 41 deletions
+48 -40
View File
@@ -15,10 +15,20 @@ from hivision.utils import (
import base64
import numpy as np
import cv2
from starlette.middleware.cors import CORSMiddleware
app = FastAPI()
creator = IDCreator()
# 添加 CORS 中间件 解决跨域问题
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # 允许的请求来源
allow_credentials=True, # 允许携带 Cookie
allow_methods=["*"], # 允许的请求方法,例如:GET, POST 等,也可以指定 ["GET", "POST"]
allow_headers=["*"], # 允许的请求头,也可以指定具体的头部
)
# 将图像转换为Base64编码
def numpy_2_base64(img: np.ndarray):
@@ -31,18 +41,17 @@ def numpy_2_base64(img: np.ndarray):
# 证件照智能制作接口
@app.post("/idphoto")
async def idphoto_inference(
input_image: UploadFile,
height: int = Form(413),
width: int = Form(295),
human_matting_model: str = Form("hivision_modnet"),
face_detect_model: str = Form("mtcnn"),
hd: bool = Form(True),
head_measure_ratio: float = 0.2,
head_height_ratio: float = 0.45,
top_distance_max: float = 0.12,
top_distance_min: float = 0.10,
input_image: UploadFile,
height: int = Form(413),
width: int = Form(295),
human_matting_model: str = Form("hivision_modnet"),
face_detect_model: str = Form("mtcnn"),
hd: bool = Form(True),
head_measure_ratio: float = 0.2,
head_height_ratio: float = 0.45,
top_distance_max: float = 0.12,
top_distance_min: float = 0.10,
):
image_bytes = await input_image.read()
nparr = np.frombuffer(image_bytes, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
@@ -79,8 +88,8 @@ async def idphoto_inference(
# 人像抠图接口
@app.post("/human_matting")
async def human_matting_inference(
input_image: UploadFile,
human_matting_model: str = Form("hivision_modnet"),
input_image: UploadFile,
human_matting_model: str = Form("hivision_modnet"),
):
image_bytes = await input_image.read()
nparr = np.frombuffer(image_bytes, np.uint8)
@@ -108,10 +117,10 @@ async def human_matting_inference(
# 透明图像添加纯色背景接口
@app.post("/add_background")
async def photo_add_background(
input_image: UploadFile,
color: str = Form("000000"),
kb: int = Form(50),
render: int = Form(0),
input_image: UploadFile,
color: str = Form("000000"),
kb: int = Form(50),
render: int = Form(0),
):
render_choice = ["pure_color", "updown_gradient", "center_gradient"]
@@ -153,10 +162,10 @@ async def photo_add_background(
# 六寸排版照生成接口
@app.post("/generate_layout_photos")
async def generate_layout_photos(
input_image: UploadFile,
height: int = Form(413),
width: int = Form(295),
kb: int = Form(50),
input_image: UploadFile,
height: int = Form(413),
width: int = Form(295),
kb: int = Form(50),
):
# try:
image_bytes = await input_image.read()
@@ -197,13 +206,13 @@ async def generate_layout_photos(
# 透明图像添加纯色背景接口
@app.post("/watermark")
async def watermark(
input_image: UploadFile,
text: str = Form("Hello"),
size: int = 20,
opacity: float = 0.5,
angle: int = 30,
color: str = "#000000",
space: int = 25,
input_image: UploadFile,
text: str = Form("Hello"),
size: int = 20,
opacity: float = 0.5,
angle: int = 30,
color: str = "#000000",
space: int = 25,
):
image_bytes = await input_image.read()
nparr = np.frombuffer(image_bytes, np.uint8)
@@ -228,8 +237,8 @@ async def watermark(
# 设置照片KB值接口(RGB图)
@app.post("/set_kb")
async def set_kb(
input_image: UploadFile,
kb: int = Form(50),
input_image: UploadFile,
kb: int = Form(50),
):
image_bytes = await input_image.read()
nparr = np.frombuffer(image_bytes, np.uint8)
@@ -255,17 +264,16 @@ async def set_kb(
# 证件照智能裁剪接口
@app.post("/idphoto_crop")
async def idphoto_crop_inference(
input_image: UploadFile,
height: int = Form(413),
width: int = Form(295),
face_detect_model: str = Form("mtcnn"),
hd: bool = Form(True),
head_measure_ratio: float = 0.2,
head_height_ratio: float = 0.45,
top_distance_max: float = 0.12,
top_distance_min: float = 0.10,
input_image: UploadFile,
height: int = Form(413),
width: int = Form(295),
face_detect_model: str = Form("mtcnn"),
hd: bool = Form(True),
head_measure_ratio: float = 0.2,
head_height_ratio: float = 0.45,
top_distance_max: float = 0.12,
top_distance_min: float = 0.10,
):
image_bytes = await input_image.read()
nparr = np.frombuffer(image_bytes, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_UNCHANGED) # 读取图像(4通道)
+2 -1
View File
@@ -3,4 +3,5 @@ onnxruntime>=1.15.0
numpy<=1.26.4
requests
mtcnn-runtime
tqdm
tqdm
starlette