Ignore:
Timestamp:
2019-06-20T20:42:56+12:00 (5 years ago)
Author:
cpb16
Message:

refined houghlineP alogirthm

Location:
other-projects/is-sheet-music-encore/trunk/image-identification-terminal
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • other-projects/is-sheet-music-encore/trunk/image-identification-terminal/Makefile

    r33141 r33170  
    22#java -cp /Scratch/cpb16/opencv-3.4.2/build/bin/opencv-342.jar:. -Djava.library.path=/Scratch/cpb16/opencv-3.4.2/build/lib/  javaImageClassifier TestImages/Test.png 1
    33
    4 run-classifier-houghlinesP:
     4testrun-classifier-houghlinesP:
    55        #Compile
    66    javac javaAccuracyCalculator.java
  • other-projects/is-sheet-music-encore/trunk/image-identification-terminal/javaAccuracyCalculator.java

    r33141 r33170  
    44import java.io.FileReader;
    55import java.io.FileWriter;
     6import java.util.*;
    67public class javaAccuracyCalculator{
    78                   
     
    1516            }
    1617            else {
     18                Date d = new Date();
    1719                String inputFilename = args[0];
    1820                String classifierType = args[1];
     
    2325                String line = null;
    2426                String[] item;
    25                 float trueAccuracyRate  =0;
    26                 float falseAccuracyRate =0;
    27                 int trueCount   = 0;
    28                 int falseCount  = 0;               
     27                float trueAccuracyRate   =0;
     28                float falseAccuracyRate  =0;
     29                float overallAccuracyRate=0;
     30                //
    2931                int sheetMusicCount = 0;
    3032                int notSheetMusicCount  = 0;
     33                //
     34                int truePositive    = 0;
     35                int trueNegative    = 0;
     36                //
     37                int falsePositive   = 0;
     38                int falseNegative   = 0;               
     39
    3140                //Splits into each record, since readLine splits by "\n"
    3241                while ((line = buf.readLine()) != null) {
    3342                    item = line.split("\t");       
    3443                    //Calculate AccuracyRates
    35                     if(item[1].contains("TestImages/SheetMusic/")){
     44                    if(item[1].contains("/SheetMusic/")){
    3645                        sheetMusicCount++;
    3746                        if(item[3].equals("true")){
    38                             trueCount++;
     47                            truePositive++;
     48                        }
     49                        else if(item[3].equals("false")){
     50                            falseNegative++;
     51                        }
     52                        else {
     53                            System.err.println("Error log file");
    3954                        }
    4055                    }
    41                     if(item[1].contains("TestImages/NotSheetMusic/")){
     56                    if(item[1].contains("/NotSheetMusic/")){
    4257                        notSheetMusicCount++;
    4358                        if(item[3].equals("true")){
    44                             falseCount++;
     59                            falsePositive++;
     60                        }
     61                        else if(item[3].equals("false")){
     62                            trueNegative++;
     63                        }
     64                        else{
     65                            System.err.println("Error log file");
    4566                        }
    4667                    }                       
    4768                }
    4869               
    49                 //Amount of sheetMusic classified images / total amount of images read
    50                 trueAccuracyRate = ((float)trueCount/(float)sheetMusicCount)*(float)100;
    51                 //Amount of notSheetMusic classified images / total amount of images read
    52                 falseAccuracyRate = ((float)falseCount/(float)notSheetMusicCount)*(float)100;
    5370               
    54                 fw.write("Classifier: " + classifierType + '\t' + "SheetMusicAccuracyRate: " + trueAccuracyRate + "%" + '\t' + "NotSheetMusicAccuracyRate :" + falseAccuracyRate + "%" + "\n");     
     71                //Correctly identified SheetMusic as SheetMusic
     72                trueAccuracyRate = ((float)truePositive/(float)sheetMusicCount)*(float)100;
     73
     74                //Amount of Correctly identified NotSheetSheetMusic as NotSheetMusic
     75                falseAccuracyRate = ((float)trueNegative/(float)notSheetMusicCount)*(float)100;
     76               
     77                overallAccuracyRate = (truePositive + trueNegative)/(float)(sheetMusicCount+notSheetMusicCount)*(float)100;
     78               
     79                fw.write("Date: " + d.toString() + '\n'
     80                    + "Classifier: " + classifierType + '\n'
     81                    + "truePositive: "  + truePositive + '\n'
     82                    + "falseNegative: " + falseNegative + '\n'
     83                    + "falsePositve: "  + falsePositive + '\n' 
     84                    + "trueNegative: "  + trueNegative + '\n'
     85                    + "SheetMusicAccuracyRate: "    + trueAccuracyRate + "%" + '\n'
     86                    + "NotSheetMusicAccuracyRate: " + falseAccuracyRate + "%" + '\n'
     87                    + "OverallAccuracyRate: "       + overallAccuracyRate + "%" + '\n' + '\n');     
    5588                buf.close();
    5689                fw.close();                             
  • other-projects/is-sheet-music-encore/trunk/image-identification-terminal/javaImageClassifier.java

    r33141 r33170  
    3333    public static void main(String[] args) {   
    3434        try {
    35             if (args.length != 2) {
    36             System.out.println("Usage: imageClassifier <inputFilename> <classifierType>");
     35            if (args.length != 3) {
     36            System.out.println("Usage: imageClassifier <inputFilename> <classifierType> <outputFilename>");
    3737            }
    3838            else {
    3939                Boolean result = null;
    4040                String imageFilename = args[0];
    41                 String classifierType = args[1];           
     41                String classifierType = args[1];
     42                String outputFilename = args[2];           
    4243                //Execute classifierType defined from arguement
    4344                switch(classifierType){
    4445                case "houghlinesP":
    45                     result = setup_HoughLineP(imageFilename); //true or false
     46                    result = setup_HoughLinesP(imageFilename); //true or false
     47                    break;
     48                case "houghlinesP-refined":
     49                    result = setup_HoughLinesP_refined(imageFilename);
    4650                    break;
    4751                default:
    48                     System.out.println("unknown");
     52                    System.out.println("unknown algorithm");
    4953                    break;
    5054                }           
    5155                //Write output to disc
    52                 File log = new File("log.txt");
     56                File log = new File(outputFilename);
    5357                FileWriter fileWriter = new FileWriter(log, true);
    5458                BufferedWriter bw = new BufferedWriter(fileWriter);
     
    6670    //True = 1 + Filename + Status
    6771    //False= 0 + Filename + Status
    68     private static Boolean setup_HoughLineP(String filename){
     72   
     73 //******************
     74 //CLASSIFIER FUNCTIONS
     75 //******************
     76    private static Boolean setup_HoughLinesP(String filename){
    6977    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    7078    Boolean isSheetMusic = null;
     
    8593        // Draw the lines
    8694        for (int x = 0; x < linesP.rows(); x++) {
    87         double[] l = linesP.get(x, 0);
    88         Imgproc.line(edgesDetectedRGB, new Point(l[0], l[1]), new Point(l[2], l[3]), new Scalar(0, 0, 255), 3, Imgproc.LINE_AA, 0);
     95            double[] l = linesP.get(x, 0);
     96            Imgproc.line(edgesDetectedRGB, new Point(l[0], l[1]), new Point(l[2], l[3]), new Scalar(0, 0, 255), 3, Imgproc.LINE_AA, 0);
    8997        }
    9098       
     
    92100        BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB);
    93101        //Calculate if its sheet music or not
    94         isSheetMusic = classifier_HoughLineP(toBeClassifiedImg); 
    95                
    96         //Save Processed Image
    97         String processedFile = filename;
    98         if (isSheetMusic == true) {
    99         processedFile = "proc_T_"+filename;
    100         }else {
    101         processedFile = "proc_F_"+filename;
    102         }
    103         imwrite(processedFile, edgesDetectedRGB);                   
     102        isSheetMusic = classifier_HoughLinesP(toBeClassifiedImg);               
    104103    }
    105104    catch(Exception e){
     
    107106        }
    108107        return isSheetMusic;
    109     /*   
    110     if (isSheetMusic == true){   
    111         return (1 + "\t" + "Filename: " + filename + "  Status: " + isSheetMusic  +"\t" );
    112     }   
    113     else{
    114         return (0 + "\t" + "Filename: " + filename + "  Status: " + isSheetMusic  +"\t" );
    115     } */
    116108 }
    117109 
     110     private static Boolean setup_HoughLinesP_refined(String filename){
     111    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
     112    Boolean isSheetMusic = null;
     113    try{
     114        //Variables
     115        Mat edgesDetected = new Mat();
     116        Mat edgesDetectedRGB = new Mat();
     117        Mat edgesDetectedRGBProb;
     118        // Load an image
     119        Mat original = Imgcodecs.imread(filename, Imgcodecs.IMREAD_GRAYSCALE);
     120        // Edge detection
     121        Imgproc.Canny(original, edgesDetected, 50, 200, 3, false);
     122        //Copy edges to the images that will display the results in BGR
     123        Imgproc.cvtColor(edgesDetected, edgesDetectedRGB, Imgproc.COLOR_GRAY2BGR);
     124        // Probabilistic Line Transform
     125        Mat linesP = new Mat(); // will hold the results of the detection
     126        double minLineLength = edgesDetectedRGB.size().width/4;
     127        Imgproc.HoughLinesP(edgesDetected, linesP, 1, Math.PI / 180, 50, minLineLength, 10);// runs the actual detection
     128        // Draw the lines
     129       
     130        for (int x = 0; x < linesP.rows(); x++) {
     131            double[] l = linesP.get(x, 0);
     132            //New angles
     133            Point p1 = new Point(l[0], l[1]);
     134            Point p2 = new Point(l[2], l[3]);
     135            double m = Math.abs(p2.y - p1.y)/(p2.x - p1.x);
     136            //System.out.println(l[0]);
     137            //System.out.println(l[1]);
     138            //System.out.println(l[2]);
     139            //System.out.println(l[3]);
     140            if(m<0.1) {
     141                //System.out.println("m: " + m);
     142                Imgproc.line(edgesDetectedRGB, new Point(l[0], l[1]), new Point(l[2], l[3]), new Scalar(0, 0, 255), 3, Imgproc.LINE_AA, 0);
     143            }
     144        }
     145       
     146        //Convert MAT into a BufferedImage
     147        BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB);
     148        //Calculate if its sheet music or not
     149        isSheetMusic = classifier_HoughLinesP(toBeClassifiedImg); 
     150           
     151    }
     152    catch(Exception e){
     153            System.err.println(e);
     154        }
     155        return isSheetMusic;
     156 }
     157 
    118158 //******************
    119159 //INTERNAL FUNCTIONS
    120160 //******************
    121     private static boolean classifier_HoughLineP(BufferedImage img){
     161    private static boolean classifier_HoughLinesP(BufferedImage img){
    122162    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    123163    try {
     
    143183        percentage = ((float)redCount/(float)pixelCount)*(float)100;
    144184        //If more than %10 and less than %50 then its sheet music!
    145         if(percentage > 10 && percentage < 50){
     185        if(percentage > CLASSIFIER_HOUGHLINESP_MIN && percentage < CLASSIFIER_HOUGHLINESP_MAX){
    146186            return true;}
    147187        }
  • other-projects/is-sheet-music-encore/trunk/image-identification-terminal/results.txt

    r33110 r33170  
    1 Classifier:Probabilistic HoughLines 30.08%
     1Date: Thu Jun 20 18:40:41 NZST 2019
     2Classifier: houghlinesP
     3truePositive: 3098
     4falseNegative: 63
     5falsePositve: 624
     6trueNegative: 1216
     7SheetMusicAccuracyRate: 98.00696%
     8NotSheetMusicAccuracyRate: 66.08695%
     9OverallAccuracyRate: 86.26274%
     10
     11Date: Thu Jun 20 18:40:42 NZST 2019
     12Classifier: houghlinesP-refined
     13truePositive: 1666
     14falseNegative: 1495
     15falsePositve: 123
     16trueNegative: 1717
     17SheetMusicAccuracyRate: 52.70484%
     18NotSheetMusicAccuracyRate: 93.315216%
     19OverallAccuracyRate: 67.64648%
     20
  • other-projects/is-sheet-music-encore/trunk/image-identification-terminal/runClassifer.sh

    r33141 r33170  
    11#!/bin/bash
    22
    3 if [ $# != 2 ] ; then
    4   echo "Usage: ./testClassifier.sh file_name classifier_type" 1>&2
     3if [ $# != 4 ] ; then
     4  echo "Usage: ./testClassifier.sh file_name classifier_type output_filename num_doc(-a = all -test = 100)" 1>&2
    55  exit 1
    66fi
     
    88folder_name=$1
    99classifier_type=$2
    10 #Runs javaImageClassifier on ALL images
    11 for file_name in $folder_name/*/*.png; do
    12     echo $file_name
    13     echo ""
    14     java -cp /Scratch/cpb16/opencv-3.4.2/build/bin/opencv-342.jar:. -Djava.library.path=/Scratch/cpb16/opencv-3.4.2/build/lib/  javaImageClassifier $file_name $classifier_type
    15 done
     10output_filename=$3
     11num_doc=$4
     12i=0
     13case "$4" in
     14    -all)
     15        #Runs javaImageClassifier on ALL images
     16        for file_name in $folder_name/*/*.png; do
     17            echo $file_name
     18            echo $output_filename
     19            echo $i
     20            echo ""
     21            java -cp /Scratch/cpb16/opencv-3.4.2/build/bin/opencv-342.jar:. -Djava.library.path=/Scratch/cpb16/opencv-3.4.2/build/lib/  javaImageClassifier $file_name $classifier_type $output_filename
     22            i=$[$i+1]
     23        done
     24        ;;
     25    -test)
     26        #Runs javaImageClassifier on 100 images
     27        for file_name in $folder_name/*/*.png; do
     28        if [ $i -lt 200 ] ; then
     29            echo $file_name
     30            echo $output_filename
     31            echo $i
     32            echo ""
     33            java -cp /Scratch/cpb16/opencv-3.4.2/build/bin/opencv-342.jar:. -Djava.library.path=/Scratch/cpb16/opencv-3.4.2/build/lib/  javaImageClassifier $file_name $classifier_type $output_filename
     34            i=$[$i+1]
     35        else
     36            exit 0
     37        fi     
     38        done
     39        ;;
     40esac       
Note: See TracChangeset for help on using the changeset viewer.