前言
技術(shù)棧是Fastapi。
FastAPI 是一個(gè)現(xiàn)代、快速(基于 Starlette 和 Pydantic)、易于使用的 Python web 框架,主要用于構(gòu)建 RESTful API。以下是 FastAPI 的一些優(yōu)勢(shì):
-
性能卓越: FastAPI 基于 Starlette 框架,并使用 Pydantic 進(jìn)行數(shù)據(jù)驗(yàn)證,因此具有出色的性能。它通過(guò)異步編程利用 Python 3.7+ 中的
async/await
特性,使其能夠處理大量并發(fā)請(qǐng)求。 -
自動(dòng)文檔生成: FastAPI 自動(dòng)生成交互式 API 文檔(Swagger UI 和 ReDoc),讓開(kāi)發(fā)者能夠輕松地查看和測(cè)試 API 端點(diǎn),同時(shí)提供即時(shí)的反饋和文檔。
-
強(qiáng)類(lèi)型注解: FastAPI 使用 Python 的類(lèi)型提示來(lái)定義 API,同時(shí)利用 Pydantic 模型進(jìn)行請(qǐng)求和響應(yīng)的驗(yàn)證,這提供了強(qiáng)大的靜態(tài)類(lèi)型檢查和自動(dòng)文檔的支持。
-
自動(dòng)驗(yàn)證: 使用 Pydantic 模型,F(xiàn)astAPI 自動(dòng)驗(yàn)證請(qǐng)求的數(shù)據(jù),并在數(shù)據(jù)不符合預(yù)期時(shí)返回錯(cuò)誤。這有助于提高代碼的穩(wěn)健性和可維護(hù)性。
-
異步支持: 支持異步處理請(qǐng)求,可以使用異步函數(shù)來(lái)處理請(qǐng)求,使得 FastAPI 在處理高并發(fā)時(shí)表現(xiàn)出色。
-
便捷的依賴注入系統(tǒng): FastAPI 提供了一個(gè)靈活的依賴注入系統(tǒng),讓你能夠方便地注入和管理依賴項(xiàng),使代碼更加清晰和可測(cè)試。
-
WebSocket 支持: FastAPI 提供了對(duì) WebSocket 的原生支持,能夠輕松地實(shí)現(xiàn)實(shí)時(shí)通信。
-
易于學(xué)習(xí): FastAPI 的語(yǔ)法和設(shè)計(jì)理念使其易于學(xué)習(xí)和使用,特別是對(duì)于熟悉 Python 的開(kāi)發(fā)者。
?
安裝modelscope
conda create -n modelscope python=3.8
conda activate modelscope
pip install modelscope
激活虛擬環(huán)境
conda activate modelscope
?
server.py代碼
import argparse
import uvicorn
from fastapi import FastAPI
from modelscope.server.api.routers.router import api_router
from modelscope.server.core.event_handlers import (start_app_handler,
stop_app_handler)
def get_app(args) -> FastAPI:
app = FastAPI(
title='modelscope_server',
version='0.1',
debug=True,
swagger_ui_parameters={'tryItOutEnabled': True})
app.state.args = args
app.include_router(api_router)
app.add_event_handler('startup', start_app_handler(app))
app.add_event_handler('shutdown', stop_app_handler(app))
return app
def add_server_args(parser):
parser.add_argument(
'--model_id', required=True, type=str, help='The target model id')
parser.add_argument(
'--revision', required=True, type=str, help='Model revision')
parser.add_argument('--host', default='0.0.0.0', help='Host to listen')
parser.add_argument('--port', type=int, default=8000, help='Server port')
parser.add_argument('--debug', default='debug', help='Set debug level.')
parser.add_argument(
'--llm_first',
type=bool,
default=True,
help='Use LLMPipeline first for llm models.')
if __name__ == '__main__':
parser = argparse.ArgumentParser('modelscope_server')
add_server_args(parser)
args = parser.parse_args()
app = get_app(args)
uvicorn.run(app, host=args.host, port=args.port)
?任務(wù)一:人臉檢測(cè)
命令行中虛擬環(huán)境中運(yùn)行腳本
python server.py --model_id damo/cv_resnet50_face-detection_retinaface --revision v2.0.2
?
訪問(wèn)http://127.0.0.1:8000/docs打開(kāi)文檔
?
- describe方法描述請(qǐng)求參數(shù)和輸出形式?
{
"schema": {
"task_name": "face-detection",
"schema": {
"input": {
"type": "object",
"properties": {
"image": {
"type": "string",
"description": "Base64 encoded image file or url string."
}
}
},
"parameters": {},
"output": {
"type": "object",
"properties": {
"scores": {
"type": "array",
"items": {
"type": "number"
}
},
"boxes": {
"type": "array",
"items": {
"type": "number"
}
},
"keypoints": {
"type": "array",
"items": {
"type": "number"
}
}
}
}
}
},
"sample": null
}
-
call方法(是模型推理的入口)
-
兩種請(qǐng)求方式(post)
-
curl方式(encode_base64表示圖片轉(zhuǎn)換為base64后的形式)
-
-
圖片轉(zhuǎn)換base64的鏈接可以使用在線轉(zhuǎn)https://tool.jisuapi.com/pic2base64.html
curl -X 'POST' \ 'http://127.0.0.1:8000/call' \ -H 'accept: application/json' \ -H 'Content-Type: application/json' \ -d '{ "input":{"image":"encode_base64"}'
-
face-detection_retinaface請(qǐng)求參數(shù)體(界面請(qǐng)求)直接使用fastapi界面請(qǐng)求或者使用apifox等
-
{"input":{"image":"encode_base64"}}
請(qǐng)求結(jié)果
?返回結(jié)果解釋
{
"scores": [
0.9998026490211487
],
"boxes": [
[
164.9207000732422,
82.86209106445312,
353.395263671875,
340.145263671875
]
],
"keypoints": [
[
214.5664520263672,
188.255859375,
303.5237121582031,
190.91671752929688,
256.9284362792969,
242.95065307617188,
223.42758178710938,
283.54241943359375,
287.28448486328125,
286.402587890625
]
]
}
返回圖像中人臉的分?jǐn)?shù),越大表示有人臉的可能性越大,boxes表示人臉的矩形框,左上角x,y坐標(biāo)和右下角x,y坐標(biāo),keypoints返回左眼、右眼、鼻尖、左嘴角、右嘴角的坐標(biāo)值x,y。?
輸入圖片
?根據(jù)boxes和keypoints畫(huà)圖
?
import cv2
# 讀取圖像
image = cv2.imread('0.png')
# 定義矩形框的坐標(biāo)和大小
x, y, x1, y1 = 164,82,353,340
w = 353-164
h = 340-82
# 畫(huà)矩形框
cv2.rectangle(image, (x, y), (x + w, y + h), (0, 255, 0), 2)
# 顯示結(jié)果
# cv2.imshow('Rectangle', image)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
cv2.imwrite('0_rectangle.png', image)
?
import cv2
# 讀取圖像
image = cv2.imread('0.png')
radius = 5 # 點(diǎn)的半徑
color = (0, 0, 255) # 點(diǎn)的顏色,通常使用BGR格式
thickness = -1 # 為了畫(huà)一個(gè)實(shí)心圓,線條寬度設(shè)置為-1
keypoints = [214.5664520263672,
188.255859375,
303.5237121582031,
190.91671752929688,
256.9284362792969,
242.95065307617188,
223.42758178710938,
283.54241943359375,
287.28448486328125,
286.402587890625]
print(len(keypoints))
for i in range(0,len(keypoints),2):
cv2.circle(image, (int(keypoints[i]),int(keypoints[i+1])), radius, color, thickness)
# 顯示結(jié)果
# cv2.imshow('Point', image)
# cv2.waitKey(0)
# cv2.destroyAllWindows()
cv2.imwrite('0_keypoints.png', image)
?
?需要注意的是不同的任務(wù)請(qǐng)求體的內(nèi)容不一樣,需要明確每個(gè)任務(wù)的請(qǐng)求參數(shù)具體有哪些。
?任務(wù)二:人臉融合
?第二種任務(wù),人臉融合,需要重啟服務(wù),將model_id和revision替換。
?
python server.py --model_id damo/cv_unet-image-face-fusion_damo --revision v1.3
此時(shí)訪問(wèn)http://127.0.0.1/docs?
執(zhí)行一下describe方法?
?
{
"schema": {
"task_name": "image-face-fusion",
"schema": {
"input": {
"type": "object",
"properties": {
"template": {
"type": "string",
"description": "Base64 encoded image file or url string."
},
"user": {
"type": "string",
"description": "Base64 encoded image file or url string."
}
}
},
"parameters": {
"type": "object",
"properties": {
"user": {
"type": "object",
"default": null
}
}
},
"output": {
"type": "object",
"properties": {
"output_img": {
"type": "string",
"description": "The base64 encoded image."
}
}
}
}
},
"sample": null
}
input有兩個(gè)參數(shù),第一個(gè)是template,表示模版;第二個(gè)參數(shù)是user,表示用戶的圖片,最終的目的就是將用戶的圖片的臉替換到模版上
parameters參數(shù)一般使用默認(rèn)的就行,不填,如果有特殊需求可自行嘗試
output會(huì)返回一個(gè)換好臉圖像的base64編碼
請(qǐng)求體
{
"input": {
"template": "base64_template",
"user":"bas64_user"
}
}
?template
?User
?
?Ourput
參考鏈接:文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-780266.html
https://github.com/modelscope/modelscope/blob/master/modelscope/server/api_server.py?文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-780266.html
到了這里,關(guān)于【ModelScope】部署一個(gè)屬于自己的AI服務(wù)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!