package hypothesis_validation; import java.awt.image.BufferedImage; import java.io.BufferedReader; import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; import java.io.LineNumberReader; import java.io.ObjectInputStream; import java.util.ArrayList; import java.util.LinkedList; import javax.imageio.ImageIO; import descriptors.HOG; import descriptors.HoG; import descriptors.hog; import fr.lip6.classifier.Classifier; import fr.lip6.classifier.SMOSVM; import parameters.detection_arguments; import preprocessing.Parser; import preprocessing.Util; import regrouping.Box; import regrouping.Groupment; import tracking.Atributes; public class HoG_Functions { public static BufferedImage getSubImage (BufferedImage image, int x1, int y1, int w1, int h1) { /*T-HOG margin. Ensure to enclose the top and bottom parts of Roman-Texts*/ int margin = (int)(0.15*h1 + 0.5); int xmin = x1; int xmax = x1 + w1 - 1; int ymin = y1 - margin; int ymax = y1 + h1 - 1 + margin; BufferedImage region = Util.getSubImage(image, xmin, ymin, xmax, ymax); return region; } public static ArrayList HoG_Classification_not_scaled ( BufferedImage image, LinkedList list, detection_arguments detection_parameters, String image_name) { double bias = 0.0; ArrayList set_D = new ArrayList(); SMOSVM cls = null; try { ObjectInputStream obj = new ObjectInputStream(new FileInputStream(detection_parameters.hog)); cls = (SMOSVM) obj.readObject(); obj.close(); } catch (Exception e){ e.printStackTrace(); } Parser parser = new Parser(); ArrayList hog_parameters_list = parser.Get_Array_List (detection_parameters.hog_file_name); for (int i = 0; i < list.size(); i++) { Box box = list.get(i); if ( (box.getDec() > 0) && (box.getCount() < detection_parameters.Get_Minimum_Grouping_Set()) ) { continue; } /*Increase the detected window by some pixels because the ICDAR annotation is not tight*/ int margin_top = 2; int margin_bot = 5; int Bx = box.getXMin() - margin_top; int By = box.getYMin() - margin_top; int Bw = box.getXMax() - box.getXMin() + margin_bot; int Bh = box.getYMax() - box.getYMin() + margin_bot; BufferedImage iBox = getSubImage (image, Bx, By, Bw, Bh); double v; if (detection_parameters.use_hog) { v = score (iBox, cls, hog_parameters_list); } else { v = 1.0; } if ((v >= bias)) { box.setValid (-1.0); /*copying the detected regions*/ int x = box.getXMin(); int y = box.getYMin(); int w = (box.getXMax()-box.getXMin()); int h = (box.getYMax()-box.getYMin()); Atributes new_box = new Atributes(); /*Adicionar uma borda de -1 e +1 sobre essa area - CUIDADO*/ new_box.set_x (x); new_box.set_y (y); new_box.set_width (w); new_box.set_height (h); new_box.set_number_of_characters(box.getCount()); new_box.set_flag (false); new_box.set_fp (0); //new_box.set_weight (box.getDec()); //Ver isso melhor depois o PESO new_box.set_weight (-Double.MIN_VALUE);// -> COMENTAR ISSO PARA O TRACKING new_box.image = iBox; set_D.add(new_box); } else { /*try { ImageIO.write(iBox, "png", new File("./negatives/"+image_name+String.format("%03d", i)+".png")); //ImageIO.write(Original, "png", new File(out_path + String.format("%05d", iframe) + "/detection.png")); } catch (Exception e) { System.err.println("cannot store image"); }*/ box.setValid (1.0); } } /*BufferedImage output = Util.imageScale (image, 1.0); Util.write_boxes (output, list, "./hog/"+image_name); long stop = System.currentTimeMillis(); //System.out.println("Segmentation : " + (stop-start)); Groupment.Dispose_Small_Regions_Agroupments (output, list, detection_parameters); if (detection_parameters.write_image) { try { ImageIO.write(output, "png", new File("testando.png")); //ImageIO.write(Original, "png", new File(out_path + String.format("%05d", iframe) + "/detection.png")); } catch (Exception e) { System.err.println("cannot store image"); } } */ //System.err.printf("O TAMANHO DO CONJUNTO SET_D EH: %d\n", set_D.size()); return set_D; } public static void HoG_ClassificationA ( BufferedImage image, LinkedList list, detection_arguments detection_parameters, String image_name) { double bias = 0.0; SMOSVM cls = null; try { ObjectInputStream obj = new ObjectInputStream(new FileInputStream(detection_parameters.hog)); cls = (SMOSVM) obj.readObject(); obj.close(); } catch (Exception e){ e.printStackTrace(); } Parser parser = new Parser(); ArrayList hog_parameters_list = parser.Get_Array_List (detection_parameters.hog_file_name); for (int i = 0; i < list.size(); i++) { Box box = list.get(i); if ( (box.getDec() > 0) && (box.getCount() < detection_parameters.Get_Minimum_Grouping_Set()) ) { continue; } double safe_margin = 2.5; int Bx = (int)(box.getXMin()/2.0 - safe_margin); int By = (int)(box.getYMin()/2.0 - safe_margin); int Bw = (int)((box.getXMax() - box.getXMin())/2.0 + 2*safe_margin); int Bh = (int)((box.getYMax() - box.getYMin())/2.0 + 2*safe_margin); BufferedImage iBox = getSubImage (image, Bx, By, Bw, Bh); double v; if (detection_parameters.use_hog) { v = score (iBox, cls, hog_parameters_list); System.out.printf("score: %f\n", v); } else { v = 1.0; } if (v < bias) { list.remove(i); i--; } } } public static ArrayList HoG_Classification ( BufferedImage image, LinkedList list, detection_arguments detection_parameters, String image_name) { ArrayList set_D = new ArrayList(); for (int i = 0; i < list.size(); i++) { Box box = list.get(i); if ( (box.getDec() > 0) && (box.getCount() < detection_parameters.Get_Minimum_Grouping_Set()) ) { continue; } double safe_margin = 2.5; int Bx = (int)(box.getXMin()/2.0 - safe_margin); int By = (int)(box.getYMin()/2.0 - safe_margin); int Bw = (int)((box.getXMax() - box.getXMin())/2.0 + 2*safe_margin); int Bh = (int)((box.getYMax() - box.getYMin())/2.0 + 2*safe_margin); BufferedImage iBox = getSubImage (image, Bx, By, Bw, Bh); Atributes new_box = new Atributes(); new_box.set_x (Bx); new_box.set_y (By); new_box.set_width (Bw); new_box.set_height (Bh); new_box.set_number_of_characters(box.getCount()); new_box.set_flag (false); new_box.set_fp (0); new_box.set_weight (-Double.MIN_VALUE); new_box.image = iBox; set_D.add(new_box); } return set_D; } public static int read_hog_vectors ( ArrayList mean, ArrayList std, String hog_file) { int hog_size = -1; try { Parser obj = new Parser(); LineNumberReader file = new LineNumberReader(new FileReader(hog_file)); hog_size = Integer.parseInt(obj.getLineParameter (file, "\n", 0)); for(int i = 0; i < hog_size; i++) { String[] s = obj.getTokens (file, "\n"); mean.add( Double.parseDouble(s[0])); } for(int i = 0; i < hog_size; i++) { String[] s = obj.getTokens (file, "\n"); std.add(Double.parseDouble(s[0])); } file.close(); } catch (IOException e) { System.err.println("exception: failed to read hog files!"); e.printStackTrace(); } return hog_size; } /*public static double Get_HoG_Score ( BufferedImage image, ArrayList mean, ArrayList std, SMOSVM cls, detection_arguments detection_parameters) { HOG hog_object = new HOG(); //System.err.printf("cells x : %d, cells y : %d\n", detection_parameters.number_of_cells_x, detection_parameters.number_of_cells_y); double[] hog_descriptor = hog_object.hog ( image, detection_parameters.rwt, detection_parameters.gauss_norm_weight, detection_parameters.number_of_cells_x, detection_parameters.number_of_cells_y, detection_parameters.bins_per_cell, detection_parameters.new_height, detection_parameters.normalize_image, detection_parameters.option, detection_parameters.normalize_histogram_L1, detection_parameters.use_interval ); if (hog_descriptor == null) { System.exit(1); //return -Double.MAX_VALUE; } return cls.valueOf(hog_descriptor); }*/ /*public static double[] get_descriptor ( BufferedImage image, ArrayList list ) { double rwt = Double.parseDouble(list.get(0)); boolean gauss_norm_weight = Boolean.parseBoolean(list.get(1)); int number_of_cells_x = Integer.parseInt(list.get(2)); int number_of_cells_y = Integer.parseInt(list.get(3)); int bins_per_cell = Integer.parseInt(list.get(4)); int new_height = Integer.parseInt(list.get(5)); boolean normalize_image = Boolean.parseBoolean(list.get(6)); int option = Integer.parseInt(list.get(7)); boolean normalize_histogram_L1 = Boolean.parseBoolean(list.get(8)); boolean use_interval = Boolean.parseBoolean(list.get(9)); HOG hog = new HOG(); double[] descriptor = hog.hog ( image, rwt, gauss_norm_weight, number_of_cells_x, number_of_cells_y, bins_per_cell, new_height, normalize_image, option, normalize_histogram_L1, use_interval ); return descriptor; }*/ public static double score ( BufferedImage image, Classifier cls, ArrayList list ) { boolean debug = false; double[] descriptor = get_descriptor (debug, image, list); return cls.valueOf (descriptor); } public static double[] get_descriptor ( boolean debug, BufferedImage image, ArrayList list ) { int new_height = Integer.parseInt(list.get(0)); int number_of_cells_x = Integer.parseInt(list.get(1)); int number_of_cells_y = Integer.parseInt(list.get(2)); int bins_per_cell = Integer.parseInt(list.get(3)); boolean image_normalization = Boolean.parseBoolean(list.get(4)); String image_normalization_weight = list.get(5); double image_normalization_weight_radius = Double.parseDouble(list.get(6)); String gradient_option = list.get(7); String histogram_normalization_metric = list.get(8); String weight_function = list.get(9); boolean deformable_weights = Boolean.parseBoolean(list.get(10)); HOG hog = new HOG(); double[] descriptor = hog.hog_descriptor ( debug, image, new_height, number_of_cells_x, number_of_cells_y, bins_per_cell, image_normalization, image_normalization_weight, image_normalization_weight_radius, gradient_option, histogram_normalization_metric, weight_function, deformable_weights ); return descriptor; } /*public static double score ( BufferedImage image, SMOSVM cls, ArrayList list ) { double rwt = Double.parseDouble(list.get(0)); boolean gauss_norm_weight = Boolean.parseBoolean(list.get(1)); int number_of_cells_x = Integer.parseInt(list.get(2)); int number_of_cells_y = Integer.parseInt(list.get(3)); int bins_per_cell = Integer.parseInt(list.get(4)); int new_height = Integer.parseInt(list.get(5)); boolean normalize_image = Boolean.parseBoolean(list.get(6)); int option = Integer.parseInt(list.get(7)); boolean normalize_histogram_L1 = Boolean.parseBoolean(list.get(8)); boolean use_interval = Boolean.parseBoolean(list.get(9)); HOG hog = new HOG(); double[] descriptor = hog.hog ( image, rwt, gauss_norm_weight, number_of_cells_x, number_of_cells_y, bins_per_cell, new_height, normalize_image, option, normalize_histogram_L1, use_interval ); return cls.valueOf (descriptor); } */ public static ArrayList get_hog_parameters (String hog_parameters) { Parser parser = new Parser(); BufferedReader arguments = parser.Open_File_Stream (hog_parameters); ArrayList list = new ArrayList(); String[] s = null; do { s = parser.Get_Stream_Tokens (arguments, " "); if (s == null) { break; } list.add(s[0]); } while (s != null); parser.Close_File_Stream (arguments, hog_parameters); return list; } }