-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuas_splitterlogic.pas
More file actions
195 lines (158 loc) · 4.61 KB
/
uas_splitterlogic.pas
File metadata and controls
195 lines (158 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
unit UAS_SplitterLogic;
{$mode ObjFPC}{$H+}
interface
uses
Classes, SysUtils, INIFiles, FPImage, MouseAndKeyInput, LCLType, UAS_GraphicUtils;
type
{ TSplitElement }
TSplitElement = class
private
FTitle: string;
FImage: TFPCustomImage;
FSimilarity: double;
FKeyInput: word;
FWaitMillis: integer;
public
constructor Create(imagefilename: string);
destructor Destroy; override;
function CalculateSimilarity(Frame: TFPCustomImage): double;
property Title: string read FTitle write FTitle;
property Similarity: double read FSimilarity write FSimilarity;
property KeyInput: word read FKeyInput write FKeyInput;
property WaitMillis: integer read FWaitMillis write FWaitMillis;
end;
{ TSplitter }
TSplitter = class
private
FElements: array of TSplitElement;
FElementCount, FCurrentElement: integer;
FElementSetTime: int64;
FLogFolder: string;
function GetCurrentElement: TSplitElement;
public
constructor Create(inifilename: string);
destructor Destroy; override;
procedure SetCurrentElement(CurrentElement: integer);
function Process(Frame: TFPCustomImage): double;
property CurrentElement: TSplitElement read GetCurrentElement;
property ElementCount: integer read FElementCount;
end;
implementation
{ TSplitElement }
constructor TSplitElement.Create(imagefilename: string);
begin
FImage := TFPMemoryImage.Create(0, 0);
FImage.LoadFromFile(imagefilename);
end;
destructor TSplitElement.Destroy;
begin
FImage.Free;
inherited Destroy;
end;
function TSplitElement.CalculateSimilarity(Frame: TFPCustomImage): double;
begin
if Frame.Width * Frame.Height < FImage.Width * FImage.Height then
begin
Result := CalculateImageSimilarity(Frame, FImage);
end
else
begin
Result := CalculateImageSimilarity(FImage, Frame);
end;
end;
{ TSplitter }
function TSplitter.GetCurrentElement: TSplitElement;
begin
Result := FElements[FCurrentElement];
end;
constructor TSplitter.Create(inifilename: string);
var
iniFile: TINIFile;
CurrentIniSection: string;
begin
FElementCount := 0;
SetLength(FElements, FElementCount);
FCurrentElement := 0;
FElementSetTime := GetTickCount64;
iniFile := TINIFile.Create(inifilename);
FLogFolder := iniFile.ReadString('global', 'logs', '');
if FLogFolder <> '' then
begin
FLogFolder := IncludeTrailingPathDelimiter(FLogFolder);
ForceDirectories(FLogFolder);
end;
CurrentIniSection := 'split' + IntToStr(FElementCount);
while iniFile.SectionExists(CurrentIniSection) do
begin
SetLength(FElements, FElementCount + 1);
FElements[FElementCount] :=
TSplitElement.Create(iniFile.ReadString(CurrentIniSection, 'image', ''));
FElements[FElementCount].Title :=
iniFile.ReadString(CurrentIniSection, 'title', CurrentIniSection);
FElements[FElementCount].Similarity :=
iniFile.ReadFloat(CurrentIniSection, 'similarity', 0.95);
FElements[FElementCount].KeyInput :=
iniFile.ReadInteger(CurrentIniSection, 'keyinput', 0);
FElements[FElementCount].WaitMillis :=
iniFile.ReadInteger(CurrentIniSection, 'waitmillis', 0);
Inc(FElementCount);
CurrentIniSection := 'split' + IntToStr(FElementCount);
end;
iniFile.Free;
end;
destructor TSplitter.Destroy;
var
i: integer;
begin
for i := 0 to FElementCount - 1 do
begin
FElements[i].Free;
end;
inherited Destroy;
end;
procedure TSplitter.SetCurrentElement(CurrentElement: integer);
begin
if (CurrentElement >= 0) and (CurrentElement < FElementCount) then
begin
FCurrentElement := CurrentElement;
FElementSetTime := GetTickCount64;
end;
end;
function TSplitter.Process(Frame: TFPCustomImage): double;
var
Element: TSplitElement;
Similarity: double;
LogImg: TFPCustomImage;
begin
Element := FElements[FCurrentElement];
if Element.WaitMillis >= GetTickCount64 - FElementSetTime then
begin
Exit(0.0);
end;
Similarity := Element.CalculateSimilarity(Frame);
if Similarity < Element.Similarity then
begin
Exit(Similarity);
end;
if FLogFolder <> '' then
begin
LogImg := TFPMemoryImage.Create(160, 120);
ScaleImage(Frame, LogImg, LogImg.Width, LogImg.Height);
LogImg.SaveToFile(FLogFolder + FormatDateTime('YY-MM-DD_hh-nn-ss_', Now) +
FloatToStrF(Similarity, ffFixed, 0, 5) + '_' + Element.Title + '.png');
end;
if Element.KeyInput > 0 then
begin
KeyInput.Press(Element.KeyInput);
end;
if FCurrentElement + 1 >= FElementCount then
begin
FCurrentElement := 0;
FElementSetTime := GetTickCount64;
Exit(Similarity);
end;
Inc(FCurrentElement);
FElementSetTime := GetTickCount64;
Exit(Similarity);
end;
end.