Commit 6728517c authored by petter's avatar petter

Prepared switch to tesseract OCR instead of proprietary Omnipage

git-svn-id: https://www2.in.tum.de/repos/ttt/trunk@133 0463f305-d864-43cb-8a47-61cf597d4139
parent 904dcd23
......@@ -104,7 +104,7 @@ import ttt.record.LectureProfileDialog;
import ttt.record.Recording;
public class TTT extends JFrame {
static final String version = "06.06.2013";
static final String version = "14.06.2013";
public static boolean debug = !true;
public static boolean verbose = true;
......
......@@ -50,6 +50,7 @@ import javax.xml.parsers.SAXParserFactory;
import ttt.Constants;
import ttt.TTT;
import ttt.audio.Exec;
import ttt.helper.LibraryChecker;
import ttt.messages.Annotation;
import ttt.messages.DeleteAllAnnotation;
......@@ -798,7 +799,8 @@ public class Index {
// write input for Optical Character Recognition
if ((mode & ScriptCreator.OCR_OPTIMIZED) != 0){
scriptCreator.writeOCRScreenshot(i, screenshot);}
scriptCreator.writeOCRScreenshot(i, screenshot);
}
if (((mode & ScriptCreator.HTML_SCRIPT) != 0) || ((mode & ScriptCreator.PDF_SCRIPT) != 0)) {
// add (future) annotations of index
......
package ttt.gui;
/*
* MODIFIED VERSION OF
*
* @(#)Echo01.java 1.5 99/02/09
*
* Copyright (c) 1998 Sun Microsystems, Inc. All Rights Reserved.
*
* Sun grants you ("Licensee") a non-exclusive, royalty free, license to use, modify and redistribute this software in
* source and binary code form, provided that i) this copyright notice and license appear on all copies of the software;
* and ii) Licensee does not utilize the software in a manner which is disparaging to Sun.
*
* This software is provided "AS IS," without a warranty of any kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS
* AND WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR
* NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE
* AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THE SOFTWARE OR ITS DERIVATIVES. IN NO EVENT WILL SUN OR ITS
* LICENSORS BE LIABLE FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, INCIDENTAL
* OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY
* TO USE SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
*
* This software is not designed or intended for use in on-line control of aircraft, air traffic, aircraft navigation or
* aircraft communications; or in the design, construction, operation or maintenance of any nuclear facility. Licensee
* represents and warrants that it will not use or redistribute the Software for such purposes.
*/
import java.awt.Dimension;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.Writer;
import java.util.ArrayList;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.helpers.XMLReaderFactory;
public class hOCRHandler extends DefaultHandler {
public static void main(String argv[]) {
if (argv.length < 1) {
System.err.println("Usage: cmd [-txt] <xml-serachbase>");
System.err.println(" -txt: create ASCII searchbase");
System.exit(1);
}
boolean createASCII = argv[0].equals("-txt");
System.arraycopy(argv, 1, argv, 0, argv.length - 1);
// Use an instance of ourselves as the SAX event handler
hOCRHandler handler = new hOCRHandler();
handler.output = !createASCII;
// Use the default (non-validating) parser
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
System.out.println("Parsing input in "+argv[0]);
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
xmlReader.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
xmlReader.setContentHandler(handler);
xmlReader.parse(new InputSource(new FileReader(argv[0])));
// Parse the input
System.out.println("Parsed input");
if (createASCII) {
// create output file
String filename = argv[0];
if (filename.endsWith(".hocr"))
filename = filename.substring(0, filename.length() - 5) + ".txt";
File file = new File(filename);
System.out.println("create ASCII searchbase: '" + file.getCanonicalPath() + "'");
// backup existing file
if (file.exists()) {
System.out.println(" backup exiting file");
file.renameTo(new File(filename + "bak"));
file = new File(filename);
}
Writer out = new BufferedWriter(new FileWriter(filename));
ArrayList<ArrayList<SearchBaseEntry>> pages = handler.getResult();
for (ArrayList<SearchBaseEntry> words : pages) {
System.out.print(".");
boolean first = true;
for (SearchBaseEntry entry : words) {
out.write((first ? "" : " ") + entry.searchTextOriginal);
first = false;
}
// pagebreak
out.write(12);
}
out.flush();
out.close();
System.out.println(" done");
}
} catch (Throwable t) {
t.printStackTrace();
}
System.out.println("Program ended");
}
public boolean output = false;
// ===========================================================
// SAX DocumentHandler methods
// ===========================================================
public void startDocument() throws SAXException {
}
public void endDocument() throws SAXException {
}
int page = 0;
boolean wdFlag;
int left, right, top, bottom;
String searchText = null;
int width, height;
// list of pages - each containing a list of words
ArrayList<ArrayList<SearchBaseEntry>> pages = new ArrayList<ArrayList<SearchBaseEntry>>();
// temporary list of words for current page
ArrayList<SearchBaseEntry> words;
public ArrayList<ArrayList<SearchBaseEntry>> getResult() {
return pages;
}
// specify desktop resolution (used to calculate ratio)
// NOTE: desktop resolution is not store within OmniPage 14 XML file but within OmniPage 15 XML files
Dimension desktopResulotion;
public void setDesktopResolution(int width, int height) {
System.out.println("Desktop resolution is now "+width+"x"+height);
desktopResulotion = new Dimension(width, height);
}
@Override
public void startElement(String namespaceURI, String localName, String qualifiedName, Attributes attrs)
throws SAXException {
wdFlag=false;
if (localName.equals("div") && attrs.getValue("class").equals("ocr_page")){
String resolution = attrs.getValue("title").split(";")[1].substring(9).trim();
setDesktopResolution(Integer.parseInt(resolution.split(" ")[0]),Integer.parseInt(resolution.split(" ")[1]));
}
if (localName.equals("span") && attrs.getValue("class").equals("ocrx_word")){
wdFlag = true;
// reset searchtext
searchText = null;
String[] coords = attrs.getValue("title").substring(5).split(" ");
left = Integer.parseInt(coords[0]);
top = Integer.parseInt(coords[1]);
right = Integer.parseInt(coords[2]);
bottom = Integer.parseInt(coords[3]);
}
if (localName.equals("div") && attrs.getValue("class").equals("ocr_page")) {
// new page - new page list
words = new ArrayList<SearchBaseEntry>();
pages.add(words);
}
}
static boolean flag = true;
@Override
public void endElement(String namespaceURI, String simpleName, String qualifiedName) throws SAXException {
// word completed
if (simpleName.equals("span") && wdFlag) {
wdFlag = false;
if (searchText == null)
searchText = "";
if (flag)
flag = false;
SearchBaseEntry entry = new SearchBaseEntry(searchText, left, top, right - left, bottom - top, 1);
// System.out.println("\tset:\t" + searchText);
words.add(entry);
}
}
@Override
public void characters(char buf[], int offset, int len) throws SAXException {
// buffer current word
if (wdFlag) {
if (searchText == null) {
// set
searchText = new String(buf, offset, len);
} else {
// concatenate
searchText += new String(buf, offset, len);
}
}
}
}
\ No newline at end of file
// TeleTeachingTool - Presentation Recording With Automated Indexing
//
// Copyright (C) 2003-2008 Peter Ziewer - Technische Universitt Mnchen
// Copyright (C) 2003-2008 Peter Ziewer - Technische Universit�t M�nchen
//
// This file is part of TeleTeachingTool.
//
......@@ -42,6 +42,7 @@ import javax.imageio.ImageIO;
import ttt.Constants;
import ttt.TTT;
import ttt.audio.Exec;
import ttt.postprocessing.podcast.ImageCreator;
import ttt.record.Recording;
......@@ -239,8 +240,25 @@ public class ScriptCreator {
// create html, screenshot and thumbnail for current index
public void writeOCRScreenshot(int nr, Image screenshot) {
ImageCreator.writeImage(screenshot, ocr_path + File.separator + file_base + "." + ((nr + 1) < 10 ? "0" : "")
+ ((nr + 1) < 100 ? "0" : "") + (nr + 1) + ".png");
String filename = ocr_path + File.separator + file_base + "." + ((nr + 1) < 10 ? "0" : "")
+ ((nr + 1) < 100 ? "0" : "") + (nr + 1) + ".png";
ImageCreator.writeImage(screenshot, filename);
String os = System.getProperty("os.name");
if (os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0 ){
Exec exec = new Exec();
try {
exec.exec(new String[] {
"tesseract",
filename,
filename+".hocr",
"hocr"
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// create html, screenshot and thumbnail for current index
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment