From 88c815dd9dc6c82daa989bdc24fec3cf565cecf7 Mon Sep 17 00:00:00 2001 From: zchen Date: Tue, 26 Mar 2024 18:00:35 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=B3=E4=BA=8E=E4=BF=9D=E5=AD=98=E7=89=B9?= =?UTF-8?q?=E5=BE=81=E7=9A=84=E4=B8=80=E4=BA=9B=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 +- app/api/api_outfit_matcher.py | 4 +- app/api/api_simiar_match.py | 16 ++ app/core/config.py | 15 +- app/logs/errors.log | 140 ++++++++++++++++++ app/schemas/similar_match.py | 5 + .../outfit_matcher/outfit_evaluator.py | 7 +- app/service/outfit_matcher/service.py | 9 +- app/service/similar_match/__init__.py | 0 app/service/similar_match/service.py | 102 +++++++++++++ requirements.txt | Bin 694 -> 1106 bytes .../1.create databases.py | 5 + .../2.create collection.py | 23 +++ test/outfit_matcher_milvus/3.create index.py | 23 +++ .../4.load clollection.py | 10 ++ test/outfit_matcher_milvus/5.similar match.py | 27 ++++ 16 files changed, 378 insertions(+), 10 deletions(-) create mode 100644 app/api/api_simiar_match.py create mode 100644 app/schemas/similar_match.py create mode 100644 app/service/similar_match/__init__.py create mode 100644 app/service/similar_match/service.py create mode 100644 test/outfit_matcher_milvus/1.create databases.py create mode 100644 test/outfit_matcher_milvus/2.create collection.py create mode 100644 test/outfit_matcher_milvus/3.create index.py create mode 100644 test/outfit_matcher_milvus/4.load clollection.py create mode 100644 test/outfit_matcher_milvus/5.similar match.py diff --git a/.gitignore b/.gitignore index e2493c6..12821b0 100644 --- a/.gitignore +++ b/.gitignore @@ -119,7 +119,7 @@ dmypy.json .test #runtime produce -test +#test logs seg_result/ seg_result diff --git a/app/api/api_outfit_matcher.py b/app/api/api_outfit_matcher.py index be507cb..ce9cbf9 100644 --- a/app/api/api_outfit_matcher.py +++ b/app/api/api_outfit_matcher.py @@ -27,7 +27,9 @@ def outfit_matcher(request_item: OutfitMatcher): start_time = time.time() for item in request_item['query']: outfits = fashion_dataset.generate_outfit(item, request_item["topk"], request_item["max_outfits"]) - scores = service.get_result(outfits) + scores, features = service.get_result(outfits) + # save features in databases + if request_item['is_best']: best_outfits, best_scores = service.visualize(outfits, scores, request_item["topk"], best=True, # output_path=os.path.join(r"E:\workspace\outfit_matcher\2024 SS Outfit", f"{item['item_name']}_best_{param['topk']}.png") diff --git a/app/api/api_simiar_match.py b/app/api/api_simiar_match.py new file mode 100644 index 0000000..dc67b2a --- /dev/null +++ b/app/api/api_simiar_match.py @@ -0,0 +1,16 @@ +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/core/config.py b/app/core/config.py index 430e997..5b1ef5a 100644 --- a/app/core/config.py +++ b/app/core/config.py @@ -27,18 +27,23 @@ MINIO_SECURE = False MINIO_ACCESS = "e8zc55mzDOh4IzRrZ9Oa" MINIO_SECRET = "uHfqJ7UkwA1PTDGfnA44Hp9ux5YkZTkzZLjeOYhE" -OM_TRITON_IP = "10.1.1.240" -OM_TRITON_PORT = "10010" +OM_TRITON_IP = "10.1.1.150" +OM_TRITON_PORT = "9000" ATT_TRITON_IP = "10.1.1.240" ATT_TRITON_PORT = "10020" # service env -LOGS_PATH = "app/logs/errors.log" -FASHION_CATEGORIES = "app/service/outfit_matcher/config/fashion_categories.json" -FASHION_CATEGORIES_MAPPING = "app/service/outfit_matcher/config/fashion_category_mapping.json" +# LOGS_PATH = "app/logs/errors.log" +# FASHION_CATEGORIES = "app/service/outfit_matcher/config/fashion_categories.json" +# FASHION_CATEGORIES_MAPPING = "app/service/outfit_matcher/config/fashion_category_mapping.json" # pycharm debug # LOGS_PATH = "logs/errors.log" # FASHION_CATEGORIES = "service/outfit_matcher/config/fashion_categories.json" # FASHION_CATEGORIES_MAPPING = "service/outfit_matcher/config/fashion_category_mapping.json" + + +LOGS_PATH = "app/logs/errors.log" +FASHION_CATEGORIES = "./config/fashion_categories.json" +FASHION_CATEGORIES_MAPPING = "./config/fashion_category_mapping.json" \ No newline at end of file diff --git a/app/logs/errors.log b/app/logs/errors.log index e69de29..3619c22 100644 --- a/app/logs/errors.log +++ b/app/logs/errors.log @@ -0,0 +1,140 @@ +2024-03-21 13:51:37,818 decorator.py [line:11] INFO function:【load_image】,runtime:【1.2232704162597656】s +2024-03-21 13:51:37,818 decorator.py [line:11] INFO function:【load_image】,runtime:【1.2232704162597656】s +2024-03-21 13:51:38,052 decorator.py [line:11] INFO function:【load_image】,runtime:【0.19752931594848633】s +2024-03-21 13:51:38,052 decorator.py [line:11] INFO function:【load_image】,runtime:【0.19752931594848633】s +2024-03-21 13:51:38,385 decorator.py [line:11] INFO function:【load_image】,runtime:【0.3032798767089844】s +2024-03-21 13:51:38,385 decorator.py [line:11] INFO function:【load_image】,runtime:【0.3032798767089844】s +2024-03-21 13:51:38,721 decorator.py [line:11] INFO function:【load_image】,runtime:【0.30768489837646484】s +2024-03-21 13:51:38,721 decorator.py [line:11] INFO function:【load_image】,runtime:【0.30768489837646484】s +2024-03-21 13:51:39,043 decorator.py [line:11] INFO function:【load_image】,runtime:【0.29396677017211914】s +2024-03-21 13:51:39,043 decorator.py [line:11] INFO function:【load_image】,runtime:【0.29396677017211914】s +2024-03-21 13:51:39,470 decorator.py [line:11] INFO function:【load_image】,runtime:【0.39403533935546875】s +2024-03-21 13:51:39,470 decorator.py [line:11] INFO function:【load_image】,runtime:【0.39403533935546875】s +2024-03-21 13:51:39,888 decorator.py [line:11] INFO function:【load_image】,runtime:【0.3915884494781494】s +2024-03-21 13:51:39,888 decorator.py [line:11] INFO function:【load_image】,runtime:【0.3915884494781494】s +2024-03-21 13:51:40,318 decorator.py [line:11] INFO function:【load_image】,runtime:【0.40258264541625977】s +2024-03-21 13:51:40,318 decorator.py [line:11] INFO function:【load_image】,runtime:【0.40258264541625977】s +2024-03-21 13:51:40,737 decorator.py [line:11] INFO function:【load_image】,runtime:【0.3909275531768799】s +2024-03-21 13:51:40,737 decorator.py [line:11] INFO function:【load_image】,runtime:【0.3909275531768799】s +2024-03-21 13:51:40,948 decorator.py [line:11] INFO function:【load_image】,runtime:【0.1841294765472412】s +2024-03-21 13:51:40,948 decorator.py [line:11] INFO function:【load_image】,runtime:【0.1841294765472412】s +2024-03-21 13:51:41,379 decorator.py [line:11] INFO function:【load_image】,runtime:【0.40169310569763184】s +2024-03-21 13:51:41,379 decorator.py [line:11] INFO function:【load_image】,runtime:【0.40169310569763184】s +2024-03-21 13:51:41,708 decorator.py [line:11] INFO function:【load_image】,runtime:【0.29892587661743164】s +2024-03-21 13:51:41,708 decorator.py [line:11] INFO function:【load_image】,runtime:【0.29892587661743164】s +2024-03-21 13:51:42,124 decorator.py [line:11] INFO function:【load_image】,runtime:【0.38617706298828125】s +2024-03-21 13:51:42,124 decorator.py [line:11] INFO function:【load_image】,runtime:【0.38617706298828125】s +2024-03-21 13:51:42,430 decorator.py [line:11] INFO function:【load_image】,runtime:【0.27953577041625977】s +2024-03-21 13:51:42,430 decorator.py [line:11] INFO function:【load_image】,runtime:【0.27953577041625977】s +2024-03-21 13:51:42,953 decorator.py [line:11] INFO function:【load_image】,runtime:【0.49825477600097656】s +2024-03-21 13:51:42,953 decorator.py [line:11] INFO function:【load_image】,runtime:【0.49825477600097656】s +2024-03-21 13:51:43,492 decorator.py [line:11] INFO function:【load_image】,runtime:【0.5133931636810303】s +2024-03-21 13:51:43,492 decorator.py [line:11] INFO function:【load_image】,runtime:【0.5133931636810303】s +2024-03-21 13:51:43,923 decorator.py [line:11] INFO function:【load_image】,runtime:【0.40549373626708984】s +2024-03-21 13:51:43,923 decorator.py [line:11] INFO function:【load_image】,runtime:【0.40549373626708984】s +2024-03-21 13:51:44,233 decorator.py [line:11] INFO function:【load_image】,runtime:【0.27601003646850586】s +2024-03-21 13:51:44,233 decorator.py [line:11] INFO function:【load_image】,runtime:【0.27601003646850586】s +2024-03-21 13:51:44,662 decorator.py [line:11] INFO function:【load_image】,runtime:【0.4001624584197998】s +2024-03-21 13:51:44,662 decorator.py [line:11] INFO function:【load_image】,runtime:【0.4001624584197998】s +2024-03-21 13:51:44,980 decorator.py [line:11] INFO function:【load_image】,runtime:【0.2901315689086914】s +2024-03-21 13:51:44,980 decorator.py [line:11] INFO function:【load_image】,runtime:【0.2901315689086914】s +2024-03-21 13:51:45,403 decorator.py [line:11] INFO function:【load_image】,runtime:【0.3971738815307617】s +2024-03-21 13:51:45,403 decorator.py [line:11] INFO function:【load_image】,runtime:【0.3971738815307617】s +2024-03-21 13:51:45,713 decorator.py [line:11] INFO function:【load_image】,runtime:【0.2824699878692627】s +2024-03-21 13:51:45,713 decorator.py [line:11] INFO function:【load_image】,runtime:【0.2824699878692627】s +2024-03-21 13:51:46,136 decorator.py [line:11] INFO function:【load_image】,runtime:【0.3949100971221924】s +2024-03-21 13:51:46,136 decorator.py [line:11] INFO function:【load_image】,runtime:【0.3949100971221924】s +2024-03-21 13:51:46,439 decorator.py [line:11] INFO function:【load_image】,runtime:【0.2752203941345215】s +2024-03-21 13:51:46,439 decorator.py [line:11] INFO function:【load_image】,runtime:【0.2752203941345215】s +2024-03-21 13:51:46,863 decorator.py [line:11] INFO function:【load_image】,runtime:【0.3972630500793457】s +2024-03-21 13:51:46,863 decorator.py [line:11] INFO function:【load_image】,runtime:【0.3972630500793457】s +2024-03-21 13:51:47,175 decorator.py [line:11] INFO function:【load_image】,runtime:【0.2835068702697754】s +2024-03-21 13:51:47,175 decorator.py [line:11] INFO function:【load_image】,runtime:【0.2835068702697754】s +2024-03-21 13:51:47,604 decorator.py [line:11] INFO function:【load_image】,runtime:【0.3998706340789795】s +2024-03-21 13:51:47,604 decorator.py [line:11] INFO function:【load_image】,runtime:【0.3998706340789795】s +2024-03-21 13:51:48,026 decorator.py [line:11] INFO function:【load_image】,runtime:【0.39388179779052734】s +2024-03-21 13:51:48,026 decorator.py [line:11] INFO function:【load_image】,runtime:【0.39388179779052734】s +2024-03-21 13:51:48,453 decorator.py [line:11] INFO function:【load_image】,runtime:【0.3989236354827881】s +2024-03-21 13:51:48,453 decorator.py [line:11] INFO function:【load_image】,runtime:【0.3989236354827881】s +2024-03-21 13:51:49,100 decorator.py [line:11] INFO function:【load_image】,runtime:【0.6190431118011475】s +2024-03-21 13:51:49,100 decorator.py [line:11] INFO function:【load_image】,runtime:【0.6190431118011475】s +2024-03-21 13:51:49,631 decorator.py [line:11] INFO function:【load_image】,runtime:【0.501615047454834】s +2024-03-21 13:51:49,631 decorator.py [line:11] INFO function:【load_image】,runtime:【0.501615047454834】s +2024-03-21 13:51:50,279 decorator.py [line:11] INFO function:【load_image】,runtime:【0.6200711727142334】s +2024-03-21 13:51:50,279 decorator.py [line:11] INFO function:【load_image】,runtime:【0.6200711727142334】s +2024-03-21 13:51:50,804 decorator.py [line:11] INFO function:【load_image】,runtime:【0.49776339530944824】s +2024-03-21 13:51:50,804 decorator.py [line:11] INFO function:【load_image】,runtime:【0.49776339530944824】s +2024-03-21 13:51:51,341 decorator.py [line:11] INFO function:【load_image】,runtime:【0.5108270645141602】s +2024-03-21 13:51:51,341 decorator.py [line:11] INFO function:【load_image】,runtime:【0.5108270645141602】s +2024-03-21 13:51:51,867 decorator.py [line:11] INFO function:【load_image】,runtime:【0.49677276611328125】s +2024-03-21 13:51:51,867 decorator.py [line:11] INFO function:【load_image】,runtime:【0.49677276611328125】s +2024-03-21 13:51:52,289 decorator.py [line:11] INFO function:【load_image】,runtime:【0.39499711990356445】s +2024-03-21 13:51:52,289 decorator.py [line:11] INFO function:【load_image】,runtime:【0.39499711990356445】s +2024-03-21 13:51:52,815 decorator.py [line:11] INFO function:【load_image】,runtime:【0.49742674827575684】s +2024-03-21 13:51:52,815 decorator.py [line:11] INFO function:【load_image】,runtime:【0.49742674827575684】s +2024-03-21 13:51:53,226 decorator.py [line:11] INFO function:【load_image】,runtime:【0.383328914642334】s +2024-03-21 13:51:53,226 decorator.py [line:11] INFO function:【load_image】,runtime:【0.383328914642334】s +2024-03-21 13:51:53,751 decorator.py [line:11] INFO function:【load_image】,runtime:【0.4970104694366455】s +2024-03-21 13:51:53,751 decorator.py [line:11] INFO function:【load_image】,runtime:【0.4970104694366455】s +2024-03-21 13:51:54,166 decorator.py [line:11] INFO function:【load_image】,runtime:【0.3887917995452881】s +2024-03-21 13:51:54,166 decorator.py [line:11] INFO function:【load_image】,runtime:【0.3887917995452881】s +2024-03-21 13:51:54,693 decorator.py [line:11] INFO function:【load_image】,runtime:【0.49755120277404785】s +2024-03-21 13:51:54,693 decorator.py [line:11] INFO function:【load_image】,runtime:【0.49755120277404785】s +2024-03-21 13:51:55,109 decorator.py [line:11] INFO function:【load_image】,runtime:【0.38889598846435547】s +2024-03-21 13:51:55,109 decorator.py [line:11] INFO function:【load_image】,runtime:【0.38889598846435547】s +2024-03-21 13:51:55,636 decorator.py [line:11] INFO function:【load_image】,runtime:【0.49726033210754395】s +2024-03-21 13:51:55,636 decorator.py [line:11] INFO function:【load_image】,runtime:【0.49726033210754395】s +2024-03-21 13:51:56,062 decorator.py [line:11] INFO function:【load_image】,runtime:【0.39782261848449707】s +2024-03-21 13:51:56,062 decorator.py [line:11] INFO function:【load_image】,runtime:【0.39782261848449707】s +2024-03-21 13:51:56,603 decorator.py [line:11] INFO function:【load_image】,runtime:【0.5127170085906982】s +2024-03-21 13:51:56,603 decorator.py [line:11] INFO function:【load_image】,runtime:【0.5127170085906982】s +2024-03-21 13:51:57,135 decorator.py [line:11] INFO function:【load_image】,runtime:【0.4967665672302246】s +2024-03-21 13:51:57,135 decorator.py [line:11] INFO function:【load_image】,runtime:【0.4967665672302246】s +2024-03-21 13:51:57,770 decorator.py [line:11] INFO function:【load_image】,runtime:【0.6081705093383789】s +2024-03-21 13:51:57,770 decorator.py [line:11] INFO function:【load_image】,runtime:【0.6081705093383789】s +2024-03-21 13:51:58,190 decorator.py [line:11] INFO function:【load_image】,runtime:【0.3914616107940674】s +2024-03-21 13:51:58,190 decorator.py [line:11] INFO function:【load_image】,runtime:【0.3914616107940674】s +2024-03-21 13:51:58,827 decorator.py [line:11] INFO function:【load_image】,runtime:【0.61067795753479】s +2024-03-21 13:51:58,827 decorator.py [line:11] INFO function:【load_image】,runtime:【0.61067795753479】s +2024-03-21 13:51:59,338 decorator.py [line:11] INFO function:【load_image】,runtime:【0.4832015037536621】s +2024-03-21 13:51:59,338 decorator.py [line:11] INFO function:【load_image】,runtime:【0.4832015037536621】s +2024-03-21 13:52:00,078 decorator.py [line:11] INFO function:【load_image】,runtime:【0.7159032821655273】s +2024-03-21 13:52:00,078 decorator.py [line:11] INFO function:【load_image】,runtime:【0.7159032821655273】s +2024-03-21 13:52:00,589 decorator.py [line:11] INFO function:【load_image】,runtime:【0.48383021354675293】s +2024-03-21 13:52:00,589 decorator.py [line:11] INFO function:【load_image】,runtime:【0.48383021354675293】s +2024-03-21 13:52:01,326 decorator.py [line:11] INFO function:【load_image】,runtime:【0.7128510475158691】s +2024-03-21 13:52:01,326 decorator.py [line:11] INFO function:【load_image】,runtime:【0.7128510475158691】s +2024-03-21 13:52:01,478 decorator.py [line:11] INFO function:【load_image】,runtime:【0.1243746280670166】s +2024-03-21 13:52:01,478 decorator.py [line:11] INFO function:【load_image】,runtime:【0.1243746280670166】s +2024-03-21 13:52:02,094 decorator.py [line:11] INFO function:【load_image】,runtime:【0.6098945140838623】s +2024-03-21 13:52:02,094 decorator.py [line:11] INFO function:【load_image】,runtime:【0.6098945140838623】s +2024-03-21 13:52:02,716 decorator.py [line:11] INFO function:【load_image】,runtime:【0.5928947925567627】s +2024-03-21 13:52:02,716 decorator.py [line:11] INFO function:【load_image】,runtime:【0.5928947925567627】s +2024-03-21 13:52:03,349 decorator.py [line:11] INFO function:【load_image】,runtime:【0.6093297004699707】s +2024-03-21 13:52:03,349 decorator.py [line:11] INFO function:【load_image】,runtime:【0.6093297004699707】s +2024-03-21 13:52:03,549 decorator.py [line:11] INFO function:【load_image】,runtime:【0.17124557495117188】s +2024-03-21 13:52:03,549 decorator.py [line:11] INFO function:【load_image】,runtime:【0.17124557495117188】s +2024-03-21 13:52:04,188 decorator.py [line:11] INFO function:【load_image】,runtime:【0.6077630519866943】s +2024-03-21 13:52:04,188 decorator.py [line:11] INFO function:【load_image】,runtime:【0.6077630519866943】s +2024-03-21 13:52:04,381 decorator.py [line:11] INFO function:【load_image】,runtime:【0.1649329662322998】s +2024-03-21 13:52:04,381 decorator.py [line:11] INFO function:【load_image】,runtime:【0.1649329662322998】s +2024-03-21 13:52:05,027 decorator.py [line:11] INFO function:【load_image】,runtime:【0.6105599403381348】s +2024-03-21 13:52:05,027 decorator.py [line:11] INFO function:【load_image】,runtime:【0.6105599403381348】s +2024-03-21 13:52:05,423 decorator.py [line:11] INFO function:【load_image】,runtime:【0.3696911334991455】s +2024-03-21 13:52:05,423 decorator.py [line:11] INFO function:【load_image】,runtime:【0.3696911334991455】s +2024-03-21 13:52:06,274 decorator.py [line:11] INFO function:【load_image】,runtime:【0.8220889568328857】s +2024-03-21 13:52:06,274 decorator.py [line:11] INFO function:【load_image】,runtime:【0.8220889568328857】s +2024-03-21 13:52:06,687 decorator.py [line:11] INFO function:【load_image】,runtime:【0.38632655143737793】s +2024-03-21 13:52:06,687 decorator.py [line:11] INFO function:【load_image】,runtime:【0.38632655143737793】s +2024-03-21 13:52:07,436 decorator.py [line:11] INFO function:【load_image】,runtime:【0.7190515995025635】s +2024-03-21 13:52:07,436 decorator.py [line:11] INFO function:【load_image】,runtime:【0.7190515995025635】s +2024-03-21 13:52:07,622 decorator.py [line:11] INFO function:【load_image】,runtime:【0.15749859809875488】s +2024-03-21 13:52:07,622 decorator.py [line:11] INFO function:【load_image】,runtime:【0.15749859809875488】s +2024-03-21 13:52:08,375 decorator.py [line:11] INFO function:【load_image】,runtime:【0.7181243896484375】s +2024-03-21 13:52:08,375 decorator.py [line:11] INFO function:【load_image】,runtime:【0.7181243896484375】s +2024-03-21 13:52:08,782 decorator.py [line:11] INFO function:【load_image】,runtime:【0.37945127487182617】s +2024-03-21 13:52:08,782 decorator.py [line:11] INFO function:【load_image】,runtime:【0.37945127487182617】s +2024-03-21 13:52:09,530 decorator.py [line:11] INFO function:【load_image】,runtime:【0.7160646915435791】s +2024-03-21 13:52:09,530 decorator.py [line:11] INFO function:【load_image】,runtime:【0.7160646915435791】s +2024-03-21 13:52:09,813 decorator.py [line:11] INFO function:【load_image】,runtime:【0.25645899772644043】s +2024-03-21 13:52:09,813 decorator.py [line:11] INFO function:【load_image】,runtime:【0.25645899772644043】s diff --git a/app/schemas/similar_match.py b/app/schemas/similar_match.py new file mode 100644 index 0000000..5deb058 --- /dev/null +++ b/app/schemas/similar_match.py @@ -0,0 +1,5 @@ +from pydantic import BaseModel + + +class SimilarMatchMItem(BaseModel): + image_path: str diff --git a/app/service/outfit_matcher/outfit_evaluator.py b/app/service/outfit_matcher/outfit_evaluator.py index ff8f9af..2404454 100644 --- a/app/service/outfit_matcher/outfit_evaluator.py +++ b/app/service/outfit_matcher/outfit_evaluator.py @@ -327,9 +327,12 @@ class OutfitMaterTypeAware(OutfitMatcher): # 输出集 outputs = [ httpclient.InferRequestedOutput("output__0", binary_data=True), + httpclient.InferRequestedOutput("output__1", binary_data=True) ] results = client.infer(model_name="outfit_matcher_type_aware", inputs=inputs, outputs=outputs) # 推理 # 取结果 - scores = torch.from_numpy(results.as_numpy("output__0")) - return scores # Shape (N, 1) + scores = torch.from_numpy(results.as_numpy("output__0")) # Shape (N, 1) + features = torch.from_numpy(results.as_numpy("output__1")) # Shape (N, 64) + + return scores, features diff --git a/app/service/outfit_matcher/service.py b/app/service/outfit_matcher/service.py index 117075f..59961cb 100644 --- a/app/service/outfit_matcher/service.py +++ b/app/service/outfit_matcher/service.py @@ -14,7 +14,14 @@ if __name__ == '__main__': bad_list = [] for item in param["query"]: outfits = fashion_dataset.generate_outfit(item, param["topk"], param["max_outfits"]) - scores = service.get_result(outfits) + scores, features = service.get_result(outfits) + # save features + + # 链接milvus + + # 存入数据库 + # 关闭链接 + # print(scores) # print(len(scores)) best_outfits, best_scores = service.visualize(outfits, scores, param["topk"], best=True, diff --git a/app/service/similar_match/__init__.py b/app/service/similar_match/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/app/service/similar_match/service.py b/app/service/similar_match/service.py new file mode 100644 index 0000000..dae4af9 --- /dev/null +++ b/app/service/similar_match/service.py @@ -0,0 +1,102 @@ +import io +import json + +import numpy as np +import tritonclient.http as httpclient +from PIL import Image +from minio import Minio +from pymilvus import MilvusClient + +from app.core.config import * +from torchvision import transforms + + +class SimilarMatch: + def __init__(self): + 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}") + + @staticmethod + def resize_image(img): + """ + Args: + img: ndarray (height, width, channel) + """ + image_transforms = transforms.Compose([ + transforms.Resize(112), + transforms.CenterCrop(112), + transforms.ToTensor(), + transforms.Normalize(mean=[0.485, 0.456, 0.406], + std=[0.229, 0.224, 0.225]), + ]) + resized_img = image_transforms(img).numpy() + return resized_img + + def load_image(self, img_path): + # 从 MinIO 中获取对象(图像文件) + image_data = self.minio_client.get_object(img_path.split("/", 1)[0], img_path.split("/", 1)[1]) + # 读取图像数据并转换为 PIL 图像对象 + pil_image = Image.open(io.BytesIO(image_data.data)).convert("RGB") + # 将 PIL 图像转换为 NumPy 数组 + # image_array = np.array(pil_image) + return pil_image + + def preprocess(self, img_path): + image = self.load_image(img_path) + image = self.resize_image(image) + image = np.stack([[image]], axis=0) + + category = np.stack([[1, 6]], axis=0) + + 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) + # 输入集 + inputs = [ + httpclient.InferInput("input__0", image.shape, datatype="FP32"), + httpclient.InferInput("input__1", category.shape, datatype="INT16"), + httpclient.InferInput("input__2", mask.shape, datatype="FP32"), + ] + inputs[0].set_data_from_numpy(image.astype(np.float32), binary_data=True) + inputs[1].set_data_from_numpy(category.astype(np.int16), binary_data=True) + inputs[2].set_data_from_numpy(mask.astype(np.float32), binary_data=True) + # 输出集 + outputs = [ + httpclient.InferRequestedOutput("output__0", binary_data=True), + httpclient.InferRequestedOutput("output__1", binary_data=True) + ] + results = self.triton_client.infer(model_name="outfit_matcher_type_aware", inputs=inputs, outputs=outputs) + # 推理 + # 取结果 + features = results.as_numpy("output__1") # Shape (N, 64) + return features + + def match_features(self, features): + # 连接milvus + # 连接milvus + client = MilvusClient(uri="http://10.1.1.240:19530", db_name="mixi") + try: + res = 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 + output_fields=["id", "image_path"], # Search parameters + ) + return res + finally: + client.close() + + +if __name__ == '__main__': + service = SimilarMatch() + features = service.get_features(img_path="test/2024 SS/MKTS27000.jpg") + res = service.match_features(features) + + print(json.dumps(res, indent=4)) diff --git a/requirements.txt b/requirements.txt index b7c5889a5b110788668ee7185943134ff83a1d9a..084644b37865819a8afa522ced1c8b5a81855483 100644 GIT binary patch literal 1106 zcmZXTTTjA35QXR2#Q#9TqoGhPf+qeF<3qVgBNT)M^v|o`ne9@7X||TRoO9;v`1`Z7 z($+j%JKGCSZavGa&*|B{-4Wl}!YZ5D$vS+0Td)<;%BzReJ$Y?rle#0=9?al1GmuN~ z5B6$>Grf7I7=|{u@XSCh=$um>oO#3V0Hd@8$C-MkdM2DfT~(r`q(5E=u7eC}hWk)ckR3(IwPoF#6&bx(i@Vu~fVs`2A zz*~-|{=CMIULWyT+*^Ez{zjG2VJW!wzk^;mquV)M^r|B=?#X}Ir*o_b&oB!YQ{TGmatP) zla-EExW{d;EUEslbi>_fOViD1H~Wsi}HobdAhJKB4QV+{MHeIwtY=rx|*FF*dz6Ex8uIa znKV=1vqQ&dIF)U-?V(oIM~AqYy6YQjHc