ESDK中的ai识别推流后卡顿
已完成我们用esdk进行ai识别然后再sample_liveview_main.cc中添加了推流功能来进行测试 显示推流还行但是播放就很卡 想问一下是为什么 以及我把推送mqtt也放在这个文件中 发送mqtt时ai会卡顿 是否应该单独写出 以下是代码
#include <iostream>
#include <vector>
#include <unistd.h>
#include <cstdio>
#include <cstring>
#include <opencv2/opencv.hpp> // 确保包含 OpenCV 头文件
#include "error_code.h"
#include "image_processor.h"
#include "logger.h"
#include "sample_liveview.h"
#include "cloud_api.h"
#include <unordered_set>
#include "image_processor_yolovfastest.h"
using namespace edge_sdk;
using namespace edge_app;
ErrorCode ESDKInit();
// Global pipe for FFmpeg
FILE* ffmpeg_pipe = nullptr;
// Function to initialize FFmpeg
void initFFmpeg() {
if (ffmpeg_pipe) return; // FFmpeg process already started
// 修改 FFmpeg 命令以匹配新的分辨率、帧率和其他参数
std::string command = "ffmpeg -loglevel debug -f rawvideo -pix_fmt bgr24 -s 1440x1080 -framerate 15 -i - "
"-b:v 2000k -bufsize 3000k -c:v libx264 -preset fast -tune zerolatency -pix_fmt yuv420p "
"-f flv rtmp://39.104.204.208:1935/live/stream";
ffmpeg_pipe = popen(command.c_str(), "w");
if (!ffmpeg_pipe) {
ERROR("Failed to open pipe to FFmpeg");
}
}
// Function to close FFmpeg
void closeFFmpeg() {
if (ffmpeg_pipe) {
pclose(ffmpeg_pipe);
ffmpeg_pipe = nullptr;
}
}
// Function to send detection results to cloud
void SendDetectionResultToCloud(const std::unordered_set<std::string>& detections) {
std::string combined_result;
for (const auto& detection : detections) {
if (!combined_result.empty()) {
combined_result += ", "; // 添加分隔符
}
combined_result += detection;
}
// 确保结果字符串是 UTF-8 编码
INFO("send message to cloud: %s", combined_result.c_str());
CloudAPI_SendCustomEventsMessage(reinterpret_cast<const uint8_t*>(combined_result.c_str()), combined_result.size());
}
// Function to process image and send it to FFmpeg
void processImage(const std::shared_ptr<cv::Mat>& image) {
if (image) {
size_t img_size = image->total() * image->elemSize();
if (ffmpeg_pipe) {
size_t written = fwrite(image->data, 1, img_size, ffmpeg_pipe);
if (written != img_size) {
ERROR("Failed to write full frame to FFmpeg");
}
fflush(ffmpeg_pipe); // Ensure data is flushed to FFmpeg
} else {
ERROR("FFmpeg pipe is not open");
}
} else {
ERROR("Image is null");
}
}
int main(int argc, char** argv) {
auto rc = ESDKInit();
if (rc != kOk) {
ERROR("pre init failed");
return -1;
}
// Initialize FFmpeg process
initFFmpeg();
// 创建 payload liveview
auto payload_liveview = std::make_shared<LiveviewSample>("Payload");
StreamDecoder::Options decoder_option = {.name = std::string("ffmpeg")};
// 创建 payload 解码器
auto payload_decoder = CreateStreamDecoder(decoder_option);
// 创建 payload 图像处理器并添加回调函数
ImageProcessor::Options image_processor_option = {
.name = std::string("yolovfastest"),
.alias = std::string("PlayloadCamera: Yolovfastest"),
.callback = [](const std::shared_ptr<cv::Mat>& image) {
processImage(image);
}
};
auto payload_image_processor = CreateImageProcessor(image_processor_option);
if (0 != InitLiveviewSample(
payload_liveview, Liveview::kCameraTypePayload, Liveview::kStreamQuality1080pHigh,
payload_decoder, payload_image_processor)) {
ERROR("Init payload liveview sample failed");
} else {
// 启动 payload liveview
payload_liveview->Start();
}
// 创建 fpv liveview
auto fpv_liveview = std::make_shared<LiveviewSample>("Fpv");
// 创建 fpv 解码器
auto fpv_decoder = CreateStreamDecoder(decoder_option);
// 创建 fpv 图像处理器
ImageProcessor::Options fpv_image_processor_option = {
.name = std::string("display"),
.alias = std::string("FPVCamera"),
.userdata = fpv_liveview
};
auto fpv_image_processor = CreateImageProcessor(fpv_image_processor_option);
if (0 != InitLiveviewSample(
fpv_liveview, Liveview::kCameraTypeFpv, Liveview::kStreamQuality720p,
fpv_decoder, fpv_image_processor)) {
ERROR("Init fpv liveview sample failed");
} else {
// 启动 fpv liveview
fpv_liveview->Start();
}
// 运行主循环
while (true) {
sleep(3);
// 使用互斥锁保护 detected_objects
{
std::lock_guard<std::mutex> lock(detected_objects_mutex);
// 处理检测到的物品并发送到云端
if (detected_objects.empty()) {
SendDetectionResultToCloud({"null"});
} else {
SendDetectionResultToCloud(detected_objects);
}
// 清空 detected_objects 以准备处理下一帧
detected_objects.clear();
}
}
// 退出时关闭 FFmpeg 进程
closeFFmpeg();
return 0;
}
请先登录再写评论。
评论
1 条评论