Reverse image search is a search for images that are similar to a given reference image.
In the GroupDocs.Search index, reverse image search allows you to search for similar images in ZIP archives, various documents, and individual files.
The image search is performed by comparing the perceptual hash of the reference image with the hashes of the images in the index. The idea of a perceptual hash is that for very similar images it has a value with a minimum number of different bits, and for very different images it gives a large number of different bits.
In the GroupDocs.Search engine, reverse image search, like full-text search, consists of two stages: the indexing stage and the actual search stage.
To search for images in the index, you must enable at least one of the flags in the options at the indexing stage:
enabled_for_separate_images is for indexing images in separate files.
enabled_for_embedded_images is for indexing images embedded in various documents.
enabled_for_container_item_images is for indexing images that are elements of container documents, such as ZIP archives, OST/PST storages.
For more information, see the Indexing options page.
The following options are available during the search stage:
hash_differences sets the maximum number of different bits in the hashes of found images. The hash is 64 bits long, so this value ranges from 0 to 64.
max_result_count sets the maximum number of found images.
search_document_filter sets the filter for found documents.
For more information, see the Image search options page.
A reference image is created with one of the overloads of the SearchImage.create method:
create(file_path) creates a reference image from an image file.
create(stream) creates a reference image from a stream containing an image, for example, a binary file object.
create(file_path, image_index) and create(stream, image_index) create a reference image from the specified frame (zero-based index) of a multipage image, for example a multipage TIFF image.
The search result is returned as an instance of the ImageSearchResult class. The image_count property returns the number of found images, and the get_found_image method returns a found image by its index. The found images with fewer different bits in the hash are returned first. Each found image is represented by an instance of the FoundImageFrame class, which has the following properties:
document_info returns information about the document containing the found image.
image_index returns the ordinal number of the image in the document.
frame_index returns the index of the frame in the image.
hash_differences returns the number of different bits in the hash of the found image.
The following code example demonstrates all stages of the reverse image search:
importosfromgroupdocs.searchimportIndexfromgroupdocs.search.commonimportSearchImagefromgroupdocs.search.optionsimportImageSearchOptions,IndexingOptions,SearchDocumentFilter# Creating an indexindex=Index("./index")# Setting the image indexing optionsindexing_options=IndexingOptions()indexing_options.image_indexing_options.enabled_for_container_item_images=Trueindexing_options.image_indexing_options.enabled_for_embedded_images=Trueindexing_options.image_indexing_options.enabled_for_separate_images=True# Indexing documents in a document folderindex.add("./documents",indexing_options)# Setting the image search optionsimage_search_options=ImageSearchOptions()image_search_options.hash_differences=10image_search_options.max_result_count=100image_search_options.search_document_filter=SearchDocumentFilter.create_file_extension(".zip",".png",".jpg")# Creating a reference image for searchsearch_image=SearchImage.create("./image.png")# Searching in the indexresult=index.search(search_image,image_search_options)print("Images found: "+str(result.image_count))foriinrange(result.image_count):image=result.get_found_image(i)document_info=image.document_infoname=os.path.basename(document_info.file_path)ifdocument_info.inner_path:name+=" / "+document_info.inner_path# An item of a container document, such as a ZIP archiveprint(name+", image "+str(image.image_index)+", frame "+str(image.frame_index)+", different bits: "+str(image.hash_differences))