-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtesseract_test.cpp
More file actions
2509 lines (2183 loc) · 85.4 KB
/
tesseract_test.cpp
File metadata and controls
2509 lines (2183 loc) · 85.4 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
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
#include "tesseract_test.h"
#include "workflow_proj.h"
#include "workflow_ctrl.h"
#include "img_scratch.h"
char datapath[256]="E:\\SRC_REF\\tesseract\\self-bulid\\test\\tessdata";
#define STRINGIFY(x) #x
#define TOSTRING(x) STRINGIFY(x)
char section_name[100][256]={
#define enum_names(enum_code,enum_desc,enum_name) \
enum_desc "(" enum_name ")" ,
//TOSTRING(enum_code) ":" enum_desc "(" enum_name ")" ,
ENUMNAMES_SECTION
#undef enum_names
};
char page_name[20][256]={
#define enum_names(enum_code,enum_desc,enum_name) \
enum_desc "(" enum_name ")" ,
//TOSTRING(enum_code) ":" enum_desc "(" enum_name ")" ,
ENUMNAMES_PAGE
#undef enum_names
};
/////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
TRZ sections[]={
//直播间主页面标志图(识别页面)
{
38,1432,695-38,1500-1432,STREAMER_ROOM_IMG,NULL,STREAMER_ROOM,
},
{
97,72,136,33,STREAMER,NULL,STREAMER_ROOM,
//97,72,60,33,STREAMER,NULL
},
{
97,107,136,18,THUMBUP_OR_STREAMER_DESC,NULL,STREAMER_ROOM,
},
{
573,83,50,30,ALIVECOUNT,NULL,STREAMER_ROOM,"eng","0123456789",
},
{
20,210,300,70,FORTUNEGIFT_OR_DIAMOND_RANGE,NULL,STREAMER_ROOM,
},
//以下均需要在运行中计算
//---------------------------------------
//福袋点击区域
{
0,0,0,0,FORTUNEGIFT_CLICKRECT,NULL,STREAMER_ROOM,
},
//斗钻点击区域
{
0,0,0,0,DIAMOND_CLICKRECT,NULL,STREAMER_ROOM,
},
//斗钻价值描述区域 这个不一定有,可能程序中会被取消
{
0,0,0,0,DOU_DIAMOND_DESC,NULL,STREAMER_ROOM,
},
//斗钻倒计时
{
18,47,45,18,DOU_DIAMOND_COUNTDOWN,NULL,STREAMER_ROOM,"eng","0123456789:",
},
//福袋倒计时
{
18,47,45,18,DOU_FORTUNEGIFT_COUNTDOWN,NULL,STREAMER_ROOM,"eng","0123456789:",
},
//-------------------------------------------
//福袋详情页面-详情页标志页图(识别页面)
{
230,700,510-230,120,DOU_FORTUNEGIFT_DETAIL_IMG,NULL,FORTUNEGIFT_DETAIL,
},
//福袋详情页面-倒计时
{
277,785,310-277,815-785,DOU_FORTUNEGIFT_COUNTDOWN_DETAIL,NULL,FORTUNEGIFT_DETAIL,"eng","0123456789",
},
//福袋详情页面-倒计时2
{
333,785,366-333,815-785,DOU_FORTUNEGIFT_COUNTDOWN_DETAIL2,NULL,FORTUNEGIFT_DETAIL,"eng","0123456789",
},
//福袋详情页面-福袋描述
{
260,865,670-260,1020-865,DOU_FORTUNEGIFT_DETAIL_TEXT,NULL,FORTUNEGIFT_DETAIL,
},
{
31,1117,657,194,DOU_FORTUNEGIFT_TASKRECT,NULL,FORTUNEGIFT_DETAIL,
},
//福袋详情页面-任务描述 1
{
50,1205,670-50,1245-1205,DOU_FORTUNEGIFT_DETAIL_TASK1,NULL,FORTUNEGIFT_DETAIL,
},
//福袋详情页面-任务描述 2
{
50,1250,670-50,1280-1250,DOU_FORTUNEGIFT_DETAIL_TASK2,NULL,FORTUNEGIFT_DETAIL,
},
//福袋详情页面- 参与任务
{
50,1370,610,70,DOU_FORTUNEGIFT_DETAIL_EXECTASK,NULL,FORTUNEGIFT_DETAIL,
},
//----------------------------------------------------
//斗钻详情页面
{
30,800,660,100,DOU_DIAMOND_DETAIL_IMG,NULL,DIAMOND_DETAIL,
},
//----------------------------------------------------
//福袋抽奖结果界面-未中奖
{
115,430,500,80,DOU_FORTUNEGIFT_DRAW_NOPRIZE_IMG,NULL,FORTUNEGIFT_NOPRIZE,
},
{
135,815,440,60,DOU_FORTUNEGIFT_DRAW_NOPRIZE_EXIT,NULL,FORTUNEGIFT_NOPRIZE,
},
//----------------------------------------------------
//福袋抽奖结果界面-中奖
// 福袋中奖页面 只有 DOU_FORTUNEGIFT_DRAW_JACKPOT_POPUP 、DOU_FORTUNEGIFT_DRAW_JACKPOT_CLOSEBTN 是可用坐标
// 其余坐标需要推算
{
115,430,500,80,DOU_FORTUNEGIFT_DRAW_JACKPOT_IMG,NULL,FORTUNEGIFT_JACKPOT,
},
{
110,497,505,103,DOU_FORTUNEGIFT_DRAW_JACKPOT_PRIZEDESC,NULL,FORTUNEGIFT_JACKPOT,
},
{
110,820,500,100,DOU_FORTUNEGIFT_DRAW_JACKPOT_CFMAWARD,NULL,FORTUNEGIFT_JACKPOT,
},
{
139,942,50,44,DOU_FORTUNEGIFT_DRAW_JACKPOT_AFMPROTO,NULL,FORTUNEGIFT_JACKPOT,
},
{
139,942,50,44,DOU_FORTUNEGIFT_DRAW_JACKPOT_AFMPROTO_CHECKED,NULL,FORTUNEGIFT_JACKPOT,
},
{
338,1069,44,42,DOU_FORTUNEGIFT_DRAW_JACKPOT_CLOSEBTN,NULL,FORTUNEGIFT_JACKPOT,
},
{
74,395,573,637,DOU_FORTUNEGIFT_DRAW_JACKPOT_POPUP,NULL,FORTUNEGIFT_JACKPOT,
},
//----------------------------------------------------
//直播间 已经关闭
{
254,120,213,49,STREAMER_ROOMCLOSED_IMG,NULL,STREAMER_ROOMCLOSED,
},
//----------------------------------------------------
//福袋中奖后领奖界面
{
2,48,718,109,DOU_FORTUNEGIFT_AWARDFORTUNEGIFT_IMG,NULL,FORTUNEGIFT_AFMAWARD,
},
{
452,1410,237,83,DOU_FORTUNEGIFT_AWARDFORTUNEGIFT_AFMBTN,NULL,FORTUNEGIFT_AFMAWARD,
},
//----------------------------------------------------
// fixed new trz sections.
{
996,145,35,35,STREAMERROOM_QUIT,NULL,STREAMER_ROOM,
},
};
const int sections_counts=sizeof(sections)/sizeof(TRZ);
pMajor GMajor;
/*
* streamer_room_ocr:
* 用途:识别直播间画面要素
*/
int streamer_room_ocr(tesseract::TessBaseAPI* api,pTRZ trz,char* img_file) {
//int test(char* img_file) {
char* outputText=NULL;
Pix* img=pixRead(img_file);
InitialSections(api,img,trz);
char fortunegift_clip_img[256]={0};
char eh_img_name[256]={0};
char path[256]={0};
GetModulePath(path);
sprintf(fortunegift_clip_img,"%spics\\%d.png",path,(int)FORTUNEGIFT_OR_DIAMOND_RANGE);
ImgEhanceBeforeOCR(fortunegift_clip_img,eh_img_name);
int has_diamond=(searchtemplate(fortunegift_clip_img,DIAMOND_CLICKRECT,trz)==0?1:0);
int has_fortunegift=(searchtemplate(fortunegift_clip_img,FORTUNEGIFT_CLICKRECT,trz)==0?1:0);
GMajor->hasfg=has_fortunegift;
//计算DOU_FORTUNEGIFT_COUNTDOWN
if(has_fortunegift==1) {
//fortunegift_countdown
pTRZ trz_fc=gettrzbycode(trz,DOU_FORTUNEGIFT_COUNTDOWN);
if(trz_fc==NULL) {
printf("Something was wrong (DOU_FORTUNEGIFT_COUNTDOWN)...\n");
}
//FORTUNEGIFT_OR_DIAMOND_RANGE
pTRZ trz_fodr=gettrzbycode(trz,FORTUNEGIFT_OR_DIAMOND_RANGE);
if(trz_fodr==NULL) {
printf("Something was wrong (FORTUNEGIFT_OR_DIAMOND_RANGE)...\n");
}
//裁剪出 DOU_FORTUNEGIFT_COUNTDOWN
trz_fc->x=trz_fodr->x+trz_fc->x;
trz_fc->y=trz_fodr->y+trz_fc->y;
TextTRZ(api,img,trz_fc);
trz_fc->x-=trz_fodr->x;
trz_fc->y-=trz_fodr->y;
}
if(has_diamond==1) {
//DOU_DIAMOND_COUNTDOWN
pTRZ trz_dc=gettrzbycode(trz,DOU_DIAMOND_COUNTDOWN);
if(trz_dc==NULL) {
printf("Something was wrong (DOU_DIAMOND_COUNTDOWN)...\n");
}
//FORTUNEGIFT_OR_DIAMOND_RANGE
pTRZ trz_fodr=gettrzbycode(trz,FORTUNEGIFT_OR_DIAMOND_RANGE);
if(trz_fodr==NULL) {
printf("Something was wrong (FORTUNEGIFT_OR_DIAMOND_RANGE)...\n");
}
//裁剪出 DOU_DIAMOND_COUNTDOWN
trz_dc->x=trz_fodr->x+trz_dc->x;
trz_dc->y=trz_fodr->y+trz_dc->y;
TextTRZ(api,img,trz_dc);
trz_dc->x-=trz_fodr->x;
trz_dc->y-=trz_fodr->y;
}
pTRZ trz_streamer=gettrzbycode(trz,STREAMER);
pTRZ trz_ac=gettrzbycode(trz,ALIVECOUNT);
pTRZ trz_fc=gettrzbycode(trz,DOU_FORTUNEGIFT_COUNTDOWN);
int alivecount=0;
if(1!=sscanf(trz_ac->pText,"%d",&alivecount)) {
printf("直播间人数转换出现问题[%s]\n",trz_ac->pText);
}
GMajor->alivecount=alivecount;
printf("【%s】的直播间,目前有 %s 人,该直播间福袋:[%s],抖钻[%s]",trz_streamer->pText,trz_ac->pText,
(has_fortunegift==1)?"√":"×",
(has_diamond==1)?"√":"×");
if(has_fortunegift==1) printf(",福袋开启剩余时间【%s】\n",trz_fc->pText);
pixDestroy(&img);
return 0;
}
/*
* fg_detail_ocr
* 用途:识别超级福袋详情页面要素 (一步任务)
*/
int fg_detail_ocr(tesseract::TessBaseAPI* api,pTRZ trz,char* img_file) {
int result=-1;
Pix* img=pixRead(img_file);
//1.识别倒计时
pTRZ trz_cd1=gettrzbycode(trz,DOU_FORTUNEGIFT_COUNTDOWN_DETAIL);
pTRZ trz_cd2=gettrzbycode(trz,DOU_FORTUNEGIFT_COUNTDOWN_DETAIL2);
if(!trz_cd1||!trz_cd2) {
printf("福袋详情-识别福袋详情页面出错:未能取到倒计时\n");
goto fg_detail_ocr_exit;
}
/*
* 此处有猫腻
* --------------------------------------
* 如何编写通用的倒计时识别?
*
* come on
* 最好能识别出弹框
* 继而识别出弹框中的三个大矩形
* 1、福袋物品框
* 2、任务框
* 3、点击按钮
*
* --------------------------------------
* 任务框中的内容很重要,两个未“达成”的任务
*
*
*
*
*
*
*/
//trz_cd1->y+=48;trz_cd2->y+=48;
TextTRZ(api,img,trz_cd1);
TextTRZ(api,img,trz_cd2);
//trz_cd1->y-=48;trz_cd2->y-=48;
printf("倒计时[%s:%s]\n",trz_cd1->pText,trz_cd2->pText);
int minutes=0,seconds=0;
if(1!=sscanf(trz_cd1->pText,"%d",&minutes)) {
printf("福袋详情-倒计时分钟%s 转换出错\n",trz_cd1->pText);
goto fg_detail_ocr_exit;
}
if(1!=sscanf(trz_cd2->pText,"%d",&seconds)) {
printf("福袋详情-倒计时秒数%s 转换出错\n",trz_cd2->pText);
goto fg_detail_ocr_exit;
}
GMajor->timestamp=time(NULL);
GMajor->fg_countdown=minutes*60+seconds;
//识别福袋内容 DOU_FORTUNEGIFT_DETAIL_TEXT
pTRZ trz_fdt=gettrzbycode(trz,DOU_FORTUNEGIFT_DETAIL_TEXT);
if(!trz_fdt) {
printf("福袋详情-识别福袋详情页面出错:未能取到福袋内容文本\n");
goto fg_detail_ocr_exit;
}
//trz_fdt->y+=48;
TextTRZ(api,img,trz_fdt);
//trz_fdt->y-=48;
printf("福袋内容[%s]\n",trz_fdt->pText);
GMajor->fg_desc=trz_fdt->pText;
//两步任务?一步任务?
//... 没想好咋做
//初步思路如下:
//扫描图片,检测任务类型,一步?两步?执行了几步?当前状态?
//收集完信息后,外面做决策
/*
* 经过图像对比发现,一个任务和两个任务的图略有区别,(超级福袋)
* 的起始位置不同
* 先找到 其位置,以此为基准,判断是一步任务、两步任务
* 在本机 720 × 1600 , 两部任务比一步任务 y 小 48
*
*/
/*
* 上述方案无法通用,简单依靠像素差可能存在问题。
* 现在计划将方案升级
*/
// pTRZ trz_tk1=gettrzbycode(trz,DOU_FORTUNEGIFT_DETAIL_TASK1);
// if(!trz_tk1) {
// printf("福袋详情-识别福袋详情页面出错:未能取到任务1区域\n");
// goto fg_detail_ocr_exit;
// }
pTRZ trz_tk=gettrzbycode(trz,DOU_FORTUNEGIFT_TASKRECT);
if(!trz_tk) {
printf("福袋详情-识别福袋详情页面出错:未能取到任务描述区域\n");
goto fg_detail_ocr_exit;
}
//trz_tk1->y+=48;
//TextTRZ(api,img,trz_tk1);
TextTRZEh(api,img_file,trz_tk);//使用处理后的图片提高识别准确率
//trz_tk1->y-=48;
if(trz_tk->pText) {
printf("任务1[%s]\n",trz_tk->pText);
GMajor->taskstatus[0]=((strstr(trz_tk->pText,"已达成"))?1:0);
} else {
GMajor->taskstatus[0]=0;
}
//检查执行任务文本
pTRZ trz_et=gettrzbycode(trz,DOU_FORTUNEGIFT_DETAIL_EXECTASK);
if(!trz_et) {
printf("福袋详情-识别福袋详情页面出错:未能取到执行任务按钮的文本\n");
goto fg_detail_ocr_exit;
}
TextTRZ(api,img,trz_et);
printf("执行任务按钮的文本[%s]\n",trz_et->pText);
result=0;
fg_detail_ocr_exit:
pixDestroy(&img);
return result;
}
/*
* fg_detail_2task_ocr
* 用途:识别超级福袋详情页面要素(两步任务)
*/
int fg_detail_2task_ocr(tesseract::TessBaseAPI* api,pTRZ trz,char* img_file) {
int result=-1;
Pix* img=pixRead(img_file);
//1.识别倒计时
pTRZ trz_cd1=gettrzbycode(trz,DOU_FORTUNEGIFT_COUNTDOWN_DETAIL);
pTRZ trz_cd2=gettrzbycode(trz,DOU_FORTUNEGIFT_COUNTDOWN_DETAIL2);
if(!trz_cd1||!trz_cd2) {
printf("福袋详情-识别福袋详情页面出错:未能取到倒计时\n");
goto fg_detail_2task_ocr_exit;
}
TextTRZ(api,img,trz_cd1);
TextTRZ(api,img,trz_cd2);
printf("倒计时[%s:%s]\n",trz_cd1->pText,trz_cd2->pText);
int minutes=0,seconds=0;
if(1!=sscanf(trz_cd1->pText,"%d",&minutes)) {
printf("福袋详情-倒计时分钟%s 转换出错\n",trz_cd1->pText);
goto fg_detail_2task_ocr_exit;
}
if(1!=sscanf(trz_cd2->pText,"%d",&seconds)) {
printf("福袋详情-倒计时秒数%s 转换出错\n",trz_cd2->pText);
goto fg_detail_2task_ocr_exit;
}
GMajor->timestamp=time(NULL);
GMajor->fg_countdown=minutes*60+seconds;
//识别福袋内容 DOU_FORTUNEGIFT_DETAIL_TEXT
pTRZ trz_fdt=gettrzbycode(trz,DOU_FORTUNEGIFT_DETAIL_TEXT);
if(!trz_fdt) {
printf("福袋详情-识别福袋详情页面出错:未能取到福袋内容文本\n");
goto fg_detail_2task_ocr_exit;
}
TextTRZ(api,img,trz_fdt);
GMajor->fg_desc=trz_fdt->pText;
printf("福袋内容[%s]\n",trz_fdt->pText);
//两步任务?一步任务?
//... 没想好咋做
//初步思路如下:
//扫描图片,检测任务类型,一步?两步?执行了几步?当前状态?
//收集完信息后,外面做决策
/*
* 经过图像对比发现,一个任务和两个任务的图略有区别,(超级福袋)
* 的起始位置不同
* 先找到 其位置,以此为基准,判断是一步任务、两步任务
* 在本机 720 × 1600 , 两部任务比一步任务 y 小 48
*
*/
// pTRZ trz_tk1=gettrzbycode(trz,DOU_FORTUNEGIFT_DETAIL_TASK1);
// if(!trz_tk1) {
// printf("福袋详情-识别福袋详情页面出错:未能取到任务1区域\n");
// goto fg_detail_2task_ocr_exit;
// }
// pTRZ trz_tk2=gettrzbycode(trz,DOU_FORTUNEGIFT_DETAIL_TASK2);
// if(!trz_tk2) {
// printf("福袋详情-识别福袋详情页面出错:未能取到任务2区域\n");
// goto fg_detail_2task_ocr_exit;
// }
// //TextTRZ(api,img,trz_tk1);
// TextTRZEh(api,img_file,trz_tk1);//使用处理后的图片提高识别准确率
// if(trz_tk1->pText) printf("任务1[%s]\n",trz_tk1->pText);
// //TextTRZ(api,img,trz_tk2);
// TextTRZEh(api,img_file,trz_tk2);//使用处理后的图片提高识别准确率
// if(trz_tk2->pText) printf("任务2[%s]\n",trz_tk2->pText);
// GMajor->taskstatus[0]=((strstr(trz_tk1->pText,"已达成"))?1:0);
// GMajor->taskstatus[1]=((strstr(trz_tk2->pText,"已达成"))?1:0);
pTRZ trz_tk=gettrzbycode(trz,DOU_FORTUNEGIFT_TASKRECT);
if(!trz_tk) {
printf("福袋详情-识别福袋详情页面出错:未能取到任务描述区域\n");
goto fg_detail_2task_ocr_exit;
}
if(!trz_tk->pText) {
GMajor->taskstatus[0]=GMajor->taskstatus[1]=0;
} else {
char* plt=strstr(trz_tk->pText,"达成");
char* plt2=strstr(plt+4,"达成");
if(plt&&plt2) {
plt=plt-2;
plt2=plt2-2;
GMajor->taskstatus[0]=((strstr(plt,"已达成")==plt)?1:0);
GMajor->taskstatus[1]=((strstr(plt2,"已达成")==plt2)?1:0);
} else {
printf("福袋详情-识别福袋详情页面出错:未能取到任务描述区域关于任务状态的描述\n");
goto fg_detail_2task_ocr_exit;
}
}
//检查执行任务文本
pTRZ trz_et=gettrzbycode(trz,DOU_FORTUNEGIFT_DETAIL_EXECTASK);
if(!trz_et) {
printf("福袋详情-识别福袋详情页面出错:未能取到执行任务按钮的文本\n");
goto fg_detail_2task_ocr_exit;
}
TextTRZ(api,img,trz_et);
printf("执行任务按钮的文本[%s]\n",trz_et->pText);
result=0;
fg_detail_2task_ocr_exit:
pixDestroy(&img);
return result;
}
/*
* 福袋开奖结果-中奖页面要素识别
*/
int fg_draw_jackpot_ocr(tesseract::TessBaseAPI* api,pTRZ trz,char* img_file) {
printf("中奖页面.\n");
int ret=-1;
//将中奖图片存入文件夹 ./pics/award/
time_t now=time(NULL);
struct tm* local=localtime(&now);
char award_name[256]="";
char path[256]={0};
GetModulePath(path);
sprintf(award_name,"%spics\\award\\%04d_%02d_%02d_%02d_%02d_%02d.png",
path,
local->tm_year+1900,local->tm_mon+1,local->tm_mday,
local->tm_hour,local->tm_min,local->tm_sec
);
CopyFile(img_file,award_name,TRUE);
/*
* 由于领奖按钮 和 确认协议的 checkbox 的位置不确定
* 此处需要查找修正
* 首先裁剪出popup
* 继而在裁剪的popup上寻找相关的 afmbtn/proto_checkbox
*/
pTRZ trz_popup=gettrzbycode(trz,DOU_FORTUNEGIFT_DRAW_JACKPOT_POPUP);
if(!trz_popup) {
printf("获取选区DOU_FORTUNEGIFT_DRAW_JACKPOT_POPUP失败。\n");
goto fg_draw_jackpot_ocr_exit;
}
//裁剪
char clip_file[256]={0};
//对图像指定区域裁切保存
Pix* pixSource=pixRead(img_file);
ClipTRZ(pixSource,trz_popup,clip_file);
pixDestroy(&pixSource);
pTRZ trz_affimbtn=gettrzbycode(trz,DOU_FORTUNEGIFT_DRAW_JACKPOT_CFMAWARD,0);
if(!trz_affimbtn) {
printf("获取选区 DOU_FORTUNEGIFT_DRAW_JACKPOT_CFMAWARD 失败。\n");
goto fg_draw_jackpot_ocr_exit;
}
if(searchtemplate(clip_file,DOU_FORTUNEGIFT_DRAW_JACKPOT_CFMAWARD,trz)!=0) {
printf("查找匹配的领奖确认按钮失败.\n");
goto fg_draw_jackpot_ocr_exit;
} else {
/*
* 修正相对坐标的DOU_FORTUNEGIFT_DRAW_JACKPOT_CFMAWARD
*/
trz_affimbtn->x+=trz_popup->x;trz_affimbtn->y+=trz_popup->y;
}
pTRZ trz_proto=gettrzbycode(trz,DOU_FORTUNEGIFT_DRAW_JACKPOT_AFMPROTO,0);
if(!trz_proto) {
printf("获取选区 DOU_FORTUNEGIFT_DRAW_JACKPOT_AFMPROTO 失败。\n");
goto fg_draw_jackpot_ocr_exit;
}
/*
* 检查选区内有无圆圈
*/
if(searchtemplate(clip_file,DOU_FORTUNEGIFT_DRAW_JACKPOT_AFMPROTO,trz)!=0) {
printf("获取 DOU_FORTUNEGIFT_DRAW_JACKPOT_AFMPROTO 失败。\n");
goto fg_draw_jackpot_ocr_exit;
} else {
trz_proto->x+=trz_popup->x;trz_proto->y+=trz_popup->y;
}
pTRZ trz_proto_checked=gettrzbycode(trz,DOU_FORTUNEGIFT_DRAW_JACKPOT_AFMPROTO_CHECKED,0);
if(!trz_proto_checked) {
printf("获取选区 DOU_FORTUNEGIFT_DRAW_JACKPOT_AFMPROTO_CHECKED 失败。\n");
goto fg_draw_jackpot_ocr_exit;
}
/*
* 将 trz_proto_checked的坐标更新为 trz_proto;
*/
trz_proto_checked->x=trz_proto->x;trz_proto_checked->y=trz_proto->y;
trz_proto_checked->width=trz_proto->width;trz_proto_checked->height=trz_proto->height;
/*
* 未找到 同意协议(未选中)的checkbox
* 判定为 同意协议(已选中)
* 此处进一步识别确认
* 注意 这里调用 searchtemplate 确认协议是否点选的时候, update_trz 标志 必须置为0,即 不得更新已经刷新的
* DOU_FORTUNEGIFT_DRAW_JACKPOT_AFMPROTO / DOU_FORTUNEGIFT_DRAW_JACKPOT_AFMPROTO_CHECKED
* trz 坐标
*/
int isproto_checked=-1;
if(searchtemplate(clip_file,DOU_FORTUNEGIFT_DRAW_JACKPOT_AFMPROTO,trz,0)==0) {
isproto_checked=0;
} else {
if(searchtemplate(clip_file,DOU_FORTUNEGIFT_DRAW_JACKPOT_AFMPROTO_CHECKED,trz,0)==0) {
isproto_checked=1;
}
}
GMajor->proto_checked=isproto_checked;
if(isproto_checked==-1) {
printf("判定是否点选同意协议失败.\n");
goto fg_draw_jackpot_ocr_exit;
} else {
/*
* 更新 trz_proto、trz_proto_checked
*/
// if(isproto_checked==0) {
// trz_proto->x+=trz_popup->x;trz_proto->y+=trz_popup->y;
// trz_proto_checked->x=trz_proto->x;trz_proto_checked->y=trz_proto->y;
// trz_proto_checked->width=trz_proto->width;trz_proto_checked->height=trz_proto->height;
// } else {
// trz_proto_checked->x+=trz_popup->x;trz_proto_checked->y+=trz_popup->y;
// trz_proto->x=trz_proto_checked->x;trz_proto->y=trz_proto_checked->y;
// trz_proto->width=trz_proto_checked->width;trz_proto->height=trz_proto_checked->height;
// }
}
ret=0;
fg_draw_jackpot_ocr_exit:
return ret;
}
/*
* 福袋开奖结果 -- 没有中奖 页面要素识别
*/
int fg_draw_noprize_ocr(tesseract::TessBaseAPI* api,pTRZ trz,char* img_file) {
printf("未中奖页面.\n");
return 0;
}
int ImgEhanceBeforeOCR(const char* img_file,char* eh_img_file) {
cv::Mat image=cv::imread(img_file);
if(image.empty()) {
printf("无法打开图像[%s]。\n",img_file);
return -1;
}
//灰度处理
cv::Mat gray;
cv::cvtColor(image,gray,cv::COLOR_BGR2GRAY);
//自适应阈值
cv::Mat adaptiveThresh;
cv::adaptiveThreshold(gray,adaptiveThresh,255,cv::ADAPTIVE_THRESH_GAUSSIAN_C, cv::THRESH_BINARY, 15, -2);
//cv::Mat hline=getStructuringElement(cv::MORPH_RECT,cv::Size(image.cols/16,1),cv::Point(-1,-1));
//cv::Mat vline=getStructuringElement(cv::MORPH_RECT,cv::Size(1,image.rows/16),cv::Point(-1,-1));
cv::Mat kernel=getStructuringElement(cv::MORPH_RECT,cv::Size(1,1),cv::Point(-1,-1));
//膨胀与腐蚀
cv::Mat dilated,eroded;
//cv::Mat kernel=cv::getStructuringElement(cv::MORPH_RECT,cv::Size(2,2));
cv::erode(adaptiveThresh,eroded,kernel);
cv::dilate(eroded,dilated,kernel);
// 反色处理
cv::Mat inverted;
cv::bitwise_not(dilated,inverted);
//边缘处理
//cv::blur(inverted,inverted,cv::Size(2,2),cv::Point(-1,-1));
//去噪
cv::Mat denoised;
cv::fastNlMeansDenoising(inverted,denoised,50,7,21);
if(eh_img_file[0]=='\0') getehimagename(img_file,eh_img_file);
cv::imwrite(eh_img_file,denoised);
return 0;
}
int ClipTRZ(Pix* pix,pTRZ trz) {
if(!pix||!trz) {
printf("Clip TRZ 失败:图像或选区数据无效。\n");
return -1;
}
//裁剪图片
BOX* clipBox=boxCreate(trz->x,trz->y,trz->width,trz->height);
if(clipBox==NULL) {
printf("创建裁剪区失败,TRZ CODE:%d.\n",trz->code);
}
PIX* clipImg=pixClipRectangle(pix,clipBox,NULL);
if(clipImg==NULL) {
printf("图片裁剪失败\n");
} else {
//写入文件
char clip_file[256]={0};
char path[256]={0};
GetModulePath(path);
sprintf(clip_file,"%spics\\%d.png",path,(int)trz->code);
if(0!=pixWrite(clip_file,clipImg,IFF_PNG)) {
printf("裁剪图片写入文件%s 失败\n",clip_file);
} else {
//printf("裁剪图片写入文件%s\n",clip_file);
}
}
//释放资源
pixDestroy(&clipImg);
boxDestroy(&clipBox);
return 0;
}
int ClipTRZ(Pix* pix,pTRZ trz,char* clip_file) {
if(!pix||!trz) {
printf("Clip TRZ 失败:图像或选区数据无效。\n");
return -1;
}
//裁剪图片
BOX* clipBox=boxCreate(trz->x,trz->y,trz->width,trz->height);
if(clipBox==NULL) {
printf("创建裁剪区失败,TRZ CODE:%d.\n",trz->code);
}
PIX* clipImg=pixClipRectangle(pix,clipBox,NULL);
if(clipImg==NULL) {
printf("图片裁剪失败\n");
} else {
char path[256]={0};
GetModulePath(path);
//写入文件
if(clip_file[0]=='\0') sprintf(clip_file,"%spics\\%d.png",path,(int)trz->code);
if(0!=pixWrite(clip_file,clipImg,IFF_PNG)) {
printf("裁剪图片写入文件%s 失败\n",clip_file);
} else {
//printf("裁剪图片写入文件%s\n",clip_file);
}
}
//释放资源
pixDestroy(&clipImg);
boxDestroy(&clipBox);
return 0;
}
/*
* TextTRZ的增强版,需要对图像进行预处理
* 该版本效率极低,一个图片需要3读2写,待优化,后期考虑是否可以使用内存而非文件过度
*/
int TextTRZEh(tesseract::TessBaseAPI* api,const char* img_file,pTRZ trz) {
char clip_file[256]={0};
char eh_img_file[256]={0};
//1.对图像指定区域裁切保存
Pix* pixSource=pixRead(img_file);
ClipTRZ(pixSource,trz,clip_file);
pixDestroy(&pixSource);
//2.对裁切图像进行识别前的处理
// 对 eh_img_file 赋值
// 增强图为原图名加上 "_Eh"
getehimagename(clip_file,eh_img_file);
ImgEhanceBeforeOCR(clip_file,eh_img_file);
//3.识图
Pix* pix=pixRead(eh_img_file);
TextTRZ(api,pix,trz,0);//不需要裁剪
pixDestroy(&pix);
return 0;
}
/*
* 取回的OCR文本附加处理
*/
int TextTRZExtra(pTRZ trz) {
if(!trz||!trz->pText) return 0;
char* plt=trz->pText;
size_t len=strlen(plt);
if(plt[len-1]=='\n') plt[len-1]='\0';
//福袋详情内容去回车换行符
if(trz->code==DOU_FORTUNEGIFT_DETAIL_TEXT&&trz->pText) {
char* plt_buffer=(char*)calloc(sizeof(char)*(len+1),1);
if(!plt_buffer) return -1;
for(int idx=0,dst_idx=0;idx<len;idx++) {
if(plt[idx]!='\r'&&plt[idx]!='\n') {
plt_buffer[dst_idx]=plt[idx];
dst_idx++;
}
}
memcpy(trz->pText,plt_buffer,len);
trz->pText[len]='\0';
if(plt_buffer) free(plt_buffer);
}
return 0;
}
int TextTRZ(tesseract::TessBaseAPI* api,Pix* pix,pTRZ trz,int clip_flag) {
if(clip_flag!=0) ClipTRZ(pix,trz);
//获取图片高宽
int img_width=pixGetWidth(pix);
int img_height=pixGetHeight(pix);
if(img_width<=0||img_height<=0) return -1;
//检查TRZ设置选区是否合法
if(clip_flag!=0) {
if(trz->x<0||trz->x>=img_width||
trz->y<0||trz->y>=img_height||
trz->x+trz->width>img_width||
trz->y+trz->height>img_height
) {
if(f->extra) {
pwfpj_param wfpj=(pwfpj_param)f->extra;
SendMessage(wfpj->imgsratch,MSG_SECTIONSETTINGS,(WPARAM)trz,(LPARAM)trz->code);
}
}
} else {
if(trz->width<=0||trz->width>img_width||
trz->height<=0||trz->height>img_height
) {
if(f->extra) {
pwfpj_param wfpj=(pwfpj_param)f->extra;
SendMessage(wfpj->imgsratch,MSG_SECTIONSETTINGS,(WPARAM)trz,(LPARAM)trz->code);
}
}
}
//设置api的traindata
if(trz->traindata[0]=='\0') strcpy(trz->traindata,"chi_sim");
if(api->Init(datapath,trz->traindata)) {
printf("Initial Tesseract failed.\n");
return -1;
}
//加载图片
api->SetImage(pix);
//设置tessedit_char_whitelist参数
if(trz->whitelist[0]!='\0') api->SetVariable("tessedit_char_whitelist",trz->whitelist);
else api->SetVariable("tessedit_char_whitelist", "");
if(trz->pText) {
free(trz->pText);
trz->pText=NULL;
}
int result;
if(clip_flag!=0) result=TextRectOCR(api,trz->x,trz->y,trz->width,trz->height,&(trz->pText));
else result=TextRectOCR(api,0,0,trz->width,trz->height,&(trz->pText));
if(result==0) result=TextTRZExtra(trz);
//终止当前的识别绘画
api->End();
return result;
}
int TextTRZ(tesseract::TessBaseAPI* api,Pix* pix,pTRZ trz) {
return TextTRZ(api,pix,trz,1);
}
int InitialSections(tesseract::TessBaseAPI* api,Pix* pix,pTRZ trz) {
size_t trz_size=_msize(trz)/sizeof(TRZ);
for(int idx=0;idx<trz_size;idx++) {
if((trz+idx)->code<=FORTUNEGIFT_OR_DIAMOND_RANGE&&
(trz+idx)->code>=STREAMER) {
if((trz+idx)->code==ALIVECOUNT) {
api->SetVariable("tessedit_char_whitelist", "0123456789");
} else if((trz+idx)->code==DOU_DIAMOND_COUNTDOWN||
(trz+idx)->code==DOU_DIAMOND_COUNTDOWN) {
api->SetVariable("tessedit_char_whitelist", "0123456789:");
}
if((trz+idx)->code==STREAMER) {
char eh_name[256]="";
char path[256]={0};
GetModulePath(path);
sprintf(eh_name,"%spics\\%d.png",path,(int)STREAMER);
TRZ rtmp;
memcpy(&rtmp,trz+idx,sizeof(rtmp));
rtmp.x=rtmp.y=0;
TextTRZEh(api,eh_name,&rtmp);
(trz+idx)->pText=rtmp.pText;
} else TextTRZ(api,pix,trz+idx);
}
}
return 0;
}
void ReleaseSections(pTRZ trz) {
if(!trz) return;
size_t trz_size=_msize(trz)/sizeof(TRZ);
for(int idx=0;idx<trz_size;idx++) {
if((trz+idx)->pText) delete[] (trz+idx)->pText;
(trz+idx)->pText=NULL;
}
delete[] trz;
}
int TextRectOCR(tesseract::TessBaseAPI* api,int x,int y,int width,int height,char** pText) {
api->SetRectangle(x,y,width,height);
char* outputTextUTF8=NULL;
outputTextUTF8=api->GetUTF8Text();
if(outputTextUTF8==NULL) return -1;
//utf8 转 ACII
wchar_t* text_wchar=NULL;
int wchar_length=MultiByteToWideChar(CP_UTF8,0,outputTextUTF8,-1,NULL,0);
if(wchar_length<=0) {
delete[] outputTextUTF8;
outputTextUTF8=NULL;
return -1;
}
text_wchar=new wchar_t[wchar_length+1];
MultiByteToWideChar(CP_UTF8,0,outputTextUTF8,-1,text_wchar,wchar_length);
char* outputText=NULL;
int gbk_length=WideCharToMultiByte(CP_ACP,0,text_wchar,-1,NULL,0,NULL,NULL);
if(gbk_length<=0) {
delete[] outputTextUTF8;
outputTextUTF8=NULL;
if(text_wchar) delete[] text_wchar;
text_wchar=NULL;
return -1;
}
outputText=new char[gbk_length+1];
WideCharToMultiByte(CP_ACP,0,text_wchar,-1,outputText,gbk_length,NULL,NULL);
if(outputTextUTF8) delete[] outputTextUTF8;
outputTextUTF8=NULL;
if(text_wchar) delete[] text_wchar;
text_wchar=NULL;
//gbk_length=strlen(outputText);
//if(outputText[gbk_length-1]=='\n') outputText[gbk_length-1]='\0';
*pText=outputText;
return 0;
}
int getehimagename(const char* img_name,char* eh_name) {
#define EHANCE_IMG_EXT "_Eh"
for(int idx=strlen(img_name)-1;idx>=0;idx--)
if(img_name[idx]=='.') {
strncpy(eh_name,img_name,idx);
strcat(eh_name,EHANCE_IMG_EXT);
strcat(eh_name,&img_name[idx]);
//printf("source name:%s, ehance name:%s\n",img_name,eh_name);
return 0;
}
return -1;
}
int searchtemplate(const char* source_img,TRZCODE code,pTRZ list) {
return searchtemplate(source_img,code,list,1);
}
/*
* update_trz : 默认值为 1 - 更新匹配的trz
*/
int searchtemplate(const char* source_img,TRZCODE code,pTRZ list,int update_trz) {
char template_imgs[][256]={
"pics\\diamond_template.png",
"pics\\gift_template.png",
"pics\\fortunegift_award_template.png",
"pics\\fortunegift_protunchecked_template.png", //协议未点选
"pics\\fortunegift_protchecked_template.png",
"pics\\fortunegift_detail_template.png",
};
char path[256]={0};
GetModulePath(path);
for(int idx=0;idx<sizeof(template_imgs)/sizeof(char[256]);idx++) {
char tmp[256]={0};
sprintf(tmp,"%s%s",path,template_imgs[idx]);
strcpy(template_imgs[idx],tmp);
}
char* template_img=NULL;
switch((int)code) {
case DIAMOND_CLICKRECT:
template_img=&template_imgs[0][0];
break;
case FORTUNEGIFT_CLICKRECT:
template_img=&template_imgs[1][0];
break;
case DOU_FORTUNEGIFT_DRAW_JACKPOT_CFMAWARD:
template_img=&template_imgs[2][0];
break;
case DOU_FORTUNEGIFT_DRAW_JACKPOT_AFMPROTO:
template_img=&template_imgs[3][0];
break;
case DOU_FORTUNEGIFT_DRAW_JACKPOT_AFMPROTO_CHECKED:
template_img=&template_imgs[4][0];
break;
case DOU_FORTUNEGIFT_DETAIL_IMG:
template_img=&template_imgs[5][0];
break;
}
//printf("source img:%s,template img:%s\n",source_img,template_img);
cv::Mat source=cv::imread(source_img);
cv::Mat templt=cv::imread(template_img);
if(source.empty()||templt.empty()) {
printf("加载图片或者模板图片失败\n");
return -1;
}
// 转换为灰度图像