Search in sources :

Example 1 with ImageClassificationPanel

use of boofcv.gui.ImageClassificationPanel in project BoofCV by lessthanoptimal.

the class ExampleImageClassification method main.

public static void main(String[] args) throws IOException {
    // Test set 89.9% for 10 categories
    ClassifierAndSource cs = FactoryImageClassifier.vgg_cifar10();
    // ClassifierAndSource cs = FactoryImageClassifier.nin_imagenet(); // Test set 62.6% for 1000 categories
    File path = DeepBoofDataBaseOps.downloadModel(cs.getSource(), new File("download_data"));
    ImageClassifier<Planar<GrayF32>> classifier = cs.getClassifier();
    classifier.loadModel(path);
    List<String> categories = classifier.getCategories();
    String imagePath = UtilIO.pathExample("recognition/pixabay");
    List<File> images = Arrays.asList(UtilIO.findMatches(new File(imagePath), "\\w*.jpg"));
    Collections.sort(images);
    ImageClassificationPanel gui = new ImageClassificationPanel();
    ShowImages.showWindow(gui, "Image Classification", true);
    for (File f : images) {
        BufferedImage buffered = UtilImageIO.loadImage(f.getPath());
        if (buffered == null)
            throw new RuntimeException("Couldn't find input image");
        Planar<GrayF32> image = new Planar<>(GrayF32.class, buffered.getWidth(), buffered.getHeight(), 3);
        ConvertBufferedImage.convertFromPlanar(buffered, image, true, GrayF32.class);
        classifier.classify(image);
        // add image and results to the GUI for display
        gui.addImage(buffered, f.getName(), classifier.getAllResults(), categories);
    }
}
Also used : ImageClassificationPanel(boofcv.gui.ImageClassificationPanel) GrayF32(boofcv.struct.image.GrayF32) ClassifierAndSource(boofcv.factory.scene.ClassifierAndSource) Planar(boofcv.struct.image.Planar) File(java.io.File) BufferedImage(java.awt.image.BufferedImage) ConvertBufferedImage(boofcv.io.image.ConvertBufferedImage)

Aggregations

ClassifierAndSource (boofcv.factory.scene.ClassifierAndSource)1 ImageClassificationPanel (boofcv.gui.ImageClassificationPanel)1 ConvertBufferedImage (boofcv.io.image.ConvertBufferedImage)1 GrayF32 (boofcv.struct.image.GrayF32)1 Planar (boofcv.struct.image.Planar)1 BufferedImage (java.awt.image.BufferedImage)1 File (java.io.File)1