Search for similar images with images (visual words)

Source: Internet
Author: User
Tags install matplotlib

Code address
    • Code GitHub Address
Preparing files
    • Vlfeat (open source implementation of SIFT):
    • The decompression will be vlfeat-0.9.20\bin\win32 added to the path path of the system in order to use the SIFT command at the command line
    • Install Pysqlite:pip install pysqlite
    • Install Matplotlib:pip isntall matplotlib
    • Install CherryPy (Lightweight Web server):pip install cherrypy
    • Photo Gallery (cat cat): 10000 cat pictures
Process
    • Extracting feature points for each picture with Vlfeat (Sift feature model)
    • To sample the feature points of each graph and cluster them into a visual word of a picture
    • That is, the visual word, is the representative of the corresponding picture
    • Create a database, put the visual word of each picture into the library, and build an index
    • Write CherryPy Web Apps, search for similar images based on clicked images
Extracting image feature points and generating Word files
    • Prerequisite to ensure that CMD executes the SIFT command
    • CMD executionStep1.py
#-*-Coding:utf-8-*-# step1.py: Extracts the feature points of a picture and generates a Word file vocabulary.pklImportPickleImportVocabularyImportImtoolsImportSift# imlist is a list of the name of the picture, the picture is placed under the static folderImlist = Imtools.get_imlist (' static/')# Total number of picturesNbr_images = Len (imlist)# Store the feature points of each picture in the corresponding. Sift signature fileFeatlist = [imlist[i][:-3]+' Sift '  forIinchRange (Nbr_images)] forIinchRange (nbr_images): Sift.process_image (Imlist[i], featlist[i])# Use K-means to train the corresponding words in the image feature file cluster# time relationship, only 46 images are used here, so only 46 words are createdVOC = vocabulary. Vocabulary (' Imagewords ') Voc.train (Featlist, $,Ten)# Save the words in Vocabulary.pkl withOpen' Vocabulary.pkl ',' WB ') asF:pickle.dump (VOC,F)# Print out the total number of wordsPrint ' vocabulary is: ', Voc.name, Voc.nbr_words
Store picture word information in a database
    • Database is SQLite
    • CMD execution:Step2.py
#-*-Coding:utf-8-*-# step2.py: According to the word file, the picture word into the SQLite databaseImportPickleImportSiftImportImagesearchImportImtools# List of picture namesImlist = Imlist = Imtools.get_imlist (' static/')# Number of picturesNbr_images = Len (imlist)# A list of image feature filesFeatlist = [imlist[i][:-3]+' Sift '  forIinchRange (Nbr_images)]# load Word file# put the word, picture name, address into the database images.db withOpen' Vocabulary.pkl ',' RB ') asF:VOC = Pickle.load (f) indx = Imagesearch. Indexer (' images.db ', VOC) indx.create_tables () forIinchRange (nbr_images): LOCS,DESCR = Sift.read_features_from_file (Featlist[i]) Indx.add_to_index (IMLIST[I],DESCR)# Submit the command to executeIndx.db_commit ()
Configure CherryPy Web Application
    • 15 images randomly displayed when I click the image
    • Click on one of the cat pictures to search for similar pictures to show out
    • The essence is to look for words that are similar to the one in the database according to the words of the clicked image, and display them.
    • CMD executionStep3.py
    • Browse in Browser:127.0.0.1:8080
#-*-Coding:utf-8-*-# step3.py: Use CherryPy to do interactive interface, display the resultImportCherryPy, OS, urllib, pickleImportImtools fromNumPyImport*ImportImagesearch# CherryPy Page# Web root directory is set in configuration file service.conf# The default port is 8080 class searchimage:     def __init__(self):        # Load Picture Name listSelf.imlist = Imtools.get_imlist (' static/') self.nbr_images = Len (self.imlist) self.ndx = Range (self.nbr_images)# Load the generated Word filef = open (' Vocabulary.pkl ',' RB ') SELF.VOC = Pickle.load (f) f.close ()# Set the number of pictures to start showingSelf.maxres = the        # Set the structure of the pageSelf.header ="" " <!doctype html>  "" "Self.footer ="" </body>  "" "    # Response to index page    # Randomly display images when no search is found    # When searching for a picture similar to that of the picture, depending on the visual word     def index(self,query=none):SELF.SRC = Imagesearch. Searcher (' images.db ', SELF.VOC) HTML = self.header html + ="" " <br/> Click an image to search.            <a href= '? query= ' > Random selection </a> of images. <br/><br/> "" "        ifQuery# Pictures showing the results of the queryres = self.src.query (query) [: Self.maxres] forDist,ndxinchRes:imname = Self.src.get_filename (ndx) HTML + ="<a href= '? query="+imname+">"HTML + ="+imname+"' width= '/>"HTML + ="</a>"        Else:# randomly displayed picturesRandom.shuffle (SELF.NDX) forIinchSelf.ndx[:self.maxres]: Imname = self.imlist[i] html + ="<a href= '? query="+imname+">"HTML + ="+imname+"' width= '/>"HTML + ="</a>"HTML + = Self.footerreturnHTML index.exposed =True# Launch AppCherrypy.quickstart (Searchimage (),'/', Os.path.join (Os.path.dirname (__file__),' service.conf '))
Effect
    • When not searching:

    • When you click Search:

Similar applications
    • Wine clicks
    • Baidu Knowledge Map
    • Tutu Search Clothes

Search for similar images with images (visual words)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.