⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filesystemretrieval.java

📁 基于MPEG 7 标准,符合未来语义网架构,很值得参考
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/*
 * This file is part of Caliph & Emir.
 *
 * Caliph & Emir is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * Caliph & Emir is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Caliph & Emir; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 * Copyright statement:
 * --------------------
 * (c) 2002-2005 by Mathias Lux (mathias@juggle.at)
 * http://www.juggle.at, http://caliph-emir.sourceforge.net
 */
package at.lux.fotoretrieval.retrievalengines;

import at.lux.fotoretrieval.FileOperations;
import at.lux.fotoretrieval.ResultListEntry;
import at.lux.fotoretrieval.RetrievalFrame;
import at.lux.fotoretrieval.RetrievalToolkit;
import at.lux.imageanalysis.ColorLayout;
import at.lux.imageanalysis.EdgeHistogram;
import at.lux.imageanalysis.ScalableColor;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;

import javax.swing.*;
import java.io.File;
import java.util.*;

/**
 * FileSystemRetrieval
 *
 * @author Mathias Lux, mathias@juggle.at
 */
public class FileSystemRetrieval extends AbstractRetrievalEngine {
    private static int MAX_COUNT = 50;
    private Vector matches1;

    public FileSystemRetrieval() {
        matches1 = new Vector();
    }

    public Vector getImagesByXPathSearch(String xPath, String whereToSearch,
                                         boolean recursive, JProgressBar progress) {

        Vector results = new Vector();
        try {
            if (progress != null)
                progress.setString("Finding MPEG-7 files ...");
            String[] descriptions = FileOperations.getAllDescriptions(new File(whereToSearch), recursive);
            double[] diffs = new double[descriptions.length];
            for (int i = 0; i < diffs.length; i++) {
                diffs[i] = -1.0;
            }
            SAXBuilder builder = new SAXBuilder();
            if (progress != null) {
                progress.setMinimum(0);
                progress.setMaximum(descriptions.length);
                progress.setValue(0);
                progress.setString("Searching for XPath ...");
            }
            double[] maxdiff = new double[MAX_COUNT];
            for (int i = 0; i < maxdiff.length; i++) {
                maxdiff[i] = -1.0;
            }
            for (int i = 0; i < descriptions.length; i++) {
                Element e = null;
                try {
                    e = builder.build(descriptions[i]).getRootElement();
                    java.util.List l = RetrievalToolkit.xpathQuery(e, xPath, null);
                    if (l.size() > 0) {
                        diffs[i] = 1.0 / (double) l.size();
                        if (i == MAX_COUNT) {
                            Arrays.sort(maxdiff);
                        }
                        if (i < MAX_COUNT) {
                            maxdiff[i] = diffs[i];
                            results.add(new ResultListEntry(diffs[i], e, descriptions[i]));
                        } else {
                            if (diffs[i] <= maxdiff[MAX_COUNT - 1] || maxdiff[MAX_COUNT - 1] < 0) {
                                results.add(new ResultListEntry(diffs[i], e, descriptions[i]));
                                maxdiff[MAX_COUNT - 1] = diffs[i];
                                for (int jj = MAX_COUNT - 1; jj > 0; jj--) {
                                    // bubble up :)
                                    if (maxdiff[jj - 1] > maxdiff[jj]) {
                                        double tmp = maxdiff[jj - 1];
                                        maxdiff[jj - 1] = maxdiff[jj];
                                        maxdiff[jj] = tmp;
                                    }
                                }
                            } else {
                                debug("Result omitted!");
                            }
                        }
                    }
                    l = null;
                    if (progress != null)
                        progress.setValue(i);
                } catch (JDOMException e1) {
                    debug("Error building document " + descriptions[i]);
                } catch (Exception e1) {
                    e1.printStackTrace();
                }
            }
            // setting up results:

            if (progress != null) {
                progress.setMinimum(0);
                progress.setMaximum(descriptions.length);
                progress.setValue(0);
                progress.setString("Sorting images");
            }
            // Sorting Vector ...
            Collections.sort(results, new Comparator() {
                public int compare(Object o1, Object o2) {
                    int retVal = 0;
                    double rel = ((ResultListEntry) o1).getRelevance() - ((ResultListEntry) o2).getRelevance();
                    if (rel < 0) retVal = -1;
                    if (rel > 0) retVal = 1;
                    return retVal;
                }
            });

        } catch (Exception e) {
            debug("Couldn't find descriptions ... " + e.getMessage());
            e.printStackTrace(System.out);
        }
        return results;
    }

    public Vector getSimilarImages(Element VisualDescriptor, String whereToSearch,
                                   boolean recursive, JProgressBar progress) {
        Vector results = new Vector();
        Namespace mpeg7 = Namespace.getNamespace("", "urn:mpeg:mpeg7:schema:2001");
        Namespace xsi = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
        String descType = VisualDescriptor.getAttributeValue("type", xsi);
        try {
            if (progress != null)
                progress.setString("Finding MPEG-7 files ...");
            String[] descriptions = FileOperations.getAllDescriptions(new File(whereToSearch), recursive);
            double[] diffs = new double[descriptions.length];
            for (int i = 0; i < diffs.length; i++) {
                diffs[i] = -1.0;
            }
            SAXBuilder builder = new SAXBuilder();
            if (progress != null) {
                progress.setMinimum(0);
                progress.setMaximum(descriptions.length);
                progress.setValue(0);
                progress.setString("Comparing images");
            }
            double[] maxdiff = new double[MAX_COUNT];
            for (int i = 0; i < maxdiff.length; i++) {
                maxdiff[i] = -1.0;
            }
            ScalableColor scc = null;
            for (int i = 0; i < descriptions.length; i++) {
                Element e = null;
                try {
                    e = builder.build(descriptions[i]).getRootElement();
                    java.util.List l = RetrievalToolkit.xpathQuery(e, "//VisualDescriptor", null);
                    if (l.size() > 0) {
//                        debug("Comparing elements ...");
                        if (descType.equals("ColorLayoutType")) {
//                            debug("Comparing ColorLayout ...");
                            Element toCompare = null;
                            for (Iterator iter = l.iterator(); iter.hasNext();) {
                                Element elem = (Element) iter.next();
                                if (elem.getAttributeValue("type", xsi).equals("ColorLayoutType")) {

                                    diffs[i] = ColorLayout.getSimilarity(VisualDescriptor, (Element) l.get(0));
                                }
                            }
                        } else if (descType.equals("EdgeHistogramType")) {
                            EdgeHistogram a = null;
                            try {
                                a = new EdgeHistogram(VisualDescriptor);
                            } catch (Exception e1) {
                                System.err.println("EdgeHistogram a is not an valid EdgeHistogram: " + e1.getMessage());
                            }
                            for (Iterator iter = l.iterator(); iter.hasNext();) {
                                Element elem = (Element) iter.next();
                                if (elem.getAttributeValue("type", xsi).equals("EdgeHistogramType")) {
                                    EdgeHistogram b = null;
                                    try {
                                        b = new EdgeHistogram(elem);
                                    } catch (Exception e1) {
                                        System.err.println("EdgeHistogram b is not an valid EdgeHistogram: " + e1.getMessage());
                                    }
                                    diffs[i] = (double) EdgeHistogram.getSimilarity(a.getHistogram(), b.getHistogram());
                                }
                            }
                        } else if (descType.equals("ScalableColorType")) {
//                            debug("Comparing ScalableColor ...");
                            String numC, numB;
                            numC = VisualDescriptor.getAttributeValue("numOfCoeff");
                            numB = VisualDescriptor.getAttributeValue("numOfBitplanesDiscarded");
                            for (Iterator iter = l.iterator(); iter.hasNext();) {
                                Element elem = (Element) iter.next();
                                if (elem.getAttributeValue("type", xsi).equals("ScalableColorType")) { // Ists der richtige D?
                                    if (numC.equals(elem.getAttributeValue("numOfCoeff"))
                                            && numB.equals(elem.getAttributeValue("numOfBitplanesDiscarded"))) { // Stimmen die Zahlen?
                                        if (!(scc != null)) {
                                            scc = new ScalableColor(VisualDescriptor);
                                        }
                                        diffs[i] = scc.getSimilarity(new ScalableColor(elem));
                                        debug(diffs[i] + " - " + descriptions[i]);
                                    } else {
                                        debug("Not matched (numC,numB): " + "(" + numC + "," + numB + ") to (" +
                                                elem.getAttributeValue("numOfCoeff") + "," +
                                                elem.getAttributeValue("numOfBitplanesDiscarded") + ")");
                                    }
                                }
                            }
                        }
                        if (diffs[i] > -1) {
                            if (i == MAX_COUNT) {
                                Arrays.sort(maxdiff);
                            }
                            if (i < MAX_COUNT) {
                                maxdiff[i] = diffs[i];
                                results.add(new ResultListEntry(diffs[i], e, descriptions[i]));
                            } else {
                                if (diffs[i] <= maxdiff[MAX_COUNT - 1] || maxdiff[MAX_COUNT - 1] < 0) {
                                    results.add(new ResultListEntry(diffs[i], e, descriptions[i]));
                                    maxdiff[MAX_COUNT - 1] = diffs[i];
                                    for (int jj = MAX_COUNT - 1; jj > 0; jj--) {
                                        // bubble up :)
                                        if (maxdiff[jj - 1] > maxdiff[jj]) {
                                            double tmp = maxdiff[jj - 1];
                                            maxdiff[jj - 1] = maxdiff[jj];
                                            maxdiff[jj] = tmp;
                                        }
                                    }
                                }
                            }
                        }
                    }
                    if (progress != null)
                        progress.setValue(i);
                } catch (JDOMException e1) {
                    debug("Error building document " + descriptions[i]);
                } catch (Exception e2) {
                    e2.printStackTrace();
                }
            }
            // setting up results:

            if (progress != null) {
                progress.setMinimum(0);
                progress.setMaximum(descriptions.length);
                progress.setValue(0);
                progress.setString("Sorting images");

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -