-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileManipulating_1.cpp
579 lines (510 loc) · 15.8 KB
/
FileManipulating_1.cpp
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
#include<iostream>
#include "StdAfx.h"
#include "HShell.h"
#include "FileManipulating.h"
#define FRAME_HT 40
#define FRAME_WD 8
#define PI 3.14
using namespace std;
FileManipulating::FileManipulating(void)
{
this->dctArr = new float * [FRAME_HT];
for(int i=0;i<FRAME_HT;i++)
{
dctArr[i] = new float[FRAME_WD];
}
}
System::String* FileManipulating::GetModelName(System::String* filePath)
{
try
{
System::IO::StreamReader* sr = System::IO::StreamReader::Null;
System::IO::StreamWriter* sw = System::IO::StreamWriter::Null;
System::String* tempStr;
// getting the model name
if(!System::IO::File::Exists(filePath))
{
sw = new System::IO::StreamWriter(filePath);
sw->Write("h0");
sw->Close();
}
sr = new System::IO::StreamReader(filePath);
tempStr=sr->ReadLine();
if(tempStr->Length==0)
{
//Forms::MessageBox::Show("Mean Removal Done!!!!"/*fLevel.ToString()*/,"Threshold Value");
// System::Windows::Forms::MessageBox::Show("sdf","Can't load the Combo box!!",System::Windows::Forms::MessageBoxButtons::OK,System::Windows::Forms::MessageBoxIcon::Error);
System::Windows::Forms::MessageBoxButtons::OK;//MessageBox::Show("No model name specified in the file","Can't get the model name!!",System::Windows::Forms::MessageBoxButtons::OK,System::Windows::Forms::MessageBoxIcon::Error);
exit(0);
}
sr->Close();
return tempStr;
}
catch(System::Exception* ex)
{
//MessageBox::Show(ex->Message->ToString(),"Can't get the model name!!",System::Windows::Forms::MessageBoxButtons::OK,System::Windows::Forms::MessageBoxIcon::Error);
exit(0);
}
}
void FileManipulating::CreatePrototypeFile(System::String* fromFile, System::String* toFile, System::String* modelName)
{
System::IO::StreamWriter* sw = System::IO::StreamWriter::Null;
System::IO::StreamReader* sr = System::IO::StreamReader::Null;
System::String* tempStr = "";
bool firstLine = true;
try
{
// read the content and store the specific text to the prototype file
sr = new System::IO::StreamReader(fromFile);
sw = new System::IO::StreamWriter(toFile);
tempStr = sr->ReadLine();
while (tempStr->Length!=0)
{
if (firstLine)
{
// just change the first line according to the appropriate model name
sw->WriteLine("~h \"" + modelName + "\"");
firstLine = false;
}
else
{
sw->WriteLine(tempStr);
}
tempStr = sr->ReadLine();
}
sr->Close();
sw->Close();
}
catch (System::Exception* excp)
{
// MessageBox::Show(excp->Message->ToString(),"Can't Create Prototype File!!",System::Windows::Forms::MessageBoxButtons::OK,System::Windows::Forms::MessageBoxIcon::Error);
exit(0);
}
}
void FileManipulating::CreateHMMFile(System::String *exeFileDir, System::String *outputHmmDir, System::String *prototypeFile, System::String *trainFile)
{
try
{
System::String* batchFileName = outputHmmDir + "\\" +"_HInit.bat";
System::IO::StreamWriter* sw = new System::IO::StreamWriter(batchFileName);
System::String* tempStr = "";
// building the associate command for the initialization
tempStr += "\"" + exeFileDir + "HInit" + "\"";
tempStr += " -m 1"; // required for training from single sample
tempStr += " -M ";
tempStr += "\"" + outputHmmDir + "\"";
tempStr += " ";
tempStr += "\"" + prototypeFile + "\"";
tempStr += " ";
tempStr += "\"" + trainFile + "\"";
sw->Write(tempStr);
sw->Close();
// create a process and execute
System::Diagnostics::Process* p = new System::Diagnostics::Process();
p->StartInfo->FileName = batchFileName;
p->Start();
p->WaitForExit();
}
catch (System::Exception* excp)
{
// System::Windows::Forms::MessageBox::Show(excp->Message->ToString(),"Can't Create HMM Training File!!",System::Windows::Forms::MessageBoxButtons::OK,System::Windows::Forms::MessageBoxIcon::Error);
exit(0);
}
}
void FileManipulating::AddModelDefToMasterModelFile(System::String *mmfFilePath, System::String *fromHmmFile)
{
try
{
System::IO::StreamReader* sr = System::IO::StreamReader::Null;
System::IO::StreamWriter* sw = System::IO::StreamWriter::Null;
System::String* tempStr;
int lineNum = 0;
// if the file does not exist then directly copy the entire content of the prototype file
if(!System::IO::File::Exists(mmfFilePath))
{
System::IO::File::Copy(fromHmmFile,mmfFilePath);
}
else
{
sw = System::IO::File::AppendText(mmfFilePath);
sr = new System::IO::StreamReader(fromHmmFile);
tempStr = sr->ReadLine();
// Add the content from line number 4 of the prototype file
while (tempStr->Length!=0)
{
lineNum++;
if (lineNum<4)
{
}
else
{
sw->WriteLine(tempStr);
}
tempStr = sr->ReadLine();
}
}
sr->Close();
sw->Close();
}
catch(System::Exception* ex)
{
// Forms::MessageBox::Show(ex->Message->ToString(),"HMM Model Name adding to MMF file Failed!!",System::Windows::Forms::MessageBoxButtons::OK,System::Windows::Forms::MessageBoxIcon::Error);
exit(0);
}
}
void FileManipulating::AddModelNameToHMMListFile(System::String *hmmListFile, System::String *hmmNameFile, System::String *modelName)
{
System::IO::StreamWriter* sw = System::IO::StreamWriter::Null;
try
{
// add model name to the HMM List file
sw = System::IO::File::AppendText(hmmListFile);
sw->WriteLine(modelName);
sw->Close();
// add model name to the HMM Name File
sw = new System::IO::StreamWriter(hmmNameFile);
sw->Write(modelName);
sw->Close();
}
catch(System::Exception* ex)
{
// Forms::MessageBox::Show(ex->Message->ToString(),"HMM Model Name adding to HMM List file Failed!!",System::Windows::Forms::MessageBoxButtons::OK,System::Windows::Forms::MessageBoxIcon::Error);
exit(0);
}
}
void FileManipulating::BuildWordNetwork(System::String *gramFile, System::String *dictFile, System::String *transFile, System::String *wdnetFileDir, System::String* execpath, System::String* modelName)
{
System::IO::StreamWriter* sw = System::IO::StreamWriter::Null;
try
{
// add model name to the grammer file
System::String* text = "";
// check the existance of grammer file
if(!System::IO::File::Exists(gramFile))
{
sw = new System::IO::StreamWriter(gramFile);
text = "$WORD = "+ modelName + ";" + "\n" + "([$WORD])";
sw->Write(text);
sw->Close();
}
else
{
// create streamreader
System::IO::StreamReader* sr = new System::IO::StreamReader(gramFile);
System::String* line;
int k;
// Read and display lines from the file until the end of
// the file is reached.
line=sr->ReadLine();
while (line->Length!=0)
{
k = line->IndexOf(";");
if(k < 0 )
{
text += line+"\n";
}
else
{
line = line->Substring(0,k);
text += line + "|" + modelName + ";" + "\n";
}
line=sr->ReadLine();
}
sr->Close();
// write to the grammer file
sw = new System::IO::StreamWriter(gramFile);
sw->Write(text);
sw->Close();
}
// add model name to the dictionary file
sw = System::IO::File::AppendText(dictFile);
modelName = modelName + " [" + modelName + "] " + modelName;
sw->WriteLine(modelName);
sw->Close();
// add model name & associated transcription to the transcripton database file
// build the world network
/*1. Creating _HParse.bat*/
System::String* parseFile = wdnetFileDir + "_HParse.bat";
sw = new System::IO::StreamWriter(parseFile);
text = "\"" + execpath + "HParse" + "\"" + " ";
text += "\"" + gramFile + "\"" + " ";
text += "\"" + wdnetFileDir + "net.slf" + "\"";
sw->Write(text);
sw->Close();
/*2. Creating _HSGen.bat*/
System::String* genFile = wdnetFileDir + "_HSGen.bat";
sw = new System::IO::StreamWriter(genFile);
text = "\"" + execpath + "HSGen" + "\"" + " ";
text += "\"" + wdnetFileDir + "net.slf" + "\"" + " ";
text += "\"" + dictFile + "\"";
sw->Write(text);
sw->Close();
// create a process and execute the batch files
System::Diagnostics::Process* p = new System::Diagnostics::Process();
// execute _HParse.bat
p->StartInfo->FileName = parseFile;
p->Start();
p->WaitForExit();
// execute _HSGen.bat
p->StartInfo->FileName = genFile;
p->Start();
p->WaitForExit();
}
catch(System::Exception* ex)
{
//System::Windows::Forms::MessageBox::Show(ex->Message->ToString(),"Building Word Network Failed!!",System::Windows::Forms::MessageBoxButtons::OK,System::Windows::Forms::MessageBoxIcon::Error);
exit(0);
}
}
void FileManipulating::RecognizeByHMM(System::String* recDir, System::String* execFile, System::String *mmfFile, System::String *mlfFile, System::String *wdNetFile, System::String *dictFile, System::String* hmmListFile, System::String *scriptFile)
{
try
{
System::String* batchFileName = recDir + "_HVite.bat";
System::IO::StreamWriter* sw = new System::IO::StreamWriter(batchFileName);
System::String* tempStr = "";
// building the associate command for the initialization
tempStr += "\"" + execFile + "HVite" + "\"";
tempStr += " -H "; // required for training from single sample
tempStr += "\"" + mmfFile + "\"";
tempStr += " -i ";
tempStr += "\"" + mlfFile + "\"";
tempStr += " -w ";
tempStr += "\"" + wdNetFile + "\"";
tempStr += " ";
tempStr += "\"" + dictFile + "\"";
tempStr += " ";
tempStr += "\"" + hmmListFile + "\"";
tempStr += " -S ";
tempStr += "\"" + scriptFile + "\"";
sw->Write(tempStr);
sw->Close();
// create a process and execute
System::Diagnostics::Process* p = new System::Diagnostics::Process();
p->StartInfo->FileName = batchFileName;
p->Start();
p->WaitForExit();
}
catch (System::Exception* excp)
{
// Forms::MessageBox::Show(excp->Message->ToString(),"Can't Create/Execute Viterbi Decoder!!",System::Windows::Forms::MessageBoxButtons::OK,System::Windows::Forms::MessageBoxIcon::Error);
exit(0);
}
}
System::Collections::ArrayList* FileManipulating::FetchOutputModels(System::String *mlfFilePath)
{
System::IO::StreamReader* sr = System::IO::StreamReader::Null;
System::Collections::ArrayList* alWord = new System::Collections::ArrayList();
System::String* tempStr;
System::String* text;
int lineNo = 0;
try
{
sr = new System::IO::StreamReader(mlfFilePath);
text = "";
int index = 0;
int stIndex = 0;
int enIndex = 0;
int length = 0;
System::String* str;
tempStr=sr->ReadLine();
while(tempStr->Length!=0 )
{
lineNo++;
if(lineNo==3)
{
index = tempStr->IndexOf(" ",0) + 1; // omit first word
stIndex = tempStr->IndexOf(" ",index) + 1; // omit second word
enIndex = tempStr->IndexOf(" ",stIndex);
length = enIndex - stIndex;
str = tempStr->Substring(stIndex,length);
text += str + "\n";
lineNo = 0;
alWord->Add(str);
}
tempStr=sr->ReadLine();
}
sr->Close();
return alWord;
}
catch(System::Exception* ex)
{
// Forms::MessageBox::Show(ex->Message->ToString(),"Error in Reading MLF File",System::Windows::Forms::MessageBoxButtons::OK,System::Windows::Forms::MessageBoxIcon::Error);
exit(0);
}
}
void FileManipulating::PrepareData(System::String *dataFileName, int **imageArr, int leftX, int rightX, int topY, int bottomY)
{
try{
int frameWidth = 8; // width of each window
int frameHeight = 40; // height of each window
int imWidth; // width of the image
int imHeight; // height of the image
int NoOfWindow = 0; // contain the number of frame
float list[150][320]; // contain the information of each frame in an one dimentional float array
int size_L = 0;
// Getting the actual width and height of the image and setting it into the window
if((((rightX - leftX) +1 ) % frameWidth) == 0)
{
imWidth = (rightX - leftX) + 1 ;
}
else
{
imWidth = (((rightX - leftX) + 1) + (8 - (((rightX - leftX)+1) % 8)) );
}
if((((bottomY - topY) + 1 ) % frameHeight) == 0)
{
imHeight = (bottomY - topY) + 1;
}
else
{
imHeight = (((bottomY - topY) + 1) + (40 - (((bottomY - topY) + 1) % 40)) );
}
// Declare and set the proper size of the window
signed int ** Window;
Window = new signed int *[imHeight];
for (int j = 0; j< imHeight ; j++)
{
Window [j] = new signed int [imWidth];
}
// initialize the array with 1
for(int i = 0; i < imHeight ;i++ )
{
for(int k = 0; k < imWidth ; k++)
{
Window[i][k]=1;
}
}
//mapping the image into the wordarray
for (int i=topY ; i<=bottomY; i++ )
{
for (int j=leftX; j<=rightX; j++ )
Window[i-topY][j-leftX] = imageArr[i][j];
}
//Working with DCT for extracting feature
int c = 0;
int win_y1 = 0;
int win_y2 = 40;
int win_x1 = 0;
int win_x2 =win_x1 + 8 ;
int MaxWinCount = (imHeight / 40 ) * (imWidth / 8);
for(int a = win_x1 ; a < win_x2 ; a++)
{
int d = 0;
for(int b = win_y1; b < win_y2; b++)
{
dctArr[d][c]= Window[b][a];
d++;
}
c++;
if(d==40 && c==8)
{
// checking for the height constrain
if( win_y2 < imHeight - 1)
{
a = win_x1 -1;
}
win_y2 = win_y2 + 40;
win_y1 = win_y1 + 40;
// applying DCT calculation on each pixel
ApplyDCT();
NoOfWindow++;
int val;
int k1=0;
for(int p = 0; p < FRAME_HT ;p++ )
{
for(int j=0;j<FRAME_WD;j++)
{
// building a feature vector hat contain the DCT info of each window
list[size_L][k1]=dctArr[p][j];
k1++;
}
}
size_L++;
}
if( c== 8)
{
c=0;
}
if(MaxWinCount != NoOfWindow && a+1 >= win_x2)//setting windows
{
if( win_y2 - 40 == imHeight )
{
win_y1 = 0;
win_y2 = 40;
win_x1 =win_x2;
win_x2 = win_x2 + 8;
a = win_x1 -1 ;
}
}
else if (MaxWinCount == NoOfWindow )
{
break;
}
}
//***************************************************************************
// required for dynamic prototyping
this->numOfFrame = NoOfWindow;
int Header1[2];
Header1[0] = NoOfWindow;
Header1[1] = 1000;
short Header2[2];
Header2[0] = 4 * 320;
Header2[1] = 9;
FILE * file;
char * FileName = new char[9];
Boolean flag = FALSE ;
vaxOrder = TRUE;
IOFilter filter = NoOFilter ;
// training file
int len = dataFileName->Length;
char *userFile;
userFile=new char[500];
for(int i=0;i<len;i++)
{
userFile[i] = dataFileName->get_Chars(i);
}
userFile[len] = '\0';
file = FOpen(userFile, filter, &flag);
WriteInt(file, Header1, 2 , TRUE);
WriteShort(file,Header2, 2 , TRUE);
for(int ad = 0; ad < NoOfWindow; ad++)
{
for(int af = 0; af < 320; af ++)
{
list[ad][af] = list[ad][af] + 8;
WriteFloat(file,&list[ad][af],1,TRUE);
}
}
FClose(file, flag);
}
catch(System::NullReferenceException* ex)
{
/// Forms::MessageBox::Show(ex->Message->ToString(),"Training Process failed",System::Windows::Forms::MessageBoxButtons::OK,System::Windows::Forms::MessageBoxIcon::Error);
exit(0);
}
}
void FileManipulating::ApplyDCT()
{
float cucv, temp;
for (int u=0;u<FRAME_HT;u++)
{
for (int v=0;v<FRAME_WD;v++)
{
if (u==0 && v==0) cucv = 1/1.4142135623730; else cucv = 1;
dctArr[u][v] = 0.081649658092772603273242802490226 * cucv * (float)Fdct(u, v);
}
}
}
float FileManipulating::Fdct(int u,int v)
{
float res = 0;
for (int i = 0 ; i < FRAME_HT ; i++ )
{
for (int j = 0 ; j < FRAME_WD ; j++ )
{
res += (dctArr[i][j] * cos( ((2*i + 1)*u*PI) / (2 * FRAME_HT) ) * cos( ((2*j + 1)*v*PI) / (2* FRAME_WD) ));
}
}
return res;
}