Java Code Examples for org.opencv.utils.Converters#vector_float_to_Mat()

The following examples show how to use org.opencv.utils.Converters#vector_float_to_Mat() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RecognitionThread.java    From ml-authentication with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the recognized Student if the cosineSimilarity was above the threshold
 * @param featureVectorToRecognize
 * @return
 */
private synchronized List<Student> getMostSimilarStudentIfInThreshold(Mat featureVectorToRecognize){
    List<StudentImageCollectionEvent> studentImageCollectionEvents = studentImageCollectionEventDao.queryBuilder().where(StudentImageCollectionEventDao.Properties.MeanFeatureVector.isNotNull()).list();
    List<Student> studentsInThreshold = new ArrayList<>();
    for (StudentImageCollectionEvent studentImageCollectionEvent : studentImageCollectionEvents){
        Student currentStudent = studentImageCollectionEvent.getStudent();
        // Skip if the students are identical (same UniqueId)
        if (!areStudentsIdentical(currentStudent)){
            List<Float> featureVectorList = gson.fromJson(studentImageCollectionEvent.getMeanFeatureVector(), new TypeToken<List<Float>>(){}.getType());
            Mat featureVector = Converters.vector_float_to_Mat(featureVectorList);
            double dotProduct = featureVector.dot(featureVectorToRecognize);
            double normFeatureVector = Core.norm(featureVector, Core.NORM_L2);
            double normFeatureVectorToRecognize = Core.norm(featureVectorToRecognize, Core.NORM_L2);
            double cosineSimilarity = dotProduct / (normFeatureVector * normFeatureVectorToRecognize);
            double absoluteCosineSimilarity = Math.abs(cosineSimilarity);
            Log.i(getClass().getName(), "getMostSimilarStudentIfInThreshold: absoluteCosineSimilarity: " + absoluteCosineSimilarity + " with Student: " + currentStudent.getUniqueId());
            if (absoluteCosineSimilarity > SIMILARITY_THRESHOLD){
                studentsInThreshold.add(currentStudent);
            }
        } else {
            Log.i(getClass().getName(), "getMostSimilarStudentIfInThreshold: currentStudent: " + currentStudent.getUniqueId() + " was skipped because it is identical with the student: " + student.getUniqueId());
        }
    }
    return studentsInThreshold;
}
 
Example 2
Source File: RecognitionThread.java    From ml-authentication with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the recognized Student if the cosineSimilarity was above the threshold
 * @param featureVectorToRecognize
 * @return
 */
private synchronized List<Student> getMostSimilarStudentIfInThreshold(Mat featureVectorToRecognize){
    List<StudentImageCollectionEvent> studentImageCollectionEvents = studentImageCollectionEventDao.queryBuilder().where(StudentImageCollectionEventDao.Properties.MeanFeatureVector.isNotNull()).list();
    List<Student> studentsInThreshold = new ArrayList<>();
    for (StudentImageCollectionEvent studentImageCollectionEvent : studentImageCollectionEvents){
        Student currentStudent = studentImageCollectionEvent.getStudent();
        // Skip if the students are identical (same UniqueId)
        if (!areStudentsIdentical(currentStudent)){
            List<Float> featureVectorList = gson.fromJson(studentImageCollectionEvent.getMeanFeatureVector(), new TypeToken<List<Float>>(){}.getType());
            Mat featureVector = Converters.vector_float_to_Mat(featureVectorList);
            double dotProduct = featureVector.dot(featureVectorToRecognize);
            double normFeatureVector = Core.norm(featureVector, Core.NORM_L2);
            double normFeatureVectorToRecognize = Core.norm(featureVectorToRecognize, Core.NORM_L2);
            double cosineSimilarity = dotProduct / (normFeatureVector * normFeatureVectorToRecognize);
            double absoluteCosineSimilarity = Math.abs(cosineSimilarity);
            Log.i(getClass().getName(), "getMostSimilarStudentIfInThreshold: absoluteCosineSimilarity: " + absoluteCosineSimilarity + " with Student: " + currentStudent.getUniqueId());
            if (absoluteCosineSimilarity > SIMILARITY_THRESHOLD){
                studentsInThreshold.add(currentStudent);
            }
        } else {
            Log.i(getClass().getName(), "getMostSimilarStudentIfInThreshold: currentStudent: " + currentStudent.getUniqueId() + " was skipped because it is identical with the student: " + student.getUniqueId());
        }
    }
    return studentsInThreshold;
}
 
Example 3
Source File: MergeThread.java    From ml-authentication with Apache License 2.0 5 votes vote down vote up
/**
 * Find similar students
 * Case 2: Student was added regularly but maybe on another tablet or due to some reason the authentication didn't recognize the student correctly in the numberOfTries
 * ---> Use the meanFeatureVector as input for the cosineSimilarityScore calculation
 * @param ppF
 * @param tensorFlow
 */
private synchronized void findSimilarStudentsUsingMeanFeatureVector(PreProcessorFactory ppF, TensorFlow tensorFlow){
    Log.i(getClass().getName(), "findSimilarStudentsUsingMeanFeatureVector");
    // Iterate through all StudentImageCollectionEvents, where the Student is not null
    List<StudentImageCollectionEvent> studentImageCollectionEvents = studentImageCollectionEventDao.queryBuilder().where(StudentImageCollectionEventDao.Properties.StudentId.notEq(0)).list();
    Log.i(getClass().getName(), "studentImageCollectionEvents.size(): " + studentImageCollectionEvents.size());
    for (StudentImageCollectionEvent studentImageCollectionEvent : studentImageCollectionEvents){
        Student student = studentImageCollectionEvent.getStudent();
        // Take the meanFeatureVector of the StudentImageCollectionEvent
        List<Float> meanFeatureVectorList = gson.fromJson(studentImageCollectionEvent.getMeanFeatureVector(), new TypeToken<List<Float>>(){}.getType());
        Mat meanFeatureVector = Converters.vector_float_to_Mat(meanFeatureVectorList);
        RecognitionThread recognitionThread = new RecognitionThread(tensorFlow, studentImageCollectionEventDao);
        recognitionThread.setImg(meanFeatureVector);
        recognitionThread.setStudent(student);
        // To indicate, that this Mat object contains the already extracted features and therefore this step can be skipped in the RecognitionThread
        recognitionThread.setFeaturesAlreadyExtracted(true);
        Log.i(getClass().getName(), "findSimilarStudentsUsingMeanFeatureVector: recognitionThread will be started to recognize student: " + student.getUniqueId());
        recognitionThread.start();
        try {
            recognitionThread.join();
            List<Student> recognizedStudents = recognitionThread.getRecognizedStudent();
            if (recognizedStudents.size() > 0){
                for (Student recognizedStudent : recognizedStudents){
                    if (recognizedStudent != null){
                        Log.i(getClass().getName(), "findSimilarStudentsUsingMeanFeatureVector: The student " + student.getUniqueId() + " has been recognized as " + recognizedStudent.getUniqueId());
                        mergeSimilarStudents(student, recognizedStudent);
                    }
                }
            } else {
                Log.i(getClass().getName(), "findSimilarStudentsUsingMeanFeatureVector: The student " + student.getUniqueId() + " was not recognized");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
 
Example 4
Source File: TrainingThread.java    From ml-authentication with Apache License 2.0 5 votes vote down vote up
/**
 * Calculate the meanFeatureVector for each StudentImageCollectionEvent using the extracted featureVectors
 */
public synchronized void trainClassifier(){
    Log.i(getClass().getName(), "trainClassifier");
    // Initiate training if a StudentImageCollectionEvent has not been trained yet but all corresponding StudentImage's features have been extracted
    List<StudentImageCollectionEvent> studentImageCollectionEvents = studentImageCollectionEventDao.queryBuilder().where(StudentImageCollectionEventDao.Properties.MeanFeatureVector.isNull()).list();
    Log.i(getClass().getName(), "Count of StudentImageCollectionEvents where MeanFeatureVector is null: " + studentImageCollectionEvents.size());
    if (studentImageCollectionEvents.size() > 0){
        for (StudentImageCollectionEvent studentImageCollectionEvent : studentImageCollectionEvents){
            Long studentImagesWithoutExtractedFeatures = studentImageDao.queryBuilder()
                    .where(StudentImageDao.Properties.StudentImageCollectionEventId.eq(studentImageCollectionEvent.getId()))
                    .where(StudentImageDao.Properties.StudentImageFeatureId.eq(0))
                    .count();
            // Skip calculation of meanFeatureVector if not all features have been extracted yet
            if (studentImagesWithoutExtractedFeatures == 0){
                Mat allFeatureVectors = new Mat();
                List<StudentImage> studentImages = studentImageCollectionEvent.getStudentImages();
                for (StudentImage studentImage : studentImages){
                    List<Float> featureVectorList = gson.fromJson(studentImage.getStudentImageFeature().getFeatureVector(), new TypeToken<List<Float>>(){}.getType());
                    Mat featureVector = Converters.vector_float_to_Mat(featureVectorList);
                    allFeatureVectors.push_back(featureVector.reshape(1, 1));
                }

                Mat meanFeatureVector = new Mat();
                Core.reduce(allFeatureVectors, meanFeatureVector, 0, Core.REDUCE_AVG);
                List<Float> meanFeatureVectorList = new ArrayList<>();
                Converters.Mat_to_vector_float(meanFeatureVector.reshape(1, meanFeatureVector.cols()), meanFeatureVectorList);
                String meanFeatureVectorString = gson.toJson(meanFeatureVectorList);
                studentImageCollectionEvent.setMeanFeatureVector(meanFeatureVectorString);

                Student student = createStudent(studentImages);

                studentImageCollectionEvent.setStudent(student);
                studentImageCollectionEventDao.update(studentImageCollectionEvent);
                Log.i(getClass().getName(), "StudentImageCollectionEvent with Id " + studentImageCollectionEvent.getId() + " has been trained in classifier");
            } else {
                Log.i(getClass().getName(), "trainClassifier: Calculation of meanFeatureVector has been skipped for the StudentImageCollectionEvent: " + studentImageCollectionEvent.getId() + " studentImagesWithoutExtractedFeatures: " + studentImagesWithoutExtractedFeatures);
            }
        }
    }
}
 
Example 5
Source File: TensorFlow.java    From Android-Face-Recognition-with-Deep-Learning-Library with Apache License 2.0 5 votes vote down vote up
public Mat getFeatureVector(Mat img){
    Imgproc.resize(img, img, new Size(inputSize, inputSize));

    inferenceInterface.feed(inputLayer, getPixels(img), 1, inputSize, inputSize, channels);
    inferenceInterface.run(new String[]{outputLayer}, logStats);
    float[] outputs = new float[outputSize];
    inferenceInterface.fetch(outputLayer, outputs);

    List<Float> fVector = new ArrayList<>();
    for(float o : outputs){
        fVector.add(o);
    }

    return Converters.vector_float_to_Mat(fVector);
}
 
Example 6
Source File: Caffe.java    From Android-Face-Recognition-with-Deep-Learning-Library with Apache License 2.0 5 votes vote down vote up
public Mat getFeatureVector(Mat img){
    float[][] vector = caffe.getRepresentationLayer(saveMatToImage(img), layer);

    List<Float> fVector = new ArrayList<>();
    for(float f : vector[0]){
        fVector.add(f);
    }

    return Converters.vector_float_to_Mat(fVector);
}
 
Example 7
Source File: MergeThread.java    From ml-authentication with Apache License 2.0 5 votes vote down vote up
/**
 * Find similar students
 * Case 2: Student was added regularly but maybe on another tablet or due to some reason the authentication didn't recognize the student correctly in the numberOfTries
 * ---> Use the meanFeatureVector as input for the cosineSimilarityScore calculation
 * @param ppF
 * @param tensorFlow
 */
private synchronized void findSimilarStudentsUsingMeanFeatureVector(PreProcessorFactory ppF, TensorFlow tensorFlow){
    Log.i(getClass().getName(), "findSimilarStudentsUsingMeanFeatureVector");
    // Iterate through all StudentImageCollectionEvents, where the Student is not null
    List<StudentImageCollectionEvent> studentImageCollectionEvents = studentImageCollectionEventDao.queryBuilder().where(StudentImageCollectionEventDao.Properties.StudentId.notEq(0)).list();
    Log.i(getClass().getName(), "studentImageCollectionEvents.size(): " + studentImageCollectionEvents.size());
    for (StudentImageCollectionEvent studentImageCollectionEvent : studentImageCollectionEvents){
        Student student = studentImageCollectionEvent.getStudent();
        // Take the meanFeatureVector of the StudentImageCollectionEvent
        List<Float> meanFeatureVectorList = gson.fromJson(studentImageCollectionEvent.getMeanFeatureVector(), new TypeToken<List<Float>>(){}.getType());
        Mat meanFeatureVector = Converters.vector_float_to_Mat(meanFeatureVectorList);
        RecognitionThread recognitionThread = new RecognitionThread(tensorFlow, studentImageCollectionEventDao);
        recognitionThread.setImg(meanFeatureVector);
        recognitionThread.setStudent(student);
        // To indicate, that this Mat object contains the already extracted features and therefore this step can be skipped in the RecognitionThread
        recognitionThread.setFeaturesAlreadyExtracted(true);
        Log.i(getClass().getName(), "findSimilarStudentsUsingMeanFeatureVector: recognitionThread will be started to recognize student: " + student.getUniqueId());
        recognitionThread.start();
        try {
            recognitionThread.join();
            List<Student> recognizedStudents = recognitionThread.getRecognizedStudent();
            if (recognizedStudents.size() > 0){
                for (Student recognizedStudent : recognizedStudents){
                    if (recognizedStudent != null){
                        Log.i(getClass().getName(), "findSimilarStudentsUsingMeanFeatureVector: The student " + student.getUniqueId() + " has been recognized as " + recognizedStudent.getUniqueId());
                        mergeSimilarStudents(student, recognizedStudent);
                    }
                }
            } else {
                Log.i(getClass().getName(), "findSimilarStudentsUsingMeanFeatureVector: The student " + student.getUniqueId() + " was not recognized");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
 
Example 8
Source File: TrainingThread.java    From ml-authentication with Apache License 2.0 5 votes vote down vote up
/**
 * Calculate the meanFeatureVector for each StudentImageCollectionEvent using the extracted featureVectors
 */
public synchronized void trainClassifier(){
    Log.i(getClass().getName(), "trainClassifier");
    // Initiate training if a StudentImageCollectionEvent has not been trained yet but all corresponding StudentImage's features have been extracted
    List<StudentImageCollectionEvent> studentImageCollectionEvents = studentImageCollectionEventDao.queryBuilder().where(StudentImageCollectionEventDao.Properties.MeanFeatureVector.isNull()).list();
    Log.i(getClass().getName(), "Count of StudentImageCollectionEvents where MeanFeatureVector is null: " + studentImageCollectionEvents.size());
    if (studentImageCollectionEvents.size() > 0){
        for (StudentImageCollectionEvent studentImageCollectionEvent : studentImageCollectionEvents){
            Long studentImagesWithoutExtractedFeatures = studentImageDao.queryBuilder()
                    .where(StudentImageDao.Properties.StudentImageCollectionEventId.eq(studentImageCollectionEvent.getId()))
                    .where(StudentImageDao.Properties.StudentImageFeatureId.eq(0))
                    .count();
            // Skip calculation of meanFeatureVector if not all features have been extracted yet
            if (studentImagesWithoutExtractedFeatures == 0){
                Mat allFeatureVectors = new Mat();
                List<StudentImage> studentImages = studentImageCollectionEvent.getStudentImages();
                for (StudentImage studentImage : studentImages){
                    List<Float> featureVectorList = gson.fromJson(studentImage.getStudentImageFeature().getFeatureVector(), new TypeToken<List<Float>>(){}.getType());
                    Mat featureVector = Converters.vector_float_to_Mat(featureVectorList);
                    allFeatureVectors.push_back(featureVector.reshape(1, 1));
                }

                Mat meanFeatureVector = new Mat();
                Core.reduce(allFeatureVectors, meanFeatureVector, 0, Core.REDUCE_AVG);
                List<Float> meanFeatureVectorList = new ArrayList<>();
                Converters.Mat_to_vector_float(meanFeatureVector.reshape(1, meanFeatureVector.cols()), meanFeatureVectorList);
                String meanFeatureVectorString = gson.toJson(meanFeatureVectorList);
                studentImageCollectionEvent.setMeanFeatureVector(meanFeatureVectorString);

                Student student = createStudent(studentImages);

                studentImageCollectionEvent.setStudent(student);
                studentImageCollectionEventDao.update(studentImageCollectionEvent);
                Log.i(getClass().getName(), "StudentImageCollectionEvent with Id " + studentImageCollectionEvent.getId() + " has been trained in classifier");
            } else {
                Log.i(getClass().getName(), "trainClassifier: Calculation of meanFeatureVector has been skipped for the StudentImageCollectionEvent: " + studentImageCollectionEvent.getId() + " studentImagesWithoutExtractedFeatures: " + studentImagesWithoutExtractedFeatures);
            }
        }
    }
}