This repository has been archived by the owner on Jul 26, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
# Conflicts: # jochre_core/pom.xml # jochre_core/src/main/java/com/joliciel/jochre/Jochre.java # jochre_distribution/pom.xml # jochre_parent/pom.xml # jochre_search/pom.xml # jochre_search_webapp/pom.xml # jochre_utils/pom.xml # jochre_web/pom.xml # jochre_yiddish/pom.xml
- Loading branch information
Showing
13 changed files
with
184 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
134 changes: 134 additions & 0 deletions
134
jochre_core/src/main/java/com/joliciel/jochre/doc/ImageFileDocumentExtractor.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
/////////////////////////////////////////////////////////////////////////////// | ||
//Copyright (C) 2012 Assaf Urieli | ||
// | ||
//This file is part of Jochre. | ||
// | ||
//Jochre is free software: you can redistribute it and/or modify | ||
//it under the terms of the GNU Affero General Public License as published by | ||
//the Free Software Foundation, either version 3 of the License, or | ||
//(at your option) any later version. | ||
// | ||
//Jochre is distributed in the hope that it will be useful, | ||
//but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
//GNU Affero General Public License for more details. | ||
// | ||
//You should have received a copy of the GNU Affero General Public License | ||
//along with Jochre. If not, see <http://www.gnu.org/licenses/>. | ||
////////////////////////////////////////////////////////////////////////////// | ||
package com.joliciel.jochre.doc; | ||
|
||
import java.awt.image.BufferedImage; | ||
import java.io.File; | ||
import java.io.FilenameFilter; | ||
|
||
import javax.imageio.ImageIO; | ||
|
||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
import com.joliciel.talismane.utils.Monitorable; | ||
import com.joliciel.talismane.utils.MultiTaskProgressMonitor; | ||
import com.joliciel.talismane.utils.ProgressMonitor; | ||
|
||
/** | ||
* An interface for extracting a JochreDocument from an image File (jpeg, gif or | ||
* png). | ||
* | ||
* @author Assaf Urieli | ||
* | ||
*/ | ||
public class ImageFileDocumentExtractor implements Monitorable, Runnable { | ||
private static final Logger LOG = LoggerFactory.getLogger(ImageFileDocumentExtractor.class); | ||
private final SourceFileProcessor documentProcessor; | ||
private MultiTaskProgressMonitor currentMonitor; | ||
private final File imageFile; | ||
private int pageNumber = 1; | ||
|
||
public ImageFileDocumentExtractor(File imageFile, SourceFileProcessor documentProcessor) { | ||
this.documentProcessor = documentProcessor; | ||
this.imageFile = imageFile; | ||
} | ||
|
||
@Override | ||
public void run() { | ||
this.extractDocument(); | ||
} | ||
|
||
public JochreDocument extractDocument() { | ||
LOG.debug("ImageDocumentExtractorImpl.extractDocument"); | ||
try { | ||
File[] files = new File[1]; | ||
|
||
if (imageFile.isDirectory()) { | ||
files = imageFile.listFiles(new FilenameFilter() { | ||
|
||
@Override | ||
public boolean accept(File dir, String name) { | ||
return (name.toLowerCase().endsWith(".png") || name.toLowerCase().endsWith(".jpg") || name.toLowerCase().endsWith(".jpeg") | ||
|| name.toLowerCase().endsWith(".gif") || name.toLowerCase().endsWith(".tif") || name.toLowerCase().endsWith(".tiff")); | ||
} | ||
}); | ||
} else { | ||
files[0] = imageFile; | ||
} | ||
|
||
JochreDocument doc = this.documentProcessor.onDocumentStart(); | ||
doc.setTotalPageCount(files.length); | ||
|
||
int currentPageNumber = this.pageNumber; | ||
for (File file : files) { | ||
JochrePage page = this.documentProcessor.onPageStart(currentPageNumber++); | ||
|
||
BufferedImage image = ImageIO.read(file); | ||
String imageName = file.getName(); | ||
|
||
if (currentMonitor != null && documentProcessor instanceof Monitorable) { | ||
ProgressMonitor monitor = ((Monitorable) documentProcessor).monitorTask(); | ||
double percentAllotted = (1 / (double) (files.length)); | ||
currentMonitor.startTask(monitor, percentAllotted); | ||
} | ||
|
||
documentProcessor.onImageFound(page, image, imageName, 0); | ||
if (currentMonitor != null && documentProcessor instanceof Monitorable) { | ||
currentMonitor.endTask(); | ||
} | ||
|
||
this.documentProcessor.onPageComplete(page); | ||
} | ||
this.documentProcessor.onDocumentComplete(doc); | ||
this.documentProcessor.onAnalysisComplete(); | ||
|
||
if (currentMonitor != null) | ||
currentMonitor.setFinished(true); | ||
return doc; | ||
} catch (Exception e) { | ||
LOG.debug("Exception occurred. Have monitor? " + currentMonitor); | ||
if (currentMonitor != null) | ||
currentMonitor.setException(e); | ||
LOG.error("Exception while processing document", e); | ||
throw new RuntimeException(e); | ||
} finally { | ||
LOG.debug("Exit ImageDocumentExtractorImpl.extractDocument"); | ||
} | ||
} | ||
|
||
@Override | ||
public ProgressMonitor monitorTask() { | ||
currentMonitor = new MultiTaskProgressMonitor(); | ||
|
||
return currentMonitor; | ||
} | ||
|
||
/** | ||
* The page number to assign to this image. | ||
*/ | ||
public int getPageNumber() { | ||
return pageNumber; | ||
} | ||
|
||
public void setPageNumber(int pageNumber) { | ||
this.pageNumber = pageNumber; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.