-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcarplate_main.cpp
More file actions
2390 lines (2098 loc) · 75.7 KB
/
carplate_main.cpp
File metadata and controls
2390 lines (2098 loc) · 75.7 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 "carplate_main.h"
#include "util/chtext.h"
#include "car_gbkcode.h"
static char debugMsg[] = "$GPRMC,051504.900,A,3108.27655273,N,12038.57309193,E,3.5,158.8,191222,,,D*62\n";
static void* GpsThread(void* arg);
static void* CarPortDetThread(void* arg);
stGpsInformat* pGpsInfo;
stGpsInformat stGpsInfo = {0};
volatile uint8 GsmstatFlag;
uint8 devSeriNum[6];
int fileDataLen = 0;
uint8 actionCode = 0;
unsigned short takePhotoSeriNo;
uint8 fileDataBuff[1024 * 1024];
uint8 alarmflag = 0;
void ParseGpsDataToJTTGPS(stGpsInformat* pGpsInfo,GPSData* gpsData);
GPSData g_gpsData;
static LatLon g_pre_latlon;
static uint8 detFlag = 0; //启动车辆检测标记
typedef struct _CarportInfo {
char areaId[50];
char portId[50];
LatLon bLatLon;
LatLon eLatLon;
}CarportInfo;
#define FRAME_WIDTH 1280
#define FRAME_HEIGHT 720
uint8 takePhotoFlag = 0;
uint8 cameraType = 0;
float carnum_score = 0.5;
float car_score = 0.5;
char activacode[64] = {0};
uint8 isRTSPEnable = 1;
float gpsoffset; //定位模块偏移量
uint8 angleRegion[2];
uint8 car_role = 0; // 0 client, 1 server
float cosAngleMin = 0;
float cosAngleMax = 0;
cv::Rect vehicle_det_rect;
pthread_mutex_t pth_lock_port_init;
pthread_mutex_t pth_lock_gps;
pthread_mutex_t pth_lock_port;
static CarportInfo g_portInfo;
vector<CarportInfo> carportList;
static int baudrate = 115200;
static uint8 uGpsBuff[1024];
static int frameErr = 0;
#define GPS_DEV "/dev/ttyS3"
#define LOGGER_PATH "/mnt/sdcard/logger.log"
#define SQRT2 sqrt(2)
#define SQRT3 sqrt(3)
#define PI 3.1415926
#define EARTH_RADIUS 6378137 //地球近似半径
#define EARTH_CIRCUMFERNCE EARTH_RADIUS*PI*2
#define PER_LON_METER 360 / (EARTH_CIRCUMFERNCE) //纬度方向1米对应度数
double radian(double d);
double angle(double d);
double get_distance(double lat1,double lng1,double lat2,double lng2);
// 求弧度
double radian(double d) {
return d * PI / 180.0; //角度1˚ = π / 180
}
// 求角度
double angle(double d) {
return d / PI * 180.0; //角度1˚ = π / 180
}
//计算距离
double get_distance(double lat1,double lng1,double lat2,double lng2) {
double radLat1 = radian(lat1);
double radLat2 = radian(lat2);
double a = radLat1 - radLat2;
double b = radian(lng1) - radian(lng2);
double dst = 2 * asin((sqrt(pow(sin(a / 2),2) + cos(radLat1) * cos(radLat2) * pow(sin(b / 2),2) )));
dst = dst * EARTH_RADIUS;
dst= round(dst * 100) / 100;
return dst;
}
int gpsLog = 0;
#define TEST_ARGB32_PIX_SIZE 4
static void set_argb8888_buffer(RK_U32 *buf, RK_U32 size, RK_U32 color) {
for (RK_U32 i = 0; buf && (i < size); i++)
*(buf + i) = color;
}
static bool quit = false;
static FILE *g_output_file;
static void sigterm_handler(int sig) {
fprintf(stderr, "signal %d\n", sig);
quit = true;
}
static FILE *g_port_log_file;
static FILE *g_gps_log_file;
MPP_CHN_S g_stViChn;
MPP_CHN_S g_stVencChn;
static char optstr[] = "?::f:g:G:o:l:L:R:b:t:";
static const struct option long_options[] = {
{"file", optional_argument, NULL, 'f'},
{"gpsLog level", optional_argument, NULL, '?'},
{"gpsData", optional_argument, NULL, 'G'},
{"output_path", required_argument, NULL, 'o'},
{"log path", required_argument, NULL, 'l'},
{"gpslog path", required_argument, NULL, 'L'},
{"rtsp", required_argument, NULL, 'R'},
{"gps baudrate", required_argument, NULL, 'b'},
{"camera type", required_argument, NULL, 't'},
{"help", optional_argument, NULL, '?'},
{NULL, 0, NULL, 0},
};
static void print_usage(const char *name) {
printf("usage example:\n");
printf("\t-f | carport folder, Default:\"carports\"\n");
printf("\t-g | gps log print\n");
printf("\t-L | gps log path\n");
printf("\t-l | portinfo match log \n");
printf("\t-R | enable RTSP \n");
printf("\t-t | camera type:0-Rignt Front 1-Right Bcak 2-Right Center \n");
printf("\t-o | --output_path: output path, Default:NULL\n");
printf("\n");
}
int UtfToGbk(char* pszBufIn, int nBufInLen,char* pszBufOut)
{
int i = 0;
int j = 0, nLen;
unsigned short unicode;
unsigned short gbk;
for(; i < nBufInLen; i++, j++)
{
if((pszBufIn[i] & 0x80) == 0x00)
{
nLen = 1;
pszBufOut[j]= pszBufIn[i];
}
/*
else if((pszBufIn[i] & 0xE0) == 0xC0)// 2λ
{
nLen = 2;
unicode = (pszBufIn[i] & 0x1F << 6) | (pszBufIn[i+1]& 0x3F);
}*/
else if ((pszBufIn[i] & 0xF0) == 0xE0) // 3λ
{
if (i+ 2 >= nBufInLen) return -1;
unicode = (((int)(pszBufIn[i] & 0x0F)) << 12) | (((int)(pszBufIn[i+1] & 0x3F)) << 6) | (pszBufIn[i+2] & 0x3F);
gbk = mb_uni2gb_table[unicode-0x4e00];
pszBufOut[j]= gbk/256;
pszBufOut[j+1] = gbk%256;
j++;
i+=2;
}
else
{
return -1;
}
}
//*pnBufOutLen = j;
//fprintf(stderr,"pnbufoutlen=%d\n",*pnBufOutLen);
return 0;
}
//排序接口
void qusort(float *data, int *index, int start, int end) {
int i, j; //定义变量为基本整型
i = start; //将每组首个元素赋给i
j = end; //将每组末尾元素赋给j
*data = *(data + start);
*index = *(index + start);
while (i < j)
{
while (i < j&&data[0] >= data[j])
j--; //位置左移
if (i < j)
{
*(data + i) = *(data + j);
*(index + i) = *(index + j);
i++; //位置右移
}
while (i < j&&data[i] >= data[0])
i++; //位置左移
if (i < j)
{
*(data + j) = *(data + i);
*(index + j) = *(index + i);
}
}
*(data + i) = *data;
*(index + i) = *index;
if (start < i)
qusort(data, index, start, j - 1); //对分割出的部分递归调用qusort()函数
if (i < end)
qusort(data, index, j + 1, end);
}
//计算矩形框面积
float calcRectArea(XHLPR_API::Rect &rect) {
return rect.width * rect.height;
}
void sort_vehicle(VehicleInfo *vehicle,int count,int *index) {
if (count == 1) {
index[1] = 0;
// return 0;
}
else {
float area[count + 1];
area[0] = 0;
for (int i = 0; i < count; i++) {
area[i + 1] = calcRectArea(vehicle[i].vehicle_rect);
index[i + 1] = i;
}
qusort(area, index, 1, count);
}
}
cv::Mat nv12Frame;
cv::Mat overlay = Mat::zeros(95, FRAME_WIDTH, CV_8UC3);
#define SCALAR_WHITE Scalar(255, 255, 255)
XHLPR_SESS sess_vd; //车辆检测会话
XHLPR_SESS sess_pd; //车牌检测会话
CvxText cvxText("simsun.ttc");
int UTF2Unicode(wchar_t *wstr, int size, char *utf8)
{
int size_s = strlen(utf8);
int size_d = size;
wchar_t *des =wstr;
char *src = utf8;
memset(des, 0, size * sizeof(wchar_t));
int s = 0, d = 0;
// bool toomuchbyte = true; //set true to skip error prefix.
while (s < size_s && d < size_d)
{
unsigned char c = src[s];
if ((c & 0x80) == 0)
{
des[d++] += src[s++];
}
else if((c & 0xE0) == 0xC0) ///< 110x-xxxx 10xx-xxxx
{
wchar_t &wideChar = des[d++];
wideChar = (src[s + 0] & 0x3F) << 6;
wideChar |= (src[s + 1] & 0x3F);
s += 2;
}
else if((c & 0xF0) == 0xE0) ///< 1110-xxxx 10xx-xxxx 10xx-xxxx
{
wchar_t &wideChar = des[d++];
wideChar = (src[s + 0] & 0x1F) << 12;
wideChar |= (src[s + 1] & 0x3F) << 6;
wideChar |= (src[s + 2] & 0x3F);
s += 3;
}
else if((c & 0xF8) == 0xF0) ///< 1111-0xxx 10xx-xxxx 10xx-xxxx 10xx-xxxx
{
wchar_t &wideChar = des[d++];
wideChar = (src[s + 0] & 0x0F) << 18;
wideChar = (src[s + 1] & 0x3F) << 12;
wideChar |= (src[s + 2] & 0x3F) << 6;
wideChar |= (src[s + 3] & 0x3F);
s += 4;
}
else
{
wchar_t &wideChar = des[d++]; ///< 1111-10xx 10xx-xxxx 10xx-xxxx 10xx-xxxx 10xx-xxxx
wideChar = (src[s + 0] & 0x07) << 24;
wideChar = (src[s + 1] & 0x3F) << 18;
wideChar = (src[s + 2] & 0x3F) << 12;
wideChar |= (src[s + 3] & 0x3F) << 6;
wideChar |= (src[s + 4] & 0x3F);
s += 5;
}
}
return d;
}
typedef struct plateupload_run_data_ {
uint8 plate_number[32];
uint8 plate_number_len;
uint8 plate_number_score;
int plate_type;
int plate_color;
uint8 port_number[50];
uint8 port_number_len;
uint8 area_id[50];
stGpsInformat sGpsInfo;
char path[128];
}plateupload_run_data;
class plateupload_safe_queue
{
private:
//互斥锁
mutable std::mutex mut;
//缓存的待发送的数据
std::queue<plateupload_run_data> data_queue;
//条件变量
std::condition_variable data_cond;
//结束标志变量
bool over;
//结束时,缓存数据是否为空的标志变量
bool flag;
public:
plateupload_safe_queue() {
over = false;
flag = true;
}
//析构函数,释放内存
~plateupload_safe_queue() {
}
//入队操作
void push(plateupload_run_data new_value)
{
std::lock_guard<std::mutex> lk(mut);
data_queue.push(new_value);
data_cond.notify_one();
}
/*pop data_queue的数据,如果data_queue为空就一直等待
over data_queue.empty() 返回
!over data_queue.empty() 等待
over !data_queue.empty() 返回
!over !data_queue.empty() 返回
*/
void wait_and_pop(plateupload_run_data& value)
{
std::unique_lock<std::mutex> lk(mut);
data_cond.wait(lk, [this] {
return ((!(data_queue.empty() ^ over)) || over);
});
if (data_queue.empty()) {
flag = false; return;
};
value = data_queue.front();
data_queue.pop();
}
//重载wait_and_pop,与上相同,本demo中未使用
std::shared_ptr<plateupload_run_data> wait_and_pop()
{
std::unique_lock<std::mutex> lk(mut);
data_cond.wait(lk, [this] {
return ((!(data_queue.empty() ^ over)) || over);
});
if (data_queue.empty()) {
flag = false; return std::shared_ptr<plateupload_run_data>();
}
std::shared_ptr<plateupload_run_data> res(std::make_shared<plateupload_run_data>(data_queue.front()));
data_queue.pop();
return res;
}
//不管有没有队首元素,如果队列有元素就返回元素,并返回true,否则返回false,本demo中未使用
bool try_pop(plateupload_run_data& value)
{
std::lock_guard<std::mutex> lk(mut);
if (data_queue.empty())
return false;
std::cout << data_queue.size() << std::endl;
value = data_queue.front();
data_queue.pop();
return true;
}
//重载try_pop,与上相同,本demo中未使用
std::shared_ptr<plateupload_run_data> try_pop()
{
std::lock_guard<std::mutex> lk(mut);
if (data_queue.empty())
return std::shared_ptr<plateupload_run_data>();
std::shared_ptr<plateupload_run_data> res(std::make_shared<plateupload_run_data>(data_queue.front()));
data_queue.pop();
return res;
}
//查看队列是否为空
bool empty() const
{
std::lock_guard<std::mutex> lk(mut);
return data_queue.empty();
}
//检测主线程是否结束
bool is_over() {
std::unique_lock<std::mutex> lk(mut);
return over;
}
//检测主线程结束,over设置为true
int set_over() {
std::unique_lock<std::mutex> lk(mut);
over = true;
data_cond.notify_one();
return 0;
}
//返回缓存检测车牌数量,本demo中未使用
int size() {
std::unique_lock<std::mutex> lk(mut);
return data_queue.size();
}
void try_send(){
cout << "start try_send" << endl;
plateupload_run_data run_data;
wait_and_pop(run_data);
if (!flag || !isBubiaoConnected()) {
printf("flag: %d, Bubiao connected: %d\n", flag, isBubiaoConnected());
return;
}
cout << "start BB_PLATEUPLOAD" << endl;
BB_PLATEUPLOAD(run_data.plate_number,run_data.plate_number_len,run_data.plate_number_score,run_data.plate_type,
run_data.plate_color,run_data.port_number,run_data.port_number_len,run_data.area_id,&run_data.sGpsInfo,run_data.path);
}
};
//车牌识别线程队列
plateupload_safe_queue plateupload_queue;
//车牌识别线程函数
void *PlateUpload_run(void *threadarg)
{
printf("-----------PlateUpload_run start------------\n");
//车牌检测结束,并且队列待识别的数据为0则结束识别
for (; !plateupload_queue.is_over() || !plateupload_queue.empty();) {
cout << "is_over: " << plateupload_queue.is_over() << " size: " << plateupload_queue.size() << endl;
plateupload_queue.try_send();
}
printf("------------PlateUpload_run end------------\n");
pthread_exit(NULL);
}
//缓存的检测图片和检测车牌信息结构体
typedef struct xhlpr_run_data_ {
uint8 vdCount;
uint8 pdCount;
PlateInfo plate;
cv::Mat img;
CarportInfo portInfo;
GPSData gpsData;
}xhlpr_run_data;
class threadsafe_queue
{
private:
//互斥锁
mutable std::mutex mut;
//缓存的待识别的检测数据
std::queue<xhlpr_run_data> data_queue;
//缓存的待发送的识别数据
std::vector<xhlpr_run_data> cache_port_plates;
//最后一个发送的识别数据
std::string last_plate;
std::string last_port;
//条件变量
std::condition_variable data_cond;
//缓存量,每隔多少缓存发送一次车牌号
int cache_size = 3;
//每缓存多少帧,清理一下前cache_size个缓存
int clean_cache_size = 6;
//检测结束标志变量
bool over;
//检测结束时,缓存数据是否为空的标志变量
bool flag;
//车牌识别会话
XHLPR_SESS sess_pr;
//预留接口,发送数据
void send(std::string &str) {
std::cout << "---------------" << str << std::endl;
}
public:
threadsafe_queue() {
last_plate = "";
last_port = "";
over = false;
flag = true;
}
/*车牌号过滤接口
如果车牌首字母不为汉字,或者车牌号中间有汉字则返回false
*/
bool plateFilter(const char *str) {
if (str[0] == '\0')return false;
int s1 = int(str[0]);
if (s1 >= 48 && s1 <= 90)return false;
s1 = int(str[2]);
if(s1 >=48 && s1 <= 90){
if(s1 >= 48 && s1 <= 57)return false;
}else{
s1 = int(str[3]);
if(s1 >= 48 && s1 <= 57)return false;
}
int str_len = 0;
int c = -1;
for (int i = 3; i < 20; i++) {
if (str[i] == '\0') {
str_len = i;
break;
}
if (c == -1) {
s1 = int(str[i]);
if (s1 < 48 || s1 > 90) c = i;
}
}
if (c == -1) return true;
if (c < str_len - 3)return false;
return true;
}
//创建车牌识别会话接口
int create_sess() {
return PRCreate(&sess_pr);
}
/*车牌识别推理接口
1、从缓存的检测数据中读取检测图片与检测信息
2、如果检测已结束并且队列为空就返回
3、把读取的检测的数据送入识别接口
4、分数大于0,并且过滤接口为true,把数据放入cache_port_plates
*/
int PRInference() {
xhlpr_run_data run_data;
wait_and_pop(run_data);
if (!flag) {
return LPR_OK;
}
uint8 logbuffer[256]={0};
static int lognum = 0;
struct timeval start, end;
double runtime_vd;
gettimeofday(&start, NULL);
if(run_data.vdCount == 0 || run_data.pdCount == 0){
cache_port_plates.push_back(run_data);
return LPR_OK;
}
int ret = PlateOCR(sess_pr, run_data.img.data, run_data.img.cols, run_data.img.rows, &(run_data.plate));
if (ret != LPR_OK) {
return ret;
}
printf("%f %f %s %d %d \n",run_data.plate.points_score,run_data.plate.number_score,run_data.plate.plateNumber,run_data.plate.color,run_data.plate.type);
gettimeofday(&end, NULL);
#if 0
runtime_vd = ((end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec)) / 1000 + 0.5;
if(lognum <= 100)
{
sprintf(logbuffer,"Plate recognize Run time is: %lf ms \n" ,runtime_vd);
savelog(logbuffer);
lognum++;
}
#endif
if (run_data.plate.number_score > 0) {
if (plateFilter(run_data.plate.plateNumber)) {
cache_port_plates.push_back(run_data);
}
}
return LPR_OK;
}
//入队操作
void push(xhlpr_run_data new_value)
{
std::lock_guard<std::mutex> lk(mut);
data_queue.push(new_value);
data_cond.notify_one();
}
/*pop data_queue的数据,如果data_queue为空就一直等待
over data_queue.empty() 返回
!over data_queue.empty() 等待
over !data_queue.empty() 返回
!over !data_queue.empty() 返回
*/
void wait_and_pop(xhlpr_run_data& value)
{
std::unique_lock<std::mutex> lk(mut);
data_cond.wait(lk, [this] {
return ((!(data_queue.empty() ^ over)) || over);
});
if (data_queue.empty()) {
flag = false;
return;
};
value = data_queue.front();
data_queue.pop();
}
//重载wait_and_pop,与上相同,本demo中未使用
std::shared_ptr<xhlpr_run_data> wait_and_pop()
{
std::unique_lock<std::mutex> lk(mut);
data_cond.wait(lk, [this] {
return ((!(data_queue.empty() ^ over)) || over);
});
if (data_queue.empty()) {
flag = false;
return std::shared_ptr<xhlpr_run_data>();
}
std::shared_ptr<xhlpr_run_data> res(std::make_shared<xhlpr_run_data>(data_queue.front()));
data_queue.pop();
return res;
}
//不管有没有队首元素,如果队列有元素就返回元素,并返回true,否则返回false,本demo中未使用
bool try_pop(xhlpr_run_data& value)
{
std::lock_guard<std::mutex> lk(mut);
if (data_queue.empty()) {
return false;
}
std::cout << data_queue.size() << std::endl;
value = data_queue.front();
data_queue.pop();
return true;
}
//重载try_pop,与上相同,本demo中未使用
std::shared_ptr<xhlpr_run_data> try_pop()
{
std::lock_guard<std::mutex> lk(mut);
if (data_queue.empty()) {
return std::shared_ptr<xhlpr_run_data>();
}
std::shared_ptr<xhlpr_run_data> res(std::make_shared<xhlpr_run_data>(data_queue.front()));
data_queue.pop();
return res;
}
//查看队列是否为空
bool empty() const
{
std::lock_guard<std::mutex> lk(mut);
return data_queue.empty();
}
//检测主线程是否结束
bool is_over() {
std::unique_lock<std::mutex> lk(mut);
return over;
}
//检测主线程结束,over设置为true
void set_over(bool state) {
std::unique_lock<std::mutex> lk(mut);
over = state;
data_cond.notify_one();
}
void try_send() {
//cout << "cache_port_plates size: " << cache_port_plates.size() << endl;
if(cache_port_plates.size() > 5){
#if 1
vector<vector<xhlpr_run_data>> portPlates;
vector<xhlpr_run_data> uploadVec;
int i;
string portId;
//1.将缓存的记录按照车位号进行二次分组,每个车位号对应多个车牌记录。
for(i=0;i<cache_port_plates.size();i++){
std::string str = std::string(cache_port_plates[i].portInfo.portId);
if(portId != str){
//新车位的记录
vector<xhlpr_run_data> ppvec;
ppvec.push_back(cache_port_plates[i]);
portPlates.push_back(ppvec);
}else{
if(portPlates.size() == 0){
vector<xhlpr_run_data> ppvec;
ppvec.push_back(cache_port_plates[i]);
portPlates.push_back(ppvec);
}else{
//同一个车位的记录
portPlates[portPlates.size()-1].push_back(cache_port_plates[i]);
}
}
portId = str;
}
//2.对分组后车位号(1)-车牌(N)进行遍历
for(i=0;i<portPlates.size();i++){
vector<xhlpr_run_data> ppvec = portPlates[i];
if(i == portPlates.size()-1){
//最后的数据是未匹配到,直接清空
if(ppvec[0].portInfo.portId[0] == '\0'){
cache_port_plates.erase(cache_port_plates.begin() + (cache_port_plates.size()- ppvec.size()),cache_port_plates.end());
ppvec.clear();
break;
}
}
//3.同车位号下车牌记录进行数据清理,1.将同一个车位重复的数据剔除 2.同一个车位无出牌/有出牌情况处理
if(ppvec.size() > 15 || (i != portPlates.size() -1)){
for(int j=0;j<ppvec.size();j++){
int upFlag = -1;
if(ppvec[j].portInfo.portId[0] == '\0'){
break;
}
std::string plateNumber = std::string(ppvec[j].plate.plateNumber);
std::string portId = std::string(ppvec[j].portInfo.portId);
float number_score = ppvec[j].plate.number_score;
if(j==0){
uploadVec.push_back(ppvec[j]);
continue;
}
for(int k=0;k<uploadVec.size();k++){
std::string plstr = std::string(uploadVec[k].plate.plateNumber);
std::string ptstr = std::string(uploadVec[k].portInfo.portId);
if(portId != ptstr){
continue;
}
if(plateNumber == plstr || (plateNumber.length()== 0 && plstr.length() != 0)){
upFlag = k;
break;
}
}
//printf("~~~~ vdCount : %d pdCount : %d portId:%s plateNumber:%s %.3f\n",ppvec[j].vdCount,ppvec[j].pdCount,portId.c_str(),plateNumber.c_str(),number_score);
if(plateNumber.length() != 0 ){
string prePlateNum = string(uploadVec[uploadVec.size() -1].plate.plateNumber);
if(prePlateNum.length() == 0){
uploadVec.erase(uploadVec.end());
}
}
if(upFlag < 0){
uploadVec.push_back(ppvec[j]);
}else{
//printf("---- %d %d %d %0.8f %0.8f \n", upFlag,ppvec[j].vdCount,uploadVec[upFlag].vdCount,number_score,uploadVec[upFlag].plate.number_score);
//同车位 取概率最大的记录上传
if(number_score > uploadVec[upFlag].plate.number_score){
//printf("######### %d %0.8f %0.8f \n",upFlag,number_score,uploadVec[upFlag].plate.number_score);
uploadVec.erase(uploadVec.begin()+upFlag);
uploadVec.push_back(ppvec[j]);
}else if(number_score == 0 && uploadVec[upFlag].plate.number_score == 0){
if(ppvec[j].vdCount > 0 ){
//未检测到车牌,根据摄像头角度 选择最早或者最晚的记录上传
if(cameraType == 1){
//最晚的记录上传
uploadVec.erase(uploadVec.begin()+upFlag);
uploadVec.push_back(ppvec[j]);
}
}else if(ppvec[j].vdCount == 0 && uploadVec[upFlag].vdCount == 0){
if(cameraType == 0 /*|| cameraType == 2*/){
uploadVec.erase(uploadVec.begin()+upFlag);
uploadVec.push_back(ppvec[j]);
}
}
}
}
}
cache_port_plates.erase(cache_port_plates.begin(),cache_port_plates.begin()+ppvec.size());
}
ppvec.clear();
}
portPlates.clear();
#endif
//int i;
//vector<xhlpr_run_data> uploadVec = cache_port_plates;
//cache_port_plates.clear();
//cout << "uploadVec size: " << uploadVec.size() << endl;
//4.数据上传
for(i = 0 ;i < uploadVec.size(); i++){
char mkdirCMD[100];
char carplate_gbk[50] = {0};
Mat image = uploadVec[i].img;
GPSData gpsData = uploadVec[i].gpsData;
CarportInfo portInfo = uploadVec[i].portInfo;
PlateInfo plate = uploadVec[i].plate;
std::string port = std::string(portInfo.portId);
std::string plateStr = std::string(plate.plateNumber);
int prerr = 0;
std::string prResult;
//printf("$$$$ %d %s_%s %s[%.3f] C:%d T:%d \n",i,uploadVec[i].portInfo.areaId,uploadVec[i].portInfo.portId,uploadVec[i].plate.plateNumber,plate.number_score,uploadVec[i].vdCount,uploadVec[i].pdCount);
if(image.empty()){
continue;
}
if(plateStr.length() >0 && plate.number_score < carnum_score){
//车牌置信度过低情况忽略
continue;
}
if(last_port == port && (last_plate == plateStr || (last_plate.length() > 0 && plateStr.length() == 0))){
//连续相同车位号,1.连续相同的车牌记录忽略 2.上一次传输车牌不为空,当前识别记录为空的忽略
continue;
}
last_port = port;
last_plate = plateStr;
cv::Mat layroi = image(cv::Rect(0, FRAME_HEIGHT - overlay.rows, FRAME_WIDTH, overlay.rows));
addWeighted(layroi, 0.5, overlay, 0.5, 0, layroi);
if (uploadVec[i].pdCount > 0){
/*
if(isRTSPEnable){
cv::line(image, cv::Point(plate.points[0].x,plate.points[0].y),cv::Point(plate.points[1].x,plate.points[1].y) , SCALAR_WHITE, 2,LINE_AA);
cv::line(image, cv::Point(plate.points[1].x,plate.points[1].y),cv::Point(plate.points[2].x,plate.points[2].y) , SCALAR_WHITE, 2,LINE_AA);
cv::line(image, cv::Point(plate.points[2].x,plate.points[2].y),cv::Point(plate.points[3].x,plate.points[3].y) , SCALAR_WHITE, 2,LINE_AA);
cv::line(image, cv::Point(plate.points[3].x,plate.points[3].y),cv::Point(plate.points[0].x,plate.points[0].y) , SCALAR_WHITE, 2,LINE_AA);
}
*/
}
//putText(image, portInfo.portId, cv::Point(10, 650),CV_FONT_HERSHEY_COMPLEX_SMALL, 1.2, CV_RGB(255, 255, 255), 2);
if(uploadVec[i].vdCount == 0){
prerr = 1;
prResult = "Empty";
}else if(uploadVec[i].vdCount != 0 && plateStr.length() == 0){
//有车,但是没有检测到车牌
if(uploadVec[i].pdCount == 0){
//未检测到车牌
/*
if(isRTSPEnable){
putText(image, "PD Failed", cv::Point(320,650),CV_FONT_HERSHEY_COMPLEX_SMALL, 1.2, CV_RGB(255, 255, 255), 2);
}
*/
prerr = 2;
prResult="ERROR_DETECT_PLATE";
}else{
/*
if(isRTSPEnable){
//未识别出车牌
putText(image, "PR Failed", cv::Point(320,650),CV_FONT_HERSHEY_COMPLEX_SMALL, 1.2, CV_RGB(255, 255, 255), 2);
}
*/
prerr = 3;
prResult="ERROR_RECOGNIZE_PLATE";
}
}else if(plateStr.length() > 0){
/*
if(isRTSPEnable){
wchar_t plateNumber[64];
UTF2Unicode(plateNumber,64,plate.plateNumber);
cvxText.putText(image, plateNumber, cv::Point(320,650));
char text[256];
sprintf(text,"[%.3f]",plate.number_score);
putText(image, text, cv::Point(435, 650),CV_FONT_HERSHEY_COMPLEX_SMALL, 1.2, CV_RGB(255, 255, 255), 2);
}
*/
}
// add watermark here
if (cameraType == 0) {
putText(image, "RF", cv::Point(1200, 650),CV_FONT_HERSHEY_COMPLEX_SMALL, 1.2, CV_RGB(255, 255, 255), 2);
} else if (cameraType == 1) {
putText(image, "RB", cv::Point(1200, 650),CV_FONT_HERSHEY_COMPLEX_SMALL, 1.2, CV_RGB(255, 255, 255), 2);
} else if (cameraType == 2) {
putText(image, "RC", cv::Point(1200, 650),CV_FONT_HERSHEY_COMPLEX_SMALL, 1.2, CV_RGB(255, 255, 255), 2);
}
// char text[256];
// sprintf(text,"[%.8f,%.8f] [%.3f km/h]",gpsData.latlon.lat,gpsData.latlon.lon,gpsData.speed);
// putText(image, text, cv::Point(10, 680),CV_FONT_HERSHEY_COMPLEX_SMALL, 1.2, CV_RGB(255, 255, 255), 2);
// memset(text, 0, sizeof(text));
// sprintf(text,"20%02d-%02d-%02d %02d:%02d:%02d.%02d%d" ,
// gpsData.gpsTime[0],gpsData.gpsTime[1],gpsData.gpsTime[2],gpsData.gpsTime[3],gpsData.gpsTime[4],gpsData.gpsTime[5],gpsData.gpsTime[6],gpsData.gpsTime[7]);
// putText(image, text, cv::Point(10, 710),CV_FONT_HERSHEY_COMPLEX_SMALL, 1.2, CV_RGB(255, 255, 255), 2);
sprintf(mkdirCMD, "mkdir -p /mnt/sdcard/carplateimg/20%02d-%02d-%02d",gpsData.gpsTime[0],gpsData.gpsTime[1],gpsData.gpsTime[2]);
system(mkdirCMD);
struct timeval tv;
gettimeofday(&tv, NULL);
char tmp[128];
sprintf(tmp,"/mnt/sdcard/carplateimg/20%02d-%02d-%02d/%02d-%02d-%02d_%s_%s_%lld.jpg" ,
gpsData.gpsTime[0],gpsData.gpsTime[1],gpsData.gpsTime[2],gpsData.gpsTime[3],gpsData.gpsTime[4],gpsData.gpsTime[5],
uploadVec[i].portInfo.portId,
prerr == 0 ? uploadVec[i].plate.plateNumber:prResult.c_str(),tv.tv_sec * 1000000LL + tv.tv_usec);
//vector<int> compression_params;
//compression_params.push_back(CV_IMWRITE_JPEG_QUALITY);
//compression_params.push_back(15);
imwrite(tmp, image);
printf("@@@@ %d %s_%s %s \n",i,uploadVec[i].portInfo.areaId,portInfo.portId,uploadVec[i].plate.plateNumber);
plateupload_run_data upload_data;
memset(&upload_data,0,sizeof(plateupload_run_data));
memset(carplate_gbk, 0, sizeof(carplate_gbk));
UtfToGbk(uploadVec[i].plate.plateNumber,strlen(uploadVec[i].plate.plateNumber),carplate_gbk);
memcpy(upload_data.plate_number,carplate_gbk,strlen(carplate_gbk));
upload_data.plate_number_score = (uploadVec[i].plate.number_score*100);
upload_data.plate_type = uploadVec[i].plate.type;
upload_data.plate_color = uploadVec[i].plate.color;
memcpy(upload_data.port_number,portInfo.portId,strlen(portInfo.portId));
upload_data.port_number_len=strlen(portInfo.portId);
memcpy(upload_data.area_id,portInfo.areaId,strlen(portInfo.areaId));
ParseGpsDataToJTTGPS(&upload_data.sGpsInfo,&gpsData);
memcpy(upload_data.path,tmp,strlen(tmp));
if(prerr == 0){
upload_data.plate_number_len = strlen(carplate_gbk);
}else if(prerr == 1){
upload_data.plate_number_len = 0xFF;
}else{
upload_data.plate_number_len = 0x0;
}
plateupload_queue.push(upload_data);
}
uploadVec.clear();
}
}
//返回缓存检测车牌数量,本demo中未使用
int size() {
std::unique_lock<std::mutex> lk(mut);
return data_queue.size();
}
//析构函数,释放内存
~threadsafe_queue() {
//调用车牌识别会话销毁接口
int ret = PRDestroy(&sess_pr);
if (ret != LPR_OK) {
std::cout << "PRDestroy ERROR:" << ret << std::endl;
}
}
};
//车牌识别线程队列
threadsafe_queue lpr_queue;
static void *GetVencBuffer(void *arg) {
printf("#Start %s thread, arg:%p\n", __func__, arg);
// init rtsp
rtsp_demo_handle rtsplive = NULL;
rtsp_session_handle session;
//if(isRTSPEnable){
if(1){
rtsplive = create_rtsp_demo(554);
session = rtsp_new_session(rtsplive, "/live/main_stream");
rtsp_set_video(session, RTSP_CODEC_ID_VIDEO_H264, NULL, 0);
rtsp_sync_video_ts(session, rtsp_get_reltime(), rtsp_get_ntptime());
}
MEDIA_BUFFER mb = NULL;
while (!quit) {
mb = RK_MPI_SYS_GetMediaBuffer(g_stVencChn.enModId, g_stVencChn.s32ChnId,
-1);
if (mb) {
if(isRTSPEnable){
rtsp_tx_video(session, (uint8_t *)RK_MPI_MB_GetPtr(mb), RK_MPI_MB_GetSize(mb),
RK_MPI_MB_GetTimestamp(mb));
}
if (g_output_file) {
fwrite(RK_MPI_MB_GetPtr(mb), 1, RK_MPI_MB_GetSize(mb), g_output_file);
}
RK_MPI_MB_ReleaseBuffer(mb);
}
if(isRTSPEnable){
rtsp_do_event(rtsplive);
}
}
// release rtsp
if(isRTSPEnable){
rtsp_del_demo(rtsplive);
}
return NULL;
}
//车牌识别线程函数