相似匹配接口

This commit is contained in:
zhouchengrong
2024-03-27 18:08:59 +08:00
parent 13416786c9
commit 455b56ca2d
5 changed files with 56 additions and 25 deletions

View File

@@ -3,9 +3,11 @@ from fastapi import APIRouter
from app.api import api_test
from app.api import api_outfit_matcher
from app.api import api_attribute
from app.api import api_similar_match
router = APIRouter()
router.include_router(api_test.router, tags=["test"], prefix="/test")
router.include_router(api_outfit_matcher.router, tags=["outfit_matcher"], prefix="/api/outfit_matcher")
router.include_router(api_attribute.router, tags=["attribute"], prefix="/api/attribute")
router.include_router(api_similar_match.router, tags=["similar_match"], prefix="/api/similar_match")

View File

@@ -1,16 +0,0 @@
import logging
import time
from fastapi import APIRouter
from app.schemas.outfit_matcher import SimilarMatchMItem
from app.service.utils.decorator import RunTime
logger = logging.getLogger()
router = APIRouter()
@RunTime
@router.post("similar_match")
def similar_match(request_item: SimilarMatchMItem):
pass

View File

@@ -0,0 +1,25 @@
import logging
import time
from fastapi import APIRouter
from app.schemas.similar_match import SimilarMatchMItem
from app.service.similar_match.service import SimilarMatch
from app.service.utils.decorator import RunTime
logger = logging.getLogger()
router = APIRouter()
@RunTime
@router.post("similar_match")
def similar_match(request_item: SimilarMatchMItem):
try:
if request_item.result_number <= 0:
raise KeyError("result number can't be less than 0")
service = SimilarMatch(request_item)
search_response = service.match_features()
return {"message": "ok", "data": search_response}
except KeyError as e:
logger.warning(str(e))
return {"message": "result number can't be less than 0", "data": []}

View File

@@ -1696,3 +1696,18 @@ RuntimeError: shape '[6, 3, -1]' is invalid for input of size 896
2024-03-27 17:05:02,615 decorator.py [line:11] INFO function【get_result】,runtime【252.62576150894165】s
2024-03-27 17:05:02,616 api_outfit_matcher.py [line:43] INFO run time is : 252.62676739692688
2024-03-27 17:05:02,616 api_outfit_matcher.py [line:43] INFO run time is : 252.62676739692688
2024-03-27 18:01:01,985 milvus_client.py [line:641] DEBUG Created new connection using: 704a3f1ccb564a0ea5281250a8583db5
2024-03-27 18:01:01,985 decorators.py [line:146] ERROR RPC error: [search], <ParamError: (code=1, message=`limit` value 0 is illegal)>, <Time:{'RPC start': '2024-03-27 18:01:01.985695', 'RPC error': '2024-03-27 18:01:01.985695'}>
2024-03-27 18:01:01,985 decorators.py [line:146] ERROR RPC error: [search], <ParamError: (code=1, message=`limit` value 0 is illegal)>, <Time:{'RPC start': '2024-03-27 18:01:01.985695', 'RPC error': '2024-03-27 18:01:01.985695'}>
2024-03-27 18:01:01,985 decorators.py [line:146] ERROR RPC error: [search], <ParamError: (code=1, message=`limit` value 0 is illegal)>, <Time:{'RPC start': '2024-03-27 18:01:01.985695', 'RPC error': '2024-03-27 18:01:01.985695'}>
2024-03-27 18:01:01,987 milvus_client.py [line:318] ERROR Failed to search collection: mixi_outfit
2024-03-27 18:01:01,987 milvus_client.py [line:318] ERROR Failed to search collection: mixi_outfit
2024-03-27 18:01:01,987 milvus_client.py [line:318] ERROR Failed to search collection: mixi_outfit
2024-03-27 18:01:08,508 milvus_client.py [line:641] DEBUG Created new connection using: ca5adf3ff3e34da0ae3f93c1669501ba
2024-03-27 18:01:08,708 decorator.py [line:11] INFO function【match_features】,runtime【0.21055293083190918】s
2024-03-27 18:01:08,708 decorator.py [line:11] INFO function【match_features】,runtime【0.21055293083190918】s
2024-03-27 18:03:26,541 milvus_client.py [line:641] DEBUG Created new connection using: 624da64059d34937a6ee6bc1ada3cc2d
2024-03-27 18:03:26,747 decorator.py [line:11] INFO function【match_features】,runtime【0.22229576110839844】s
2024-03-27 18:03:26,747 decorator.py [line:11] INFO function【match_features】,runtime【0.22229576110839844】s
2024-03-27 18:08:31,893 api_similar_match.py [line:24] WARNING "result number can't be less than 0"
2024-03-27 18:08:31,893 api_similar_match.py [line:24] WARNING "result number can't be less than 0"

View File

@@ -10,17 +10,21 @@ from pymilvus import MilvusClient
from app.core.config import *
from torchvision import transforms
from app.schemas.similar_match import SimilarMatchMItem
from app.service.utils.decorator import RunTime
class SimilarMatch:
def __init__(self):
def __init__(self, request_data):
self.minio_client = Minio(
f"{MINIO_IP}:{MINIO_PORT}",
access_key=MINIO_ACCESS,
secret_key=MINIO_SECRET,
secure=MINIO_SECURE)
self.triton_client = httpclient.InferenceServerClient(url=f"{OM_TRITON_IP}:{OM_TRITON_PORT}")
self.image_path = request_data.image_path
self.result_number = request_data.result_number
self.features = self.get_features()
@staticmethod
def resize_image(img):
@@ -57,8 +61,8 @@ class SimilarMatch:
mask = np.zeros((1, 1), dtype=np.float32)
return image, category, mask
def get_features(self, img_path):
image, category, mask = self.preprocess(img_path)
def get_features(self):
image, category, mask = self.preprocess(self.image_path)
# 输入集
inputs = [
httpclient.InferInput("input__0", image.shape, datatype="FP32"),
@@ -80,7 +84,7 @@ class SimilarMatch:
return features
@RunTime
def match_features(self, features):
def match_features(self):
# 连接milvus
# 连接milvus
client = MilvusClient(uri="http://10.1.1.240:19530", db_name="mixi")
@@ -88,8 +92,8 @@ class SimilarMatch:
search_response = client.search(
collection_name="mixi_outfit", # Replace with the actual name of your collection
# Replace with your query vector
data=[features[0]],
limit=5, # Max. number of search results to return
data=[self.features[0]],
limit=self.result_number, # Max. number of search results to return
output_fields=["id", "image_path"], # Search parameters
)
return search_response
@@ -98,8 +102,9 @@ class SimilarMatch:
if __name__ == '__main__':
service = SimilarMatch()
features = service.get_features(img_path="test/2024 SS/MKTS27000.jpg")
search_response = service.match_features(features)
request_data = SimilarMatchMItem(image_path="test/top/test_top1.jpg", result_number=1)
service = SimilarMatch(request_data)
search_response = service.match_features()
print(json.dumps(search_response, indent=4))