package main; import hypothesis_validation.HoG_Score; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.PrintStream; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import javax.imageio.ImageIO; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; import preprocessing.Parser; import preprocessing.Util; import fr.lip6.classifier.Classifier; import xml.text_region_parameters; import xml.xml_image_register; public class HoG_Classification { public List runExample (String XMLFileName) { /*parse the XML file and get the document object*/ Document doc = parseXMLFile(XMLFileName); /*get each image element and create a Icdar object*/ List list = parseDocument(doc); return list; } private Document parseXMLFile (String XMLFileName) { Document doc = null; /*get the factory*/ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); try { /*using factory get an instance of document builder*/ DocumentBuilder db = dbf.newDocumentBuilder(); /*parse using builder to get the document representation of the XML file*/ doc = db.parse(XMLFileName); } catch(ParserConfigurationException pce) { pce.printStackTrace(); } catch(SAXException se) { se.printStackTrace(); } catch(IOException ioe) { ioe.printStackTrace(); } return doc; } private List parseDocument(Document doc){ List list = new ArrayList();; /*get the root elememt*/ Element docEle = doc.getDocumentElement(); /*get a nodelist of elements*/ NodeList nl = docEle.getElementsByTagName("image"); if(nl != null && nl.getLength() > 0) { /*getting each image tag do*/ for(int i = 0 ; i < nl.getLength();i++) { /*get the element*/ Element elem = (Element)nl.item(i); /*get the image information element*/ xml_image_register e = getInfo(elem); /*add it to list*/ list.add(e); } } return list; } /*get each image register in the ICDAR 2003 format*/ private xml_image_register getInfo (Element node) { int width = 0, height = 0; LinkedList bParameters = new LinkedList(); String imageName = getTextValue(node, "imageName"); NodeList resolution = node.getElementsByTagName("resolution"); for(int i = 0 ; i < resolution.getLength(); i++) { Element elem = (Element)resolution.item(i); /*getting the image width*/ String x = elem.getAttribute("x"); width = Integer.parseInt(x); /*getting the image height*/ String y = elem.getAttribute("y"); height = Integer.parseInt(y); } NodeList boxes = node.getElementsByTagName("taggedRectangle"); NodeList tags = node.getElementsByTagName("tag"); /*for each box in the image do*/ for(int i = 0 ; i < boxes.getLength(); i++) { text_region_parameters b = new text_region_parameters (); Element elem = (Element)boxes.item(i); /*getting the beginnig of the box (x coordinate)*/ String x = elem.getAttribute("x"); b.setX (Double.parseDouble(x)); /*getting the beginnig of the box (y coordinate)*/ String y = elem.getAttribute("y"); b.setY (Double.parseDouble(y)); /*getting the box width*/ String w = elem.getAttribute("width"); b.setW (Double.parseDouble(w)); /*getting the box height*/ String h = elem.getAttribute("height"); b.setH (Double.parseDouble(h)); /*getting the svm score if it exist*/ String score = elem.getAttribute("score"); if (!score.isEmpty()) { b.setScore (Double.parseDouble(score)); } else { b.setScore(-Double.MAX_VALUE); } String id = elem.getAttribute("id"); if (!id.isEmpty()) { b.setId (Integer.parseInt(id)); } else { b.setId (0); } /*getting the offset parameters of the box (inclination (italic))*/ String offset = elem.getAttribute("offset"); b.setOffset (Double.parseDouble(offset)); /*getting the rotation value*/ String rotation = elem.getAttribute("rotation"); if (!rotation.isEmpty()) { b.setRotation (Double.parseDouble(rotation)); } else { b.setRotation (0.0); } /*getting the userName value*/ String userName = elem.getAttribute("userName"); if (!userName.isEmpty()) { b.setUserName (userName); } else { b.setUserName (""); } String text = elem.getAttribute("text"); if (!text.isEmpty()) { b.setText (text); } else { b.setText(""); } if (tags.getLength() > 0) { Element elem_tag = (Element)tags.item(i); String tag = elem_tag.getTextContent(); if (!tag.isEmpty()) { b.setTag(tag); } else { b.setTag (""); } } /*adding the box structure inside the list*/ bParameters.add(b); } xml_image_register e = new xml_image_register (imageName, width, height, bParameters, boxes.getLength()); return e; } private String getTextValue (Element ele, String tagName) { String textVal = null; NodeList nl = ele.getElementsByTagName(tagName); if(nl != null && nl.getLength() > 0) { Element el = (Element)nl.item(0); textVal = el.getFirstChild().getNodeValue(); } return textVal; } public static BufferedImage getSubImage (BufferedImage image, int x1, int y1, int w1, int h1) { int margin = (int)(0.15*h1 + 0.5); int xmin = x1 - margin; int xmax = x1 + w1 - 1 + margin; int ymin = y1 - margin; int ymax = y1 + h1 - 1 + margin; BufferedImage region = Util.getSubImage(image, xmin, ymin, xmax, ymax); return region; } static void classify_xml_regions ( List XML, String image_path, ArrayList hog_parameters_list, Classifier cls, double threshold, String image_type, String xml_out_file_name) { FileOutputStream xml_out_file = null; try { xml_out_file = new FileOutputStream(xml_out_file_name); } catch (FileNotFoundException e) { System.err.printf("error: can't open file :%s\n", xml_out_file_name); e.printStackTrace(); } PrintStream xml_out = new PrintStream(xml_out_file); xml_out.println(""); xml_out.println(""); int xml_size = XML.size(); HoG_Score score = new HoG_Score(); for(int index = 0; index < xml_size; index++) { xml_image_register xml_reg = XML.get(index); LinkedList list = new LinkedList(); BufferedImage image = null; try { image = ImageIO.read(new File(image_path + xml_reg.getImageName() + image_type)); } catch (Exception e) { System.err.printf("error: can't open image : %s\n", image_path + xml_reg.getImageName() + image_type); System.exit(1); } for (int text_region_index = 0; text_region_index < xml_reg.getNboxes(); text_region_index++) { text_region_parameters text_region = xml_reg.getBox(text_region_index); int x = (int) text_region.getX(); int y = (int) text_region.getY(); int w = (int) text_region.getW(); int h = (int) text_region.getH(); BufferedImage region = getSubImage (image, x, y, w, h); double v = score.get_score (region, cls, hog_parameters_list); if (v > threshold) { list.add(text_region); } } xml_image_register.toXML (xml_out, xml_reg.getImageName(), xml_reg.getWidth(), xml_reg.getHeight(), list); } xml_out.println(""); xml_out.close(); } public static void main(String[] args) throws Exception{ /*Input parameters*/ String xml_file_name = args[0]; String image_path = args[1]; double threshold = Double.parseDouble(args[2]); String hog_object = args[3]; String hog_parameters = args[4]; String xml_out_file_name = args[5]; String image_type = args[6]; /*End*/ HoG_Classification xml_file = new HoG_Classification(); List XML = xml_file.runExample(xml_file_name); Parser parser = new Parser(); ArrayList hog_parameters_list = parser.Get_Array_List (hog_parameters); Classifier cls = null; try { ObjectInputStream obj = new ObjectInputStream(new FileInputStream(hog_object)); cls = (Classifier) obj.readObject(); obj.close(); } catch (Exception e){ e.printStackTrace(); } classify_xml_regions (XML, image_path, hog_parameters_list, cls, threshold, image_type, xml_out_file_name); } }