§
    l¬¸j³&  ã                  óÆ   — d dl mZ d dlZd dlmZ d dlmZ d dlZd dl	m
Z
 d dlmZ d dlmZmZ d dlmZ d d	lmZmZ d
ej        d<    G d„ d¦  «        Z G d„ d¦  «        ZdS )é    )ÚannotationsN)ÚPath)ÚAny)ÚImage)ÚIMG_FORMATS)ÚLOGGERÚTORCH_VERSION)Úcheck_requirements)Ú	TORCH_2_4Úselect_deviceÚTRUEÚKMP_DUPLICATE_LIB_OKc                  ó\   — e Zd ZdZdd„Zdd
„Zdd„Zedd„¦   «         Zd d„Z	d!d"d„Z
d#d„ZdS )$ÚVisualAISearchay  A semantic image search system that leverages OpenAI's CLIP for generating high-quality image and text embeddings
    and NumPy cosine similarity for fast similarity-based retrieval.

    This class aligns image and text embeddings in a shared semantic space, enabling users to search large collections
    of images using natural language queries with high accuracy and speed.

    Attributes:
        data (str): Directory containing images.
        device (str): Computation device, e.g., 'cpu' or 'cuda'.
        index_path (str): Path to the numpy file storing image embeddings.
        data_path_npy (str): Path to the numpy file storing image paths.
        data_dir (Path): Path object for the data directory.
        model: Loaded CLIP model.
        index (np.ndarray): L2-normalized image embeddings used for cosine similarity search.
        image_paths (list[str]): List of image file paths.

    Methods:
        extract_image_feature: Extract CLIP embedding from an image.
        extract_text_feature: Extract CLIP embedding from text.
        load_or_build_index: Load existing embeddings or build them from images.
        search: Perform semantic search for similar images.

    Examples:
        Initialize and search for images
        >>> searcher = VisualAISearch(data="path/to/images", device="cuda")
        >>> results = searcher.search("a cat sitting on a chair", k=10)
    Úkwargsr   ÚreturnÚNonec                óF  — t           sJ dt          › d�¦   «         ‚ddlm} d| _        d| _        t          |                     dd¦  «        ¦  «        | _        t          |                     d	d
¦  «        ¦  «        | _
        | j                             ¦   «         sPddlm} t          j        | j        › d|› d�¦  «         ddlm}  ||› d�dd¬¦  «         t          d¦  «        | _         |d| j
        ¬¦  «        | _        d| _        g | _        |                      ¦   «          dS )zLInitialize the VisualAISearch class with the embedding index and CLIP model.z1VisualAISearch requires torch>=2.4 (found torch==ú)r   )Úbuild_text_modelzembeddings.npyz	paths.npyÚdataÚimagesÚdeviceÚcpu)Ú
ASSETS_URLz( not found. Downloading images.zip from z/images.zip)Úsafe_downloadTé   )ÚurlÚunzipÚretryzclip:ViT-B/32)r   N)r   r	   Úultralytics.nn.text_modelr   Ú
index_pathÚdata_path_npyr   ÚgetÚdata_dirr   r   ÚexistsÚultralytics.utilsr   r   ÚwarningÚultralytics.utils.downloadsr   ÚmodelÚindexÚimage_pathsÚload_or_build_index)Úselfr   r   r   r   s        úw/root/private/project/pallet_detection_v2/.venv/lib/python3.11/site-packages/ultralytics/solutions/similarity_search.pyÚ__init__zVisualAISearch.__init__1   sG  € åÐ^Ð^Ð^ÍmÐ^Ð^Ð^Ñ^Ô^ˆyØ>Ð>Ð>Ð>Ð>Ð>à*ˆŒØ(ˆÔÝ˜VŸZšZ¨°Ñ9Ô9Ñ:Ô:ˆŒÝ# F§J¢J¨x¸Ñ$?Ô$?Ñ@Ô@ˆŒàŒ}×#Ò#Ñ%Ô%ð 	+Ø4Ð4Ð4Ð4Ð4Ð4åŒN˜dœmÐlÐlÐU_ÐlÐlÐlÑmÔmÐmØAÐAÐAÐAÐAÐAàˆM Ð8Ð8Ð8ÀÈAÐNÑNÔNÐNÝ  ™NœNˆDŒMà%Ð% o¸d¼kÐJÑJÔJˆŒ
àˆŒ
ØˆÔà× Ò Ñ"Ô"Ð"Ð"Ð"ó    Úpathr   ú
np.ndarrayc                óÆ   — | j                              t          j        |¦  «        ¦  «                             ¦   «                              ¦   «                              ¦   «         S )z7Extract CLIP image embedding from the given image path.)r*   Úencode_imager   ÚopenÚdetachr   Únumpy)r.   r2   s     r/   Úextract_image_featurez$VisualAISearch.extract_image_featureK   sF   € àŒz×&Ò&¥u¤z°$Ñ'7Ô'7Ñ8Ô8×?Ò?ÑAÔA×EÒEÑGÔG×MÒMÑOÔOÐOr1   ÚtextÚstrc                óÔ   — | j                              | j                              |g¦  «        ¦  «                             ¦   «                              ¦   «                              ¦   «         S )z6Extract CLIP text embedding from the given text query.)r*   Úencode_textÚtokenizer7   r   r8   )r.   r:   s     r/   Úextract_text_featurez#VisualAISearch.extract_text_featureO   sN   € àŒz×%Ò% d¤j×&9Ò&9¸4¸&Ñ&AÔ&AÑBÔB×IÒIÑKÔK×OÒOÑQÔQ×WÒWÑYÔYÐYr1   Úxc                ór   — | t          j        t           j                             | dd¬¦  «        d¦  «        z  S )a  L2-normalize each row of `x` so inner products equal cosine similarity.

        Args:
            x (np.ndarray): Feature array of shape (N, D).

        Returns:
            (np.ndarray): Row-wise L2-normalized array with the same shape as the input.
        é   T)ÚaxisÚkeepdimsgê-�™—q=)ÚnpÚmaximumÚlinalgÚnorm©r@   s    r/   Ú
_normalizezVisualAISearch._normalizeS   s/   € ð •2”:�bœiŸnšn¨Q°QÀ˜nÑFÔFÈÑNÔNÑNÐNr1   c                ó¢  — t          | j        ¦  «                             ¦   «         rxt          | j        ¦  «                             ¦   «         rRt	          j        d¦  «         t          j        | j        ¦  «        | _        t          j        | j        ¦  «        | _	        dS t	          j        d¦  «         g }| j
                             ¦   «         D ]´}|j                             ¦   «                              d¦  «        t          vrŒ6	 |                     |                      |¦  «        ¦  «         | j	                             |j        ¦  «         Œ# t&          $ r)}t	          j        d|j        › d|› �¦  «         Y d}~Œ­d}~ww xY w|st+          d¦  «        ‚t          j        |¦  «                             d¦  «        }|                      |¦  «        | _        t          j        | j        | j        ¦  «         t          j        | j        t          j        | j	        ¦  «        ¦  «         t	          j        d	t7          | j	        ¦  «        › d
�¦  «         dS )ak  Load existing image embeddings or build them from the image directory.

        Checks if the embeddings and image paths exist on disk. If found, loads them directly. Otherwise, builds the
        index by extracting features from all images in the data directory, L2-normalizes them, and saves both the
        embeddings and image paths for future use.
        zLoading existing embeddings...Nz"Building embeddings from images...ú.z	Skipping z: z'No image embeddings could be generated.Úfloat32zIndexed z images.)r   r"   r&   r#   r   ÚinforE   Úloadr+   r,   r%   ÚiterdirÚsuffixÚlowerÚlstripr   Úappendr9   ÚnameÚ	Exceptionr(   ÚRuntimeErrorÚvstackÚastyperJ   ÚsaveÚarrayÚlen)r.   ÚvectorsÚfileÚes       r/   r-   z"VisualAISearch.load_or_build_index_   s   € õ �”Ñ Ô ×'Ò'Ñ)Ô)ð 	­d°4Ô3EÑ.FÔ.F×.MÒ.MÑ.OÔ.Oð 	ÝŒKÐ8Ñ9Ô9Ð9Ýœ ¤Ñ1Ô1ˆDŒJÝ!œw tÔ'9Ñ:Ô:ˆDÔØˆFõ 	ŒÐ8Ñ9Ô9Ð9Øˆð ”M×)Ò)Ñ+Ô+ð 		=ð 		=ˆDàŒ{× Ò Ñ"Ô"×)Ò)¨#Ñ.Ô.µkÐAÐAØð=à—’˜t×9Ò9¸$Ñ?Ô?Ñ@Ô@Ð@ØÔ ×'Ò'¨¬	Ñ2Ô2Ð2Ð2øÝð =ð =ð =Ý”Ð;¨4¬9Ð;Ð;¸Ð;Ð;Ñ<Ô<Ð<Ð<Ð<Ð<Ð<Ð<øøøøð=øøøð ð 	JÝÐHÑIÔIÐIå”)˜GÑ$Ô$×+Ò+¨IÑ6Ô6ˆØ—_’_ WÑ-Ô-ˆŒ
Ý
Œ�” ¤Ñ,Ô,Ð,Ý
Œ�Ô"¥B¤H¨TÔ-=Ñ$>Ô$>Ñ?Ô?Ð?åŒÐ>�s 4Ô#3Ñ4Ô4Ð>Ð>Ð>Ñ?Ô?Ð?Ð?Ð?s   ÄAEÅ
F ÅE;Å;F é   çš™™™™™¹?ÚqueryÚkÚintÚsimilarity_threshÚfloatú	list[str]c                óÚ  ‡ ‡‡	— ‰                       ‰                      |¦  «                             d¦  «        ¦  «        }‰ j        |d         z  Š	t	          j        ‰	¦  «        ddd…         dt          |d¦  «        …         }ˆ	ˆ ˆfd„|D ¦   «         }|                     d„ d¬¦  «         t          j	        d	¦  «         |D ] \  }}t          j	        d
|› d|d›�¦  «         Œ!d„ |D ¦   «         S )al  Return top-k semantically similar images to the given query.

        Args:
            query (str): Natural language text query to search for.
            k (int, optional): Maximum number of results to return.
            similarity_thresh (float, optional): Minimum similarity threshold for filtering results.

        Returns:
            (list[str]): List of image filenames ranked by similarity score.

        Examples:
            Search for images matching a query
            >>> searcher = VisualAISearch(data="images")
            >>> results = searcher.search("red car", k=5, similarity_thresh=0.2)
        rM   r   Néÿÿÿÿc                ól   •— g | ]0}‰|         ‰k    ¯‰j         |         t          ‰|         ¦  «        f‘Œ1S © )r,   rf   )Ú.0ÚiÚscoresr.   re   s     €€€r/   ú
<listcomp>z)VisualAISearch.search.<locals>.<listcomp>›   sB   ø€ ÐlÐlÐl¸qÈVÐTUÌYÐZkÒMkÐMk�DÔ$ QÔ'­¨v°a¬yÑ)9Ô)9Ð:ÐMkÐMkÐMkr1   c                ó   — | d         S )NrB   rk   rI   s    r/   ú<lambda>z'VisualAISearch.search.<locals>.<lambda>œ   s
   €  1 Q¤4€ r1   T)ÚkeyÚreversez
Ranked Results:z  - z | Similarity: z.4fc                ó   — g | ]
}|d          ‘ŒS )r   rk   )rl   Úrs     r/   ro   z)VisualAISearch.search.<locals>.<listcomp>¢   s   € Ð&Ð&Ð&˜��!”Ð&Ð&Ð&r1   )
rJ   r?   rY   r+   rE   ÚargsortÚmaxÚsortr   rN   )
r.   rb   rc   re   Ú	text_featÚtop_kÚresultsrU   Úscorern   s
   `  `     @r/   ÚsearchzVisualAISearch.searchˆ   s  øøø€ ð  —O’O D×$=Ò$=¸eÑ$DÔ$D×$KÒ$KÈIÑ$VÔ$VÑWÔWˆ	Ø”˜i¨œlÑ*ˆÝ”
˜6Ñ"Ô" 4 4 R 4Ô(¨­3¨q°!©9¬9¨Ô5ˆØlÐlÐlÐlÐlÐlÀEÐlÑlÔlˆØ�Š˜˜°ˆÑ6Ô6Ð6åŒÐ'Ñ(Ô(Ð(Ø"ð 	Að 	A‰KˆD�%ÝŒKÐ?˜tÐ?Ð?°EÐ?Ð?Ð?Ñ@Ô@Ð@Ð@à&Ð&˜gÐ&Ñ&Ô&Ð&r1   c                ó,   — |                       |¦  «        S )z.Direct call interface for the search function.)r}   )r.   rb   s     r/   Ú__call__zVisualAISearch.__call__¤   s   € à�{Š{˜5Ñ!Ô!Ð!r1   N)r   r   r   r   )r2   r   r   r3   )r:   r;   r   r3   )r@   r3   r   r3   )r   r   )r`   ra   )rb   r;   rc   rd   re   rf   r   rg   )rb   r;   r   rg   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__r0   r9   r?   ÚstaticmethodrJ   r-   r}   r   rk   r1   r/   r   r      sÌ   € € € € € ðð ð8#ð #ð #ð #ð4Pð Pð Pð PðZð Zð Zð Zð ð	Oð 	Oð 	Oñ „\ð	Oð'@ð '@ð '@ð '@ðR'ð 'ð 'ð 'ð 'ð8"ð "ð "ð "ð "ð "r1   r   c                  ó.   — e Zd ZdZddd
„Zdd„Zddd„ZdS )Ú	SearchAppa  A Flask-based web interface for semantic image search with natural language queries.

    This class provides a clean, responsive frontend that enables users to input natural language queries and instantly
    view the most relevant images retrieved from the indexed database.

    Attributes:
        render_template: Flask template rendering function.
        request: Flask request object.
        searcher (VisualAISearch): Instance of the VisualAISearch class.
        app (Flask): Flask application instance.

    Methods:
        index: Process user queries and display search results.
        run: Start the Flask web application.

    Examples:
        Start a search application
        >>> app = SearchApp(data="path/to/images", device="cuda")
        >>> app.run(debug=True)
    r   Nr   r;   r   ú
str | Noner   r   c                ó8  — t          d¦  «         ddlm}m}m} || _        || _        t          ||¬¦  «        | _         |t          dt          |¦  «         	                    ¦   «         d¬¦  «        | _
        | j
                             d| j        d	d
g¬¦  «         dS )zýInitialize the SearchApp with VisualAISearch backend.

        Args:
            data (str, optional): Path to directory containing images to index and search.
            device (str, optional): Device to run inference on (e.g. 'cpu', 'cuda').
        zflask>=3.0.1r   )ÚFlaskÚrender_templateÚrequest)r   r   Ú	templatesz/images)Útemplate_folderÚstatic_folderÚstatic_url_pathú/ÚGETÚPOST)Ú	view_funcÚmethodsN)r
   Úflaskr‰   rŠ   r‹   r   Úsearcherr€   r   ÚresolveÚappÚadd_url_ruler+   )r.   r   r   r‰   rŠ   r‹   s         r/   r0   zSearchApp.__init__¿   s´   € õ 	˜>Ñ*Ô*Ð*Ø9Ð9Ð9Ð9Ð9Ð9Ð9Ð9Ð9Ð9à.ˆÔØˆŒÝ&¨D¸Ð@Ñ@Ô@ˆŒØ�5ÝØ'Ý˜t™*œ*×,Ò,Ñ.Ô.Ø%ð	
ñ 
ô 
ˆŒð 	Œ×Ò˜c¨T¬ZÀ%ÈÀÐÑQÔQÐQÐQÐQr1   c                óâ   — g }| j         j        dk    rG| j         j                             dd¦  «                             ¦   «         }|                      |¦  «        }|                      d|¬¦  «        S )zCProcess user query and display search results in the web interface.r’   rb   Ú zsimilarity-search.html)r{   )r‹   ÚmethodÚformr$   Ústripr–   rŠ   )r.   r{   rb   s      r/   r+   zSearchApp.indexÔ   sh   € àˆØŒ<Ô &Ò(Ð(Ø”LÔ%×)Ò)¨'°2Ñ6Ô6×<Ò<Ñ>Ô>ˆEØ—m’m EÑ*Ô*ˆGØ×#Ò#Ð$<ÀgÐ#ÑNÔNÐNr1   FÚdebugÚboolc                ó<   — | j                              |¬¦  «         dS )z'Start the Flask web application server.)rŸ   N)r˜   Úrun)r.   rŸ   s     r/   r¢   zSearchApp.runÜ   s   € àŒ�Š˜5ˆÑ!Ô!Ð!Ð!Ð!r1   )r   N)r   r;   r   r‡   r   r   )r   r;   )F)rŸ   r    r   r   )r€   r�   r‚   rƒ   r0   r+   r¢   rk   r1   r/   r†   r†   ©   sm   € € € € € ðð ð*Rð Rð Rð Rð Rð*Oð Oð Oð Oð"ð "ð "ð "ð "ð "ð "r1   r†   )Ú
__future__r   ÚosÚpathlibr   Útypingr   r8   rE   ÚPILr   Úultralytics.data.utilsr   r'   r   r	   Úultralytics.utils.checksr
   Úultralytics.utils.torch_utilsr   r   Úenvironr   r†   rk   r1   r/   ú<module>r¬      s(  ðð #Ð "Ð "Ð "Ð "Ð "à 	€	€	€	Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð à Ð Ð Ð Ø Ð Ð Ð Ð Ð à .Ð .Ð .Ð .Ð .Ð .Ø 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ð 3Ø 7Ð 7Ð 7Ð 7Ð 7Ð 7Ø BÐ BÐ BÐ BÐ BÐ BÐ BÐ Bà%+€„
Ð!Ñ "ðR"ð R"ð R"ð R"ð R"ñ R"ô R"ð R"ðj5"ð 5"ð 5"ð 5"ð 5"ñ 5"ô 5"ð 5"ð 5"ð 5"r1   