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 b7c5889..084644b 100644 Binary files a/requirements.txt and b/requirements.txt differ diff --git a/test/outfit_matcher_milvus/1.create databases.py b/test/outfit_matcher_milvus/1.create databases.py new file mode 100644 index 0000000..f1bfadb --- /dev/null +++ b/test/outfit_matcher_milvus/1.create databases.py @@ -0,0 +1,5 @@ +from pymilvus import connections, db + +conn = connections.connect(host="10.1.1.240", port=19530) + +database = db.create_database("mixi") diff --git a/test/outfit_matcher_milvus/2.create collection.py b/test/outfit_matcher_milvus/2.create collection.py new file mode 100644 index 0000000..70edb0e --- /dev/null +++ b/test/outfit_matcher_milvus/2.create collection.py @@ -0,0 +1,23 @@ +from pymilvus import MilvusClient, DataType + +# 创建client ,配置databases +client = MilvusClient( + uri="http://10.1.1.240:19530", + token="root:Milvus", + db_name="mixi" +) +from pymilvus import MilvusClient, DataType + +schema = MilvusClient.create_schema( + auto_id=True, + enable_dynamic_field=False, +) + +schema.add_field(field_name="id", datatype=DataType.INT64, is_primary=True) +schema.add_field(field_name="image_path", datatype=DataType.VARCHAR, max_length=200) +schema.add_field(field_name="vector", datatype=DataType.FLOAT_VECTOR, dim=64) + +client.create_collection( + collection_name="mixi_outfit", + schema=schema +) diff --git a/test/outfit_matcher_milvus/3.create index.py b/test/outfit_matcher_milvus/3.create index.py new file mode 100644 index 0000000..27aa3dd --- /dev/null +++ b/test/outfit_matcher_milvus/3.create index.py @@ -0,0 +1,23 @@ +from pymilvus import MilvusClient, Collection + +client = MilvusClient( + uri="http://10.1.1.240:19530", + token="root:Milvus", + db_name="mixi" +) + +index_params = client.prepare_index_params() +index_params.add_index( + field_name="id", + index_type="STL_SORT" +) +index_params.add_index( + field_name="vector", + index_type="IVF_FLAT", + metric_type="L2", + params={"nlist": 1024} +) +client.create_index( + collection_name="mixi_outfit", + index_params=index_params +) diff --git a/test/outfit_matcher_milvus/4.load clollection.py b/test/outfit_matcher_milvus/4.load clollection.py new file mode 100644 index 0000000..08c5698 --- /dev/null +++ b/test/outfit_matcher_milvus/4.load clollection.py @@ -0,0 +1,10 @@ +from pymilvus import MilvusClient + +client = MilvusClient( + uri="http://10.1.1.240:19530", + token="root:Milvus", + db_name="mixi" +) +client.load_collection( + collection_name="mixi_outfit" +) diff --git a/test/outfit_matcher_milvus/5.similar match.py b/test/outfit_matcher_milvus/5.similar match.py new file mode 100644 index 0000000..2ffaed6 --- /dev/null +++ b/test/outfit_matcher_milvus/5.similar match.py @@ -0,0 +1,27 @@ +import json +import time + +from pymilvus import MilvusClient + +client = MilvusClient( + uri="http://10.1.1.240:19530", + token="root:Milvus", + db_name="mixi" +) +data = [0.019687360152602196, 0.839404821395874, 0.5053166747093201, 0.6062483787536621, 0.5455009341239929, 0.07595491409301758, 0.028354600071907043, 0.24453534185886383, 0.6116685271263123, 0.4527449309825897, 0.22063420712947845, 0.09205381572246552, 0.22853578627109528, 0.3041312098503113, + 0.8354143500328064, 0.05135197564959526, 0.9292615652084351, 0.03914223983883858, 0.7091595530509949, 0.17939062416553497, 0.2958671748638153, 0.46751415729522705, 0.05523946136236191, 0.976833164691925, 0.3593502938747406, 0.0806853398680687, 0.3097323179244995, 0.12855321168899536, + 0.12651172280311584, 0.3173355162143707, 0.17060844600200653, 0.9340737462043762, 0.8437095880508423, 0.7500482797622681, 0.22598184645175934, 0.8127533197402954, 0.39825528860092163, 0.9043431878089905, 0.9064653515815735, 0.14613617956638336, 0.582768976688385, 0.4516744315624237, + 0.6479957699775696, 0.909612774848938, 0.7674093842506409, 0.47747865319252014, 0.5617552995681763, 0.967750072479248, 0.9146659970283508, 0.28031912446022034, 0.5092940330505371, 0.21442186832427979, 0.43696293234825134, 0.7705745100975037, 0.09395607560873032, 0.9103220701217651, + 0.2616001069545746, 0.7469480037689209, 0.24508604407310486, 0.6890515089035034, 0.704613447189331, 0.7213652729988098, 0.3660031855106354, 0.2150406688451767] +start_time = time.time() +res = client.search( + collection_name="mixi_outfit", # Replace with the actual name of your collection + # Replace with your query vector + data=[data], + limit=5, # Max. number of search results to return + # search_params={"metric_type": "IP", "params": {}} # Search parameters +) +print(time.time() - start_time) + +result = json.dumps(res, indent=4) +print(result)