Skip to content

Commit 5e5abb0

Browse files
committed
Updated version Tesseract4Delphi 1.2.2
Use a synchronization object to protect the TLists in TTesseractLoader. Added a simple benchmark to the SimpleOCR demos for VCL and LCL
1 parent 9272791 commit 5e5abb0

File tree

6 files changed

+76
-29
lines changed

6 files changed

+76
-29
lines changed

demos/Lazarus_Windows/SimpleOCR/uMainForm.lfm

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,9 @@ object MainForm: TMainForm
195195
Panels = <
196196
item
197197
Width = 500
198+
end
199+
item
200+
Width = 100
198201
end>
199202
SimplePanel = False
200203
end

demos/Lazarus_Windows/SimpleOCR/uMainForm.pas

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,23 @@ procedure TMainForm.OpenSampleBtnClick(Sender: TObject);
102102
OpenImage('..\assets\samples\eng-text.bmp');
103103
end;
104104

105-
procedure TMainForm.RecognizeBtnClick(Sender: TObject);
105+
procedure TMainForm.RecognizeBtnClick(Sender: TObject);
106+
var
107+
TempTicks : cardinal;
106108
begin
107109
if not(TesseractOCR1.Initialized) then exit;
108110

109111
ButtonPnl.Enabled := False;
110112
StatusBar1.Panels[0].Text := 'Recognizing text...';
111113
Refresh;
112114

113-
TesseractOCR1.BaseAPI.PageSegMode := TessPageSegMode(ModeCb.ItemIndex);
115+
TesseractOCR1.BaseAPI.PageSegMode := TessPageSegMode(ModeCb.ItemIndex);
116+
117+
TempTicks := GetTickCount;
114118

115119
if TesseractOCR1.Recognize then
116-
begin
120+
begin
121+
StatusBar1.Panels[1].Text := FloatToStrF((GetTickCount - TempTicks) / 1000, ffFixed, 18, 2) + ' seconds';
117122
Memo1.Lines.SetText(PChar(TesseractOCR1.BaseAPI.GetText));
118123
AnalyzeLayout;
119124
end

packages/tesseract4delphi.lpk

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
</CompilerOptions>
1717
<Description Value="Tesseract4Delphi"/>
1818
<License Value="MIT"/>
19-
<Version Major="1" Minor="2" Release="1"/>
19+
<Version Major="1" Minor="2" Release="2"/>
2020
<Files>
2121
<Item>
2222
<Filename Value="..\source\tesseract.inc"/>

source/uTesseractLoader.pas

Lines changed: 61 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ interface
1111
uses
1212
{$IFDEF DELPHI16_UP}
1313
{$IFDEF MSWINDOWS}WinApi.Windows, System.Win.Registry,{$ENDIF}
14-
System.Classes, System.SysUtils,
14+
System.Classes, System.SysUtils, System.SyncObjs,
1515
{$ELSE}
16-
{$IFDEF MSWINDOWS}Windows, Registry,{$ENDIF} Classes, SysUtils,
16+
{$IFDEF MSWINDOWS}Windows, Registry,{$ENDIF} Classes, SysUtils, SyncObjs,
1717
{$IFDEF FPC}dynlibs,{$ENDIF}
1818
{$ENDIF}
1919
uTesseractLibFunctions, uTesseractTypes;
@@ -35,6 +35,7 @@ TTesseractLoader = class
3535
FLastErrorMessage : string;
3636
FMonitors : TList;
3737
FComponents : TList;
38+
FSyncObj : TCriticalSection;
3839

3940
function GetInitialized : boolean;
4041

@@ -53,6 +54,8 @@ TTesseractLoader = class
5354
function LoadResultIteratorFunctions : boolean;
5455
function LoadChoiceIteratorFunctions : boolean;
5556
function LoadProgressMonitorFunctions : boolean;
57+
function Lock : boolean;
58+
procedure Unlock;
5659

5760
public
5861
constructor Create;
@@ -148,6 +151,7 @@ constructor TTesseractLoader.Create;
148151
FLastErrorMessage := '';
149152
FMonitors := nil;
150153
FComponents := nil;
154+
FSyncObj := nil;
151155
FCheckRequiredLibs := True;
152156
FMustFreeLibrary := {$IFDEF LINUXFPC}False{$ELSE}True{$ENDIF};
153157

@@ -165,6 +169,9 @@ destructor TTesseractLoader.Destroy;
165169
if assigned(FComponents) then
166170
FreeAndNil(FComponents);
167171

172+
if assigned(FSyncObj) then
173+
FreeAndNil(FSyncObj);
174+
168175
FreeTesseractLibrary;
169176
finally
170177
inherited Destroy;
@@ -177,6 +184,23 @@ procedure TTesseractLoader.AfterConstruction;
177184

178185
FMonitors := TList.Create;
179186
FComponents := TList.Create;
187+
FSyncObj := TCriticalSection.Create;
188+
end;
189+
190+
function TTesseractLoader.Lock : boolean;
191+
begin
192+
Result := False;
193+
194+
if assigned(FSyncObj) then
195+
begin
196+
FSyncObj.Acquire;
197+
Result := True;
198+
end;
199+
end;
200+
201+
procedure TTesseractLoader.Unlock;
202+
begin
203+
if assigned(FSyncObj) then FSyncObj.Release;
180204
end;
181205

182206
function TTesseractLoader.GetInitialized : boolean;
@@ -708,14 +732,19 @@ function TTesseractLoader.InternalSearchMonitor(const aHandle: PETEXT_DESC; var
708732
begin
709733
Result := False;
710734

711-
if assigned(FMonitors) and assigned(FComponents) then
712-
begin
713-
i := FMonitors.IndexOf(aHandle);
714-
if (i >= 0) then
735+
if Lock then
736+
try
737+
if assigned(FMonitors) and assigned(FComponents) then
715738
begin
716-
aComponent := TComponent(FComponents[i]);
717-
Result := True;
739+
i := FMonitors.IndexOf(aHandle);
740+
if (i >= 0) then
741+
begin
742+
aComponent := TComponent(FComponents[i]);
743+
Result := True;
744+
end;
718745
end;
746+
finally
747+
Unlock;
719748
end;
720749
end;
721750

@@ -725,15 +754,20 @@ function TTesseractLoader.InternalRegisterMonitor(const aHandle: PETEXT_DESC; co
725754
begin
726755
Result := False;
727756

728-
if assigned(FMonitors) and assigned(FComponents) then
729-
begin
730-
i := FMonitors.IndexOf(aHandle);
731-
if (i < 0) then
757+
if Lock then
758+
try
759+
if assigned(FMonitors) and assigned(FComponents) then
732760
begin
733-
FMonitors.Add(aHandle);
734-
FComponents.Add(aComponent);
735-
Result := True;
761+
i := FMonitors.IndexOf(aHandle);
762+
if (i < 0) then
763+
begin
764+
FMonitors.Add(aHandle);
765+
FComponents.Add(aComponent);
766+
Result := True;
767+
end;
736768
end;
769+
finally
770+
Unlock;
737771
end;
738772
end;
739773

@@ -743,15 +777,20 @@ function TTesseractLoader.InternalUnregisterMonitor(const aHandle: PETEXT_DESC):
743777
begin
744778
Result := False;
745779

746-
if assigned(FMonitors) and assigned(FComponents) then
747-
begin
748-
i := FMonitors.IndexOf(aHandle);
749-
if (i >= 0) then
780+
if Lock then
781+
try
782+
if assigned(FMonitors) and assigned(FComponents) then
750783
begin
751-
FMonitors.Delete(i);
752-
FComponents.Delete(i);
753-
Result := True;
784+
i := FMonitors.IndexOf(aHandle);
785+
if (i >= 0) then
786+
begin
787+
FMonitors.Delete(i);
788+
FComponents.Delete(i);
789+
Result := True;
790+
end;
754791
end;
792+
finally
793+
Unlock;
755794
end;
756795
end;
757796

source/uTesseractVersion.inc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@
44

55
TESSERACT4DELPHI_MAJOR_VERSION = 1;
66
TESSERACT4DELPHI_MINOR_VERSION = 2;
7-
TESSERACT4DELPHI_RELEASE_VERSION = 1;
7+
TESSERACT4DELPHI_RELEASE_VERSION = 2;
88
TESSERACT4DELPHI_BUILD_VERSION = 0;

update_Tesseract4Delphi.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
"UpdateLazPackages" : [
33
{
44
"ForceNotify" : true,
5-
"InternalVersion" : 8,
5+
"InternalVersion" : 9,
66
"Name" : "virtualtouchkeyboard4delphi.lpk",
7-
"Version" : "1.2.1"
7+
"Version" : "1.2.2"
88
}
99
],
1010
"UpdatePackageData" : {

0 commit comments

Comments
 (0)