Skip to content

Commit

Permalink
Add a separate checkbox for OCR preprocessing
Browse files Browse the repository at this point in the history
  • Loading branch information
cyanfish committed Feb 25, 2024
1 parent 3304325 commit c8eab25
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 44 deletions.
3 changes: 3 additions & 0 deletions NAPS2.Lib/Config/CommonConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,9 @@ public class CommonConfig
[Common]
public LocalizedOcrMode OcrMode { get; set; }

[Common]
public bool OcrPreProcessing { get; set; }

[Common]
public bool OcrAfterScanning { get; set; }

Expand Down
14 changes: 7 additions & 7 deletions NAPS2.Lib/Config/ConfigExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,18 @@ public static OcrParams DefaultOcrParams(this Naps2Config config)
}
return new OcrParams(
config.Get(c => c.OcrLanguageCode),
MapOcrMode(config.Get(c => c.OcrMode)),
MapOcrMode(config.Get(c => c.OcrMode), config.Get(c => c.OcrPreProcessing)),
config.Get(c => c.OcrTimeoutInSeconds));
}

private static OcrMode MapOcrMode(LocalizedOcrMode ocrMode)
private static OcrMode MapOcrMode(LocalizedOcrMode ocrMode, bool preProcessing)
{
return ocrMode switch
return (ocrMode, preProcessing) switch
{
LocalizedOcrMode.Fast => OcrMode.Fast,
LocalizedOcrMode.FastWithPreProcess => OcrMode.FastWithPreProcess,
LocalizedOcrMode.Best => OcrMode.BestWithPreProcess,
LocalizedOcrMode.BestWithPreProcess => OcrMode.BestWithPreProcess,
(LocalizedOcrMode.Fast, false) => OcrMode.Fast,
(LocalizedOcrMode.Fast, true) => OcrMode.FastWithPreProcess,
(LocalizedOcrMode.Best, false) => OcrMode.Best,
(LocalizedOcrMode.Best, true) => OcrMode.BestWithPreProcess,
_ => OcrMode.Default
};
}
Expand Down
1 change: 1 addition & 0 deletions NAPS2.Lib/Config/InternalDefaults.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ public static CommonConfig GetCommonConfig() =>
OcrLanguageCode = "",
LastOcrMultiLangCode = "",
OcrMode = LocalizedOcrMode.Fast,
OcrPreProcessing = false,
OcrAfterScanning = true,
LastImageExt = "",
LastPdfOrImageExt = "",
Expand Down
8 changes: 6 additions & 2 deletions NAPS2.Lib/EtoForms/Ui/OcrSetupForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ public class OcrSetupForm : EtoDialogBase

private readonly CheckBox _enableOcr = C.CheckBox(UiStrings.MakePdfsSearchable);
private readonly DropDown _ocrLang = C.DropDown();
private readonly DropDown _ocrMode = C.EnumDropDown(LocalizedOcrMode.Fast, LocalizedOcrMode.FastWithPreProcess,
LocalizedOcrMode.Best, LocalizedOcrMode.BestWithPreProcess);
private readonly DropDown _ocrMode = C.EnumDropDown(LocalizedOcrMode.Fast, LocalizedOcrMode.Best);
private readonly CheckBox _ocrPreProcessing = C.CheckBox(UiStrings.OcrPreProcessing);
private readonly CheckBox _ocrAfterScanning = C.CheckBox(UiStrings.RunOcrAfterScanning);
private readonly LinkButton _moreLanguages = C.Link(UiStrings.GetMoreLanguages);

Expand Down Expand Up @@ -42,6 +42,7 @@ public OcrSetupForm(Naps2Config config, TesseractLanguageManager tesseractLangua
_ocrLang.SelectedIndexChanged += OcrLang_SelectedIndexChanged;
_ocrMode.SelectedIndex = (int) configOcrMode;
if (_ocrMode.SelectedIndex == -1) _ocrMode.SelectedIndex = 0;
_ocrPreProcessing.Checked = Config.Get(c => c.OcrPreProcessing);
_ocrAfterScanning.Checked = Config.Get(c => c.OcrAfterScanning);

LoadLanguages();
Expand All @@ -65,6 +66,7 @@ protected override void BuildLayout()
C.Label(UiStrings.OcrModeLabel).AlignCenter().Padding(right: 40),
_ocrMode.Scale()
).Aligned(),
_ocrPreProcessing,
_ocrAfterScanning,
C.Filler(),
L.Row(
Expand Down Expand Up @@ -125,6 +127,7 @@ private void UpdateView()
_enableOcr.Enabled = !Config.AppLocked.Has(c => c.EnableOcr);
_ocrLang.Enabled = isEnabled && !Config.AppLocked.Has(c => c.OcrLanguageCode);
_ocrMode.Enabled = isEnabled && !Config.AppLocked.Has(c => c.OcrMode);
_ocrPreProcessing.Enabled = isEnabled && !Config.AppLocked.Has(c => c.OcrPreProcessing);
_ocrAfterScanning.Enabled = isEnabled && !Config.AppLocked.Has(c => c.OcrAfterScanning);
_moreLanguages.Enabled = !Config.AppLocked.Has(c => c.OcrLanguageCode);
}
Expand Down Expand Up @@ -172,6 +175,7 @@ private void Save()
Config.User.Set(c => c.LastOcrMultiLangCode, _multiLangCode);
}
transact.Set(c => c.OcrMode, (LocalizedOcrMode) _ocrMode.SelectedIndex);
transact.Set(c => c.OcrPreProcessing, _ocrPreProcessing.IsChecked());
transact.Set(c => c.OcrAfterScanning, _ocrAfterScanning.IsChecked());
transact.Commit();
}
Expand Down
18 changes: 0 additions & 18 deletions NAPS2.Lib/Lang/Resources/SettingsResources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 0 additions & 6 deletions NAPS2.Lib/Lang/Resources/SettingsResources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -267,15 +267,9 @@
<data name="OcrMode_Fast" xml:space="preserve">
<value>Fast</value>
</data>
<data name="OcrMode_FastWithPreProcess" xml:space="preserve">
<value>Fast + Pre-Processing</value>
</data>
<data name="OcrMode_Best" xml:space="preserve">
<value>Best</value>
</data>
<data name="OcrMode_BestWithPreProcess" xml:space="preserve">
<value>Best + Pre-Processing</value>
</data>
<data name="SaveButtonDefaultAction_SaveAll" xml:space="preserve">
<value>Save All</value>
</data>
Expand Down
9 changes: 9 additions & 0 deletions NAPS2.Lib/Lang/Resources/UiStrings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions NAPS2.Lib/Lang/Resources/UiStrings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,9 @@
<data name="OcrModeLabel" xml:space="preserve">
<value>OCR mode:</value>
</data>
<data name="OcrPreProcessing" xml:space="preserve">
<value>Fix white balance and remove noise</value>
</data>
<data name="RunOcrAfterScanning" xml:space="preserve">
<value>Automatically run OCR after scanning</value>
</data>
Expand Down
4 changes: 0 additions & 4 deletions NAPS2.Lib/Ocr/LocalizedOcrMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,7 @@ public enum LocalizedOcrMode
{
[LocalizedDescription(typeof(SettingsResources), "OcrMode_Fast")]
Fast,
[LocalizedDescription(typeof(SettingsResources), "OcrMode_FastWithPreProcess")]
FastWithPreProcess,
[LocalizedDescription(typeof(SettingsResources), "OcrMode_Best")]
Best,
[LocalizedDescription(typeof(SettingsResources), "OcrMode_BestWithPreProcess")]
BestWithPreProcess,
Legacy // Deprecated, not mapped to the Sdk OcrMode
}
12 changes: 7 additions & 5 deletions NAPS2.Sdk/Ocr/OcrMode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
/// <summary>
/// The mode of an OCR request (fast/best), if supported by the engine.
/// </summary>
[Flags]
public enum OcrMode
{
Default,
Fast,
FastWithPreProcess,
Best,
BestWithPreProcess
Default = 0,
Fast = 1,
Best = 2,
WithPreProcess = 4,
FastWithPreProcess = Fast | WithPreProcess,
BestWithPreProcess = Best | WithPreProcess
}
4 changes: 2 additions & 2 deletions NAPS2.Sdk/Ocr/TesseractOcrEngine.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ private TesseractOcrEngine(string tesseractPath, string? languageDataBasePath =
string tempHocrFilePathWithExt = tempHocrFilePath + ".hocr";
try
{
if (ocrParams.Mode is OcrMode.FastWithPreProcess or OcrMode.BestWithPreProcess)
if (ocrParams.Mode.HasFlag(OcrMode.WithPreProcess))
{
PreProcessImage(scanningContext, imagePath);
}
Expand All @@ -88,7 +88,7 @@ private TesseractOcrEngine(string tesseractPath, string? languageDataBasePath =
string languageDataPath = _languageDataBasePath;
if (_withModes)
{
string subfolder = ocrParams.Mode is OcrMode.Best or OcrMode.BestWithPreProcess ? "best" : "fast";
string subfolder = ocrParams.Mode.HasFlag(OcrMode.Best) ? "best" : "fast";
languageDataPath = Path.Combine(languageDataPath, subfolder);
}
startInfo.EnvironmentVariables["TESSDATA_PREFIX"] = languageDataPath;
Expand Down

0 comments on commit c8eab25

Please sign in to comment.