- Timestamp:
- 2019-06-20T20:42:56+12:00 (5 years ago)
- 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 2 2 #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 3 3 4 run-classifier-houghlinesP:4 testrun-classifier-houghlinesP: 5 5 #Compile 6 6 javac javaAccuracyCalculator.java -
other-projects/is-sheet-music-encore/trunk/image-identification-terminal/javaAccuracyCalculator.java
r33141 r33170 4 4 import java.io.FileReader; 5 5 import java.io.FileWriter; 6 import java.util.*; 6 7 public class javaAccuracyCalculator{ 7 8 … … 15 16 } 16 17 else { 18 Date d = new Date(); 17 19 String inputFilename = args[0]; 18 20 String classifierType = args[1]; … … 23 25 String line = null; 24 26 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 // 29 31 int sheetMusicCount = 0; 30 32 int notSheetMusicCount = 0; 33 // 34 int truePositive = 0; 35 int trueNegative = 0; 36 // 37 int falsePositive = 0; 38 int falseNegative = 0; 39 31 40 //Splits into each record, since readLine splits by "\n" 32 41 while ((line = buf.readLine()) != null) { 33 42 item = line.split("\t"); 34 43 //Calculate AccuracyRates 35 if(item[1].contains(" TestImages/SheetMusic/")){44 if(item[1].contains("/SheetMusic/")){ 36 45 sheetMusicCount++; 37 46 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"); 39 54 } 40 55 } 41 if(item[1].contains(" TestImages/NotSheetMusic/")){56 if(item[1].contains("/NotSheetMusic/")){ 42 57 notSheetMusicCount++; 43 58 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"); 45 66 } 46 67 } 47 68 } 48 69 49 //Amount of sheetMusic classified images / total amount of images read50 trueAccuracyRate = ((float)trueCount/(float)sheetMusicCount)*(float)100;51 //Amount of notSheetMusic classified images / total amount of images read52 falseAccuracyRate = ((float)falseCount/(float)notSheetMusicCount)*(float)100;53 70 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'); 55 88 buf.close(); 56 89 fw.close(); -
other-projects/is-sheet-music-encore/trunk/image-identification-terminal/javaImageClassifier.java
r33141 r33170 33 33 public static void main(String[] args) { 34 34 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>"); 37 37 } 38 38 else { 39 39 Boolean result = null; 40 40 String imageFilename = args[0]; 41 String classifierType = args[1]; 41 String classifierType = args[1]; 42 String outputFilename = args[2]; 42 43 //Execute classifierType defined from arguement 43 44 switch(classifierType){ 44 45 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); 46 50 break; 47 51 default: 48 System.out.println("unknown ");52 System.out.println("unknown algorithm"); 49 53 break; 50 54 } 51 55 //Write output to disc 52 File log = new File( "log.txt");56 File log = new File(outputFilename); 53 57 FileWriter fileWriter = new FileWriter(log, true); 54 58 BufferedWriter bw = new BufferedWriter(fileWriter); … … 66 70 //True = 1 + Filename + Status 67 71 //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){ 69 77 System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 70 78 Boolean isSheetMusic = null; … … 85 93 // Draw the lines 86 94 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); 89 97 } 90 98 … … 92 100 BufferedImage toBeClassifiedImg = toBufferedImage(edgesDetectedRGB); 93 101 //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); 104 103 } 105 104 catch(Exception e){ … … 107 106 } 108 107 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 108 } 117 109 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 118 158 //****************** 119 159 //INTERNAL FUNCTIONS 120 160 //****************** 121 private static boolean classifier_HoughLine P(BufferedImage img){161 private static boolean classifier_HoughLinesP(BufferedImage img){ 122 162 System.loadLibrary(Core.NATIVE_LIBRARY_NAME); 123 163 try { … … 143 183 percentage = ((float)redCount/(float)pixelCount)*(float)100; 144 184 //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){ 146 186 return true;} 147 187 } -
other-projects/is-sheet-music-encore/trunk/image-identification-terminal/results.txt
r33110 r33170 1 Classifier:Probabilistic HoughLines 30.08% 1 Date: Thu Jun 20 18:40:41 NZST 2019 2 Classifier: houghlinesP 3 truePositive: 3098 4 falseNegative: 63 5 falsePositve: 624 6 trueNegative: 1216 7 SheetMusicAccuracyRate: 98.00696% 8 NotSheetMusicAccuracyRate: 66.08695% 9 OverallAccuracyRate: 86.26274% 10 11 Date: Thu Jun 20 18:40:42 NZST 2019 12 Classifier: houghlinesP-refined 13 truePositive: 1666 14 falseNegative: 1495 15 falsePositve: 123 16 trueNegative: 1717 17 SheetMusicAccuracyRate: 52.70484% 18 NotSheetMusicAccuracyRate: 93.315216% 19 OverallAccuracyRate: 67.64648% 20 -
other-projects/is-sheet-music-encore/trunk/image-identification-terminal/runClassifer.sh
r33141 r33170 1 1 #!/bin/bash 2 2 3 if [ $# != 2] ; then4 echo "Usage: ./testClassifier.sh file_name classifier_type " 1>&23 if [ $# != 4 ] ; then 4 echo "Usage: ./testClassifier.sh file_name classifier_type output_filename num_doc(-a = all -test = 100)" 1>&2 5 5 exit 1 6 6 fi … … 8 8 folder_name=$1 9 9 classifier_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 10 output_filename=$3 11 num_doc=$4 12 i=0 13 case "$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 ;; 40 esac
Note:
See TracChangeset
for help on using the changeset viewer.