이미지 검색

이미지 검색은 이미지를 검색 입력으로 사용해 벡터 유사도 검색을 수행합니다. 텍스트 대신 이미지 하나를 던지면, 그 이미지와 시각적으로 유사한 객체를 찾아줘요.

이미지를 검색 입력으로 쓰려면 컬렉션에 이미지 벡터라이저 통합을 구성해야 해요. 사용 가능한 통합 목록은 모델 프로바이더 통합 페이지를 참고하세요.

출처: 공식문서

로컬 이미지 경로로 검색

Near Image 연산자로 이미지 검색을 실행합니다. 쿼리 이미지가 파일로 저장돼 있다면, 클라이언트 라이브러리로 파일 이름을 지정해 검색할 수 있어요.

from pathlib import Path

dogs = client.collections.use("Dog")
response = dogs.query.near_image(
    near_image=Path("./images/search-image.jpg"),  # Path 객체 제공
    return_properties=["breed"],
    limit=1,
)

base64 표현으로 검색

이미지의 base64 표현으로도 검색할 수 있어요.

base64_string="SOME_BASE_64_REPRESENTATION"

dogs = client.collections.use("Dog")
response = dogs.query.near_image(
    near_image=base64_string,
    return_properties=["breed"],
    limit=1,
)

온라인 이미지의 base64 표현 만들기

온라인 이미지를 base64로 변환해 유사도 검색 입력으로 사용할 수 있습니다. 위에서 본 방식 그대로예요.

import base64, requests

def url_to_base64(url):
    image_response = requests.get(url)
    content = image_response.content
    return base64.b64encode(content).decode("utf-8")

base64_img = url_to_base64("https://.../image.jpg")

최대 거리(distance) 설정

반환할 객체의 유사도 임계값을 distance로 정할 수 있고, 메타데이터로 원본 이미지와의 거리를 함께 반환받을 수 있어요.

from pathlib import Path
from weaviate.classes.query import MetadataQuery

response = dogs.query.near_image(
    near_image=Path("./images/search-image.jpg"),
    distance=0.8,                            # 최대 허용 거리
    return_metadata=MetadataQuery(distance=True),
    return_properties=["breed"],
    limit=5
)

다른 연산자와 결합

Near Image 검색은 다른 유사도 검색 연산자와 마찬가지로 필터, limit 등 이미지 검색 하이라이트를 제외한 어떤 다른 연산자와도 결합할 수 있습니다.

더 알아보기 (Learn more)