From 455b56ca2d41c0c55709295f7c88cb46dec86c59 Mon Sep 17 00:00:00 2001 From: zhouchengrong Date: Wed, 27 Mar 2024 18:08:59 +0800 Subject: [PATCH] =?UTF-8?q?=E7=9B=B8=E4=BC=BC=E5=8C=B9=E9=85=8D=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/api/api_route.py | 2 ++ app/api/api_simiar_match.py | 16 ---------------- app/api/api_similar_match.py | 25 +++++++++++++++++++++++++ app/logs/errors.log | 15 +++++++++++++++ app/service/similar_match/service.py | 23 ++++++++++++++--------- 5 files changed, 56 insertions(+), 25 deletions(-) delete mode 100644 app/api/api_simiar_match.py create mode 100644 app/api/api_similar_match.py diff --git a/app/api/api_route.py b/app/api/api_route.py index 298626f..983e725 100644 --- a/app/api/api_route.py +++ b/app/api/api_route.py @@ -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") diff --git a/app/api/api_simiar_match.py b/app/api/api_simiar_match.py deleted file mode 100644 index dc67b2a..0000000 --- a/app/api/api_simiar_match.py +++ /dev/null @@ -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 diff --git a/app/api/api_similar_match.py b/app/api/api_similar_match.py new file mode 100644 index 0000000..1a49e7b --- /dev/null +++ b/app/api/api_similar_match.py @@ -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": []} diff --git a/app/logs/errors.log b/app/logs/errors.log index 546eed1..9740bb1 100644 --- a/app/logs/errors.log +++ b/app/logs/errors.log @@ -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], , +2024-03-27 18:01:01,985 decorators.py [line:146] ERROR RPC error: [search], , +2024-03-27 18:01:01,985 decorators.py [line:146] ERROR RPC error: [search], , +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" diff --git a/app/service/similar_match/service.py b/app/service/similar_match/service.py index a071461..87d9440 100644 --- a/app/service/similar_match/service.py +++ b/app/service/similar_match/service.py @@ -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))