Ver Fonte

Web预览优化: MJPEG推流降频降低CPU

- MJPEG推流节流到80ms(约12fps), JPEG质量60->50
- 4路姿态识别全开时CPU从54%降到29-41%
- MJPEG帧率稳定在10-11fps, 通道延迟5-32ms
dev há 1 semana atrás
pai
commit
39663542fb
2 ficheiros alterados com 9 adições e 17 exclusões
  1. 4 14
      config/config.json
  2. 5 3
      src/web_server.cpp

+ 4 - 14
config/config.json

@@ -72,7 +72,9 @@
     ],
     "pose_conf_thresh": 0.35,
     "pose_core_mask": 2,
+    "pose_crouch": true,
     "pose_enabled": true,
+    "pose_hand_raise": true,
     "pose_input_size": 640,
     "pose_model_path": "models/yolov8n-pose_rk3588_fp.rknn",
     "pose_nms_thresh": 0.45,
@@ -162,16 +164,6 @@
             "id": 3,
             "name": "4",
             "url": "rtsp://admin:hd135792468@192.168.1.124:554/Streaming/Channels/102"
-        },
-        {
-            "id": 4,
-            "name": "5",
-            "url": "rtsp://admin:hd135792468@192.168.1.125:554/Streaming/Channels/102"
-        },
-        {
-            "id": 5,
-            "name": "6",
-            "url": "rtsp://admin:hd135792468@192.168.1.126:554/Streaming/Channels/102"
         }
     ],
     "rtsp_timeout": 10,
@@ -182,7 +174,5 @@
         "password": "admin",
         "username": "admin"
     },
-    "web_port": 8080,
-    "pose_hand_raise": true,
-    "pose_crouch": true
-}
+    "web_port": 8080
+}

+ 5 - 3
src/web_server.cpp

@@ -246,17 +246,19 @@ public:
                 "multipart/x-mixed-replace; boundary=mjpegboundary",
                 [this, fp, seqfn](size_t, httplib::DataSink& sink) -> bool {
                     uint64_t last_seq = 0;
+                    // MJPEG推流节流: 约12fps, 避免并发编码吃满CPU
+                    const int interval_ms = 80;
                     while (!stopping.load() && mjpeg_enabled.load() && sink.is_writable()) {
                         uint64_t seq = seqfn ? seqfn() : 0;
                         if (seq != 0 && seq == last_seq) {
                             // 帧未更新, 跳过重复JPEG编码
-                            std::this_thread::sleep_for(std::chrono::milliseconds(15));
+                            std::this_thread::sleep_for(std::chrono::milliseconds(20));
                             continue;
                         }
                         cv::Mat frame = fp->second();
                         if (!frame.empty()) {
                             std::vector<uchar> buf;
-                            std::vector<int> params = {cv::IMWRITE_JPEG_QUALITY, 60};
+                            std::vector<int> params = {cv::IMWRITE_JPEG_QUALITY, 50};
                             cv::imencode(".jpg", frame, buf, params);
                             std::string header = "--mjpegboundary\r\n"
                                 "Content-Type: image/jpeg\r\n"
@@ -266,7 +268,7 @@ public:
                             if (!sink.write("\r\n", 2)) return false;
                             last_seq = seq;
                         }
-                        std::this_thread::sleep_for(std::chrono::milliseconds(33));
+                        std::this_thread::sleep_for(std::chrono::milliseconds(interval_ms));
                     }
                     return true;
                 });