edit : 벡터파일 생성 관련 분리

This commit is contained in:
2025-12-02 14:15:49 +09:00
parent 18b24dcafa
commit 8b7ad2e973
15 changed files with 188 additions and 71 deletions

View File

@@ -1,4 +1,4 @@
# FM_TEST_REST_SERVER # GLASSES_AI_SERVER
RESTful API Server RESTful API Server

View File

27
custom_apps/FEATURE_VECTOR_SIMILARITY_FAISS/const.py Executable file → Normal file
View File

@@ -18,24 +18,23 @@ ViTL14_336 = "clip-vit-large-patch14-336"
#download/save path #download/save path
PRETRAINED_MODEL_PATH = "./datas" PRETRAINED_MODEL_PATH = "./datas"
FAISS_VECTOR_PATH = "./datas/Eyewear_001~500_ComponentsCapture_20251121" FAISS_VECTOR_PATH = "./datas"
VECTOR_IMG_LIST_PATH = "./datas/Eyewear_001~500_ComponentsCapture_20251121" IMG_LIST_PATH = "../Eyewear_data"
INDEX_IMAGES_PATH = "./datas/eyewear_all"
# INDEX_IMAGES_PATH = "./datas/eyewear_all"
class VectorSearchItem: class VectorSearchItem:
glass = "glass" glass = "glass"
bridge = "Bridge" tips = "Tips"
hinge = "Hinges"
lens = "Lens"
nosepad = "Nose pad"
other = "other"
padarm = "Padarm"
rim = "Rim"
rivet = "Rivet"
soltex = "Soltex"
temple = "Temple" temple = "Temple"
templetip = "Temple tip" rim = "Rim"
rim2 = "Rim2"
noesPad = "Nose pad"
noseArm = "Nose arm"
lens = "Lens"
endPiece = "End piece"
hinges = "Hinges"
screw = "Screw"
bridge = "Bridge"
class ImageDepths: class ImageDepths:
parts = "Parts" parts = "Parts"

View File

@@ -104,34 +104,28 @@ def get_clip_info(model, query_image_path, item_info, top_k=4):
""" """
이미지 유사도 검색 이미지 유사도 검색
""" """
index_file_path = os.path.join(VECTOR_IMG_LIST_PATH,f'{model.name}_{os.path.basename(model.value[1].trained_model)}_{model.value[1].index_type}_{item_info}_{DEFAULT_INDEX_NAME_SUFFIX}') index_file_path = os.path.join(FAISS_VECTOR_PATH,f'{model.name}_{os.path.basename(model.value[1].trained_model)}_{model.value[1].index_type}_{item_info}_{DEFAULT_INDEX_NAME_SUFFIX}')
txt_file_path = os.path.join(VECTOR_IMG_LIST_PATH,f'{model.name}_{os.path.basename(model.value[1].trained_model)}_{model.value[1].index_type}_{item_info}.txt') txt_file_path = os.path.join(FAISS_VECTOR_PATH,f'{model.name}_{os.path.basename(model.value[1].trained_model)}_{model.value[1].index_type}_{item_info}.txt')
if not os.path.exists(index_file_path) or not os.path.exists(txt_file_path):
raise FileNotFoundError(f"Index file {index_file_path} or txt file {txt_file_path} does not exist.")
vector_model = VectorSimilarity(model_info=model, vector_model = VectorSimilarity(model_info=model,
index_file_path=index_file_path, index_file_path=index_file_path,
txt_file_path=txt_file_path) txt_file_path=txt_file_path)
image_lists = [] # image_lists = []
if not os.path.exists(txt_file_path):
if item_info == VectorSearchItem.glass: # with open(txt_file_path, 'r') as f:
image_lists = find_glass_folder_images(VECTOR_IMG_LIST_PATH) # image_lists = [line.strip() for line in f.readlines()]
else:
image_lists = find_parts_folder_images(VECTOR_IMG_LIST_PATH, item_info)
if image_lists:
with open(txt_file_path, 'w') as f:
for img_path in image_lists:
f.write(f"{img_path}\n")
else:
with open(txt_file_path, 'r') as f:
image_lists = [line.strip() for line in f.readlines()]
# vector_model.create_feature_extraction_model(FEMUsageInfo.openaiclipvit) # vector_model.create_feature_extraction_model(FEMUsageInfo.openaiclipvit)
vector_model.save_index_from_files(images_path_lists=image_lists, # vector_model.save_index_from_files(images_path_lists=image_lists,
save_index_dir=FAISS_VECTOR_PATH, # save_index_dir=FAISS_VECTOR_PATH,
save_txt_dir=VECTOR_IMG_LIST_PATH, # save_txt_dir=VECTOR_IMG_LIST_PATH,
item_info=item_info, # item_info=item_info,
index_type=model.value[1].index_type) # index_type=model.value[1].index_type)
inference_times, result_img_paths, result_percents = vector_model.query_faiss(query_image_path, top_k=top_k) inference_times, result_img_paths, result_percents = vector_model.query_faiss(query_image_path, top_k=top_k)

View File

@@ -372,33 +372,33 @@ class VectorSimilarity:
return inference_times, result_img_paths, result_percents return inference_times, result_img_paths, result_percents
def test(): # def test():
""" # """
module test function # module test function
""" # """
log.info('\nModule: faiss_similarity_search.py') # log.info('\nModule: faiss_similarity_search.py')
# index_file_path = f'{FILE_BASE_PATH}openaiclipvit_clip-vit-base-patch32_index.faiss' # # index_file_path = f'{FILE_BASE_PATH}openaiclipvit_clip-vit-base-patch32_index.faiss'
model = FEMUsageInfo.openaiclip_vit_l14_cos # model = FEMUsageInfo.openaiclip_vit_l14_cos
index_file_path = os.path.join(VECTOR_IMG_LIST_PATH,f'{model.name}_{os.path.basename(model.value[1].trained_model)}_{model.value[1].index_type}_{DEFAULT_INDEX_NAME_SUFFIX}') # index_file_path = os.path.join(VECTOR_IMG_LIST_PATH,f'{model.name}_{os.path.basename(model.value[1].trained_model)}_{model.value[1].index_type}_{DEFAULT_INDEX_NAME_SUFFIX}')
cm = VectorSimilarity(model_info=model, # cm = VectorSimilarity(model_info=model,
index_file_path=index_file_path, # index_file_path=index_file_path,
txt_file_path=os.path.join(VECTOR_IMG_LIST_PATH,f'{model.name}_{os.path.basename(model.value[1].trained_model)}_{model.value[1].index_type}.txt')) # txt_file_path=os.path.join(VECTOR_IMG_LIST_PATH,f'{model.name}_{os.path.basename(model.value[1].trained_model)}_{model.value[1].index_type}.txt'))
# cm.create_feature_extraction_model(FEMUsageInfo.openaiclipvit) # # cm.create_feature_extraction_model(FEMUsageInfo.openaiclipvit)
cm.save_index_from_files(images_path_lists=INDEX_IMAGES_PATH, # cm.save_index_from_files(images_path_lists=INDEX_IMAGES_PATH,
save_index_dir=FAISS_VECTOR_PATH, # save_index_dir=FAISS_VECTOR_PATH,
save_txt_dir=VECTOR_IMG_LIST_PATH, # save_txt_dir=VECTOR_IMG_LIST_PATH,
index_type=model.value[1].index_type) # index_type=model.value[1].index_type)
cm.query_faiss(os.path.join(INDEX_IMAGES_PATH,"img1.png")) # cm.query_faiss(os.path.join(INDEX_IMAGES_PATH,"img1.png"))
if __name__ == '__main__': if __name__ == '__main__':
""" """
test module test module
""" """
test() # test()

View File

View File

View File

@@ -21,6 +21,12 @@ def search_glass_parts(image_path_list):
return result return result
def file_name_to_parts(file_path): def file_name_to_parts(file_path):
"""
파일경로로 파츠이름 파싱
:param file_path: 파일경로
:return: 파츠이름
"""
filename, ext = os.path.splitext(os.path.basename(file_path)) filename, ext = os.path.splitext(os.path.basename(file_path))
name_list = filename.split('_') name_list = filename.split('_')
@@ -28,4 +34,6 @@ def file_name_to_parts(file_path):
result = name_list[2:][0] result = name_list[2:][0]
return result return result
if __name__ == '__main__':
print(file_name_to_parts(os.path.join(FAISS_VECTOR_PATH,"Glass_001",ImageDepths.parts,"Glass_001_Temple_L.png")))

View File

@@ -2,7 +2,7 @@
""" """
@file : custom_log.py @file : custom_log.py
@author: hsj100 @author: hsj100
@license: DAOOLDNS @license: A2TEC
@brief: @brief:
@section Modify History @section Modify History
- 2024-05-08 오후 2:33 hsj100 base - 2024-05-08 오후 2:33 hsj100 base

View File

@@ -583,11 +583,6 @@ class VitModelType:
l14_336 = "l14_336" l14_336 = "l14_336"
class PartsType(str, Enum):
bridge = VectorSearchItem.bridge
hinge = VectorSearchItem.hinge
class ImageGenerateReq(BaseModel): class ImageGenerateReq(BaseModel):
""" """
### [Request] image generate request ### [Request] image generate request
@@ -663,7 +658,7 @@ class VectorGlassesImageResult(BaseModel):
class VectorPartsImageResult(BaseModel): class VectorPartsImageResult(BaseModel):
image : str = Field("", description='이미지 데이터', example='') image : str | None = Field("", description='이미지 데이터', example='')
percents: float = Field(0.0, description='percents 값', example='') percents: float = Field(0.0, description='percents 값', example='')
imageInfo : str = Field("", description='원본이미지 이름', example='') imageInfo : str = Field("", description='원본이미지 이름', example='')
#=============================================================================== #===============================================================================

View File

@@ -446,7 +446,7 @@ async def image_generator(request: Request, request_body_info: M.ImageGenerateRe
full_prompt = f"{request_body_info.prefix}, {request_body_info.prompt}" if request_body_info.prefix else f"{request_body_info.prompt}" full_prompt = f"{request_body_info.prefix}, {request_body_info.prompt}" if request_body_info.prefix else f"{request_body_info.prompt}"
temp_image_path = gemini_image(full_prompt) #TODO(jwkim) imagen3 으로 변경예정 temp_image_path = gemini_image(full_prompt)
if os.path.exists(temp_image_path): if os.path.exists(temp_image_path):
image_data = image_to_base64_string(temp_image_path) image_data = image_to_base64_string(temp_image_path)
@@ -483,7 +483,7 @@ async def vactor_vit_input_glasses_img_data(request: Request, request_body_info:
> 입력이미지(inputImage)는 base64로 변환된 데이터 입력 > 입력이미지(inputImage)는 base64로 변환된 데이터 입력
### Output ### Output
> - 결과(vectorResult)는 base64로 변환된 데이터(image), 유사도(percents)가 쌍으로 나오며, 요청한 searchNum 갯수에 맞춰서 결과가 나옴 > - 결과(vectorResult)는 base64로 변환된 데이터(image), 유사도(percents), 파츠정보(parts)가 쌍으로 나오며, 요청한 searchNum 갯수에 맞춰서 결과가 나옴
> - queryImage는 Input시 입력한 inputImage 이미지 데이터 > - queryImage는 Input시 입력한 inputImage 이미지 데이터
### Options ### Options
@@ -534,7 +534,9 @@ async def vactor_vit_input_glasses_img_data(request: Request, request_body_info:
vector_image_results = [] vector_image_results = []
for img, percents, parts in zip(result_image_paths, result_percents, result_parts): for img, percents, parts in zip(result_image_paths, result_percents, result_parts):
b64_data = image_to_base64_string(img) b64_data = None
if os.path.exists(img):
b64_data = image_to_base64_string(img)
float_percent = float(percents) float_percent = float(percents)
info = M.VectorGlassesImageResult(image=b64_data, percents=float_percent, imageInfo=os.path.split(img)[-1], parts=parts) info = M.VectorGlassesImageResult(image=b64_data, percents=float_percent, imageInfo=os.path.split(img)[-1], parts=parts)
@@ -568,7 +570,8 @@ async def vactor_vit_input_parts_img_data(request: Request, request_body_info: M
> 입력된 이미지로 vector 검색 그후 결과 이미지데이터 return > 입력된 이미지로 vector 검색 그후 결과 이미지데이터 return
### Input ### Input
> 입력이미지(inputImage)는 파츠이미지 이름 (확장자 포함) > 입력이미지(inputImage)는 파츠 이미지파일 이름 (확장자 포함)
> ⚠️ /vectorImageSearch/vit/inputImage/data/glasses API response인 parts 값중 하나를 선택
### Output ### Output
> - 결과(vectorResult)는 base64로 변환된 데이터(image), 유사도(percents)가 쌍으로 나오며, 요청한 searchNum 갯수에 맞춰서 결과가 나옴 > - 결과(vectorResult)는 base64로 변환된 데이터(image), 유사도(percents)가 쌍으로 나오며, 요청한 searchNum 갯수에 맞춰서 결과가 나옴
@@ -592,7 +595,7 @@ async def vactor_vit_input_parts_img_data(request: Request, request_body_info: M
raise Exception(f"indexType is invalid (current value = {request_body_info.indexType})") raise Exception(f"indexType is invalid (current value = {request_body_info.indexType})")
glass_name = f"{request_body_info.inputImage.split('_')[0]}_{request_body_info.inputImage.split('_')[1]}" glass_name = f"{request_body_info.inputImage.split('_')[0]}_{request_body_info.inputImage.split('_')[1]}"
query_image_path = os.path.join(VECTOR_IMG_LIST_PATH, glass_name, ImageDepths.parts, request_body_info.inputImage) query_image_path = os.path.join(IMG_LIST_PATH, glass_name, ImageDepths.parts, request_body_info.inputImage)
query_image_data = image_to_base64_string(query_image_path) query_image_data = image_to_base64_string(query_image_path)
vector_request_data = {'query_image_path' : query_image_path, vector_request_data = {'query_image_path' : query_image_path,
@@ -617,7 +620,9 @@ async def vactor_vit_input_parts_img_data(request: Request, request_body_info: M
vector_image_results = [] vector_image_results = []
for img, percents in zip(result_image_paths, result_percents): for img, percents in zip(result_image_paths, result_percents):
b64_data = image_to_base64_string(img) b64_data = None
if os.path.exists(img):
b64_data = image_to_base64_string(img)
float_percent = float(percents) float_percent = float(percents)
info = M.VectorPartsImageResult(image=b64_data, percents=float_percent, imageInfo=os.path.split(img)[-1]) info = M.VectorPartsImageResult(image=b64_data, percents=float_percent, imageInfo=os.path.split(img)[-1])

117
make_vector_files.py Normal file
View File

@@ -0,0 +1,117 @@
# from custom_apps.FEATURE_VECTOR_SIMILARITY_FAISS.faiss_functions import get_clip_info
import os
from custom_apps.FEATURE_VECTOR_SIMILARITY_FAISS.faiss_similarity_search import VectorSimilarity
from custom_apps.FEATURE_VECTOR_SIMILARITY_FAISS.const import *
from custom_apps.FEATURE_VECTOR_SIMILARITY_FAISS.faiss_functions import get_models, find_glass_folder_images, find_parts_folder_images
from vector_rest.app import models as VM
def make_image_files(item_info, index_type:VM.VitIndexType, model_type:VM.VitModelType):
model = get_models(index_type=index_type, model_type=model_type)
txt_file_path = os.path.join(FAISS_VECTOR_PATH,f'{model.name}_{os.path.basename(model.value[1].trained_model)}_{model.value[1].index_type}_{item_info}.txt')
image_lists = []
if not os.path.exists(txt_file_path):
if item_info == VectorSearchItem.glass:
image_lists = find_glass_folder_images(IMG_LIST_PATH)
else:
image_lists = find_parts_folder_images(IMG_LIST_PATH, item_info)
if image_lists:
with open(txt_file_path, 'w') as f:
for img_path in image_lists:
f.write(f"{img_path}\n")
else:
temp_path = os.path.join(FAISS_VECTOR_PATH,f'{model.name}_{os.path.basename(model.value[1].trained_model)}_{model.value[1].index_type}_{item_info}.bak')
try:
os.rename(txt_file_path, temp_path)
if item_info == VectorSearchItem.glass:
image_lists = find_glass_folder_images(IMG_LIST_PATH)
else:
image_lists = find_parts_folder_images(IMG_LIST_PATH, item_info)
if image_lists:
with open(txt_file_path, 'w') as f:
for img_path in image_lists:
f.write(f"{img_path}\n")
except Exception as e:
print(f"Error updating image file list: {e}")
finally:
if os.path.exists(txt_file_path):
os.remove(temp_path)
else:
os.rename(temp_path, txt_file_path)
return txt_file_path
def make_vector_files(item_info, index_type:VM.VitIndexType, model_type:VM.VitModelType):
model = get_models(index_type=index_type, model_type=model_type)
txt_file_path = make_image_files(item_info=item_info, index_type=index_type, model_type=model_type)
index_file_path = os.path.join(IMG_LIST_PATH,f'{model.name}_{os.path.basename(model.value[1].trained_model)}_{model.value[1].index_type}_{item_info}_{DEFAULT_INDEX_NAME_SUFFIX}')
if not os.path.exists(index_file_path):
vector_model = VectorSimilarity(model_info=model,
index_file_path=index_file_path,
txt_file_path=txt_file_path)
with open(txt_file_path, 'r') as f:
image_lists = [line.strip() for line in f.readlines()]
vector_model.save_index_from_files(images_path_lists=image_lists,
save_index_dir=FAISS_VECTOR_PATH,
save_txt_dir=IMG_LIST_PATH,
item_info=item_info,
index_type=model.value[1].index_type)
else:
temp_path = os.path.join(IMG_LIST_PATH,f'{model.name}_{os.path.basename(model.value[1].trained_model)}_{model.value[1].index_type}_{item_info}_{DEFAULT_INDEX_NAME_SUFFIX}.bak')
try:
os.rename(index_file_path, temp_path)
vector_model = VectorSimilarity(model_info=model,
index_file_path=index_file_path,
txt_file_path=txt_file_path)
with open(txt_file_path, 'r') as f:
image_lists = [line.strip() for line in f.readlines()]
vector_model.save_index_from_files(images_path_lists=image_lists,
save_index_dir=FAISS_VECTOR_PATH,
save_txt_dir=IMG_LIST_PATH,
item_info=item_info,
index_type=model.value[1].index_type)
except Exception as e:
print(f"Error updating vector file: {e}")
finally:
if os.path.exists(index_file_path):
os.remove(temp_path)
else:
os.rename(temp_path, index_file_path)
print(f"Vector file created: {index_file_path} :::::::::: item_info: {item_info}, index_type: {index_type}, model_type: {model_type}")
return index_file_path
if __name__ == '__main__':
import time
class_attributes = dict(VectorSearchItem.__dict__)
pure_data_dict = {
key: value
for key, value in class_attributes.items()
if not key.startswith('__')
}
for item_key, item_value in pure_data_dict.items():
make_vector_files(item_info=item_value, index_type=VM.VitIndexType.l2, model_type=VM.VitModelType.b32)
time.sleep(5) # huggingface api 요청 제한 회피 위해 대기 TODO(jwkim) huggingface 로그인은 한번만 진행하게 변경

View File

@@ -39,7 +39,7 @@ def send_mail():
sender = 'jolimola@gmail.com' sender = 'jolimola@gmail.com'
sender_pw = '!ghkdtmdwns1' sender_pw = '!ghkdtmdwns1'
# recipient = 'hsj100@a2tec.co.kr' # recipient = 'hsj100@a2tec.co.kr'
recipient = 'jwkim@daooldns.co.kr' recipient = 'jwkim@a2tec.co.kr'
list_cc = ['cc1@gmail.com', 'cc2@naver.com'] list_cc = ['cc1@gmail.com', 'cc2@naver.com']
str_cc = ','.join(list_cc) str_cc = ','.join(list_cc)

View File

@@ -110,7 +110,7 @@ async def vactor_vit(request: Request, request_body_info: M.VectorSearchVitReq):
@router.post("/faiss/vector/search/vit/parts", summary="vit search parts", response_model=M.VectorSearchVitRes) @router.post("/faiss/vector/search/vit/parts", summary="vit search parts", response_model=M.VectorSearchVitRes)
async def vactor_vit_parts(request: Request, request_body_info: M.VectorSearchVitReq): async def vactor_vit_parts(request: Request, request_body_info: M.VectorSearchVitReq):
""" """
이미지 경로 를 입력받아 vit 방식으로 검색 이미지 경로 를 입력받아 vit 방식으로 parts 검색
""" """
response = M.VectorSearchVitRes() response = M.VectorSearchVitRes()
try: try:
@@ -119,7 +119,6 @@ async def vactor_vit_parts(request: Request, request_body_info: M.VectorSearchVi
model = get_models(index_type=request_body_info.index_type, model_type=request_body_info.model_type) model = get_models(index_type=request_body_info.index_type, model_type=request_body_info.model_type)
#TODO parts 데이터 확인필요
parts = file_name_to_parts(request_body_info.query_image_path) parts = file_name_to_parts(request_body_info.query_image_path)
report_info = get_clip_info(model=model, report_info = get_clip_info(model=model,