Changeset 33141 for other-projects


Ignore:
Timestamp:
2019-06-10T13:48:01+12:00 (5 years ago)
Author:
cpb16
Message:

Completed end-to-end pipeline and one classifier

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

Legend:

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

    r33110 r33141  
    11#https://www.tensorflow.org/install/lang_java
    2 #testClassifier runs the cmd below
    32#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
    43
    5 test-run-classifier01:
     4run-classifier-houghlinesP:
     5        #Compile
    66    javac javaAccuracyCalculator.java
    77    javac -cp /Scratch/cpb16/opencv-3.4.2/build/bin/opencv-342.jar javaImageClassifier.java;
    8     ./testClassifer.sh TestImages 1
     8        #Run javaImageClassifier on all images in TestImages
     9    ./runClassifer.sh TestImages houghlinesP
    910    echo "Displaying javaImageClassifier output"
     11        #Display output of javaImageClassifier
    1012    less log.txt
    11     java javaAccuracyCalculator log.txt 1 results.txt
     13        #Calculate the Accuracy of the classifier
     14    java javaAccuracyCalculator log.txt houghlinesP results.txt
     15        #Display output
    1216    less results.txt
    1317    rm log.txt
     18    rm results.txt
    1419
    1520
    16 image-run-classifier01:
    17     javac javaAccuracyCalculator.java
    18     javac -cp /Scratch/cpb16/opencv-3.4.2/build/bin/opencv-342.jar javaImageClassifier.java;
    19     ./testClassifer.sh ../download-images/BK 1
    20     echo "Displaying javaImageClassifier output"
    21     less log.txt
    22     java javaAccuracyCalculator log.txt 1 results.txt
    23     less results.txt
    24     rm log.txt
  • other-projects/is-sheet-music-encore/trunk/image-identification-terminal/javaAccuracyCalculator.java

    r33097 r33141  
    1 
    21import java.io.File;
    32import java.io.BufferedReader;
     
    54import java.io.FileReader;
    65import java.io.FileWriter;
    7 
    8 
    96public class javaAccuracyCalculator{
    10 
    11     public static void main(String[] args) {
    12        
     7                   
     8    //Read in log.txt, grab first item in array of each line
     9    //Counter for everytime have readline
     10    //Calculate accuracy rate
     11    public static void main(String[] args) {   
    1312        try {
    1413            if (args.length != 3) {
     
    2120                FileReader fileReader = new FileReader(inputFilename);
    2221                BufferedReader buf = new BufferedReader(fileReader);
    23                 FileWriter fw = new FileWriter(outputFilename);
     22                FileWriter fw = new FileWriter(outputFilename,true);
    2423                String line = null;
    2524                String[] item;
    26                 float accuracyRate = 0;
    27                 int trueCount   = 0;               
    28                 int lineCount   = 0;
    29                
    30                 //RENAME CLASSIFIER NUMBER TO ACTUALL CLASSIFIER
    31                 if(classifierType.equals("1")){classifierType = "Probabilistic HoughLines";}
    32                
     25                float trueAccuracyRate  =0;
     26                float falseAccuracyRate =0;
     27                int trueCount   = 0;
     28                int falseCount  = 0;               
     29                int sheetMusicCount = 0;
     30                int notSheetMusicCount  = 0;
    3331                //Splits into each record, since readLine splits by "\n"
    3432                while ((line = buf.readLine()) != null) {
    35                     lineCount++;
    36                     //Splits by tab, leaves "" for every blank entry
    37                     item = line.split("\t");
    38                     //Check first element (true or false)
    39                     if(item[0].equals("1")){
    40                         trueCount++;
     33                    item = line.split("\t");       
     34                    //Calculate AccuracyRates
     35                    if(item[1].contains("TestImages/SheetMusic/")){
     36                        sheetMusicCount++;
     37                        if(item[3].equals("true")){
     38                            trueCount++;
     39                        }
     40                    }
     41                    if(item[1].contains("TestImages/NotSheetMusic/")){
     42                        notSheetMusicCount++;
     43                        if(item[3].equals("true")){
     44                            falseCount++;
     45                        }
    4146                    }                       
    4247                }
    43                 accuracyRate = ((float)trueCount/(float)lineCount)*(float)100;
    44                 fw.write("Classifier:" + classifierType + "\t" + accuracyRate + "%" + "\n");       
     48               
     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;
     53               
     54                fw.write("Classifier: " + classifierType + '\t' + "SheetMusicAccuracyRate: " + trueAccuracyRate + "%" + '\t' + "NotSheetMusicAccuracyRate :" + falseAccuracyRate + "%" + "\n");     
    4555                buf.close();
    46                 fw.close();
    47                    
    48                 //Read in log.txt, grab first item in array of each line
    49                 //counter for everytime have readline
    50 
    51                 //Calculate accuracy rate
    52                                
     56                fw.close();                             
    5357            }
    5458        }
  • other-projects/is-sheet-music-encore/trunk/image-identification-terminal/javaImageClassifier.java

    r33097 r33141  
    2222
    2323
    24 //GOAL for 21st
     24//OUTPUT OF THIS JAVA PROGRAM FOUND IN log.txt
     25//Each image processed will have an output of
     26//True  =classifierType + 1 + Filename + Status
     27//False =classifierType + 0 + Filename + Status
     28public class javaImageClassifier{
     29    //Constants
     30    static int CLASSIFIER_HOUGHLINESP_MIN = 10;
     31    static int CLASSIFIER_HOUGHLINESP_MAX = 50;
    2532
    26 
    27 //Classifier 01
    28 //Have args so can call "java image-identification-classifier01 XX XX"
    29 //args can be parameters in algorthim such as threshold or theta?
    30 //Run on 5000 images.
    31 //Record success rates
    32 //All done with makefile
    33 
    34 
    35 //But first understand houghline transform
    36 //Know what the algorithm being used is doing.
    37 //MAke constants for this classifier
    38 //Make java be able to run on CMD line
    39 
    40 public class javaImageClassifier{
    41 
    42     public static void main(String[] args) {
    43        
     33    public static void main(String[] args) {   
    4434        try {
    4535            if (args.length != 2) {
     
    4737            }
    4838            else {
     39                Boolean result = null;
    4940                String imageFilename = args[0];
    50                 int classifierType = Integer.parseInt(args[1]);
    51                
    52                 String result = null;
     41                String classifierType = args[1];           
    5342                //Execute classifierType defined from arguement
    5443                switch(classifierType){
    55                 case 1:
    56                     result = classifier01(imageFilename);
     44                case "houghlinesP":
     45                    result = setup_HoughLineP(imageFilename); //true or false
    5746                    break;
    58                 case 2:
     47                default:
    5948                    System.out.println("unknown");
    6049                    break;
    61                 }
    62            
     50                }           
    6351                //Write output to disc
    6452                File log = new File("log.txt");
    6553                FileWriter fileWriter = new FileWriter(log, true);
    6654                BufferedWriter bw = new BufferedWriter(fileWriter);
    67                 bw.write(result + classifierType + '\n');
    68                 bw.close();
    69 
    70                
     55                //Split output by tab for processing in next java program
     56                //imageFilename = 1, result = 3, classifierType = 4
     57                bw.write("Filename:" + '\t' + imageFilename + '\t' + "Classified as:" + '\t' + result + '\t' + classifierType + '\n');
     58                bw.close();             
    7159            }
    7260        }
     
    7563        }
    7664    } 
    77     private static String classifier01(String filename){
     65    //Returns
     66    //True = 1 + Filename + Status
     67    //False= 0 + Filename + Status
     68    private static Boolean setup_HoughLineP(String filename){
    7869    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    7970    Boolean isSheetMusic = null;
     
    9788        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);
    9889        }
     90       
    9991        //Convert MAT into a BufferedImage
    10092        BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB);
    10193        //Calculate if its sheet music or not
    102         isSheetMusic = Classifier(toBeClassifiedImg); 
     94        isSheetMusic = classifier_HoughLineP(toBeClassifiedImg); 
    10395               
    10496        //Save Processed Image
     
    109101        processedFile = "proc_F_"+filename;
    110102        }
    111         imwrite(processedFile, edgesDetectedRGB);
    112        
    113          //Display Results
    114             //HighGui.imshow("Source", original);
    115             //HighGui.imshow("Just Edges", justEdges); //TESTING
    116             //HighGui.imshow("Detected Lines (in red) - Standard Hough Line Transform", edgesDetectedRGB);
    117             //HighGui.imshow("Detected Lines (in red) - Probabilistic Line Transform", edgesDetectedRGBProb);
    118 
    119             // Wait and Exit
    120             //HighGui.waitKey();
    121             //System.exit(0);
    122                    
     103        imwrite(processedFile, edgesDetectedRGB);                   
    123104    }
    124105    catch(Exception e){
    125106            System.err.println(e);
    126107        }
    127         if (isSheetMusic == true){   
    128             return (1 + "\t" + "Filename: " + filename + "  Status: " + isSheetMusic  +"\t" );}
    129        
    130         else{
    131             return (0 + "\t" + "Filename: " + filename + "  Status: " + isSheetMusic  +"\t" );}
    132     }
    133    
    134     private static boolean Classifier(BufferedImage img){
     108        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    } */
     116 }
     117 
     118 //******************
     119 //INTERNAL FUNCTIONS
     120 //******************
     121    private static boolean classifier_HoughLineP(BufferedImage img){
    135122    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
    136123    try {
    137124        //Read file
    138         //BufferedImage img = ImageIO.read(new File(processedFile));
    139125        int x = img.getWidth();
    140126        int y = img.getHeight();
     
    142128        int redCount    = 0;
    143129        float percentage = 0;
    144 
    145130        //Go Thru every pixel
    146131        for(int i=0; i < y; i++){
     
    157142        //Calculate percentage of Red in image
    158143        percentage = ((float)redCount/(float)pixelCount)*(float)100;
    159    
    160              
    161144        //If more than %10 and less than %50 then its sheet music!
    162         if(percentage > 10 && percentage < 50){ //MAKE THESE CONSTANTS!!
     145        if(percentage > 10 && percentage < 50){
    163146            return true;}
    164147        }
     
    168151        return false;
    169152    }
    170 
    171153        private static  BufferedImage toBufferedImage(Mat mat){
    172154        //MOSTLY COPY PASTE!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
     
    189171        }
    190172        return null;
    191     }
    192 
    193  
     173    }
    194174}
Note: See TracChangeset for help on using the changeset viewer.