Search in sources :

Example 1 with ObjectClassifier

use of qupath.lib.classifiers.object.ObjectClassifier in project qupath by qupath.

the class QP method loadObjectClassifier.

/**
 * Load an object classifier for a project or file path.
 *
 * @param names the names of the classifier within the current project, or file paths to a classifier to load from disk.
 * 				If more than one name is provided, a composite classifier is created (applying each classifier in sequence).
 * @return the requested {@link ObjectClassifier}
 * @throws IllegalArgumentException if the classifier cannot be found
 */
public static ObjectClassifier<BufferedImage> loadObjectClassifier(String... names) throws IllegalArgumentException {
    var project = getProject();
    List<ObjectClassifier<BufferedImage>> classifiers = new ArrayList<>();
    for (String name : names) {
        ObjectClassifier<BufferedImage> classifier = null;
        Exception exception = null;
        if (project != null) {
            try {
                var objectClassifiers = project.getObjectClassifiers();
                if (objectClassifiers.contains(name))
                    classifier = objectClassifiers.get(name);
            } catch (Exception e) {
                exception = e;
                logger.debug("Object classifier '{}' not found in project", name);
            }
        }
        if (classifier == null) {
            try {
                var path = Paths.get(name);
                if (Files.exists(path))
                    classifier = ObjectClassifiers.readClassifier(path);
            } catch (Exception e) {
                exception = e;
                logger.debug("Object classifier '{}' cannot be read from file", name);
            }
        }
        if (classifier == null) {
            throw new IllegalArgumentException("Unable to find object classifier " + name, exception);
        }
        // Try to fix URIs, if we can
        if (classifier instanceof UriResource) {
            UriUpdater.fixUris((UriResource) classifier, project);
        }
        if (names.length == 1)
            return classifier;
        else
            classifiers.add(classifier);
    }
    return ObjectClassifiers.createCompositeClassifier(classifiers);
}
Also used : UriResource(qupath.lib.io.UriResource) PathObjectClassifier(qupath.lib.classifiers.PathObjectClassifier) ObjectClassifier(qupath.lib.classifiers.object.ObjectClassifier) ArrayList(java.util.ArrayList) BufferedImage(java.awt.image.BufferedImage) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) NoSuchElementException(java.util.NoSuchElementException)

Example 2 with ObjectClassifier

use of qupath.lib.classifiers.object.ObjectClassifier in project qupath by qupath.

the class QP method runObjectClassifier.

/**
 * Apply an object classifier to the specified {@link ImageData}.
 * This method throws an {@link IllegalArgumentException} if the classifier cannot be found.
 * @param imageData
 * @param names the name of the classifier within the current project, or file path to a classifier to load from disk.
 * 				If more than one name is provided, a composite classifier is created.
 * @throws IllegalArgumentException if the classifier cannot be found
 */
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void runObjectClassifier(ImageData imageData, String... names) throws IllegalArgumentException {
    if (names.length == 0) {
        logger.warn("Cannot run object classifier - no names provided!");
        return;
    }
    if (imageData == null) {
        logger.warn("Cannot run object classifier - no ImageData available!");
        return;
    }
    ObjectClassifier classifier = loadObjectClassifier(names);
    var pathObjects = classifier.getCompatibleObjects(imageData);
    if (classifier.classifyObjects(imageData, pathObjects, true) > 0)
        imageData.getHierarchy().fireObjectClassificationsChangedEvent(classifier, pathObjects);
}
Also used : PathObjectClassifier(qupath.lib.classifiers.PathObjectClassifier) ObjectClassifier(qupath.lib.classifiers.object.ObjectClassifier)

Aggregations

PathObjectClassifier (qupath.lib.classifiers.PathObjectClassifier)2 ObjectClassifier (qupath.lib.classifiers.object.ObjectClassifier)2 BufferedImage (java.awt.image.BufferedImage)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 NoSuchElementException (java.util.NoSuchElementException)1 UriResource (qupath.lib.io.UriResource)1