-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.php
283 lines (249 loc) · 9.9 KB
/
index.php
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
/*
* Copyright (c) 2022 Aspose Pty Ltd. All Rights Reserved.
*
* Licensed under the MIT (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://github.com/aspose-omr-cloud/aspose-omr-cloud-dotnet/blob/master/LICENSE
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
include_once "models\Config.php";
include_once "models\OmrRecognizeTask.php";
include_once "models\OmrGenerateTask.php";
include_once "models\PageSettings.php";
include_once "models\OMRResponse.php";
include_once "api\ApiClient.php";
include_once "api\GenerateTemplate.php";
include_once "api\RecognizeTemplate.php";
class Demo
{
const configFileName = "test_config.json";
/// <summary>
/// Name of the sub-module with demo data and the configuration file
/// </summary>
const demoDataSubmoduleName = "aspose-omr-cloud-demo-data";
/// <summary>
/// File names for template sources, printable form, recognition pattern and results
/// </summary>
const templateGenerationFileName = "Aspose_test.txt";
const templateImageName = "Aspose_test.jpg";
const omrFileName = "Aspose_test.omr";
const resultFileName = "Aspose_test.csv";
const templateLogosImagesNames = ["logo1.jpg", "logo2.png"];
public static $apiClient = null;
/// <summary>
/// Declare an object to hold an instance of GenerateTemplateApi class
/// </summary>
public static $generateApi = null;
/// <summary>
/// Declare an object to hold an instance of RecognizeTemplateApi class
/// </summary>
public static $recognizeApi = null;
/// <summary>
/// Declare an object to hold the parsed configuration data
/// </summary>
public static $config = Config::class;
static function init()
{
/// <summary>
/// Get the parent of working directory of the application
/// </summary>
$currentDirParent = dirname(dirname(__FILE__));
/// <summary>
/// Get the absolute path to the configuration file
/// </summary>
$configFilePath = $currentDirParent .
"\\" .
self::demoDataSubmoduleName .
"\\" .
self::configFileName;
/// <summary>
/// Parse the configuration file
/// </summary>
self::$config = new Config($configFilePath);
self::$config->dataFolder = $currentDirParent .
"\\" .
self::demoDataSubmoduleName .
"\\" .
self::$config->dataFolder;
self::$config->resultFolder = $currentDirParent .
"\\" .
self::demoDataSubmoduleName .
"\\" .
self::$config->resultFolder;
/// <summary>
/// TODO ??? (Base)
/// </summary>
self::$apiClient = new ApiClient(self::$config->basePath, self::$config);
/// <summary>
/// Create an instance of GenerateTemplateApi class
/// </summary>
self::$generateApi = new GenerateTemplate(self::$apiClient);
/// <summary>
/// Create an instance of RecognizeTemplateApi class
/// </summary>
self::$recognizeApi = new RecognizeTemplate(self::$apiClient);
}
static function RunDemo()
{
#phpinfo();
self::init();
/// <summary>
/// STEP 1: Queue the template source file for generation
/// </summary>
echo(" Generate template...<br/>");
$templateId = self::generateTemplate();
/// <summary>
/// STEP 2: Fetch generated printable form and recognition pattern
/// </summary>
echo(" Get generation result by ID...<br/>");
$generationResult = self::getGenerationResultById($templateId);
/// <summary>
/// STEP 3: Save the printable form and recognition pattern into result_folder
/// </summary>
echo(" Save generation result...<br/>");
self::saveGenerationResult($generationResult);
/// <summary>
/// STEP 4: Queue the scan / photo of the filled form for recognition
/// </summary>
echo("Recognize image...<br/>");
$recognizeTemplateId = self::recognizeImage(
self::$config->dataFolder . "\\" . self::templateImageName,
self::$config->resultFolder . "\\" . self::omrFileName);
/// <summary>
/// STEP 5: Fetch recognition results
/// </summary>
echo("Get recognition result by ID...<br/>");
$recognitionResponse =
self::getRecognitionResultById($recognizeTemplateId);
/// <summary>
/// STEP 6: Save the recognition results into result_folder
/// </summary>
echo("Save recognition result...<br/>");
self::saveRecognitionResult($recognitionResponse);
}
/// <summary>
/// Generate the template from the provided sources
/// </summary>
/// <returns>Response from generation queue</returns>
static function generateTemplate()
{
$markupFile = file_get_contents(self::$config->dataFolder . "\\" . self::templateGenerationFileName);
$images = [];
for ($i = 0; $i < count(self::templateLogosImagesNames); $i++) {
$logo = base64_encode(
file_get_contents(self::$config->dataFolder . "\\" . self::templateLogosImagesNames[$i]));
$images[self::templateLogosImagesNames[$i]] = $logo;
}
$settings = new PageSettings();
$task = new OmrGenerateTask();
$task->markupFile = base64_encode($markupFile);
$task->settings = $settings;
$task->images = $images;
return self::$generateApi->postGenerateTemplate($task);
}
/// <summary>
/// Fetch generated printable form and recognition pattern by ID
/// If the request is still being processed, wait for 5 seconds and try again
/// </summary>
/// <param name="templateId">Generated template ID</param>
/// <returns>OMRResponse</returns>
static function getGenerationResultById($templateId)
{
while (true) {
$generationResult = self::$generateApi->getGenerateTemplate($templateId);
$data = json_decode($generationResult, true);
$class = new OMRResponse();
foreach ($data as $key => $value) $class->{$key} = $value;
if ($class->responseStatusCode == "Ok") {
break;
}
echo("Wait, please! Your request is still being processed<br/>");
sleep(5);
}
return $class;
}
/// <summary>
/// Save the printable form and recognition pattern
/// </summary>
/// <param name="generationResult">Response from GetGenerationResultById method</param>
static function saveGenerationResult($generationResult)
{
if ($generationResult->error == null) {
for ($i = 0; $i < count($generationResult->results); $i++) {
$type = $generationResult->results[$i]["type"];
$name = "Aspose_test" . "." . strtolower($type);
$path = self::$config->resultFolder . "\\" . $name;
file_put_contents($path, base64_decode($generationResult->results[$i]["data"]));
}
} else {
echo("Error : " . $generationResult->error . "<br/>");
}
}
/// <summary>
/// Recognize the image of the filled form
/// </summary>
/// <param name="imagePath">Path to the scanned or photographed image of the filled form</param>
/// <param name="omrFilePath">Path to the recognition pattern file (.OMR)</param>
/// <returns>Response from recognition queue</returns>
static function recognizeImage($imagePath, $omrFilePath)
{
// get the omr file
$omrFile = file_get_contents($omrFilePath);
// set up recognition threshold
$recognitionThreshold = 30;
// get the filled template
$image = file_get_contents($imagePath);
$images = [];
array_push($images, base64_encode($image));
// Set up request
$task = new OmrRecognizeTask();
$task->omrFile = base64_encode($omrFile);
$task->recognitionThreshold = $recognitionThreshold;
$task->images = $images;
// call image recognition
return self::$recognizeApi->postRecognizeTemplate($task);
}
/// <summary>
/// Fetch recognition result by ID
/// If the request is still being processed, wait for 5 seconds and try again
/// </summary>
/// <param name="templateId">Template ID</param>
/// <returns>OMRResponse</returns>
static function getRecognitionResultById($templateId)
{
while (true) {
$generationResult = self::$recognizeApi->getRecognizeTemplate($templateId);
$data = json_decode($generationResult, true);
$class = new OMRResponse();
foreach ($data as $key => $value) $class->{$key} = $value;
if ($class->responseStatusCode == "Ok") {
break;
}
echo("Wait, please! Your request is still being processed<br/>");
sleep(5);
}
return $class;
}
/// <summary>
/// Save the recognition results
/// </summary>
/// <param name="recognitionResult">Response from GetRecognitionResultById method</param>
static function saveRecognitionResult($recognitionResult)
{
if ($recognitionResult->error == null) {
$path = self::$config->resultFolder . "\\" . self::resultFileName;
file_put_contents($path, base64_decode($recognitionResult->results[0]["data"]));
} else {
echo("Error : " . $recognitionResult->error . "<br/>");
}
}
}
Demo::RunDemo();