-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Initial implementation for OCRmyPDF #15712
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
47 commits
Select commit
Hold shift + click to select a range
d1bf4c4
Initial implementation for OCRmyPDF
ZiadAbdElFatah af44508
Addressed the review comments
ZiadAbdElFatah 1170da2
Used pre-existing StreamGobbler for BufferedReader
ZiadAbdElFatah f8546b5
Reomved unused variables
ZiadAbdElFatah 349cee5
Reformated the code to meet JabRef's code guidelines
ZiadAbdElFatah 4173f5d
Merge branch 'main' into feature-OCR
ZiadAbdElFatah 2cdb734
Merge branch 'main' into feature-OCR
ZiadAbdElFatah e6ba274
Added GUI to perform OCR
ZiadAbdElFatah 45a9fbc
Added localized messages
ZiadAbdElFatah c08ab99
Merge branch 'main' into feature-OCR
ZiadAbdElFatah b536820
Merge branch 'JabRef:main' into feature-OCR
ZiadAbdElFatah 0ea7950
Merge branch 'main' into feature-OCR
ZiadAbdElFatah 9c06e02
Addressed the review comments
ZiadAbdElFatah 8a92bfe
Reverted unintended change
ZiadAbdElFatah 8a6e9be
Addressed the review comments
ZiadAbdElFatah e0311c6
Solved checkstyle failing check
ZiadAbdElFatah d8610e6
Added linking the new OCRed file to the used entry
ZiadAbdElFatah b3a58f6
Addressed the review commits
ZiadAbdElFatah dd79d9e
Merge branch 'main' into feature-OCR
ZiadAbdElFatah e0d19e8
Added localized string to JabRef_en.preperties
ZiadAbdElFatah 796b5d4
Added check for OCRMYPDF availability
ZiadAbdElFatah 1a07f5d
Added a comment for readability
ZiadAbdElFatah b3ea8d6
Reduced the time of checking the avaialabilty of OCRmyPDF
ZiadAbdElFatah 9c318d0
Added --skip-text to handle partial searchable pdfs
ZiadAbdElFatah 9c46aac
Update jabgui/src/main/java/org/jabref/gui/linkedfile/OcrLinkedFileAc…
ZiadAbdElFatah f3490ea
Apply suggestion from @InAnYan
ZiadAbdElFatah a9dfa18
Merge branch 'main' into feature-OCR
ZiadAbdElFatah f5ec242
Extracted the wait time in a single variable
ZiadAbdElFatah 239542d
Apply suggestion from @InAnYan
InAnYan 1793269
Merge branch 'main' into feature-OCR
ZiadAbdElFatah 8acb6c5
Added the PDF file type for the new OCRed file
ZiadAbdElFatah afc4b1d
Added some missing strings to JabRef_en.propeties
ZiadAbdElFatah 9d4bd44
Fix comment formatting for LinkedFile constructor
ZiadAbdElFatah 72d33fa
Merge branch 'JabRef:main' into feature-OCR
ZiadAbdElFatah e843ab8
Addressed some openrewrite comments
ZiadAbdElFatah e6b51fe
Added a changelog entry
ZiadAbdElFatah 11de63b
Merge branch 'main' into feature-OCR
ZiadAbdElFatah ce515cd
Link OCRed file to entries
ZiadAbdElFatah b548fb7
Improve OCRed file linking
ZiadAbdElFatah 49c9b15
Remove unintended file
ZiadAbdElFatah 4434905
Merge branch 'main' into feature-OCR
ZiadAbdElFatah a984da7
Address review comments
ZiadAbdElFatah 7fd2621
Fix comment formatting in LinkedFile constructor
ZiadAbdElFatah c584be5
Fix comment formatting in LinkedFile constructor
ZiadAbdElFatah 3cca2fb
Merge branch 'main' into feature-OCR
ZiadAbdElFatah 199a8b4
Address review comment
ZiadAbdElFatah 84a7f97
fix(ocr): fix failure message
InAnYan File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -48,6 +48,7 @@ public enum KeyBinding { | |
| // We have to put Entry Editor Previous before, because otherwise the decrease font size is found first | ||
| ENTRY_EDITOR_PREVIOUS_PANEL_2("Entry editor, previous panel 2", Localization.lang("Entry editor, previous panel 2"), "shortcut+MINUS", KeyBindingCategory.VIEW), | ||
| DELETE_ENTRY("Delete entry", Localization.lang("Delete entry"), "DELETE", KeyBindingCategory.BIBTEX), | ||
| PERFORM_OCR("Perform OCR", Localization.lang("Perform OCR"), "", KeyBindingCategory.FILE), | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this should have an appropriate shortcut. Pinging @Siedlerchr for suggestions. |
||
| DEFAULT_DIALOG_ACTION("Execute default action in dialog", Localization.lang("Execute default action in dialog"), "shortcut+ENTER", KeyBindingCategory.VIEW), | ||
| DOWNLOAD_FULL_TEXT("Download full text documents", Localization.lang("Download full text documents"), "alt+F7", KeyBindingCategory.QUALITY), | ||
| OPEN_CLOSE_ENTRY_EDITOR("Open / close entry editor", Localization.lang("Open / close entry editor"), "shortcut+E", KeyBindingCategory.VIEW), | ||
|
|
||
114 changes: 114 additions & 0 deletions
114
jabgui/src/main/java/org/jabref/gui/linkedfile/OcrLinkedFileAction.java
This file contains hidden or 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,114 @@ | ||
| package org.jabref.gui.linkedfile; | ||
|
|
||
| import java.nio.file.Path; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| import javax.swing.undo.UndoManager; | ||
|
|
||
| import org.jabref.gui.DialogService; | ||
| import org.jabref.gui.StateManager; | ||
| import org.jabref.gui.actions.SimpleCommand; | ||
| import org.jabref.gui.externalfiles.ImportHandler; | ||
| import org.jabref.gui.preferences.GuiPreferences; | ||
| import org.jabref.logic.l10n.Localization; | ||
| import org.jabref.logic.ocr.OcrEngine; | ||
| import org.jabref.logic.ocr.OcrMyPdfEngine; | ||
| import org.jabref.logic.ocr.OcrResult; | ||
| import org.jabref.logic.util.BackgroundTask; | ||
| import org.jabref.logic.util.TaskExecutor; | ||
| import org.jabref.model.database.BibDatabaseContext; | ||
| import org.jabref.model.entry.BibEntry; | ||
| import org.jabref.model.entry.LinkedFile; | ||
| import org.jabref.model.util.FileUpdateMonitor; | ||
|
|
||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| public class OcrLinkedFileAction extends SimpleCommand { | ||
| private static final Logger LOGGER = LoggerFactory.getLogger(OcrLinkedFileAction.class); | ||
|
|
||
| private final LinkedFile linkedFile; | ||
| private final BibDatabaseContext databaseContext; | ||
| private final DialogService dialogService; | ||
| private final GuiPreferences preferences; | ||
| private final TaskExecutor taskExecutor; | ||
| private final OcrEngine ocrEngine; | ||
| private final List<BibEntry> linkedEntries; | ||
| private final ImportHandler importHandler; | ||
|
|
||
| public OcrLinkedFileAction(LinkedFile linkedFile, | ||
| List<BibEntry> bibEntries, | ||
|
InAnYan marked this conversation as resolved.
|
||
| BibDatabaseContext databaseContext, | ||
| DialogService dialogService, | ||
| GuiPreferences preferences, | ||
| TaskExecutor taskExecutor, | ||
| FileUpdateMonitor fileUpdateMonitor, | ||
| UndoManager undoManager, | ||
| StateManager stateManager) { | ||
| this.linkedFile = linkedFile; | ||
| this.linkedEntries = bibEntries; | ||
| this.databaseContext = databaseContext; | ||
| this.dialogService = dialogService; | ||
| this.preferences = preferences; | ||
| this.taskExecutor = taskExecutor; | ||
| this.ocrEngine = new OcrMyPdfEngine(); | ||
| this.importHandler = new ImportHandler( | ||
| databaseContext, | ||
| preferences, | ||
| fileUpdateMonitor, | ||
| undoManager, | ||
| stateManager, | ||
| dialogService, | ||
| taskExecutor | ||
| ); | ||
| } | ||
|
|
||
| @Override | ||
| public void execute() { | ||
| Optional<Path> pdfPath = linkedFile.findIn(databaseContext, preferences.getFilePreferences()); | ||
| if (pdfPath.isEmpty()) { | ||
| dialogService.showErrorDialogAndWait(Localization.lang("Could not find a file to OCR")); | ||
| return; | ||
| } | ||
| BackgroundTask<OcrResult> ocrTask = BackgroundTask.wrap(() -> ocrEngine.performOcrAndEmbedText(pdfPath.get())); | ||
|
qodo-free-for-open-source-projects[bot] marked this conversation as resolved.
|
||
|
|
||
| ocrTask.titleProperty().set(Localization.lang("Performing OCR")); | ||
| ocrTask.showToUser(true); | ||
| ocrTask.onSuccess(result -> { | ||
| switch (result) { | ||
| case OcrResult.Success success -> { | ||
| dialogService.notify(Localization.lang("OCR succeeded")); | ||
| Path ocredPdf = success.outputFile(); | ||
| for (BibEntry entry : linkedEntries) { | ||
| importHandler.getFileLinker().linkFilesToEntry(entry, List.of(ocredPdf)); | ||
| } | ||
| } | ||
|
qodo-free-for-open-source-projects[bot] marked this conversation as resolved.
|
||
| case OcrResult.Failure failure -> { | ||
| String failureReason = getFailureResult(failure); | ||
| dialogService.showErrorDialogAndWait(Localization.lang("OCR failed"), failureReason); | ||
| } | ||
| } | ||
| }); | ||
| ocrTask.onFailure(exception -> { | ||
| LOGGER.error("Unexpected error during OCR", exception); | ||
| dialogService.notify(Localization.lang("OCR failed. See the logs for the details")); | ||
| }); | ||
| taskExecutor.execute(ocrTask); | ||
| } | ||
|
|
||
| String getFailureResult(OcrResult.Failure failure) { | ||
| return switch (failure.reason()) { | ||
| case NOT_AVAILABLE -> | ||
| Localization.lang("OCRmyPDF is not available"); | ||
| case TIMEOUT -> | ||
| Localization.lang("OCR timed out"); | ||
| case NON_ZERO_EXIT -> | ||
| Localization.lang("OCR process failed"); | ||
| case IO_ERROR -> | ||
| Localization.lang("Could not start OCR process"); | ||
| case INTERRUPTED -> | ||
| Localization.lang("OCR was cancelled"); | ||
| }; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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,25 @@ | ||
| package org.jabref.logic.ocr; | ||
|
|
||
| import java.nio.file.Path; | ||
|
|
||
| /// Interface for OCR engines. | ||
| /// | ||
| /// Any engine in the future can implement this interface. | ||
| public interface OcrEngine { | ||
|
|
||
| /// Performs OCR on the given input file and returns the result, whether the path of the OCRed with searchable text file or the error message. | ||
| /// | ||
| /// @param pdfPath the file to perform OCR on. | ||
| /// @return the result of the OCR operation with the extracted text or an error message. | ||
| OcrResult performOcrAndEmbedText(Path pdfPath); | ||
|
|
||
| /// Checks if the OCR engine is available for use. | ||
| /// | ||
| /// @return true if the engine is available, false otherwise. | ||
| boolean isAvailable(); | ||
|
|
||
| /// Gets the name of the OCR engine. | ||
| /// | ||
| /// @return the name of the OCR engine (e.g., "OCRmyPDF", "Tesseract"). | ||
| String getName(); | ||
| } |
6 changes: 6 additions & 0 deletions
6
jablib/src/main/java/org/jabref/logic/ocr/OcrFailureReason.java
This file contains hidden or 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,6 @@ | ||
| package org.jabref.logic.ocr; | ||
|
|
||
| /// Enums for the reasons that can lead the OCR process to fail. | ||
| public enum OcrFailureReason { | ||
| NOT_AVAILABLE, TIMEOUT, NON_ZERO_EXIT, IO_ERROR, INTERRUPTED | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should change "Perform OCR" in the message shown to the user. I think most of the users don't know what OCR is.
I think "Make the PDF searchable" or something like this would be better.