To run a reverse image search, use the search_first method of the Searcher class. This method returns the first portion of results as a NetworkImageSearchResult object, which contains the images found in one shard.
To continue the search, the search_next method is used, returning the second and subsequent portions of results. Continue the search while the network_image_search_token property of the result is not None.
The search image object and search options object are the same as those used for single index searches. Images can be found only if image indexing was enabled when the documents were added.
The examples on this page use the master node of a running search network. The following code creates and starts the master node as described on the Search network deployment page; the slave nodes must be started beforehand. The examples that follow continue this code in the same Python process.
fromgroupdocs.search.commonimportFileLoggerfromgroupdocs.search.scalingimportSearchNetworkNodefromgroupdocs.search.scaling.configuringimportConfiguration,TcpSettingsbase_path="./SearchNetwork/"base_port=49100# Loading the search network configuration from a fileconfiguration=Configuration.load(base_path+"Configuration.xml")# Creating, configuring and starting the master node; the timeouts are specified in millisecondsnode=SearchNetworkNode.create_master_node(0,base_path+"Node0",TcpSettings(base_port,3000,3000,3000,1000),FileLogger(base_path+"Log.txt",4.0),configuration)node.configure_all_nodes()node.start()
The following code example, which continues the previous one, demonstrates performing a reverse image search on the search network.
fromgroupdocs.search.commonimportSearchImagefromgroupdocs.search.optionsimportImageSearchOptions# Creating a search image from a filesearch_image=SearchImage.create("./documents/Image.png")hash_differences=8# The maximum allowed difference between the image hashesprint("First image search")searcher=node.searcheroptions=ImageSearchOptions()options.hash_differences=hash_differencestotal=0result=searcher.search_first(search_image,options)print("Images found (shard %d): %d"%(result.shard_index,result.image_count))total+=result.image_countwhileresult.network_image_search_tokenisnotNone:print()print("Next image search")result=searcher.search_next(result.network_image_search_token)print("Images found (shard %d): %d"%(result.shard_index,result.image_count))total+=result.image_countprint()print("Total images found (diffs = %d): %d"%(hash_differences,total))