-
Notifications
You must be signed in to change notification settings - Fork 1
/
imagewidget.cpp
executable file
·1045 lines (961 loc) · 34.5 KB
/
imagewidget.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
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// imagewidget.cpp
#include "imagewidget.h"
#include <QDir>
#include <QStringList>
#include <QListWidget>
#include <QVBoxLayout>
#include <QListWidgetItem>
#include <QDebug>
#include <QTextCodec>
int index[3];
static Mat img_original, img_drawing;
static Point quad[100][4];
static int pointNum = 0;
static int quadNum = 0;
static bool drawFlag = 0;
#define FOURPOINT 0
#define RECTANGLE 1
//int openwindow = 0;
//int maxwindow = 0;
static QStringList *pstring = NULL;
static QStringList *pscene = NULL;
static QStringList *planguage = NULL;
static QStringList *pquality = NULL;
static int currentquad = -1;
static int currentcontext = currentquad;
static bool flags;
ImageWidget::ImageWidget(QWidget *parent, bool flag, QString dirname)
: QWidget(parent),
flag(flag),
dirname(dirname)
{
this->init();
}
ImageWidget::~ImageWidget(){}
void ImageWidget::init() {
// 窗口基本设置
this->setWindowTitle("Image Preview");
// this->setMinimumSize(780, 600);
m_showWidget.setWindowTitle("Image Show");
m_showWidget.setAlignment(Qt::AlignCenter);
const QSize IMAGE_SIZE(240, 160);
const QSize ITEM_SIZE(240, 190);
// 判断路径是否存在
m_strPath = dirname;
QDir dir(m_strPath);
if (!dir.exists()) {
return;
}
// 设置过滤器
dir.setFilter(QDir::Files | QDir::NoSymLinks);
QStringList filters;
if(flag){
filters << "*.png" << "*.jpg" << "*.bmp";
}else{
filters << "*.avi"<<"*.mp4";//设置过滤类型 x
}
dir.setNameFilters(filters);
m_imgList = dir.entryList();
if (m_imgList.count() <= 0) {
return;
}
// QListWidget基本设置
m_listWidget = new QListWidget(this);
m_listWidget->setIconSize(IMAGE_SIZE);
m_listWidget->setResizeMode(QListView::Adjust);
m_listWidget->setViewMode(QListView::IconMode);
m_listWidget->setMovement(QListView::Static);
m_listWidget->setSpacing(8);
// 创建单元项
for (int i = 0; i<m_imgList.count(); ++i) {
QPixmap pixmap(m_strPath + "/" + m_imgList.at(i));
QListWidgetItem *listWidgetItem = new QListWidgetItem(QIcon(pixmap.scaled(IMAGE_SIZE)), m_imgList.at(i));
listWidgetItem->setSizeHint(ITEM_SIZE);
m_listWidget->insertItem(i, listWidgetItem);
}
textEdit = new QTextEdit(this);
textEdit->setObjectName(QStringLiteral("textEdit"));
textEdit->setGeometry(QRect(300, 10, 150, 450));
textEdit->setText("打标内容:");
textEdit->setFocusPolicy(Qt::NoFocus);
// 窗口布局
QVBoxLayout *mainLayout = new QVBoxLayout;
mainLayout->addWidget(m_listWidget);
mainLayout->setMargin(0);
this->setLayout(mainLayout);
// 信号与槽
connect(m_listWidget, SIGNAL(itemDoubleClicked(QListWidgetItem*)), this, SLOT(slot_itemClicked(QListWidgetItem*)));
flags = flag;
}
// 最大化窗口显示图像
void ImageWidget::slot_itemClicked(QListWidgetItem * item) {
// m_showWidget.setPixmap(QPixmap(m_strPath + "\\" + m_imgList.at(m_listWidget->row(item))));
//m_showWidget.showMaximized();
// openwindow++;
// maxwindow = openwindow;
ClearAllVariable();
int xxx;
int counter = m_listWidget->row(item);
int framecounter = 0;
pstring = &str;
pscene = &scene;
planguage = &language;
pquality = &quality;
Point face_point[110];
// QTextCodec *code = QTextCodec::codecForName("gbk");
// std::string namestr = code->fromUnicode(m_strPath + "\\" + m_imgList.at(counter)).data();
// QString name = QString::fromStdString(namestr);
QString name = m_strPath + "/" + m_imgList.at(counter);
QFileInfo file(name);
if(!file.isFile())
{
return;
}
qDebug() << name;
QString name_withoutsuffix = name;
name_withoutsuffix.truncate(name_withoutsuffix.lastIndexOf("."));
VideoCapture capture(name.toLocal8Bit().toStdString());
capture >> img_original;
// fileoperation fileout;
// fileout.InitFrameMessage();
InitFrameMessage();
qDebug() << "Start!";
if(flag){
namedWindow("Picture", 0);
img_original = imread(name.toLocal8Bit().toStdString());
img_original.copyTo(img_drawing);
int result = ReadPicQuad(name_withoutsuffix, &xxx);
if ( !result )
{
ClearAllVariable();
destroyWindow("Picture");
return;
}
// destroyWindow("Picture");
// namedWindow("Picture", 0);
if(img_drawing.empty())
return;
imshow("Picture",img_drawing);
if(JosnToPoint(name_withoutsuffix, face_point))
{
qDebug() << "draw";
int num_face;
for(num_face = 0; num_face < 106; num_face++)
circle(img_drawing, cvPoint(face_point[num_face].x,face_point[num_face].y), 0.5,Scalar(0, 255, 0),2,8,0);
}
setMouseCallback("Picture", onMouse, 0);
}else{
// fileout.ReadFile(name);
ReadXmlFile(name_withoutsuffix);
namedWindow("Video", 0);
img_original.copyTo(img_drawing);
// QObject::connect(&fileout,SIGNAL(senddata(QString)),this,SLOT(receiveDataSave(QString)));
// fileout.ReadFramequad(img_drawing);
ReadFramequad(img_drawing);
imshow("Video", img_drawing);
setMouseCallback("Video", onMouse, 0);
}
while (1){
int c = waitKey(0);
if ((c & 255) == 27)
{
cout << "Exiting ...\n";
// qDebug() << quadNum;
// qDebug() << c;
if (quadNum)
{
if (flag){
PicInfInput(name_withoutsuffix, quadNum);
}
else{
SetFrameMessage(framecounter, quadNum);
}
}
ClearAllVariable();
if(flag){
// openwindow--;
destroyWindow("Picture");
}else{
// fileout.VideoFinalInfInput(name);
VideoFinalInfInput(framecounter,name_withoutsuffix);
destroyWindow("Video");
}
return;
}
// qDebug() << c;
switch ((char)c)
{
case 'n':
//set down the statistics
if (pointNum != 0)
{
cout << "No Transcription exists. If no label, please clear the points first." << endl;
break;
}
if(flag)
{
if(!PicInfInput(name_withoutsuffix, quadNum))//write the pic
{
cout << "Please check it again!" << endl;
break;
}
}
else
{
if(!SetFrameMessage(framecounter, quadNum))//write the xml
{
cout << "Please check it again!" << endl;
break;
}
}
++counter;
++framecounter;
ClearAllVariable();
if(flag){
if(m_imgList.count() == counter){
qDebug() << "destroy!";
// openwindow--;
destroyWindow("Picture");
img_drawing.release();
return;
}
name = m_strPath + "/" + m_imgList.at(counter);
name_withoutsuffix = name;
name_withoutsuffix.truncate(name_withoutsuffix.lastIndexOf("."));
img_original = imread(name.toLocal8Bit().toStdString());
img_original.copyTo(img_drawing);
int result = ReadPicQuad(name_withoutsuffix, &xxx);
if(JosnToPoint(name_withoutsuffix, face_point))
{
qDebug() << "draw";
}
if ( !result )
{
ClearAllVariable();
destroyWindow("Picture");
img_drawing.release();
return;
}
}else{
capture >> img_original; //move to the next frame
img_original.copyTo(img_drawing);
// QObject::connect(&fileout,SIGNAL(senddata(QString)),this,SLOT(receiveDataSave(QString)));
// fileout.ReadFramequad(img_drawing);
ReadFramequad(img_drawing);
}
if (img_original.empty()){
if(flag){
destroyWindow("Picture");
}else{
// fileout.VideoFinalInfInput(name);
VideoFinalInfInput(framecounter, name_withoutsuffix);
destroyWindow("Video");
}
return;
}
break;
case 'z':
//undo the latest labeling point
if(pointNum == 0)
{
cout<<"if you want to clear the existent quad please press 'c'"<<endl;
break;
}
pointNum--;
quad[quadNum][pointNum].x=0;
quad[quadNum][pointNum].y=0;
img_original.copyTo(img_drawing);
drawAllQuadri(0, str); //redraw the points and the lines on the image.
for(int j = 0 ; j < pointNum; j++)
{
circle(img_drawing,cvPoint(quad[quadNum][j].x,quad[quadNum][j].y),1,Scalar(0, 255, 0),2,8,0);
}
break;
case 'c':
//clear the last quad array
if (quadNum > 0 && pointNum == 0) //clear the current quad
{
for(int tempquad = currentquad; tempquad < quadNum -1; tempquad++) //use the next quad to replace the tempquad
{
for(int pointSn = 0; pointSn < 4; pointSn++)
{
quad[tempquad][pointSn].x = quad[tempquad + 1][pointSn].x;
quad[tempquad][pointSn].y = quad[tempquad + 1][pointSn].y;
}
}
memset(quad[--quadNum],0,4*sizeof(Point));
//remove the label from the content shown on ui
str.removeAt(currentquad);
scene.removeAt(currentquad);
language.removeAt(currentquad);
quality.removeAt(currentquad);
showallcontext();
// str.removeAt(currentquad);
// QStringList contextlist = this->textEdit->toPlainText().split("\n");
// contextlist.removeAt(currentquad + 1);
// this->textEdit->setText(contextlist.join('\n'));
//finish
if(currentquad == quadNum)
{
--currentquad;
}
img_original.copyTo(img_drawing);
drawAllQuadri(0, str); //redraw the points and the lines on the image.
}
else //clear the points that haven't generate the quad yet
{
memset(quad[quadNum],0,4*sizeof(Point));
img_original.copyTo(img_drawing);
drawAllQuadri(0, str);
pointNum = 0;
}
break;
case 45://↓,change the value of currentquad to the former one
if(currentquad > 0 && quadNum > 0)
{
currentquad--;
drawAllQuadri(0, str);
show = "打标内容:";
show = show + "\n" + str[currentquad] + "," + scene[currentquad] + "," +language[currentquad] + "," + quality[currentquad];
this->textEdit->setText(show);
}
break;
case 61://↑,change the value of currentquad to the next one
if(currentquad < quadNum - 1 && quadNum > 0)
{
currentquad++;
drawAllQuadri(0, str);
show = "打标内容:";
show = show + "\n" + str[currentquad] + "," + scene[currentquad] + "," +language[currentquad] + "," + quality[currentquad];
this->textEdit->setText(show);
}
break;
case 112://indistinct label marking
if ( currentquad > -1 )
{
str[currentquad] = "###";
scene[currentquad] = "###";
language[currentquad] = "###";
quality[currentquad] = "###";
show = "打标内容:";
show = show + '\n'+ "###" + "," + "###"+ "," + "###"+ "," + "###";
this->textEdit->setText(show); //获取传递过来的数据
drawAllQuadri(2, str); //red for indistinct label
}
break;
case 113:
showallcontext();
break;
case 'x':
if (currentquad > -1) //Only when currentquad is exist can we draw quad
{
//str[currentquad] + "," + scene[currentquad] + "," +language[currentquad] + "," + quality[currentquad]
LabelMark *widget = new LabelMark;
widget->set(str[currentquad], scene[currentquad], language[currentquad], quality[currentquad]);
widget->show();
connect(widget, SIGNAL(sendData(QString)), this, SLOT(receiveDataSave(QString)));
}
else
{
qDebug() << "Please draw the quad first";
}
break;
case 'y':
drawFlag = drawFlag ? 0 : 1;
break;
//case 8: //delete
case 0xffff:
name = m_strPath + "/" + m_imgList.at(counter);
QFile file(name);
if (file.exists())
{
file.remove();
}
++counter;
++framecounter;
ClearAllVariable();
if(flag){
if(m_imgList.count() == counter){
qDebug() << "destroy!";
// openwindow--;
destroyWindow("Picture");
img_drawing.release();
return;
}
name = m_strPath + "/" + m_imgList.at(counter);
name_withoutsuffix = name;
name_withoutsuffix.truncate(name_withoutsuffix.lastIndexOf("."));
img_original = imread(name.toLocal8Bit().toStdString());
img_original.copyTo(img_drawing);
int result = ReadPicQuad(name_withoutsuffix, &xxx);
if ( !result )
{
ClearAllVariable();
destroyWindow("Picture");
img_drawing.release();
return;
}
}else{
capture >> img_original; //move to the next frame
img_original.copyTo(img_drawing);
// QObject::connect(&fileout,SIGNAL(senddata(QString)),this,SLOT(receiveDataSave(QString)));
// fileout.ReadFramequad(img_drawing);
ReadFramequad(img_drawing);
}
if (img_original.empty()){
if(flag){
destroyWindow("Picture");
}else{
// fileout.VideoFinalInfInput(name);
VideoFinalInfInput(framecounter, name_withoutsuffix);
destroyWindow("Video");
}
return;
}
}
if(flag){
// destroyWindow("Picture");
// namedWindow("Picture", 0);
if(img_drawing.empty())
{
ClearAllVariable();
return;
}
imshow("Picture", img_drawing);
}else{
imshow("Video", img_drawing);
}
}
}
void ImageWidget::receiveDataSave(QString data){
QStringList datalist = data.split(',');
str[currentquad] = datalist[0];
scene[currentquad] = datalist[1];
language[currentquad] = datalist[2];
quality[currentquad] = datalist[3];
show = "打标内容:";
show = show + '\n' + data;
this->textEdit->setText(show); //获取传递过来的数据
}
void drawQuadri (Point * quadPoint, int blue, int green, int red) {
for(int i = 0; i < 4; i++)
{
line(img_drawing,quadPoint[i],quadPoint[(i+1)%4],Scalar(blue, green, red),3,8,0);
}
}
void drawAllQuadri (int currentquad_color, QStringList str)
{
//this function only draws the quads that have already exist.
//the rest points which haven't generate the quad won't be show in this function
for (int i = 0 ; i < quadNum ; i++) //redraw the rest of the quads
{
for(int j = 0 ; j < 4; j++)
{
circle(img_drawing,cvPoint(quad[i][j].x,quad[i][j].y),1,Scalar(0, 255, 0),2,8,0);
}
if (str[i] == "") //Transcriptions can be searched
{
drawQuadri(quad[i], 255, 255, 255); //white for the quad with no label
}
else
{
QString tmpstr = str[i];
if(tmpstr.indexOf("#") == -1)
{
drawQuadri(quad[i], 0, 255, 0); //green for normal
}
else
{
drawQuadri(quad[i], 0, 0, 255); //red for indistinct
}
}
}
if (quadNum > 0)
{
for(int j = 0 ; j < 4; j++)
{
circle(img_drawing,cvPoint(quad[currentquad][j].x,quad[currentquad][j].y),1,Scalar(255, 0, 0),2,8,0);
}
switch(currentquad_color)
{
case 0:drawQuadri(quad[currentquad], 255, 0, 0); //blue
break;
case 1:drawQuadri(quad[currentquad], 0, 255, 0); //green
break;
case 2:drawQuadri(quad[currentquad], 0, 0, 255); //red
break;
}
}
}
int isempty(Point * quad)
{
for (int i = 0 ; i < 4; i++)
{
if (quad[i].x !=0 || quad[i].y !=0 )
{
return 0;
}
}
return 1;
}
void onMouse(int event, int x, int y, int cvflag, void*param)
{
static Point pre_pt;
static Point cur_pt;
char temp[16];
if(RECTANGLE == drawFlag)
{
switch (event)
{
case CV_EVENT_LBUTTONDOWN:
pre_pt = Point(x,y);
qDebug()<<pre_pt.x<<endl;
//sprintf(temp,"(%d,%d)",x,y);
//putText(img_drawing,temp,pre_pt,FONT_HERSHEY_SIMPLEX,0.5,Scalar(0,0,0,255),1,8);//在窗口上显示坐标
//circle(img,pre_pt,2,Scalar(255,0,0,0),CV_FILLED,CV_AA,0);//划圆
circle(img_drawing,pre_pt,1,Scalar(0, 0, 255),2,8,0);
break;
case CV_EVENT_LBUTTONUP:
cur_pt = Point(x,y);
// putText(img_drawing,temp,cur_pt,FONT_HERSHEY_SIMPLEX,0.5,Scalar(0,0,0,255));
circle(img_drawing,pre_pt,2,Scalar(255,0,0,0),CV_FILLED,CV_AA,0);
//finish drawing the rect (use color green for finish)
int deltx, delty;
deltx = cur_pt.x - pre_pt.x;
delty = cur_pt.y - pre_pt.y;
quad[quadNum][0].x = pre_pt.x;
quad[quadNum][0].y = pre_pt.y;
quad[quadNum][1].x = pre_pt.x;
quad[quadNum][1].y = pre_pt.y + delty;
quad[quadNum][3].x = pre_pt.x + deltx;
quad[quadNum][3].y = pre_pt.y;
quad[quadNum][2].x = cur_pt.x;
quad[quadNum][2].y = cur_pt.y;
//cout<<"x = "<<x<<" y = "<<y<<endl;
pointNum = 4;
circle(img_drawing,cvPoint(x,y),1,Scalar(0, 0, 255),1,8,0);
if(pointNum == 4)
{
pointNum = 0;
cout<<"draw quadri line"<<endl;
quadNum++;
currentquad = quadNum - 1;
(*pstring) << "";
(*pscene) << "";
(*planguage) << "";
(*pquality) << "";
drawAllQuadri(0, *pstring);
}
break;
}
}
else
{
switch (event)
{
case CV_EVENT_LBUTTONDOWN:
// quad[quadNum][pointNum%4].x = x;
// quad[quadNum][pointNum%4].y = y;
// //cout<<"x = "<<x<<" y = "<<y<<endl;
// pointNum++;
break;
case CV_EVENT_LBUTTONUP:
//finish drawing the rect (use color green for finish)
quad[quadNum][pointNum%4].x = x;
quad[quadNum][pointNum%4].y = y;
//cout<<"x = "<<x<<" y = "<<y<<endl;
pointNum++;
circle(img_drawing,cvPoint(x,y),1,Scalar(0, 0, 255),1,8,0);
if(pointNum == 4)
{
pointNum = 0;
cout<<"draw quadri line"<<endl;
quadNum++;
currentquad = quadNum - 1;
(*pstring) << "";
(*pscene) << "";
(*planguage) << "";
(*pquality) << "";
drawAllQuadri(0, *pstring);
}
break;
}
}
if (flags){
// destroyWindow("Picture");
// namedWindow("Picture", 0);
imshow("Picture", img_drawing);
}else{
imshow("Video", img_drawing);
}
return;
}
void ImageWidget::showallcontext()
{
if(str.count() <= 0)
{
return;
}
showlist = "打标内容:";
for(int i = 0; i< str.count(); i++)
{
if(str[i] == "")
{
showlist = showlist + "\n" + "(empty context)" + "," + scene[i] + "," +language[i] + "," + quality[i];
}
else
{
showlist = showlist + "\n" + str[i] + "," + scene[i] + "," +language[i] + "," + quality[i];
}
}
this->textEdit->setText(showlist);
}
int ImageWidget::SetFrameMessage(int frame_counter, int quadNum)
{
//set the message from quad and str to Frame
if (str.count() != quadNum)
{
cout << "please ensure the number of Transcriptions is equal to the number of the quad" << endl;
return 0;
}
QDomElement MainFrameNode = FrameMessage.documentElement();
QDomElement FrameNode = FrameMessage.createElement("frame");
FrameNode.setAttribute("ID", QString::number(frame_counter+1));
for (int i = 0; i < quadNum ; i++)
{
QStringList XList,YList;
XList << QString::number(quad[i][0].x,10)
<< QString::number(quad[i][1].x,10)
<< QString::number(quad[i][2].x,10)
<< QString::number(quad[i][3].x,10);
YList << QString::number(quad[i][0].y,10)
<< QString::number(quad[i][1].y,10)
<< QString::number(quad[i][2].y,10)
<< QString::number(quad[i][3].y,10);
QDomElement ObjectNode = FrameMessage.createElement("object");
ObjectNode.setAttribute("Transcription", str[i]);
ObjectNode.setAttribute("ID", i+1001);
ObjectNode.setAttribute("Quality", quality[i]);
ObjectNode.setAttribute("Language", language[i]);
ObjectNode.setAttribute("Scene", scene[i]);
for(int j = 0; j < 4; ++j) {
QDomElement attribNode = FrameMessage.createElement("Point");
attribNode.setAttribute("x", XList.at(j));
attribNode.setAttribute("y", YList.at(j));
ObjectNode.appendChild(attribNode);
}
FrameNode.appendChild(ObjectNode);
}
MainFrameNode.appendChild(FrameNode);
FrameMessage.appendChild(MainFrameNode);
return 1;
}
void ImageWidget::VideoFinalInfInput(int counter, QString name)
{
while ( !TmpFileFrameNode.isNull() )
{
counter++;
QDomNode ObjectNode = TmpFileFrameNode.firstChild();
while ( !ObjectNode.isNull() )
{
QString datashow = ObjectNode.toElement().attributeNode("Transcription").value()+","
+ ObjectNode.toElement().attributeNode("Language").value()+","
+ ObjectNode.toElement().attributeNode("Scene").value()+","
+ ObjectNode.toElement().attributeNode("Quality").value();
QStringList datashowlist = datashow.split(',');
str << datashowlist[0];
language << datashowlist[1];
scene << datashowlist[2];
quality << datashowlist[3];
QDomNode PointNode = ObjectNode.firstChild();
for (int i=0; i<4 ;i++)
{
QDomElement PointElement = PointNode.toElement();
quad[quadNum][i].x = PointElement.attributeNode("x").value().toInt();
quad[quadNum][i].y = PointElement.attributeNode("y").value().toInt();
PointNode = PointNode.nextSibling();
}
quadNum++;
ObjectNode = ObjectNode.nextSibling();
}
SetFrameMessage(counter, quadNum);
ClearAllVariable();
TmpFileFrameNode = TmpFileFrameNode.nextSibling();
}
//write to the file
QString postfix = ".xml";
QString txtname = name + postfix;
// ofstream ofresult(txtname.toStdString(), ios::app);
// ofresult << "</frames>" << endl;
// ofresult.close();
QFile file(txtname);
if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
return;
QTextStream stream(&file);
FrameMessage.save(stream, 4);
FrameMessage.clear();
FileMessage.clear();
file.close();
}
void ImageWidget::InitFrameMessage()
{
//set the header and <frames> for the new FrameMessage
QString strHeader( "version=\"1.0\" encoding=\"us-ascii\"" );
FrameMessage.appendChild( FrameMessage.createProcessingInstruction("xml", strHeader) );
QDomElement MainFrameNode = FrameMessage.createElement("frames");
FrameMessage.appendChild( MainFrameNode );
}
void ImageWidget::ReadXmlFile(QString name)
{
//if file already exist
QFileInfo FI(name+".xml");
if (FI.isFile())
{
QFile file(name+".xml");
FileMessage.setContent(&file);
FIleMainFrameNode = FileMessage.documentElement();
TmpFileFrameNode = FIleMainFrameNode.firstChild();
}
}
void ImageWidget::ReadFramequad(Mat img_drawing)
{
showlist= "打标内容:";
this->textEdit->setText(showlist);
//Read the next Frame
if ( !TmpFileFrameNode.isNull() )
{
QDomNode ObjectNode = TmpFileFrameNode.firstChild();
while ( !ObjectNode.isNull() )
{
QString datashow = ObjectNode.toElement().attributeNode("Transcription").value()+","
+ ObjectNode.toElement().attributeNode("Language").value()+","
+ ObjectNode.toElement().attributeNode("Scene").value()+","
+ ObjectNode.toElement().attributeNode("Quality").value();
showlist = this->textEdit->toPlainText() + '\n' + datashow;
this->textEdit->setText(showlist); //获取传递过来的数据
QStringList datashowlist = datashow.split(',');
str << datashowlist[0];
language << datashowlist[1];
scene << datashowlist[2];
quality << datashowlist[3];
QDomNode PointNode = ObjectNode.firstChild();
for (int i=0; i<4 ;i++)
{
QDomElement PointElement = PointNode.toElement();
quad[quadNum][i].x = PointElement.attributeNode("x").value().toInt();
quad[quadNum][i].y = PointElement.attributeNode("y").value().toInt();
PointNode = PointNode.nextSibling();
}
quadNum++;
ObjectNode = ObjectNode.nextSibling();
}
currentquad = quadNum - 1;
drawAllQuadri(0, str);
}
TmpFileFrameNode = TmpFileFrameNode.nextSibling();
}
int ImageWidget::PicInfInput(QString name, int quadNum)
{
if (str.count() != quadNum)
{
cout << "please ensure the number of Transcriptions is equal to the number of the quad" << endl;
return 0;
}
/*QFile file(name+".txt");
if(!file.open(QIODevice::WriteOnly | QIODevice::Truncate))
return 0;
QTextStream stream(&file);
for (int i=0; i<quadNum ; i++) //write
{
QString tmpstr = str[i];
if (tmpstr.isNull())
{
stream << quad[i][0].x << "," << quad[i][0].y << ","
<< quad[i][1].x << "," << quad[i][1].y << ","
<< quad[i][2].x << "," << quad[i][2].y << ","
<< quad[i][3].x << "," << quad[i][3].y << "," << "" << "," << "" << "," << "" << "," << "" << "\r\n" ;
}
else
{
stream << quad[i][0].x << "," << quad[i][0].y << ","
<< quad[i][1].x << "," << quad[i][1].y << ","
<< quad[i][2].x << "," << quad[i][2].y << ","
<< quad[i][3].x << "," << quad[i][3].y << "," << str[i] << "," << scene[i] << "," << language[i] << "," << quality[i] << "\r\n" ;
}
}
stream << "row=" << img_drawing.rows << "," << "columns=" << img_drawing.cols;
file.close();
*/
return 1;
}
int ImageWidget::ReadPicQuad(QString name, int* pcounter)
{
showlist= "打标内容:";
this->textEdit->setText(showlist);
QFileInfo FI(name+".txt");
if (FI.isFile())
{
QFile file(name+".txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug() << "The txt file is in wrong state, please ensure the txt file is readable.";
return 0;
}
QTextStream in(&file);
QString line = in.readLine();
while (!line.isNull())
{
TxtStr << line;
line = in.readLine();
}
file.close();
for(int i=0; i<TxtStr.count()-1 ; i++) //Get the n-1 lines. The nth line is the pixel statistic
{
QString tmpstr = TxtStr[i];
QStringList PointandTrans = tmpstr.split(",");
if (PointandTrans.count() < 9)
{
continue ;
//return File_Error_Warning(name, pcounter);
}
else
{
if (!TxtlineToPointandtrans(i, PointandTrans))
{
memset(quad[i], 0 , 8*sizeof(Point));
continue;
//return File_Error_Warning(name, pcounter);
}
}
str << Transcrption;
language << languagetranscription;
scene << scenetranscription;
quality << qualitytranscription;
if (Transcrption == "")
{
Transcrption += "(empty context)";
}
showlist = this->textEdit->toPlainText() + '\n' + Transcrption + "," + scenetranscription + "," + languagetranscription + "," + qualitytranscription;
this->textEdit->setText(showlist); //获取传递过来的数据
Transcrption.clear();
quadNum++;
}
TxtStr.clear();
currentquad = quadNum - 1;
drawAllQuadri(0, str);
}
return 1;
}
void ImageWidget::ClearAllVariable()
{
memset(quad,0,400*sizeof(Point)); //clear all the variables
quadNum = 0;
pointNum = 0;
currentquad = -1;
str.clear();
quality.clear();
language.clear();
scene.clear();
}
int ImageWidget::File_Error_Warning(QString name, int *pcounter)
{
QMessageBox msgBox(QMessageBox::Warning, "File error", ///--这里是设置消息框标题
"The format of the content in the txt file of the picture is error\n Mark it again or switch to the next one?"); ///--这里是设置消息框显示的内容
msgBox.setStandardButtons(QMessageBox::Ok|QMessageBox::Cancel);
msgBox.setButtonText(QMessageBox::Ok,QString("Mark it"));
msgBox.setButtonText(QMessageBox::Cancel,QString("Next one"));
if(msgBox.exec() == QMessageBox::Cancel)
{
(*pcounter)++;
qDebug() << m_imgList.count() << " " << *pcounter;
if(m_imgList.count() == *pcounter){
return 0;
}
else{
name = m_strPath + "/" + m_imgList.at(*pcounter);
img_original = imread(name.toLocal8Bit().toStdString());
img_original.copyTo(img_drawing);
int result = ReadPicQuad(name, pcounter);
return result;
}
}
else
{
return 1;
}
}
int ImageWidget::TxtlineToPointandtrans(int counter, QStringList PointandTrans)
{
int j = 0;
for (; j < 8; j++)
{
bool ok;
if( j%2 == 0)
{
quad[counter][j/2].x = PointandTrans[j].toInt(&ok,10);
}
else
{
quad[counter][j/2].y = PointandTrans[j].toInt(&ok,10);
}
if(ok == false) //error when transform the string to number
{
return 0;
}
}
// for (; j < PointandTrans.count()-1 ; j++) //in case there is "," in the transcription
// {
// Transcrption += (PointandTrans[j]+",");
// }
// Transcrption += PointandTrans[j]; //add the last one with no ","
while (PointandTrans.count() < 12)
{
PointandTrans << "";
}
Transcrption = PointandTrans[j++];
scenetranscription = PointandTrans[j++];
languagetranscription = PointandTrans[j++];
qualitytranscription = PointandTrans[j];
return 1;
}
bool ImageWidget::JosnToPoint(QString name, Point *face_point)
{