Commit 8282741b authored by petter's avatar petter

Added full tesseract support - removed dependency from proprietary Omnipage

git-svn-id: https://www2.in.tum.de/repos/ttt/trunk@134 0463f305-d864-43cb-8a47-61cf597d4139
parent 6728517c
......@@ -104,7 +104,7 @@ import ttt.record.LectureProfileDialog;
import ttt.record.Recording;
public class TTT extends JFrame {
static final String version = "14.06.2013";
static final String version = "17.06.2013";
public static boolean debug = !true;
public static boolean verbose = true;
......
......@@ -48,6 +48,10 @@ import javax.swing.ProgressMonitor;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.XMLReaderFactory;
import ttt.Constants;
import ttt.TTT;
import ttt.audio.Exec;
......@@ -799,7 +803,15 @@ public class Index {
// write input for Optical Character Recognition
if ((mode & ScriptCreator.OCR_OPTIMIZED) != 0){
scriptCreator.writeOCRScreenshot(i, screenshot);
String filename = scriptCreator.writeOCRScreenshot(i, screenshot);
readSearchBaseFromHOCRFile(filename+".hocr.html", i);
// cleanup mess
{
File f = new File(filename);
f.delete();
f = new File(filename+".hocr.html");
f.delete();
}
}
if (((mode & ScriptCreator.HTML_SCRIPT) != 0) || ((mode & ScriptCreator.PDF_SCRIPT) != 0)) {
......@@ -1089,7 +1101,47 @@ public class Index {
/*******************************************************************************************************************
* reading search base
******************************************************************************************************************/
/***
* read hOCR/xhtml generated by tesseract-OCR
* @param fileName
* @return
*/
public boolean readSearchBaseFromHOCRFile(String fileName,int pageindex) {
File file = new File(fileName);
if (TTT.verbose) System.out.print("Reading search base: "+fileName);
if (file.exists()) {
// Use an instance of ourselves as the SAX event handler
hOCRHandler handler = new hOCRHandler();
try {
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(file)));
ArrayList<ArrayList<SearchBaseEntry>> pages = handler.getResult();
// check size
if (pages.size() != 1 && pageindex >= this.index.size()) {
// TODO: modify dialog
TTT.showMessage("Missmatch in OCR/index","expected pages: "+index.size()+" requested page: "+pageindex+" paket size: "+pages.size(), JOptionPane.WARNING_MESSAGE);
} else {
// set searchbase
index.get(pageindex).setSearchbase(pages.get(0));
searchbaseFormat = XML_SEARCHBASE;
return true;
}
} catch (Throwable t) {
t.printStackTrace();
TTT.showMessage("XML file '" + file + "' contains errors.\nError: " + t,
"TTT: Reading hOCR Searchbase File", JOptionPane.ERROR_MESSAGE);
}
} else if (TTT.verbose)
System.out.println("not found.");
return false;
}
public // read XML generated by OCR
// double ratioXMLSearchBase;
// Dimension resolutionXMLSearchBase;
......
......@@ -143,7 +143,7 @@ public class hOCRHandler extends DefaultHandler {
// 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);
// System.out.println("Desktop resolution is now "+width+"x"+height);
desktopResulotion = new Dimension(width, height);
}
......@@ -189,7 +189,6 @@ public class hOCRHandler extends DefaultHandler {
flag = false;
SearchBaseEntry entry = new SearchBaseEntry(searchText, left, top, right - left, bottom - top, 1);
// System.out.println("\tset:\t" + searchText);
words.add(entry);
}
}
......
......@@ -48,6 +48,7 @@ import javax.swing.ProgressMonitor;
import ttt.Constants;
import ttt.TTT;
import ttt.audio.Exec;
import ttt.audio.LameEncoder;
import ttt.audio.MP3Converter;
import ttt.audio.OggVorbisEncoder;
......@@ -111,8 +112,15 @@ public class PostProcessorPanel extends GradientPanel {
pdfCheckBox.setEnabled(false);
}
ocrCheckBox
.setToolTipText("generate optimized input for optical character recognition (see full text search help)");
if (Exec.getCommand("tesseract") != null)
ocrCheckBox
.setToolTipText("perform optical character recognition via tesseract");
else
{
ocrCheckBox.setToolTipText("tesseract command not found - install tesseract or include in path");
ocrCheckBox.setSelected(false);
ocrCheckBox.setEnabled(false);
}
//ogg vorbis
if (OggVorbisEncoder.isOggVorbisAvailable() && (recording.getExistingFileBySuffix("wav").exists() /*||
......@@ -699,7 +707,7 @@ public class PostProcessorPanel extends GradientPanel {
pdfCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
ocrCheckBox.setSelected(true);
ocrCheckBox.setText("OCR input");
ocrCheckBox.setText("perform Tesseract/OCR for fulltext search");
ocrCheckBox.setBorder(javax.swing.BorderFactory.createEmptyBorder(0, 0, 0, 0));
flashCheckBox.setSelected(true);
......@@ -1045,7 +1053,7 @@ public class PostProcessorPanel extends GradientPanel {
jtabPane.add("Info", jPanelInfo);
jtabPane.add("Thumbs, Script and Flash", jPanelThumbs);
jtabPane.add("Full Text Search", jPanelFullTextSearch);
//jtabPane.add("Full Text Search", jPanelFullTextSearch);
jtabPane.add("Publishing", jPanelPublishing);
this.add(jtabPane);
this.add(doneButton);
......
......@@ -238,13 +238,18 @@ public class ScriptCreator {
}
}
// create html, screenshot and thumbnail for current index
public void writeOCRScreenshot(int nr, Image screenshot) {
String filename = ocr_path + File.separator + file_base + "." + ((nr + 1) < 10 ? "0" : "")
+ ((nr + 1) < 100 ? "0" : "") + (nr + 1) + ".png";
/**
* OCR screenshots and processing with tesseract
* @param index Index of page to generate and read
* @param screenshot screenshot data
* @return name of the generated image file
*/
public String writeOCRScreenshot(int index, Image screenshot) {
String filename = ocr_path + File.separator + file_base + "." + ((index + 1) < 10 ? "0" : "")
+ ((index + 1) < 100 ? "0" : "") + (index + 1) + ".png";
ImageCreator.writeImage(screenshot, filename);
String os = System.getProperty("os.name");
if (os.indexOf("nix") >= 0 || os.indexOf("nux") >= 0 ){
if (Exec.getCommand("tesseract")!=null){
Exec exec = new Exec();
try {
exec.exec(new String[] {
......@@ -256,9 +261,11 @@ public class ScriptCreator {
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
return filename;
}
// 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