• 大小: 3.37MB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2023-10-23
  • 语言: 其他
  • 标签: Lire  Lucene  图片搜索  

资源简介

使用Lire来进行图片的搜索示例代码,包括创建索引和搜索。博客链接:https://blog.csdn.net/u010889616/article/details/81155701

资源截图

代码片段和文件信息

package com.nii.sim;


import net.semanticmetadata.lire.builders.DocumentBuilder;
import net.semanticmetadata.lire.builders.GlobalDocumentBuilder;
import net.semanticmetadata.lire.imageanalysis.features.global.CEDD;
import net.semanticmetadata.lire.searchers.GenericFastImageSearcher;
import net.semanticmetadata.lire.searchers.ImageSearchHits;
import net.semanticmetadata.lire.searchers.ImageSearcher;
import net.semanticmetadata.lire.utils.FileUtils;
import org.apache.lucene.analysis.core.WhitespaceAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.store.FSDirectory;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.Iterator;
import java.util.List;


/**
 * @author wzj
 * @create 2018-07-22 11:16
 **/
public class ImageSimilarityTest
{
    /**
     * 图片保存的路径
     */
    private static final String IMAGE_PATH = “H:\\JAVA\\ImageSim\\conf\\image“;

    /**
     * 索引保存目录
     */
    private static final String INDEX_PATH = “H:\\JAVA\\ImageSim\\conf\\index“;


    public static void main(String[] args) throws IOException
    {
        //indexImages();
        searchSimilarityImage();
    }

    private static void indexImages() throws IOException
    {
        List images = FileUtils.getAllImages(Paths.get(IMAGE_PATH).toFile() true);

        GlobalDocumentBuilder globalDocumentBuilder = new GlobalDocumentBuilder(false false);
        globalDocumentBuilder.addExtractor(CEDD.class);

        IndexWriterConfig conf = new IndexWriterConfig(new WhitespaceAnalyzer());
        IndexWriter indexWriter = new IndexWriter(FSDirectory.open(Paths.get(INDEX_PATH)) conf);

        for (Iterator it = images.iterator(); it.hasNext(); )
        {
            String imageFilePath = it.next();
            System.out.println(“Indexing “ + imageFilePath);

            BufferedImage img = ImageIO.read(new FileInputStream(imageFilePath));
            Document document = globalDocumentBuilder.createDocument(img imageFilePath);
            indexWriter.addDocument(document);
        }

        indexWriter.close();

        System.out.println(“Create index image successful.“);
    }

    private static void searchSimilarityImage() throws IOException
    {
        IndexReader ir = DirectoryReader.open(FSDirectory.open(Paths.get(INDEX_PATH)));
        ImageSearcher searcher = new GenericFastImageSearcher(5 CEDD.class);

        String inputImagePath = “H:\\JAVA\\ImageSim\\conf\\image\\5.png“;
        BufferedImage img = ImageIO.read(Paths.get(inputImagePath).toFile());

        ImageSearchHits hits = searcher.search(img ir);


        for (int i

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2018-07-22 16:59  ImageSim\
     文件         975  2018-07-22 15:14  ImageSim\build.gradle
     目录           0  2018-07-22 13:12  ImageSim\conf\
     目录           0  2018-07-22 15:20  ImageSim\conf\image\
     文件       23024  2018-07-22 11:08  ImageSim\conf\image\1.png
     文件        5172  2018-07-22 11:06  ImageSim\conf\image\10.png
     文件        5157  2018-07-22 11:07  ImageSim\conf\image\2.png
     文件       33769  2018-07-22 11:08  ImageSim\conf\image\3.png
     文件        6650  2018-07-22 11:08  ImageSim\conf\image\4.png
     文件       16975  2018-07-22 11:07  ImageSim\conf\image\5.png
     文件       10842  2018-07-22 11:06  ImageSim\conf\image\6.png
     文件       10594  2018-07-22 11:08  ImageSim\conf\image\7.png
     文件       24380  2018-07-22 11:06  ImageSim\conf\image\8.png
     文件       14227  2018-07-22 11:06  ImageSim\conf\image\9.png
     目录           0  2018-07-22 15:41  ImageSim\conf\index\
     文件         225  2018-07-22 15:41  ImageSim\conf\index\_0.cfe
     文件        1620  2018-07-22 15:41  ImageSim\conf\index\_0.cfs
     文件         363  2018-07-22 15:41  ImageSim\conf\index\_0.si
     文件         136  2018-07-22 15:41  ImageSim\conf\index\segments_1
     文件           0  2018-07-22 15:41  ImageSim\conf\index\write.lock
     目录           0  2018-07-22 13:12  ImageSim\gradle\
     目录           0  2018-07-21 22:47  ImageSim\gradle\wrapper\
     文件       53556  2018-07-17 21:42  ImageSim\gradle\wrapper\gradle-wrapper.jar
     文件         237  2018-07-17 21:42  ImageSim\gradle\wrapper\gradle-wrapper.properties
     文件        5046  2018-07-17 21:42  ImageSim\gradlew
     文件        2404  2018-07-17 21:42  ImageSim\gradlew.bat
     目录           0  2018-07-22 15:20  ImageSim\index\
     文件          71  2018-07-22 15:20  ImageSim\index\segments_1
     文件           0  2018-07-22 15:16  ImageSim\index\write.lock
     目录           0  2018-07-22 15:01  ImageSim\libs\
     文件     3366188  2018-07-22 15:00  ImageSim\libs\lire.jar
............此处省略12个文件信息

评论

共有 条评论