admin
2025-04-25 8a39c933fc3235d868545d28376af251da85c686
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
#include "PreProcessFn.h"
#include "PreProcessModel.h"
 
#pragma comment(lib,"libmysql")  
#include <boost/log/trivial.hpp>
#include <boost/log/utility/setup/file.hpp>
#include <boost/log/expressions.hpp>
#include <boost/filesystem.hpp>
#include <mysql/mysql.h> 
#include <future>
#include "iostream"
 
#include <string>
#include <locale>
#include <codecvt>
#include "cls/CRabbitmqClient.h"
#include <bitset>
#include <opencv2/imgcodecs/legacy/constants_c.h>
#include "base/base64.h"
#include "cls/warningfile.h"
#include <filesystem> // C++17 中的文件系统库
#include <boost/uuid/uuid.hpp>
#include <boost/uuid/uuid_generators.hpp>
 
#include <curl/curl.h>
 
namespace fs = boost::filesystem;
std::mutex mtx;// 全局互斥锁,用于保护队列
PreProcessModel ppm = PreProcessModel();
 
PreProcessFn::PreProcessFn()
{    
}
 
PreProcessFn::~PreProcessFn(void)
{
}
 
// 创建录像异步任务的 future 对象
std::future<void> video_writer_future;
 
int* stringToIntArray(std::string input) {
    for (int i = 0; i < input.size(); i++) {
        if (input[i] == ';') {
            input[i] = ',';
        }
    }    
 
    std::vector<int> result;
    std::stringstream ss(input);
    std::string token;
 
    // 从输入字符串中解析整数,并存储到结果向量中
    while (getline(ss, token, ',')) {
        result.push_back(std::stoi(token));
    }
 
    // 动态分配内存以存储整数数组
    int* beltregion = new int[result.size()];
 
    // 将结果向量中的整数复制到动态分配的数组中
    for (int i = 0; i < result.size(); i++) 
    {
        beltregion[i] = result[i];
    }    
 
    return beltregion;
}
 
std::vector<cv::Point> stringToPoints(const std::string& input) {
    std::vector<cv::Point> points;
    std::stringstream ss(input);
 
    int x, y;
    char discard;
    while (ss >> x >> discard >> y >> discard) {
        points.push_back(cv::Point(x, y));
        // Check for the ';' delimiter
        if (ss.peek() == ';') {
            ss.ignore();
        }
    }
 
    return points;
}
 
// 获取当前时间并格式化为指定格式的字符串
std::string getCurrentDateTime(const std::string& format) {
    // 获取当前时间
    time_t now = time(0);
    // 转换为本地时间
    tm* local_time = localtime(&now);
    // 定义存储时间字符串的缓冲区
    char buffer[80];
    // 格式化时间为字符串
    strftime(buffer, sizeof(buffer), format.c_str(), local_time);
    // 返回格式化后的时间字符串
    return std::string(buffer);
}
 
// 获取当前时间,包括毫秒
std::string getCurrentDateTimeWithMilliseconds(const std::string& format) {
    // 获取当前时间点
    auto now = std::chrono::system_clock::now();
    // 将时间点转换为时间戳
    auto now_ms = std::chrono::time_point_cast<std::chrono::milliseconds>(now);
    auto ms_part = now_ms.time_since_epoch().count() % 1000;
 
    // 转换为本地时间
    std::time_t now_time = std::chrono::system_clock::to_time_t(now);
    std::tm* local_time = std::localtime(&now_time);
 
    // 定义存储时间字符串的缓冲区
    char buffer[80];
    // 格式化时间为字符串,包括毫秒
    std::strftime(buffer, sizeof(buffer), format.c_str(), local_time);
 
    // 将毫秒部分添加到字符串中
    std::string result(buffer);
    result += std::to_string(ms_part);
 
    return result;
}
 
//imgType 包括png bmp jpg jpeg等opencv能够进行编码解码的文件
std::string Mat2Base64(const cv::Mat& image, std::string imgType) {
    //Mat转base64
    std::vector<uchar> buf;
    cv::imencode(imgType, image, buf);
    std::string img_data = base64_encode(buf.data(), buf.size(), false);
    return img_data;
}
 
//imgType 包括png bmp jpg jpeg等opencv能够进行编码解码的文件
std::string Mat2Bin(const cv::Mat& image, std::string imgType) {
    //Mat转base64
    std::vector<uchar> buf;
    cv::imencode(imgType, image, buf);
    std::string img_data = base64_encode(buf.data(), buf.size(), false);
    return img_data;
}
 
string fnWarningFileToJson(warningfile wf)
{
    // 创建 Json::Value 对象
    Json::Value root;
    root["coalCode"] = wf.getCoalCode();
    root["cameraCode"] = wf.getCameraCode();
    root["cameraName"] = wf.getCameraName();
    root["analyse"] = wf.getAnalyse();
    root["analyseResult"] = wf.getAnalyseResult();
    root["warningTime"] = wf.getWarningTime();
    root["recordFileName"] = wf.getRecordFileName();
    root["recordImgFileName"] = wf.getrecordImgFileName();
    root["rtspUrl"] = wf.getRtspUrl();
    root["duration"] = wf.getDuration();
 
    // 使用 Json::FastWriter 将 Json::Value 对象转换为 JSON 字符串
    Json::FastWriter writer;
    return writer.write(root);
}
 
//创建文件夹 年 月  日
string fnCreateFileFolder(string hdPath)
{
    // 获取当前系统时钟的当前时间点
    auto now = std::chrono::system_clock::now();
    // 将当前时间点转换为 time_t 类型
    std::time_t time_now = std::chrono::system_clock::to_time_t(now);
    // 将 time_t 类型的时间转换为本地时间
    std::tm local_time = *std::localtime(&time_now);
 
    std::string year_folder = std::to_string(local_time.tm_year + 1900);
    std::string month_folder = std::to_string(local_time.tm_mon + 1);
    std::string day_folder = std::to_string(local_time.tm_mday);
 
    std::string folder_path = hdPath + "/" + year_folder + "/" + month_folder + "/" + day_folder + "/";
 
    // // 检查文件夹是否已存在
    if (!fs::exists(folder_path.c_str())) {
        // 不存在则创建文件夹
        fs::create_directories(folder_path.c_str());
        //std::cout << "已创建文件夹:" << folder_path << std::endl;
    }
    return folder_path;//返回文件夹路径
 
}
 
/// <summary>
/// 判断结果比例
/// </summary>
/// <returns></returns>
int fnJudeRatio(vector<int> vec, int ftype,double ratio)
{
    // 计算队列中 0 和 1 的数量
    double count0 = 0;//0的个数
    double count1 = 0;//1的个数
 
    double vecSize = vec.size();
    for (int i = 0; i < vec.size(); ++i) {
        if (vec[i] == 0) {
            count0++;
        }
        else {
            count1++;
        }
    }
 
    //cout << getCurrentDateTime1("%Y%m%d%H%M%S") << endl;
    /*cout << "------------" << endl;
    cout << count0 << endl;
    cout << count1 << endl;*/
 
    // 计算比例
    double ratio0 = count0 / vecSize;
    double ratio1 = count1 / vecSize;
 
    switch (ftype)
    {
    case 0:
        if (ratio0 >= ratio)
        {
            return 0;
        }
        else
        {
            return 1;
        }
        break;
    case 1:
        if (ratio1 >= ratio)
        {
            return 1;
        }
        else
        {
            return 0;
        }
        break;
 
    default:
        return -1;
        break;
    }
}
 
/// <summary>
/// 录像并保存报警信息至MySql模块
/// </summary>
void fnVideoRecordToMysql(Mat frame, int Alarm, VideoWriter& video_writer, std::chrono::system_clock::time_point start_time, warningfile wf, MYSQL* mysql, int _recordtime)
{
    // 当前时间                    
    double seconds = std::chrono::duration<double>(std::chrono::system_clock::now() - start_time).count();
    //cout << start_time << endl;
    //cout << current_time << endl;
    // 如果录制时间超过 10 秒,则停止录制
    if (seconds >= _recordtime) {
        cout << "已录制 10 秒视频,停止录制" << endl;
        video_writer.release();
    }
    else
    {
        video_writer.write(frame);//录像处理
    }
 
    if (Alarm == 1)//报警信息保存至mysql
    {
        // 记录报警的时候,同时写入redis数据库
        wf.setWarningTime(getCurrentDateTime("%Y-%m-%d %H:%M:%S"));
 
        // 构建插入 SQL 语句
        std::stringstream ss;
        ss << "INSERT INTO alarmevent (AlarmType, AlarmName, CameraCode, AlarmState, AlarmPic, AlarmVideo, AlarmTime) VALUES ('"
            << wf.getAnalyse() << "', '"
            << wf.getAnalyseResult() << "', "
            << wf.getCameraCode() << ", '"
            << "0" << "', '"
            << wf.getrecordImgFileName() << "', '"
            << wf.getRecordFileName() << "', '"
            << wf.getWarningTime() << "')";
 
        std::string query = ss.str();
        // 执行插入语句
        if (mysql_query(mysql, query.c_str()))
        {
            string err_string = mysql_error(mysql);
            cout << err_string << endl;
        }        
    }
}
 
/// <summary>
/// 录像并保存报警信息至MySql模块,录像为以前时间段
/// </summary>
void fnVideoRecordToMysql2(vector<Mat> vecVideo, int Alarm, VideoWriter& video_writer, warningfile wf, MYSQL* mysql)
{
    for (int i = 0; i < vecVideo.size(); i++) {
        video_writer.write(vecVideo[i]);//录像处理
    }
 
    video_writer.release();
 
    if (Alarm == 1)//报警信息保存至mysql
    {
        // 记录报警的时候,同时写入redis数据库
        wf.setWarningTime(getCurrentDateTime("%Y-%m-%d %H:%M:%S"));
 
        // 构建插入 SQL 语句
        std::stringstream ss;
        ss << "INSERT INTO alarmevent (AlarmType, AlarmName, CameraCode, AlarmState, AlarmPic, AlarmVideo, AlarmTime) VALUES ('"
            << wf.getAnalyse() << "', '"
            << wf.getAnalyseResult() << "', '"
            << wf.getCameraCode() << "', '"
            << "0" << "', '"
            << wf.getrecordImgFileName() << "', '"
            << wf.getRecordFileName() << "', '"
            << wf.getWarningTime() << "')";
 
        std::string query = ss.str();
        // 执行插入语句
        if (mysql_query(mysql, query.c_str()))
        {
            string err_string = mysql_error(mysql);
            cout << err_string << endl;
        }
    }
}
 
// 回调函数,用于将返回的数据存储在 std::string 中
size_t WriteCallback(void* contents, size_t size, size_t nmemb, void* userp) {
    ((std::string*)userp)->append((char*)contents, size * nmemb);
    return size * nmemb;
}
 
/// @brief 视频流拉取处理
/// @param _rtspUrl 视频流源地址
/// @param queJC 未处理的视频流帧队列
void PreProcessFn::fnGetVideoMatToQueue(string rtspUrl,queue<Mat>& queJC)
{
    VideoCapture rtspStream(rtspUrl.c_str());// 连接RTSP流,软解码
    //Ptr<cv::cudacodec::VideoReader> d_reader = cv::cudacodec::createVideoReader(rtspUrl);
 
    int count = 0;
    long currentFrame = 1;
    long skip_frame_num = skipN;//跳帧间隔设置(每1帧)
 
    while (!asyncStop)
    {
        //std::cout << "当前线程待处理队列数据数量0000:" << queJC.size() << std::endl;
        //cout << getCurrentDateTime("%Y%m%d%H%M%S") << endl;
        //cout << skip_frame_num << endl;
 
        try
        {
            if (queJC.size() > 500)
            {
                cout << "获取视频时,存在内存溢出风险!" << endl;
            }
 
                
            if (currentFrame % skip_frame_num == 0) //跳帧处理
            {    
                Mat frame;
 
                //cv::cuda::GpuMat d_frame;
 
                //////软解码
                if (!rtspStream.isOpened()) {
                    rtspStream.open(rtspUrl.c_str());
                    if (!rtspStream.isOpened()) {
 
                        std::this_thread::sleep_for(std::chrono::seconds(5));// 等待一段时间后重试连接
                        //log.Log(rtspUrl + ":重启连接失败,请等待" + "\n");
                        continue;
                    }
                    cout << rtspUrl + ":重启连接完成," + to_string(count) + "\n" << endl;
                    //log.Log(rtspUrl + ":重启连接完成," + to_string(count) + "\n");
                    count = 0;//重连计数复位
                }            
                
                //读取视频帧
                if (!rtspStream.read(frame))
                {
                    if (count > 3)
                    {
                        cout << rtspUrl + ":连接释放,重新连接!" + "\n";
                        //log.Log(rtspUrl + ":连接释放,重新连接!" + "\n");
                        rtspStream.release();
                        //rtspStream.open(rtspUrl.c_str());
                        count = 0;//重连计数复位
                    }
                    else
                    {
                        std::this_thread::sleep_for(std::chrono::seconds(1));// 延迟1秒再去取帧
                        cout << rtspUrl + ":" + to_string(count) << endl;
                        count++;
                    }
                    continue;
                }                    
 
                ////
 
                //硬解码            
                //读取视频帧
                // if (!d_reader->nextFrame(d_frame))
                // {        
                //     d_reader = cv::cudacodec::createVideoReader(rtspUrl);
                //     continue;
                // }
 
                // d_frame.download(frame);
                // //cout << "gpu" << endl;
                // d_frame.release();
 
                ///
 
                if (frame.empty())
                {
                    std::cout << "空帧,废弃" << std::endl;
                    continue;
                }
 
                //加锁,防止别的系统操作这段内存
                std::unique_lock<std::mutex> lck(mtx);
                queJC.push(frame);  // 推入待处理队列            
                lck.unlock();
                
 
                //cv::imwrite("./img/" + std::to_string(currentFrame)+".jpg", frame);                
                //cv::imshow(rtspUrl, queJC.front());//展示帧图片
                //cv::imshow(rtspUrl, frame);//展示帧图片
                //waitKey(5);
                
                //std::this_thread::sleep_for(std::chrono::milliseconds(100));// 延迟等待一段时间
 
                frame.release();
            }
 
            if (currentFrame > 10000) {
                currentFrame = 0;
            }
 
            currentFrame++;
            
        }
        catch (const std::exception& ex)
        {
            std::string errorMessage = "拉流失败-";
            errorMessage += ex.what();
            continue;
        }            
    }
    
    //d_reader.release();
    rtspStream.release();    
}
 
std::string jsontostr(Json::Value& json)
{
    string return_str;
    if(!json.isNull()&&!json.empty())
    {
        for (int i = 0; i < json.size(); i++)
        {
            return_str += json[i].asString()+",";
        }
        if (!return_str.empty() && return_str.back() == ',') {
               return_str.pop_back();
        }
    
    }
    else
    {
        return_str = "";
    }
    return return_str;
}
 
/// @brief 视频帧处理函数
/// @param ipcsNode 摄像机节点
/// @param modelArray 模型列表
/// @param queJC 未处理的视频流帧队列
/// @param queJC2 处理完的视频流帧队列
void PreProcessFn::fnImRecognitionPro(const Json::Value& ipcsNode, Json::Value modelArray, queue<Mat>& queJC, queue<Mat>& queJC2)
{
    std::string ipccode = ipcsNode["code"].asString();//摄像机编码
    std::string ipcname = ipcsNode["name"].asString();//摄像机名称
    std::string ipcrtsppath = ipcsNode["video"].asString();//视频rtsp源    
    // std::string skipN = ipcsNode["skipN"].asString();//跳帧
    // int skipN = ipcsNode["skipN"].asString();//跳帧
    //mysql连接初始化
    MYSQL* mysql = mysql_init(nullptr);
    if (!mysql_real_connect(mysql, mysqlpath.c_str(), mysqluser.c_str(), mysqlpass.c_str(), mysqldatabase.c_str(), stoi(mysqlport), NULL, 0))
    {
        std::cout << "连接 MySQL 数据库失败: " << mysql_error(mysql) << std::endl;
        //mysql_close(mysql);  // 关闭连接
        //std::exit(0);//数据库连接错误,退出程序
    }
    else
    {
        std::cout << "连接 MySQL 数据库成功。" << std::endl;
        mysql_set_character_set(mysql, "utf8mb4");
        //mysql_query(mysql, "SET NAMES GB2312");//解决中文乱码问题       
    }    
 
    std::string limitDM = "1";//堆煤限定阈值,超过阈值进行报警,配置文件赋值,每个摄像头都不一样
 
    //新增模型处理 ------第1步
    //检测区域设定
    std::vector<Point>  areaBelt;//皮带运行检测区域
    std::vector<Point>  areaTSJ;//提升机检测区域
    std::vector<Point>  areaPerson;//人员检测区域
    std::vector<Point>  workareaPerson;//工作区域人员检测区域
    std::vector<Point>  leftareaBeltJC;//左侧皮带检测区域
    std::vector<Point>  rightareaBeltJC;//右侧皮带检测区域
    std::vector<Point>  areaBeltJC;//皮带检测区域
    std::vector<Point>  areaHC;//猴车
    std::vector<Point>  areaSleep;//睡岗
    std::vector<Point>  areaDM;//堆煤检测区域    
    std::vector<Point>  areaWear;//穿戴检测区域
    std::vector<Point>  areaFire;//烟火检测区域
    std::vector<Point> detectarea;//检测区域
    std::vector<Point> flowarea;//皮带外围区域
    std::vector<Point> flowrelate;//皮带相对区域
    std::vector<Point> leftarea;//左侧区域
    std::vector<Point> rightarea;//右侧区域
    std::vector<Point> beltarea;//中间区域
    std::vector<Point> workarea;//工作区域
 
 
    //模型处理 ------第2步
    RUN rBelt;//皮带运行模型
    rBelt.analysis1 = "0001";//运行
    rBelt.analysis2 = "0002";//停止
 
    RUN rTSJ;//提升机检测模型
    rTSJ.analysis1 = "0001";//运行
    rTSJ.analysis2 = "0002";//停止
 
    HAT hatJC;//目标检测模型
    Cover cover;//摄像头遮挡算法
    Camera camera;//摄像头移动算法
    DMJC dm;//堆煤检测模型    
    BELT beltJC;//皮带跑偏,大块异物
    HAT hatHC;//乘猴车携带大件模型
    HAT hatSleep;//睡岗
    HAT hatWear ;//穿戴模型1
    clasification cfWear;//穿戴模型2
    HAT hatFire;//烟火模型
    
    //各模型录像保存路径 ------第3步
    string videoPathBelt = "";//皮带运行挡录像保存路径
    string videoPathTSJ = "";// 提升机录像保存路径
    string videoPathPerson = "";// 人员闯入录像保存路径    
    string videoPathBeltJC = "";// 皮带检测录像保存路径    
    string videoPathCameraCover = "";// 摄像头遮挡录像保存路径
    string videoPathCameraMove = "";// 摄像头移动录像保存路径
    string videoPathDM = "";// 堆煤录像保存路径    
    string videoPathHC = "";// 猴车录像保存路径    
    string videoPathSleep = "";// 睡岗录像保存路径    
    string videoPathWear = "";// 穿戴录像保存路径
    string videoPathFire = "";// 烟火录像保存路径
 
    //最后一次录像时间 ------第4步
    auto videoTimeBelt = std::chrono::system_clock::now();// 上一次皮带运行状态录制时间
    auto videoTimeTSJ = std::chrono::system_clock::now();// 上一次提升机录制时间
    auto videoTimePerson = std::chrono::system_clock::now();// 上一次人员闯入录制时间    
    auto videoTimeBeltJC = std::chrono::system_clock::now();// 上一次皮带检测录制时间    
    //auto videoTimeCameraCover = std::chrono::system_clock::now();// 上一次摄像头遮挡录制时间
    //auto videoTimeCameraMove = std::chrono::system_clock::now();// 上一次摄像头移动录制时间    
    auto videoTimeDM = std::chrono::system_clock::now();// 上一次堆煤录制时间    
    auto videoTimeHC = std::chrono::system_clock::now();// 上一次猴车录制时间
    auto videoTimeSleep = std::chrono::system_clock::now();// 上一次睡岗录制时间
    auto videoTimeWear = std::chrono::system_clock::now();// 上一次穿戴录制时间
    auto videoTimeFire = std::chrono::system_clock::now();// 上一次烟火录制时间
 
    //当前录像开始时间 ------第5步
    auto start_timeBelt = std::chrono::system_clock::now();// 皮带运行状态开始录制时间
    auto start_timeTSJ = std::chrono::system_clock::now();// 提升机开始录制时间
    auto start_timePerson = std::chrono::system_clock::now();// 人员闯入开始录制时间    
    auto start_timeBeltJC= std::chrono::system_clock::now();// 皮带检测开始录制时间    
    auto start_timeCameraCover = std::chrono::system_clock::now();// 摄像头遮挡开始录制时间    
    auto start_timeCameraMove = std::chrono::system_clock::now();// 摄像头移动开始录制时间
    auto start_timeDM = std::chrono::system_clock::now();// 堆煤开始录制时间    
    auto start_timeHC = std::chrono::system_clock::now();// 猴车开始录制时间    
    auto start_timeSleep = std::chrono::system_clock::now();// 睡岗开始录制时间
    auto start_timeWear = std::chrono::system_clock::now();// 穿戴开始录制时间
    auto start_timeFire = std::chrono::system_clock::now();// 烟火开始录制时间
 
    //调用模型和算法的返回结果    ------第6步
    int beltStatus = 0;// 皮带最新状态
    int beltStatusold = 0;// 皮带旧状态
    int tsjStatus = -1;// 提升机最新状态 0运动 1停止  -1未知
    int tsjStatusold = -1;// 提升机旧状态 0运动 1停止  -1未知
    vector<double> personResult;//breakin:>0 人员闯入 <=0 无人员闯入;wearHat 0:全部佩戴安全帽 1:有人未佩戴安全帽    
    vector<double> beltJCResult;
    double cameracover = 0;// 摄像头遮挡标志
    double cameramove = 0;// 摄像头移动标志    
    float proportion = 0;// 煤炭占比
    vector<double> hcResult;    //猴车返回结果
    vector<double> sleepResult;    //睡岗返回结果
    vector<double> resultWearold;//穿戴返回结果,上一次的结果
    vector<double> resultWear;//穿戴返回结果
    vector<double> resultFire;//烟火返回结果
 
    //异步任务结果 ------第7步
    std::future<double> futureBelt;// 皮带运行状态检测异步任务结果
    std::future<double> futureTSJ;// 提升机状态检测异步任务结果
    std::future<vector<double>> futurePerson;// 目标检测异步任务结果    
    std::future<vector<double>> futureBeltJC;// 皮带检测异步任务结果
    std::future<double> futureCameraCover;// 摄像头遮挡检测异步任务结果
    std::future<double> futureCameraMove;// 摄像头移动检测异步任务结果    
    std::future<double> futureDM;// 堆煤检测异步任务结果    
    std::future<vector<double>> futureHC;// 乘猴车携带大件异步任务结果
    std::future<vector<double>> futureSleep;// 睡岗异步任务结果    
    std::future<vector<double>> futureWear;// 穿戴检测异步任务结果
    std::future<vector<double>> futureFire;// 烟火检测异步任务结果
 
    //VideoWriter对象 ------第8步
    VideoWriter video_writerBelt;// 皮带运行状态录像用
    VideoWriter video_writerTSJ;// 提升机录像用    
    VideoWriter video_writerPerson;// 人员闯入录像用    
    VideoWriter video_writerBeltJC;// 皮带检测录像用
    VideoWriter video_writerCameraCover;// 摄像头遮挡录像用
    VideoWriter video_writerCameraMove;// 摄像头移动录像用
    VideoWriter video_writerDM;// 堆煤录像用
    VideoWriter video_writerHC;// 猴车录像用
    VideoWriter video_writerSleep;// 睡岗录像用
    VideoWriter video_writerWear;// 穿戴录像用
    VideoWriter video_writerFire;// 烟火录像用
    
    //创建写入redis的实例 ------第9步
    warningfile wfBelt = warningfile();
    warningfile wfTSJ = warningfile();
    warningfile wfPerson = warningfile();    
    warningfile wfBeltJC = warningfile();
    warningfile wfCameraCover = warningfile();
    warningfile wfCameraMove = warningfile();    
    warningfile wfDM = warningfile();
    warningfile wfHC = warningfile();    
    warningfile wfSleep = warningfile();
    warningfile wfWear = warningfile();
    warningfile wfFire = warningfile();
 
    //第一次报警时间 ------第10步    
    auto firstSleep = std::chrono::system_clock::now();// 睡岗第一次报警时间
    auto firstFire = std::chrono::system_clock::now();// 烟火第一次报警时间
 
 
    //记录判断结果
    
    vector<Mat> vecVideoSleep;//保存睡岗视频帧
    vector<int> vecFire;//烟火判断结果
    vector<Mat> vecVideoFire;//保存烟火视频帧
 
    //提升机和皮带判断时,需要累计三张图片才能进行判断
    std::vector<cv::Mat> imagesTsj;
    std::vector<cv::Mat> imagesBelt;
    std::string color_result;
    std::string lable_title;
    
    cout <<modelArray.size()<<endl;
    //循环每个摄像头的模型,加载模型
    for (int j = 0; j < modelArray.size(); ++j) {
        Json::Value modelNode = modelArray[j];
        std::string modelCode = modelNode["code"].asString();//模型编码 区分加载不同的模型
        std::string modelPath = modelNode["path"].asString();
        std::string modelPath2 = modelNode["path2"].asString();//模型2路径
        std::string imagePath = modelNode["img_path"].asString();
 
        std::string modelName = modelNode["name"].asString();
        Json::Value modelTypeArr = modelNode["type"];////模型分析类型编码  
        std::string modelType = jsontostr(modelTypeArr);
 
        double threshold = stod(modelNode["threshold"].asString());//阈值
        float modelLimit = stof(modelNode["limit"].asString());//限定值
        std::string modelAnalysis ="";
        color_result = modelNode["color_result"].asString();//颜色反正值
 
        // std::string area = modelNode["threshold"].asString();//区域一
        // std::string area2 = modelNode.get("area2", "").asString();//区域二
        Json::Value rects = modelNode["point_rects"];////模型分析类型编码  
        for (auto k = 0; k < rects.size(); ++k) 
            {
                Json::Value pointNode = rects[k];
                lable_title=pointNode["title"].asString();
                std::string point_type=pointNode["type"].asString();
                Json::Value point_Arr= pointNode["points"];
                for (auto x = 0; x < point_Arr.size(); ++x) 
                {
                    int croodx = stoi(point_Arr[x]["x"].asString());
                    int croody = stoi(point_Arr[x]["y"].asString());
                    if(point_type=="detectarea")
                    {
                        detectarea.push_back(cv::Point(croodx, croody));
                    }
                    if (point_type=="flowarea")
                    {
                        flowarea.push_back(cv::Point(croodx, croody));
                    }
                    if (point_type=="flowrelate")
                    {
                        flowrelate.push_back(cv::Point(croodx, croody));
                    }
                    if (point_type=="leftroller")
                    {
                        leftarea.push_back(cv::Point(croodx, croody));
                    }
                    if (point_type=="rightroller")
                    {
                        rightarea.push_back(cv::Point(croodx, croody));
                    }
                    if (point_type=="beltarea")
                    {
                        beltarea.push_back(cv::Point(croodx, croody));
                    }
                    if (point_type=="workarea")
                    {
                        workarea.push_back(cv::Point(croodx, croody));
                    }
                        
                }    
            }
 
        int* arr = new int[detectarea.size() * 2];
        //加载模型处理------
        if(modelCode=="1") //皮带状态
        {
                if (!rBelt.initConfig(modelPath.c_str(),arr))
                {
                    printf("皮带运行模型初始化失败。\n");
                }
                else
                {
                    printf("皮带运行模型初始化成功。\n");
                    rBelt.isLoad = true;
                }
                areaBelt =detectarea;
                videoPathBelt = coalCode + ipccode + modelType + modelAnalysis;
                wfBelt.setCoalCode(coalCode);
                wfBelt.setCameraCode(ipccode);
                wfBelt.setCameraName(ipcname);
                wfBelt.setRtspUrl(ipcrtsppath);
                wfBelt.setAnalyse(modelType);
                //wfBelt.setAnalyseResult(modelAnalysis);
        }
        if(modelCode=="2") //提升机运行检测模型
        {
            if (!rTSJ.initConfig(modelPath.c_str(), arr))
                {
                    printf("提升机模型初始化失败。\n");
                }
                else
                {
                    printf("提升机模型初始化成功。\n");
                    rTSJ.isLoad = true;
                }
                areaTSJ = detectarea;
                videoPathTSJ = coalCode + ipccode + modelType;
                wfTSJ.setCoalCode(coalCode);
                wfTSJ.setCameraCode(ipccode);
                wfTSJ.setCameraName(ipcname);
                wfTSJ.setRtspUrl(ipcrtsppath);
                wfTSJ.setAnalyse(modelType);
                wfTSJ.setAnalyseResult(modelAnalysis);
                break;
        }
        if(modelCode=="3") //人员区域闯入模型
        {
            if (!hatJC.initConfig(modelPath.c_str(), 0.5, 0.5))
                {
                    printf("目标检测模型初始化失败\n");
                }
                else
                {
                    printf("目标检测模型初始化成功。\n");
                    hatJC.isLoad = true;
                }
                areaPerson = detectarea;//危险区域
                workareaPerson = workarea;//工作区域
                videoPathPerson = coalCode + ipccode + modelType + modelAnalysis;
                wfPerson.setCoalCode(coalCode);
                wfPerson.setCameraCode(ipccode);
                wfPerson.setCameraName(ipcname);
                wfPerson.setRtspUrl(ipcrtsppath);
                wfPerson.setAnalyse(modelType);
                wfPerson.setAnalyseResult(modelAnalysis);
        }
        if(modelCode=="4") //摄像头遮挡算法
        {
            if (!cover.initConfig()) {
                    printf("摄像头遮挡算法初始化失败\n");
                }
                else
                {
                    std::cout << "摄像头遮挡算法初始化成功" << std::endl;
                    cover.isLoad = true;
                }
                videoPathCameraCover = coalCode + ipccode + modelType + modelAnalysis;
                wfCameraCover.setCoalCode(coalCode);
                wfCameraCover.setCameraCode(ipccode);
                wfCameraCover.setCameraName(ipcname);
                wfCameraCover.setRtspUrl(ipcrtsppath);
                wfCameraCover.setAnalyse(modelType);
                wfCameraCover.setAnalyseResult(modelAnalysis);
        }
        if(modelCode=="5") //摄像头移动算法
        {
            if (!camera.initConfig()) {
                    printf("摄像头移动算法初始化失败\n");
                }
                else
                {
                    std::cout << "摄像头移动算法初始化成功" << std::endl;
                    camera.isLoad = true;
                    camera.stdMat = cv::imread(imagePath);
                }
                videoPathCameraMove = coalCode + ipccode + modelType + modelAnalysis;
                wfCameraMove.setCoalCode(coalCode);
                wfCameraMove.setCameraCode(ipccode);
                wfCameraMove.setCameraName(ipcname);
                wfCameraMove.setRtspUrl(ipcrtsppath);
                wfCameraMove.setAnalyse(modelType);
                wfCameraMove.setAnalyseResult(modelAnalysis);
        }
        if(modelCode=="6") //堆煤检测模型
        {
            if (!dm.initConfig(modelPath.c_str(), arr)) {
                    printf("堆煤模型初始化失败\n");
                }
                else
                {
                    std::cout << "堆煤模型初始化成功" << std::endl;
                    dm.isLoad = true;
                }
                videoPathDM = coalCode + ipccode + modelType + modelAnalysis;
                areaDM = detectarea;
                limitDM = modelLimit;//堆煤限定煤量占比
 
                wfDM.setCoalCode(coalCode);
                wfDM.setCameraCode(ipccode);
                wfDM.setCameraName(ipcname);
                wfDM.setRtspUrl(ipcrtsppath);
                wfDM.setAnalyse(modelType);
                wfDM.setAnalyseResult(modelAnalysis);
        }
        if(modelCode=="7") //皮带跑偏和异物检测
        {
            int* leftarr = new int[leftarea.size() * 2];
            int* rightarr = new int[rightarea.size() * 2];
            int* midd_arr = new int[beltarea.size() * 2];
 
            if (!beltJC.initConfig(modelPath.c_str(), leftarr, rightarr, midd_arr, modelLimit))
            {
                printf("皮带跑偏和异物检测模型初始化失败\n");
            }
            else
            {
                printf("皮带跑偏和异物检测模型初始化成功。\n");
                beltJC.isLoad = true;
            }
            leftareaBeltJC = leftarea;
            rightareaBeltJC = rightarea;
            areaBeltJC = beltarea;
            videoPathBeltJC = coalCode + ipccode + modelType + modelAnalysis;
            wfBeltJC.setCoalCode(coalCode);
            wfBeltJC.setCameraCode(ipccode);
            wfBeltJC.setCameraName(ipcname);
            wfBeltJC.setRtspUrl(ipcrtsppath);
            wfBeltJC.setAnalyse(modelType);
            wfBeltJC.setAnalyseResult(modelAnalysis);
        }
        if(modelCode=="8") //猴车大物件
        {
            if (!hatHC.initConfig(modelPath.c_str(), 0.5, 0.5))
            {
                printf("乘猴车携带大件模型初始化失败\n");
            }
            else
            {
                printf("乘猴车携带大件模型初始化成功。\n");
                hatHC.isLoad = true;
            }
            areaHC = detectarea;
            videoPathHC = coalCode + ipccode + modelType + modelAnalysis;
            wfHC.setCoalCode(coalCode);
            wfHC.setCameraCode(ipccode);
            wfHC.setCameraName(ipcname);
            wfHC.setRtspUrl(ipcrtsppath);
            wfHC.setAnalyse(modelType);
            wfHC.setAnalyseResult(modelAnalysis);
        }
 
        if (modelCode == "9")//睡岗判断
        {
            if (!hatSleep.initConfig(modelPath.c_str(), 0.5, 0.5))
            {
                printf("睡岗模型初始化失败\n");
            }
            else
            {
                printf("睡岗模型初始化成功。\n");
                hatSleep.isLoad = true;
            }
            areaSleep = detectarea;
            videoPathSleep = coalCode + ipccode + modelType + modelAnalysis;
            wfSleep.setCoalCode(coalCode);
            wfSleep.setCameraCode(ipccode);
            wfSleep.setCameraName(ipcname);
            wfSleep.setRtspUrl(ipcrtsppath);
            wfSleep.setAnalyse(modelType);
            wfSleep.setAnalyseResult(modelAnalysis);
        }
        if (modelCode == "10")//穿戴检测,初始化两个模型
        {
            if (!hatWear.initConfig(modelPath.c_str(), 0.5, 0.5))
            {
                printf("穿戴模型1初始化失败\n");
            }
            else
            {
                printf("穿戴模型1初始化成功。\n");
                if (!cfWear.initConfig(modelPath2.c_str()))
                {
                    printf("穿戴模型2初始化失败\n");
                }
                else
                {
                    printf("穿戴模型2初始化成功\n");
                }
                hatWear.isLoad = true;
            }
            areaWear = detectarea;
            videoPathWear = coalCode + ipccode + modelType + modelAnalysis;
            wfWear.setCoalCode(coalCode);
            wfWear.setCameraCode(ipccode);
            wfWear.setCameraName(ipcname);
            wfWear.setRtspUrl(ipcrtsppath);
            wfWear.setAnalyse(modelType);
            wfWear.setAnalyseResult(modelAnalysis);
        }
        if (modelCode == "11")//烟火模型
        {
            if (!hatFire.initConfig(modelPath.c_str())) {
                printf("烟火模型初始化失败\n");
            }
            else
            {
                printf("烟火模型初始化成功。\n");
                hatFire.isLoad = true;
            }
            areaFire = detectarea;
            videoPathFire = coalCode + ipccode + modelType + modelAnalysis;
            wfFire.setCoalCode(coalCode);
            wfFire.setCameraCode(ipccode);
            wfFire.setCameraName(ipcname);
            wfFire.setRtspUrl(ipcrtsppath);
            wfFire.setAnalyse(modelType);
            wfFire.setAnalyseResult(modelAnalysis);
        }
        if (modelCode == "12")//人员跌倒
        {
 
        }
    }    
 
    std::queue<int> quTSJ;//记录提升机运动的判断结果
    std::queue<int> quBelt;//记录皮带运动判断结果
 
    int tsjCount;//提升机计数,间隔一段时间,调用一次模型
    int beltCount;//提升机计数,间隔一段时间,调用一次模型
    Mat iMatWear;//人员穿戴的第一帧
 
    long currentFrame = 1;//记录当前帧数
    long skip_frame_num = skipN;//跳帧间隔设置
    Mat frame;
    while (!asyncStop) 
    {        
        //std::cout << ipccode + "当前线程待处理队列数量1111:" << queJC.size() << std::endl;
        
        if (queJC.size() < 1)//队列里有一定数量帧的时候,再开始处理
            continue;        
 
        try
        {
            // 在操作队列之前先锁定互斥锁
            std::unique_lock<std::mutex> lck(mtx);
            frame = queJC.front().clone();    
            queJC.pop();            
            lck.unlock();
 
            if (frame.empty())
                continue;    
 
            if (currentFrame % skip_frame_num == 0) //跳帧处理
            {
                ///////////////////////////////////执行异步任务///////////////////////////////////////////////
 
                //皮带运行算法处理及判断 modelId = 1
                if (rBelt.isLoad)
                {
                    futureBelt = std::async(std::launch::async, &PreProcessModel::fnBeltMoveRec, &ppm, std::ref(frame), std::ref(imagesBelt), ref(beltCount), std::ref(rBelt), areaBelt);
                }
 
                //提升机模型处理及判断 modelId = 2
                if (rTSJ.isLoad)
                {
                    futureTSJ = std::async(std::launch::async, &PreProcessModel::fnImRecProByModelTSJ, &ppm, std::ref(frame), std::ref(imagesTsj), ref(tsjCount), std::ref(rTSJ), std::ref(quTSJ), areaTSJ);
                }
 
                //人员闯入模型处理及判断 modelId = 3
                if (hatJC.isLoad)
                {
                    futurePerson = std::async(std::launch::async, &PreProcessModel::fnImRecProByModelHAT1, &ppm, std::ref(frame), std::ref(hatJC), areaPerson,workareaPerson,c_list,lable_title,color_result);
                }
 
                //摄像头移动和遮挡专用
                auto cameraRecordTime = std::chrono::system_clock::now() + std::chrono::hours(-1);//记录最后一次调用摄像头移动算法时间,间隔1小时调用一次摄像头移动和遮挡算法
                std::chrono::duration<double> timeDifference = std::chrono::system_clock::now() - cameraRecordTime;
                if (timeDifference.count() >= 3600)//间隔1个小时检测一次摄像头移动和遮挡
                {
                    //摄像头遮挡算法处理及判断 modelId = 4
                    if (cover.isLoad)
                    {
                        futureCameraCover = std::async(std::launch::async, &PreProcessModel::fnCameraCoverRec, &ppm, std::ref(frame), std::ref(cover));
                    }
 
                    //摄像头移动算法处理及判断 modelId = 5
                    if (camera.isLoad)
                    {
                        futureCameraMove = std::async(std::launch::async, &PreProcessModel::fnCameraMoveRec, &ppm, std::ref(frame), std::ref(camera));
                    }
 
                    cameraRecordTime = std::chrono::system_clock::now();//重新计时
                }
 
                //堆煤模型处理及判断 modelId = 6
                if (dm.isLoad)
                {
                    futureDM = std::async(std::launch::async, &PreProcessModel::fnImRecProByModelDM, &ppm, std::ref(frame), std::ref(dm), areaDM);
                }
 
                //皮带跑偏和异物检测模型处理及判断 modelId = 7
                if (beltJC.isLoad)
                {
                    futureBeltJC = std::async(std::launch::async, &PreProcessModel::fnImRecProByModelBeltJC2, &ppm, std::ref(frame), std::ref(beltJC), leftareaBeltJC,rightareaBeltJC,areaBeltJC);
                }
 
                //乘猴车携带大件 modelId = 8
                if (hatHC.isLoad)
                {
                    futureHC = std::async(std::launch::async, &PreProcessModel::fnImRecProByModelHATHC, &ppm, std::ref(frame), std::ref(hatHC), areaHC);
                }
 
                //睡岗 modelId = 9
                if (hatSleep.isLoad)
                {
                    futureSleep = std::async(std::launch::async, &PreProcessModel::fnImRecProByModelHATSleep, &ppm, std::ref(frame), std::ref(hatSleep));
                }
 
                //人员穿戴模型处理及判断 11
                if (hatWear.isLoad)
                {
                    futureWear = std::async(std::launch::async, &PreProcessModel::fnImRecProByModelWear, &ppm, std::ref(frame), std::ref(hatWear), std::ref(cfWear), areaWear);
                }
 
                //烟火模型处理及判断 12
                if (hatFire.isLoad)
                {
                    futureFire = std::async(std::launch::async, &PreProcessModel::fnImRecProByModelFire, &ppm, std::ref(frame), std::ref(hatFire));
                }
 
                ///////////////////////////////////检查异步任务,并得到返回结果///////////////////////////////////////////////
                //检查异步任务是否开启 modelId = 1
                if (futureBelt.valid())
                {
                    beltStatus = futureBelt.get();// 皮带最新运行状态
                }
 
                //检查异步任务是否开启 modelId = 2
                if (futureTSJ.valid())
                {
                    tsjStatus = futureTSJ.get();// 等待返回提升机运行状态
                }
 
                //检查异步任务是否开启 modelId = 3
                if (futurePerson.valid()) {
                    personResult = futurePerson.get();// 等待返回是否人员闯入
                }
 
                //检查异步任务是否开启 modelId = 4
                if (futureCameraCover.valid())
                {
                    cameracover = futureCameraCover.get();// 获取摄像头遮挡状态
                }
 
                //检查异步任务是否开启 modelId = 5
                if (futureCameraMove.valid())
                {
                    cameramove = futureCameraMove.get();// 获取摄像头移动状态
                }
 
                //检查异步任务是否开启 modelId = 6
                if (futureDM.valid())
                {
                    proportion = futureDM.get();// 等待返回堆煤占比
 
                    //跳帧保存数据
                    if (currentFrame % (skip_frame_num*5) == 0) //跳帧处理,每间隔一定帧数保存一次记录
                    {
                        // 实时写入煤流量
                        std::string query = "CALL insert_or_update_CoalFlow(" + ipccode + ", " + std::to_string(proportion) + ")";
 
                        // 检查连接是否仍然有效
                        if (mysql_ping(mysql) == 0) {
                            //std::cout << "连接 MySQL 数据库成功" << std::endl;
                            if (mysql_query(mysql, query.c_str())) {
                                string err_string = mysql_error(mysql);
                                cout << err_string << endl;
                            }
                        }
                        else {
                            //重新连接数据库
                            if (!mysql_real_connect(mysql, mysqlpath.c_str(), mysqluser.c_str(), mysqlpass.c_str(), mysqldatabase.c_str(), stoi(mysqlport), NULL, 0))
                            {
                                std::cout << "连接 MySQL 数据库失败: " << mysql_error(mysql) << std::endl;
                                mysql_close(mysql);  // 关闭连接
                            }
 
                            //mysql_set_character_set(mysql, "utf8mb4");
                            mysql_query(mysql, "SET NAMES GB2312");//解决中文乱码问题        
                        }
                    }
                }
 
                //皮带检测异步任务是否开启 modelId = 7
                if (futureBeltJC.valid())
                {
                    beltJCResult = futureBeltJC.get();// 等待返回是否皮带检测结果
                }
 
                //乘猴车携带大件 modelId = 8
                if (futureHC.valid()) {
                    hcResult = futureHC.get();// 等待返回结果
                }
 
                //睡岗 modelId = 9
                if (futureSleep.valid()) {
                    sleepResult = futureSleep.get();// 等待返回睡岗人员数量
                }
 
                //人员穿戴 11
                if (futureWear.valid()) {
                    resultWear = futureWear.get();// 等待返回人员穿戴结果
                }
 
                //烟火检测 12
                if (futureFire.valid()) {
                    resultFire = futureFire.get();// 等待返回烟火结果
                }
 
                queJC2.push(frame);// 推到视频推流队列            
 
                string filefolder = fnCreateFileFolder(warningFilePath);//生成文件夹路径
                string videoPath;//录像保存路径
                string firstImagPath;//录像的第一张图片
                double seconds;
 
#pragma region 进行判断报警录像处理
                // 皮带运行状态变化报警 1
                if (beltStatus != -1 && beltStatus != beltStatusold && !video_writerBelt.isOpened())
                {
                    beltStatus = beltStatusold;//皮带状态保持一致
                    seconds = std::chrono::duration<double>(std::chrono::system_clock::now() - videoTimeBelt).count();
                    if (seconds > RecIntervalime)
                    {
                        cout << "皮带运行触发报警录像后,开始保存10秒视频" << endl;
                        start_timeBelt = std::chrono::system_clock::now();
                        videoTimeBelt = std::chrono::system_clock::now();//重新开始记录最后一次录像时间
                        if (beltStatus == 0)//运输
                        {
                            videoPath = filefolder + videoPathBelt + rBelt.analysis1 + getCurrentDateTime("%Y%m%d%H%M%S") + ".mp4";//录像文件名称;
                            firstImagPath = filefolder + videoPathBelt + rBelt.analysis1 + getCurrentDateTime("%Y%m%d%H%M%S") + ".png";
                        }
                        else//停止
                        {
                            videoPath = filefolder + videoPathBelt + rBelt.analysis2 + getCurrentDateTime("%Y%m%d%H%M%S") + ".mp4";//录像文件名称;
                            firstImagPath = filefolder + videoPathBelt + rBelt.analysis2 + getCurrentDateTime("%Y%m%d%H%M%S") + ".png";
                        }
                        //cv::resize(frame, frame, Size(640, 480));
                        // 创建VideoWriter对象
                        video_writerBelt = VideoWriter(videoPath, cv::VideoWriter::fourcc('a', 'v', 'c', '1'), 20, Size(frame.cols, frame.rows));
                        wfBelt.setRecordFileName(videoPath);
                        wfBelt.setrecordImgFileName(firstImagPath);
 
                        fnVideoRecordToMysql(frame, 1, video_writerBelt, start_timeBelt, wfBelt, mysql, recordtime);                
                        //保存第一张图片
                        cv::imwrite(firstImagPath, frame);
                    }
 
                }
                else
                {
                    if (beltStatus != -1)
                        beltStatus = beltStatusold;//皮带状态保持一致        
 
                    if (video_writerBelt.isOpened())
                    {
                        //cv::resize(frame, frame, Size(640, 480));
                        fnVideoRecordToMysql(frame, 0, video_writerBelt, start_timeBelt, wfBelt, mysql, recordtime);
 
                    }
                }
 
                // 提升机状态变化报警 2
                if (tsjStatus != -1 && tsjStatus != tsjStatusold && !video_writerTSJ.isOpened())
                {
                    tsjStatusold = tsjStatus;//提升机保持状态一致
                    seconds = std::chrono::duration<double>(std::chrono::system_clock::now() - videoTimeTSJ).count();
                    if (seconds > RecIntervalime)
                    {
                        cout << "提升机触发报警录像后,开始保存10秒视频" << endl;
                        start_timeTSJ = std::chrono::system_clock::now();
                        videoTimeTSJ = std::chrono::system_clock::now();//重新开始记录最后一次录像时间
                        if (tsjStatus == 0)//运输
                        {
                            videoPath = filefolder + videoPathTSJ + rTSJ.analysis1 + getCurrentDateTime("%Y%m%d%H%M%S") + ".mp4";//录像文件名称;
                            firstImagPath = filefolder + videoPathTSJ + rTSJ.analysis1 + getCurrentDateTime("%Y%m%d%H%M%S") + ".png";
                        }
                        else//停止
                        {
                            videoPath = filefolder + videoPathTSJ + rTSJ.analysis2 + getCurrentDateTime("%Y%m%d%H%M%S") + ".mp4";//录像文件名称;
                            firstImagPath = filefolder + videoPathTSJ + rTSJ.analysis2 + getCurrentDateTime("%Y%m%d%H%M%S") + ".png";
                        }
                        //cv::resize(frame, frame, Size(640, 480));
                        // 创建VideoWriter对象
                        video_writerTSJ = VideoWriter(videoPath, cv::VideoWriter::fourcc('a', 'v', 'c', '1'), 20, Size(frame.cols, frame.rows));
                        wfTSJ.setRecordFileName(videoPath);
                        wfTSJ.setrecordImgFileName(firstImagPath);
 
                        fnVideoRecordToMysql(frame, 1, video_writerTSJ, start_timeTSJ, wfTSJ, mysql, recordtime);
                        //保存第一张图片
                        cv::imwrite(firstImagPath, frame);
                    }
                }
                else
                {
                    if (tsjStatus != -1)
                        tsjStatusold = tsjStatus;//提升机保持状态一致
 
                    if (video_writerTSJ.isOpened())
                    {
                        cv::resize(frame, frame, Size(640, 480));
                        fnVideoRecordToMysql(frame, 0, video_writerTSJ, start_timeTSJ, wfTSJ, mysql, recordtime);
                    }
                }
 
                //人员闯入或者未戴安全帽报警     3        
                if (!video_writerPerson.isOpened() && personResult.size() > 0 
                    && ((areaPerson.size()>0 && personResult[0] > 0) //人员闯入判断
                        || personResult[1] > 0 //未带安全帽判断
                        || (workareaPerson.size()>0 && personResult[2] <= 0))//离岗判断
                    )
                {
                    string videotitle = "";
 
                    if (personResult[0] > 0)
                    {
                        videotitle = "人员闯入";
                    }
 
                    if (personResult[1] > 0)
                    {
                        videotitle = videotitle + " 未佩戴安全帽";
                    }
 
                    if (personResult[2] <= 0)
                    {
                        videotitle = videotitle + " 人员脱岗离岗";
                    }
 
                    wfPerson.setAnalyseResult(videotitle);
 
                    personResult[0] = 0;//人员闯入标志复位
                    personResult[1] = 0;//未带安全帽标志复位
                    personResult[2] = 0;//工作区域离岗标志复位
 
                    seconds = std::chrono::duration<double>(std::chrono::system_clock::now() - videoTimePerson).count();
                    if (seconds > RecIntervalime)
                    {
                        cout << videotitle << "触发报警录像后,开始保存10秒视频" << endl;
                        start_timePerson = std::chrono::system_clock::now();
                        videoTimePerson = std::chrono::system_clock::now();//重新开始记录最后一次录像时间
                        videoPath = filefolder + videoPathPerson + getCurrentDateTime("%Y%m%d%H%M%S") + ".mp4";//录像文件名称
                        firstImagPath = filefolder + videoPathPerson + getCurrentDateTime("%Y%m%d%H%M%S") + ".png";
                        // 创建VideoWriter对象
                        video_writerPerson = VideoWriter(videoPath, cv::VideoWriter::fourcc('a', 'v', 'c', '1'), 20, Size(frame.cols, frame.rows));
                        wfPerson.setRecordFileName(videoPath);
                        wfPerson.setrecordImgFileName(firstImagPath);
 
                        fnVideoRecordToMysql(frame, 1, video_writerPerson, start_timePerson, wfPerson, mysql, recordtime);
                        
                        //保存第一张图片
                        cv::imwrite(firstImagPath, frame);                        
                    }
                }
                else
                {
                    if (video_writerPerson.isOpened())
                    {
                        fnVideoRecordToMysql(frame, 0, video_writerPerson, start_timePerson, wfPerson, mysql , recordtime);
                    }
                }
                
                //摄像头遮挡报警 4
                if (cameracover == 1 && !video_writerCameraCover.isOpened())
                {
                    cameracover = 0;//标志复位
 
                    cout << "摄像头遮挡触发报警录像后,开始保存10秒视频" << endl;
                    start_timeCameraCover = std::chrono::system_clock::now();
                    //videoTimeCameraCover = std::chrono::system_clock::now();//重新开始记录最后一次录像时间
                    videoPath = filefolder + videoPathCameraCover + getCurrentDateTime("%Y%m%d%H%M%S") + ".mp4";//录像文件名称
                    firstImagPath = filefolder + videoPathCameraCover + getCurrentDateTime("%Y%m%d%H%M%S") + ".png";
                    // 创建VideoWriter对象
                    video_writerCameraCover = VideoWriter(videoPath, cv::VideoWriter::fourcc('a', 'v', 'c', '1'), 20, Size(frame.cols, frame.rows));
                    wfCameraCover.setRecordFileName(videoPath);
                    wfCameraCover.setrecordImgFileName(firstImagPath);
 
                    fnVideoRecordToMysql(frame, 1, video_writerCameraCover, start_timeCameraCover, wfCameraCover, mysql , recordtime);
                    //保存第一张图片
                    cv::imwrite(firstImagPath, frame);
                }
                else
                {
                    if (video_writerCameraCover.isOpened())
                    {
                        fnVideoRecordToMysql(frame, 0, video_writerCameraCover, start_timeCameraCover, wfCameraCover, mysql , recordtime);
                    }
                }
 
                //摄像头移动报警 5
                if (cameramove == 1 && !video_writerCameraMove.isOpened())
                {
                    cameramove = 0;//标志复位
 
                    cout << "摄像头移动触发报警录像后,开始保存10秒视频" << endl;
                    start_timeCameraMove = std::chrono::system_clock::now();
                    //videoTimeCameraMove = std::chrono::system_clock::now();//重新开始记录最后一次录像时间
                    videoPath = filefolder + videoPathCameraMove + getCurrentDateTime("%Y%m%d%H%M%S") + ".mp4";//录像文件名称
                    firstImagPath = filefolder + videoPathCameraMove + getCurrentDateTime("%Y%m%d%H%M%S") + ".png";
                    // 创建VideoWriter对象
                    video_writerCameraMove = VideoWriter(videoPath, cv::VideoWriter::fourcc('a', 'v', 'c', '1'), 20, Size(frame.cols, frame.rows));
                    wfCameraMove.setRecordFileName(videoPath);
                    wfCameraMove.setrecordImgFileName(firstImagPath);
 
                    fnVideoRecordToMysql(frame, 1, video_writerCameraMove, start_timeCameraMove, wfCameraMove, mysql , recordtime);
                    //保存第一张图片
                    cv::imwrite(firstImagPath, frame);
                }
                else
                {
                    if (video_writerCameraMove.isOpened())
                    {
                        fnVideoRecordToMysql(frame, 1, video_writerCameraMove, start_timeCameraMove, wfCameraMove, mysql , recordtime);
                    }
                }
 
                // 堆煤报警 6
                if (proportion > std::stof(limitDM) && !video_writerDM.isOpened())
                {
                    wfDM.setAnalyseResult("堆煤");
 
                    proportion = 0;//标志复位
                    seconds = std::chrono::duration<double>(std::chrono::system_clock::now() - videoTimeDM).count();
                    if (seconds > RecIntervalime)
                    {
                        cout << "堆煤触发报警录像后,开始保存10秒视频" << endl;
                        start_timeDM = std::chrono::system_clock::now();
                        videoTimeDM = std::chrono::system_clock::now();//重新开始记录最后一次录像时间
                        videoPath = filefolder + videoPathDM + getCurrentDateTime("%Y%m%d%H%M%S") + ".mp4";//录像文件名称                
                        firstImagPath = filefolder + videoPathDM + getCurrentDateTime("%Y%m%d%H%M%S") + ".png";
                        // 创建VideoWriter对象
                        video_writerDM = VideoWriter(videoPath, cv::VideoWriter::fourcc('a', 'v', 'c', '1'), 20, Size(frame.cols, frame.rows));
                        wfDM.setRecordFileName(videoPath);
                        wfDM.setrecordImgFileName(firstImagPath);
                        fnVideoRecordToMysql(frame, 1, video_writerDM, start_timeDM, wfDM, mysql, recordtime);
                        //保存第一张图片
                        cv::imwrite(firstImagPath, frame);
                    }
                }
                else
                {
                    if (video_writerDM.isOpened())
                    {
                        fnVideoRecordToMysql(frame, 0, video_writerDM, start_timeDM, wfDM, mysql, recordtime);
                    }
                }
 
                //皮带跑偏,异物报警 7
                if (!video_writerBeltJC.isOpened() && beltJCResult.size() > 0 && (beltJCResult[0] > 0  || beltJCResult[1] == 0 || beltJCResult[2] > 0))
                {
                    if (beltJCResult[0] > 0 )
                    {
                        wfBeltJC.setAnalyseResult("大煤块");
                    }
                    else if (beltJCResult[1] == 0)
                    {
                        wfBeltJC.setAnalyseResult("皮带跑偏");
                    }
                    else if (beltJCResult[2] > 0)
                    {
                        wfBeltJC.setAnalyseResult("大块异物");
                    }
                    else
                    {
                        wfBeltJC.setAnalyseResult("");
                    }
 
                    beltJCResult[0] = 0;//大煤块标志复位
                    beltJCResult[1] = 1;//标志复位 0为跑偏 1为不跑偏
                    beltJCResult[2] = 0;//异物标志复位
 
                    seconds = std::chrono::duration<double>(std::chrono::system_clock::now() - videoTimeBeltJC).count();
                    if (seconds > RecIntervalime)
                    {
                        cout << "皮带跑偏,大煤块,异物触发报警录像后,开始保存10秒视频" << endl;
                        start_timeBeltJC = std::chrono::system_clock::now();
                        videoTimeBeltJC = std::chrono::system_clock::now();//重新开始记录最后一次录像时间
                        videoPath = filefolder + videoPathBeltJC + getCurrentDateTime("%Y%m%d%H%M%S") + ".mp4";//录像文件名称
                        firstImagPath = filefolder + videoPathBeltJC + getCurrentDateTime("%Y%m%d%H%M%S") + ".png";
                        // 创建VideoWriter对象
                        video_writerBeltJC = VideoWriter(videoPath, cv::VideoWriter::fourcc('a', 'v', 'c', '1'), 20, Size(frame.cols, frame.rows));
                        wfBeltJC.setRecordFileName(videoPath);
                        wfBeltJC.setrecordImgFileName(firstImagPath);
 
                        fnVideoRecordToMysql(frame, 1, video_writerBeltJC, start_timeBeltJC, wfBeltJC, mysql, recordtime);
 
                        //保存第一张图片
                        cv::imwrite(firstImagPath, frame);
                    }
                }
                else
                {
                    if (video_writerBeltJC.isOpened())
                    {
                        fnVideoRecordToMysql(frame, 0, video_writerBeltJC, start_timeBeltJC, wfBeltJC, mysql, recordtime);
                    }
                }
 
                // 猴车携带大件     8        
                if (hcResult.size() >0 && hcResult[0] > 0 && !video_writerHC.isOpened())
                {
                    wfHC.setAnalyseResult("猴车携带大件");                    
 
                    hcResult[0] = 0;//标志复位
 
                    seconds = std::chrono::duration<double>(std::chrono::system_clock::now() - videoTimeHC).count();
                    if (seconds > RecIntervalime)
                    {
                        cout << "猴车携带大件触发报警录像后,开始保存10秒视频" << endl;
                        start_timeHC = std::chrono::system_clock::now();
                        videoTimeHC = std::chrono::system_clock::now();//重新开始记录最后一次录像时间
                        videoPath = filefolder + videoPathHC + getCurrentDateTime("%Y%m%d%H%M%S") + ".mp4";//录像文件名称
                        firstImagPath = filefolder + videoPathHC + getCurrentDateTime("%Y%m%d%H%M%S") + ".png";
                        // 创建VideoWriter对象
                        video_writerHC = VideoWriter(videoPath, cv::VideoWriter::fourcc('a', 'v', 'c', '1'), 20, Size(frame.cols, frame.rows));
                        wfHC.setRecordFileName(videoPath);
                        wfHC.setrecordImgFileName(firstImagPath);
 
                        fnVideoRecordToMysql(frame, 1, video_writerHC, start_timeHC, wfHC, mysql, recordtime);
                        
                        //保存第一张图片
                        cv::imwrite(firstImagPath, frame);                        
                    }
                }
                else
                {
                    if (video_writerHC.isOpened())
                    {
                        fnVideoRecordToMysql(frame, 0, video_writerHC, start_timeHC, wfHC, mysql , recordtime);
                    }
                }
 
                // 睡岗报警     9        
                if (sleepResult.size() > 0 && sleepResult[0] > 0 && !video_writerSleep.isOpened())
                {
                    wfSleep.setAnalyseResult("睡岗");
 
                    sleepResult[0] = 0;//标志复位
 
                    seconds = std::chrono::duration<double>(std::chrono::system_clock::now() - videoTimeSleep).count();
                    if (seconds > RecIntervalime)
                    {
                        cout << "睡岗触发报警录像后,开始保存10秒视频" << endl;
                        start_timeSleep = std::chrono::system_clock::now();
                        videoTimeSleep = std::chrono::system_clock::now();//重新开始记录最后一次录像时间
                        videoPath = filefolder + videoPathSleep + getCurrentDateTime("%Y%m%d%H%M%S") + ".mp4";//录像文件名称
                        firstImagPath = filefolder + videoPathSleep + getCurrentDateTime("%Y%m%d%H%M%S") + ".png";
                        // 创建VideoWriter对象
                        video_writerSleep = VideoWriter(videoPath, cv::VideoWriter::fourcc('a', 'v', 'c', '1'), 20, Size(frame.cols, frame.rows));
                        wfSleep.setRecordFileName(videoPath);
                        wfSleep.setrecordImgFileName(firstImagPath);
 
                        fnVideoRecordToMysql(frame, 1, video_writerSleep, start_timeSleep, wfSleep, mysql, recordtime);
 
                        //保存第一张图片
                        cv::imwrite(firstImagPath, frame);
 
                    }
                }
                else
                {
                    if (video_writerSleep.isOpened())
                    {
                        fnVideoRecordToMysql(frame, 0, video_writerSleep, start_timeSleep, wfSleep, mysql, recordtime);
 
                    }
                }
 
                // 人员穿戴报警    11        
                if (resultWear.size() > 0 && !video_writerWear.isOpened())
                {
                    string AnalyseResult = "";
                    //初始化上一次的结果记录
                    if (resultWearold.size() == 0)
                    {
                        resultWearold = resultWear;
                        iMatWear = frame;//记录进入的第一个人图像
                    }
 
                    if (resultWear[0] == 0)//检测框内没人
                    {
                        if (resultWearold[0] == 1)//原来检测区域有人,则表示检测完毕,进行判断处理
                        {
                            if (resultWearold[1] < 1)
                            {
                                AnalyseResult+="未佩戴安全帽";
                            }
 
                            if (resultWearold[2] < 1)                        
                            {
                                
                                AnalyseResult += " 未携带自救器";
                            }
 
                            if (resultWearold[3] < 1)
                            {
                                AnalyseResult += " 未穿胶鞋";
                            }
 
                            if (resultWearold[4] < 1)
                            {
                                AnalyseResult += " 未穿工作服";
                            }
 
                            //wfWear.setAnalyse("AI-11-03");
                            wfWear.setAnalyseResult(AnalyseResult);
 
                            resultWear.clear();
                            resultWearold.clear();//判断完成后清空保留的状态
                        }
                        else
                        {
                            resultWearold = resultWear;
                        }
                    }
                    else if (resultWear[0] == 1)//检测框内有1人
                    {
                        resultWearold[0] = resultWear[0];
 
                        if (resultWear[1] == 1)//记录安全帽检测结果
                        {
                            resultWearold[1] = 1;
                        }
 
                        if (resultWear[2] == 1)//记录自救器检测结果
                        {
                            resultWearold[2] = 1;
                        }
 
                        if (resultWear[3] == 1)//记录鞋子检测结果
                        {
                            resultWearold[3] = 1;
                        }
 
                        if (resultWear[4] == 1)//记录工作服检测结果
                        {
                            resultWearold[4] = 1;
                        }
                    }
                    else if (resultWear[0]>1)//检测框内有多人
                    {
                        //wfWear.setAnalyseResult("检测框内有多人");
                        cout << "检测框内有多人,检测作废" << endl;
                        resultWear.clear();
                    }
 
                    if (!AnalyseResult.empty())//判断有违规行为
                    {
                        seconds = std::chrono::duration<double>(std::chrono::system_clock::now() - videoTimeWear).count();
                        if (seconds > RecIntervalime)
                        {
                            cout << "人员穿戴触发报警录像后,开始保存10秒视频" << endl;
                            start_timeWear = std::chrono::system_clock::now();
                            videoTimeWear = std::chrono::system_clock::now();//重新开始记录最后一次录像时间
                            videoPath = filefolder + videoPathWear + getCurrentDateTime("%Y%m%d%H%M%S") + ".mp4";//录像文件名称
                            firstImagPath = filefolder + videoPathWear + getCurrentDateTime("%Y%m%d%H%M%S") + ".png";
                            // 创建VideoWriter对象
                            video_writerWear = VideoWriter(videoPath, cv::VideoWriter::fourcc('a', 'v', 'c', '1'), 20, Size(frame.cols, frame.rows));
                            wfWear.setRecordFileName(videoPath);
                            wfWear.setrecordImgFileName(firstImagPath);
 
                            //fnVideoRecord(frame, 1, video_writerWear, start_timeWear, wfWear, context, recordtime);
                            fnVideoRecordToMysql(frame, 1, video_writerWear, start_timeWear, wfWear, mysql, recordtime);
 
                            //保存第一张图片
                            cv::imwrite(firstImagPath, iMatWear);
                        }
                    }                
                }
                else
                {
                    if (video_writerWear.isOpened())
                    {
                        //fnVideoRecord(frame, 0, video_writerWear, start_timeWear, wfWear, context, recordtime);
                        fnVideoRecordToMysql(frame, 0, video_writerWear, start_timeWear, wfWear, mysql, recordtime);
                    }
                }
 
                // 烟火报警     12    
                if (resultFire.size() > 0 && !video_writerFire.isOpened())
                {
                    int res = 0;
 
                    //有烟火报警
                    if (resultFire[0] || resultFire[1])
                    {
                        if (vecFire.size() == 0)
                        {
                            firstFire = std::chrono::system_clock::now();//记录第一次报警时间
                            vecFire.push_back(resultFire[0] || resultFire[1]);
                            vecVideoFire.push_back(frame);
                            resultFire[0] = 0;//火标志复位
                            resultFire[1] = 0;//烟标志复位
                            continue;
                        }
                    }
 
                    if (vecFire.size() > 0)//开始计数了
                    {
                        std::chrono::duration<double> timeDifference = std::chrono::system_clock::now() - firstFire;
 
                        if (timeDifference.count() > 5)//大于5秒,进行判断
                        {
                            res = fnJudeRatio(vecFire, 1, 0.6);
 
                            //判断有烟火
                            if (res != 0)
                            {
                                wfFire.setAnalyseResult("烟火报警");
                                seconds = std::chrono::duration<double>(std::chrono::system_clock::now() - videoTimeHC).count();
                                if (seconds > RecIntervalime)
                                {
                                    cout << "烟火触发报警录像后,开始保存10秒视频" << endl;
                                    start_timeFire = std::chrono::system_clock::now();
                                    videoTimeFire = std::chrono::system_clock::now();//重新开始记录最后一次录像时间
                                    videoPath = filefolder + videoPathFire + getCurrentDateTime("%Y%m%d%H%M%S") + ".mp4";//录像文件名称
                                    firstImagPath = filefolder + videoPathFire + getCurrentDateTime("%Y%m%d%H%M%S") + ".png";
                                    // 创建VideoWriter对象
                                    video_writerFire = VideoWriter(videoPath, cv::VideoWriter::fourcc('a', 'v', 'c', '1'), 20, Size(frame.cols, frame.rows));
                                    wfFire.setRecordFileName(videoPath);
                                    wfFire.setrecordImgFileName(firstImagPath);
 
                                    //fnVideoRecord(frame, 1, video_writerFire, start_timeFire, wfFire, context, recordtime);
                                    fnVideoRecordToMysql2(vecVideoFire, 1, video_writerFire, wfFire, mysql);
 
                                    //保存第一张图片
                                    cv::imwrite(firstImagPath, vecVideoFire[0]);
                                }
                            }
 
                            vecVideoFire.clear();
                            vecFire.clear();
                        }
                        else
                        {
                            vecFire.push_back(resultFire[0] || resultFire[1]);
                            vecVideoFire.push_back(frame);
                        }
                    }
 
                    resultFire[0] = 0;//火标志复位
                    resultFire[1] = 0;//烟标志复位
                }
 
#pragma endregion
 
            }
            
            //cv::imwrite("./img/" + std::to_string(currentFrame)+".jpg", frame);    
            //cv::resize(frame, frame, Size(1920, 1080));
            //cv::imshow(ipccode, frame);//展示帧图片
            //cv::imshow("ipccode", queJC2.front());//展示帧图片
            //cv::waitKey(20);            
 
            //std::this_thread::sleep_for(std::chrono::milliseconds(10));// 延迟等待一段时间            
 
            if (currentFrame > 10000) {
                currentFrame = 0;
            }
 
            currentFrame++;            
        }
        catch (const std::exception& ex)
        {            
            std::string errorMessage = "处理视频帧失败-";
            errorMessage += ex.what();
            cout << errorMessage << endl;
        
            continue;
        }        
    }
 
    frame.release();
}
 
/// @brief 推流到rabbitmq
/// @param queJC2 处理完的视频流帧队列
/// @param ipccode 摄像机编号
void PreProcessFn::fnPushVideoInRabbitMQ(queue<Mat>& queJC2, string ipccode)
{
    CRabbitmqClient objRabbitmq;
    int iRet = objRabbitmq.Connect(rabbitpath, stoi(rabbitport), rabbituser, rabbitpass);
    iRet = objRabbitmq.ExchangeDeclare(ipccode, "fanout");
    objRabbitmq.Publish("", ipccode, "");
    printf("Rabbitmq连接状态:%d\n", iRet);
    std::string imgStr;
    //定义接受帧
    Mat frame;
 
    long currentFrame = 1;
    long skip_frame_num = skipN;//跳帧间隔设置(每1帧)
 
    //创建url连接,定时判断有没有页面进行拉流
    CURL* curl;
    CURLcode res;
    std::string readBuffer;
    curl = curl_easy_init();
    bool pushflg = true;//是否推流标志
 
    if(curl)
    {
        string url = "http://" + rabbitpath + ":1" + rabbitport + "/api/exchanges/%2F/"+ ipccode + "/bindings/source";
        curl_easy_setopt(curl, CURLOPT_URL, url.c_str()); 
        // 如果需要验证身份,请设置用户名和密码,例如 "admin:admin"
        curl_easy_setopt(curl, CURLOPT_USERNAME, rabbituser.c_str());
        curl_easy_setopt(curl, CURLOPT_PASSWORD, rabbitpass.c_str());        
        curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteCallback);
        curl_easy_setopt(curl, CURLOPT_WRITEDATA, &readBuffer); 
    }     
 
    while (!asyncStop) {
        //std::cout << ipccode + "当前线程Rabbitmq数据数量2222:" << queJC2.size() << std::endl;        
        try
        {
            if (queJC2.size() > 500)
            {
                cout << "推送视频时,存在内存溢出风险!" << endl;
            }
            
            if (queJC2.empty() || queJC2.size() == 0)
            {
                continue;
            }
            
            //加锁,防止别的系统操作这段内存
            std::unique_lock<std::mutex> lck(mtx);
            frame = queJC2.front();
            queJC2.pop();        
            lck.unlock();        
 
            if (frame.empty())//帧为空,则舍弃
                continue;                   
             
            if (currentFrame % 20 == 0) //每20帧,判断一次是否有页面拉流
            {                
                //通过API获取指定exchange对应的queue
                res = curl_easy_perform(curl);
                if(res != CURLE_OK)
                {
                    pushflg    = false;//不可推流
                    std::cerr << "curl_easy_perform() failed: " << curl_easy_strerror(res) << std::endl;
                    continue;
                }                
                else 
                {
                    //std::cout << readBuffer << std::endl;
                    //对应的exchange中,有queue,则进行推流
                    if(readBuffer.length()>5)
                    {
                        pushflg = true;    //可推流            
                    }
                    else
                    {
                        pushflg    = false;//不可推流
                    }
                }
            }    
            
            //pushflg=true;//test
 
            //如果为true则推流
            if(pushflg)
            {                     
 
                if (iRet != 0)
                {
                    //重新连接并发送
                    objRabbitmq.Connect(rabbitpath, stoi(rabbitport), rabbituser, rabbitpass);
                    objRabbitmq.ExchangeDeclare(ipccode, "fanout");
                }    
                
                //cv::imwrite("./img/" + ipccode +".jpg", frame);    
                ////压缩图片
                //cv::resize(frame, frame, Size(1280, 720));
 
                imgStr = Mat2Base64(frame, ".jpg");//输出Base64编码的字符串                
                //std::cout << "Base64 size: " << imgStr.size() << std::endl;    
                objRabbitmq.Publish(imgStr, ipccode, "");
                
                //cv::resize(frame, frame, Size(1280, 720));        
                //cv::imshow(ipccode, frame);//展示帧图片
                //waitKey(5);
            }
                            
 
            readBuffer.clear();                
 
            //std::this_thread::sleep_for(std::chrono::milliseconds(10));// 延迟等待一段时间
 
            if (currentFrame > 10000) {
                currentFrame = 0;
            }
 
            currentFrame++;
 
        }
        catch (const std::exception& ex)
        {
            std::string errorMessage = "保存到RabbitMQ失败:-";
            errorMessage += ex.what();
            cout << errorMessage << endl;
            continue;
        }        
    }
 
    curl_easy_cleanup(curl);
    frame.release();
}
 
/// @brief 推流到流媒体服务器
/// @param queJC2 处理完的视频流帧队列
/// @param ipccode 摄像机编号
void PreProcessFn::fnPushVideo(queue<Mat>& queJC2, string ipccode)
{    
    //定义接受帧
    Mat frame;
 
    while (!asyncStop) {
        //std::cout << ipccode + "当前线程Rabbitmq数据数量2222:" << queJC2.size() << std::endl;        
        try
        {
            if (queJC2.size() > 500)
            {
                cout << "推送视频时,存在内存溢出风险!" << endl;
            }
            
            if (queJC2.empty() || queJC2.size() == 0)
            {
                continue;
            }
 
            frame = queJC2.front();
            queJC2.pop();
 
            if (frame.empty())//帧为空,则舍弃
                continue;
 
            // FFmpeg推流命令
            std::string ffmpeg_command = 
                "ffmpeg -y -f rawvideo -pixel_format bgr24 -video_size 640x480 "
                "-framerate 30 -i - -c:v libx264 -pix_fmt yuv420p -f rtsp "
                   "rtsp://192.168.1.188:8554/live/stream";
 
            // 打开FFmpeg进程
            FILE* pipe = popen(ffmpeg_command.c_str(), "w");
            if (!pipe) {
                std::cerr << "无法启动FFmpeg" << std::endl;
            }
 
            // 将帧写入到FFmpeg管道中
            fwrite(frame.data, 1, frame.total() * frame.elemSize(), pipe);
 
            //cv::resize(frame, frame, Size(1280, 720));        
            //cv::imshow(ipccode, frame);//展示帧图片
            //waitKey(10);            
 
            //std::this_thread::sleep_for(std::chrono::milliseconds(10));// 延迟等待一段时间
 
        }
        catch (const std::exception& ex)
        {
            std::string errorMessage = "保存到RabbitMQ失败:-";
            errorMessage += ex.what();
            cout << errorMessage << endl;
            continue;
        }        
    }
 
    frame.release();
}
 
 
 
/// @brief 推流到流媒体服务器
/// @param queJC2 处理完的视频流帧队列
/// @param ipccode 摄像机编号
void PreProcessFn::fnPushVideoToUrl(queue<Mat>& queJC2,string toRtsp, string fps, string ipccode)
{    
    //定义接受帧
    Mat frame;
 
    while (!asyncStop) {
        //std::cout << ipccode + "当前线程Rabbitmq数据数量2222:" << queJC2.size() << std::endl;        
        try
        {
            if (queJC2.size() > 500)
            {
                cout << "推送视频时,存在内存溢出风险!" << endl;
            }
            
            if (queJC2.empty() || queJC2.size() == 0)
            {
                continue;
            }
 
            frame = queJC2.front();
            queJC2.pop();
 
            if (frame.empty())//帧为空,则舍弃
                continue;
 
            // FFmpeg推流命令
            std::string ffmpeg_command = 
                "ffmpeg -y -f rawvideo -pixel_format bgr24 -video_size 640x480 "
                "-framerate 30 -i - -c:v libx264 -pix_fmt yuv420p -f rtsp "
                   "rtsp://192.168.1.8:8554/live/stream";
 
            // 打开FFmpeg进程
            FILE* pipe = popen(ffmpeg_command.c_str(), "w");
            if (!pipe) {
                std::cerr << "无法启动FFmpeg" << std::endl;
            }
 
            // 将帧写入到FFmpeg管道中
            fwrite(frame.data, 1, frame.total() * frame.elemSize(), pipe);
 
            //cv::resize(frame, frame, Size(1280, 720));        
            //cv::imshow(ipccode, frame);//展示帧图片
            //waitKey(10);            
 
            //std::this_thread::sleep_for(std::chrono::milliseconds(10));// 延迟等待一段时间
 
        }
        catch (const std::exception& ex)
        {
            std::string errorMessage = "保存到RabbitMQ失败:-";
            errorMessage += ex.what();
            cout << errorMessage << endl;
            continue;
        }        
    }
 
    frame.release();
}