// Last edited on 2013-01-04 17:16:46 by stolfilocal package main; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.PrintStream; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import javax.imageio.ImageIO; import thog.thog_desc; import utils.classifier_functions; import utils.file_functions; import utils.image_functions; import xml.fields; import xml.parser; import xml.register; import JKernelMachines.Classifier; public class classification { static void classify_image_regions ( List xml_regions, String image_path, ArrayList hog_parameters_list, Classifier cls, double bias, String image_type, String out_path, boolean write ) { thog_desc thog = new thog_desc (); boolean debug = false; FileOutputStream xml_out_file = null; try { xml_out_file = new FileOutputStream(out_path + "filtered.xml"); } catch (FileNotFoundException e) { System.err.printf("error: can't open file :%s\n", out_path + "filtered.xml"); e.printStackTrace(); } PrintStream xml_out = new PrintStream(xml_out_file); xml_out.println(""); xml_out.println(""); int xml_size = xml_regions.size(); for(int index = 0; index < xml_size; index++) { register regions = xml_regions.get(index); LinkedList list = new LinkedList(); BufferedImage image = null, tmp1 = null, tmp2 = null; String image_name = null; try { image_name = image_path + regions.get_image_name() + image_type; image = ImageIO.read(new File(image_name)); } catch (Exception e) { System.err.printf("error: fail to open image: %s\n", image_name); System.exit(1); } if (write) { tmp1 = image_functions.copy_image (image); tmp2 = image_functions.copy_image (image); } for (int text_region_index = 0; text_region_index < regions.get_nregions(); text_region_index++) { fields text_region = regions.get_region (text_region_index); int x = (int) text_region.get_x(); int y = (int) text_region.get_y(); int w = (int) text_region.get_w(); int h = (int) text_region.get_h(); FloatImage candidate = thog.get_thog_sub_image (image, x, y, w, h); double score = thog.score (candidate, cls, hog_parameters_list, debug); if (score > bias) { list.add(text_region); if (write) { image_functions.draw_rectangle(tmp1, x, y, w, h); } } if (write) { image_functions.draw_rectangle(tmp2, x, y, w, h); } } regions.write_to_file (list, xml_out); if (write) { try { System.out.printf("Writing image: %s\n", out_path + regions.get_image_name() + "_ythog.jpg"); ImageIO.write (tmp1, "jpg", new File(out_path + regions.get_image_name() + "_ythog.jpg")); System.out.printf("Writing image: %s\n", out_path + regions.get_image_name() + "_nthog.jpg"); ImageIO.write (tmp2, "jpg", new File(out_path + regions.get_image_name() + "_nthog.jpg")); } catch (Exception e) { System.err.printf("Cannot write image %s\n"); } } } xml_out.println(""); xml_out.close(); System.out.printf("Writing filtered xml in: %s\n", out_path + "filtered.xml"); } public static void main (String[] args) throws Exception { if (args.length < 7) { System.err.printf("use: java -ea -cp thog.jar \\ \n main.classification \\ \n {xml_in} [icdar format] \\ \n {image_path} \\ \n {svm_bias} \\ \n {thog_object} \\ \n {thog_parameters} \\ \n {xml_out} \\ \n {image_type} [png/jpg] \n"); return; } List xml_regions = parser.run (args[0]); String image_path = args[1]; double bias = Double.parseDouble(args[2]); file_functions parser = new file_functions(); Classifier trained_cls = classifier_functions.get_classifier (args[3]); ArrayList hog_parameters = parser.get_list (args[4]); String out_path = args[5]; String image_type = args[6]; boolean write = Boolean.parseBoolean(args[7]); classify_image_regions (xml_regions, image_path, hog_parameters, trained_cls, bias, image_type, out_path, write); } }