GithubHelp home page GithubHelp logo

chen0040 / java-image-embedding Goto Github PK

View Code? Open in Web Editor NEW
1.0 2.0 2.0 48.89 MB

Image embedding in Java

License: MIT License

Java 100.00%
image-classification image-encoding embedding product-recommendation image-search

java-image-embedding's Introduction

java-image-embedding

Image embedding in Java

The current project attempts to develop a pure Java image encoder that can be used in pure Java or Android program. Such an image encoder can be used for image classification or image search, or image recommend-er.

The current project contains currently two deep learning networks:

  • inception resnet
  • cifar

Usage

Run image classifier in Java

The sample codes below shows how to use the cifar image classifier to predict the genres of image:

import com.github.chen0040.tensorflow.classifiers.models.cifar10.Cifar10ImageClassifier;
import com.github.chen0040.tensorflow.classifiers.utils.ResourceUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Demo {
    public static void main(String[] args) {
        InputStream inputStream = ResourceUtils.getInputStream("tf_models/cnn_cifar10.pb");
        Cifar10ImageClassifier classifier = new Cifar10ImageClassifier();
        classifier.load_model(inputStream);
        
        List<String> paths = getImageFiles();
        
        Collections.shuffle(paths);
        
        for (String path : paths) {
            System.out.println("Predicting " + path + " ...");
            File f = new File(path);
            String label = classifier.predict_image(f);
        
            System.out.println("Predicted: " + label);
        }
    }
}

The sample codes below shows how to use the resnet v2 image classifier to predict the genres of image:

import com.github.chen0040.tensorflow.classifiers.images.models.inception.InceptionImageClassifier;
import com.github.chen0040.tensorflow.classifiers.images.utils.ResourceUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Demo {
    public static void main(String[] args){
        InceptionImageClassifier classifier = new InceptionImageClassifier();
        classifier.load_model();
        
        List<String> paths = getImageFiles();
        
        Collections.shuffle(paths);
        
        for (String path : paths) {
            System.out.println("Predicting " + path + " ...");
            File f = new File(path);
            String label = classifier.predict_image(f);
        
            System.out.println("Predicted: " + label);
        }
    }
}

Extract features from image in Java

The sample codes below shows how to use the cifar image classifier to encode an image file into an float array:

import com.github.chen0040.tensorflow.classifiers.images.models.cifar10.Cifar10ImageClassifier;
import com.github.chen0040.tensorflow.classifiers.images.utils.ResourceUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Demo {
    public static void main(String[] args) {
        InputStream inputStream = ResourceUtils.getInputStream("tf_models/cnn_cifar10.pb");
        Cifar10ImageClassifier classifier = new Cifar10ImageClassifier();
        classifier.load_model(inputStream);
        
        List<String> paths = getImageFiles();
        
        Collections.shuffle(paths);
        
        for (String path : paths) {
            System.out.println("Encoding " + path + " ...");
            File f = new File(path);
            float[] encoded_image = classifier.encode_image(f);
        
            System.out.println("Encoded: " + Arrays.toString(encoded_image));
        }
    }
}

The sample codes below shows how to the resnet v2 image classifier to encode an image file into an float array:

import com.github.chen0040.tensorflow.classifiers.images.models.inception.InceptionImageClassifier;
import com.github.chen0040.tensorflow.classifiers.images.models.utils.ResourceUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class Demo {
    public static void main(String[] args) {
        InceptionImageClassifier classifier = new InceptionImageClassifier();
        classifier.load_model();
        
        List<String> paths = getImageFiles();
        
        Collections.shuffle(paths);
        
        for (String path : paths) {
            System.out.println("Encoding " + path + " ...");
            File f = new File(path);
            float[] encoded_image = classifier.encode_image(f);
        
            System.out.println("Encoded: " + Arrays.toString(encoded_image));
        }
    }
}

Image Search Engine

The sample codes below shows how to index and search for image file using the ImageSearchEngine class:

import com.github.chen0040.tensorflow.classifiers.images.utils.FileUtils;
import com.github.chen0040.tensorflow.search.models.ImageSearchEngine;
import com.github.chen0040.tensorflow.search.models.ImageSearchEngineInception;
import com.github.chen0040.tensorflow.search.models.ImageSearchEntry;

import java.io.File;
import java.util.List;

public class Demo {
    public static void main(String[] args){
        ImageSearchEngine searchEngine = new ImageSearchEngineInception();
        if(!searchEngine.loadIndexDbIfExists()) {
            searchEngine.indexAll(FileUtils.getImageFiles());
            searchEngine.saveIndexDb();
        }
        
        int pageIndex = 0;
        int pageSize = 20;
        boolean skipPerfectMatch = true;
        for(File f : FileUtils.getImageFiles()) {
            System.out.println("querying similar image to " + f.getName());
            List<ImageSearchEntry> result = searchEngine.query(f, pageIndex, pageSize, skipPerfectMatch);
            for(int i=0; i < result.size(); ++i){
                System.out.println("# " + i + ": " + result.get(i).getPath() + " (distSq: " + result.get(i).getDistanceSq() + ")");
            }
        }
    }
}

Image Recommend-er

The sample codes below shows how to recommend images based on user's image history using the KnnImageRecommender class:

import com.github.chen0040.tensorflow.classifiers.images.utils.FileUtils;
import com.github.chen0040.tensorflow.recommenders.models.ImageUserHistory;
import com.github.chen0040.tensorflow.recommenders.models.KnnImageRecommender;
import com.github.chen0040.tensorflow.search.models.ImageSearchEntry;

import java.util.Collections;
import java.util.List;

public class Demo {
    public static void main(String[] args){
        ImageUserHistory userHistory = new ImageUserHistory();
        
        List<String> imageFiles = FileUtils.getImageFilePaths();
        Collections.shuffle(imageFiles);
        
        for(int i=0; i < 40; ++i){
            String filePath = imageFiles.get(i);
            userHistory.logImage(filePath);
            try {
                Thread.sleep(100L);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        
        KnnImageRecommender recommender = new KnnImageRecommender();
        if(!recommender.loadIndexDbIfExists()) {
            recommender.indexAll(new File("image_samples").listFiles(a -> a.getAbsolutePath().toLowerCase().endsWith(".au")));
            recommender.saveIndexDb();
        }
        
        System.out.println(userHistory.head(10));
        
        int k = 10;
        List<ImageSearchEntry> result = recommender.recommends(userHistory.getHistory(), k);
        
        for(int i=0; i < result.size(); ++i){
            ImageSearchEntry entry = result.get(i);
            System.out.println("Search Result #" + (i+1) + ": " + entry.getPath());
        }
    }
}

java-image-embedding's People

Contributors

chen0040 avatar

Stargazers

 avatar

Watchers

 avatar  avatar

java-image-embedding's Issues

Hi, your project really helps me a lot. I just wonder what dataset you used for training?

I'm working on a project involving similarity search. I've calculated the Euclidean distance from the vectors to identify the closest one. However, sometimes the results don't seem correct. Therefore, I'd like to try using a different dataset to achieve more accurate results. It would be helpful if you could tell me the dataset you used for training. Thank you!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.