c++ 处理yolov5s 前后处理、画框、rtmp推流
废话少说直接上流程
decode–>yolov5 pre_process—>nnet处理—>yolov5 post_proess–>x264 encode -->rtmp 推流
// g++ VideoFFmpeg_2.cpp -o VGAppF_2g++ pkg-config --cflags --libs opencv4
// icpx -fsycl VideoFFmpeg_2.cpp -o VGAppF_2ipcx pkg-config --cflags --libs opencv4
// Steam Server: rtmp://127.0.0.1:1935/live
#include <CL/sycl.hpp>
#include
#include
#include
#include <sys/wait.h>
#include
#include <opencv2/opencv.hpp>
using namespace cv;
using namespace std;
using namespace std::chrono;
//#define DEBUG_TIME_LOG
struct queue_root {
struct queue_node *head;
struct queue_node *tail;
volatile unsigned int size;
};
struct queue_node {
void *n;
struct queue_node *next;
};
void init_queue(struct queue_root **root)
{
*root = (struct queue_root *)malloc(sizeof(struct queue_root));
if (*root == NULL) {
printf(“malloc failed”);
exit(1);
}
(*root)->head = (struct queue_node )malloc(sizeof(struct queue_node)); / Sentinel node */
(*root)->tail = (*root)->head;
(*root)->head->n = NULL;
(*root)->head->next = NULL;
(*root)->size = 0;
}
int queue_add(struct queue_root *root, void *val)
{
struct queue_node *n;
struct queue_node *node = (struct queue_node *)malloc(sizeof(struct queue_node));
node->n = val;
node->next = NULL;
while (1) {
n = root->tail;
if (__sync_bool_compare_and_swap(&(n->next), NULL, node)) {
break;
} else {
__sync_bool_compare_and_swap(&(root->tail), n, n->next);
}
}
__sync_bool_compare_and_swap(&(root->tail), n, node);
__sync_fetch_and_add(&root->size, 1);
return 1;
}
void *queue_get(struct queue_root *root)
{
struct queue_node *n;
void *val;
while (1) {
n = root->head;
if (n->next == NULL) {
return NULL;
}
if (__sync_bool_compare_and_swap(&(root->head), n, n->next)) {
break;
}
}
val = (void *)n->next->n;
free(n);
__sync_fetch_and_sub(&root->size, 1);
return val;
}
static void deinit_queue(struct queue_root **root)
{
if (root && *root) {
void *val = NULL;
do {
val = queue_get(*root);
} while (val);
if ((*root)->head) {
free((*root)->head);
}
free(*root);
*root = NULL;
}
}
string classes[80] = {“person”, “bicycle”, “car”, “motorbike”, “aeroplane”, “bus”,
“train”, “truck”, “boat”, “traffic light”, “fire hydrant”,
“stop sign”, “parking meter”, “bench”, “bird”, “cat”, “dog”,
“horse”, “sheep”, “cow”, “elephant”, “bear”, “zebra”, “giraffe”,
“backpack”, “umbrella”, “handbag”, “tie”, “suitcase”, “frisbee”,
“skis”, “snowboard”, “sports ball”, “kite”, “baseball bat”,
“baseball glove”, “skateboard”, “surfboard”, “tennis racket”,
“bottle”, “wine glass”, “cup”, “fork”, “knife”, “spoon”, “bowl”,
“banana”, “apple”, “sandwich”, “orange”, “broccoli”, “carrot”,
“hot dog”, “pizza”, “donut”, “cake”, “chair”, “sofa”, “pottedplant”,
“bed”, “diningtable”, “toilet”, “tvmonitor”, “laptop”, “mouse”,
“remote”, “keyboard”, “cell phone”, “microwave”, “oven”, “toaster”,
“sink”, “refrigerator”, “book”, “clock”, “vase”, “scissors”,
“teddy bear”, “hair drier”, “toothbrush”};
void Stringsplit(const string& str, const string& split, vector& res)
{
char* strc = new char[str.size() + 1];
strcpy(strc, str.c_str()); // 将str拷贝到 char类型的strc中
char* temp = strtok(strc, split.c_str());
while (temp != NULL)
{
res.push_back(string(temp));
temp = strtok(NULL, split.c_str()); // 下一个被分割的串
}
delete[] strc;
}
#pragma tensor_compute model(yolov5s) input(yolov5s_relay_bs1.mlir) subkind(tc_spirv) target_spec(VASTAI_OAK:., NVIDIA_CUDA:. ,HUAWEI_ASCEND:.* ,CAMBRICON_MLU:.* ,nullptr)
constexpr cl::sycl::access::mode sycl_read = cl::sycl::access::mode::read;
constexpr cl::sycl::access::mode sycl_write = cl::sycl::access::mode::write;
struct BBox
{
float category; // 分类
float score; // 分数
float bbox_xmin; // 左上角x坐标
float bbox_ymin; // 左上角y坐标
float bbox_xmax; // 右下角x坐标
float bbox_ymax; // 右下角y坐标
};
enum type{
VIDEO,
PICTURE
};
enum encoder {
X264,
VASTAI,
NVIDA,
HUAWEI,
OTHER
};
struct InputParameters {
char *filepath_name;
char *outfile_list;
char *render;
int encoder_type;
int process_type;
char *resolution;
int dropframe;
};
class nnet_video_process{
public:
nnet_video_process(string string_ipc_filename,
string rtmp_url,
uint32_t encoder_type,
string renderD,
uint32_t process_type,
string resolution);
~nnet_video_process();
cv::VideoCapture capture;
std::string rtmp_server_url;
std::stringstream command;
cv::Mat frame;
FILE *fp = nullptr;
uint32_t frame_counter ;
uint32_t model_width;
uint32_t model_height;
size_t input_imageLen ;
size_t count_number1;
size_t count_number2;
size_t count_number3;
uint32_t output_count;
uint32_t picture_loop = 0;
vector<string> pic_file_names;
void choice_encoder(uint32_t encoder,string renderD,string resolution);
void draw_box(std::vector<BBox> bbox_info,cv::Mat origin_frame);
void video_process(string string_ipc_filename);
uint32_t nnet_picture_get();
uint32_t get_file_names(string basePath,vector<string> &files);
void Uint82float(uchar *input, float *output, uint32_t imagesize, int batchSize,
std::vector<float> mean_value, std::vector<float> std_value);
void destoryThread();
std::vector<std::thread *> m_ThreadMap;
};
uint32_t nnet_video_process::get_file_names(string basePath,vector &files)
{
DIR *dir;
struct dirent *ptr;
if ((dir=opendir(basePath.c_str())) == NULL)
{
perror("Open dir error...");
exit(1);
}
while ((ptr=readdir(dir)) != NULL)
{
if(strcmp(ptr->d_name,".")==0 || strcmp(ptr->d_name,"..")==0) ///current dir OR parrent dir
continue;
else if(ptr->d_type == 8) ///file
{
string a = ptr->d_name;
int pe = a.find_last_of(".");
string pic_name = a.substr(pe + 1);
if (pic_name=="JPEG" || pic_name == "jpg") //若想获取其他类型文件只需修改jpg为对应后缀
{
string tmpname = basePath + "/" + ptr->d_name;
files.push_back(tmpname);
}
}
else if(ptr->d_type == 4) ///dir
{
string base = basePath + "/" + ptr->d_name;
get_file_names(base,files);
}
}
closedir(dir);
return 1;
}
uint32_t nnet_video_process::nnet_picture_get(){