pref: 增加开启摄像头失败重试功能
This commit is contained in:
57
capture.cc
57
capture.cc
@@ -9,6 +9,21 @@
|
|||||||
#include "logc/log.h"
|
#include "logc/log.h"
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
|
||||||
|
void adjustImageTemperature(cv::Mat &img, float scale)
|
||||||
|
{
|
||||||
|
// 分离通道
|
||||||
|
std::vector<cv::Mat> channels;
|
||||||
|
split(img, channels);
|
||||||
|
|
||||||
|
// 增加蓝色通道和绿色通道的值,减少红色通道的值
|
||||||
|
channels[0] = channels[0] * scale; // 蓝色通道
|
||||||
|
channels[1] = channels[1] * scale; // 绿色通道
|
||||||
|
channels[2] = channels[2] / scale; // 红色通道
|
||||||
|
|
||||||
|
// 合并通道
|
||||||
|
merge(channels, img);
|
||||||
|
}
|
||||||
|
|
||||||
capture::capture(int camera_index, int zmq_port, int width_set, int height_set, int fps_set, bool _flip)
|
capture::capture(int camera_index, int zmq_port, int width_set, int height_set, int fps_set, bool _flip)
|
||||||
{
|
{
|
||||||
index = camera_index;
|
index = camera_index;
|
||||||
@@ -18,22 +33,48 @@ capture::capture(int camera_index, int zmq_port, int width_set, int height_set,
|
|||||||
fps = fps_set;
|
fps = fps_set;
|
||||||
flip = _flip;
|
flip = _flip;
|
||||||
|
|
||||||
log_info("trying open camera %d", index);
|
log_info("尝试开启摄像头 %d", index);
|
||||||
|
|
||||||
cap = new cv::VideoCapture(index, cv::CAP_V4L2);
|
cap = new cv::VideoCapture(index, cv::CAP_V4L2);
|
||||||
|
|
||||||
|
sleep(2); // 等待两秒进行构造
|
||||||
|
|
||||||
cap->set(cv::CAP_PROP_FRAME_WIDTH, width);
|
cap->set(cv::CAP_PROP_FRAME_WIDTH, width);
|
||||||
cap->set(cv::CAP_PROP_FRAME_HEIGHT, height);
|
cap->set(cv::CAP_PROP_FRAME_HEIGHT, height);
|
||||||
cap->set(cv::CAP_PROP_FPS, fps);
|
cap->set(cv::CAP_PROP_FPS, fps);
|
||||||
|
|
||||||
if (!cap->isOpened())
|
// if (10 == camera_index)
|
||||||
|
// {
|
||||||
|
// int fourcc = cv::VideoWriter::fourcc('M', 'J', 'P', 'G'); // 例如,使用 MJPG 编码
|
||||||
|
// // 设置输出视频的分辨率和帧率,与输入视频一致或根据需要调整
|
||||||
|
// cv::Size frameSize = cv::Size((int)cap->get(cv::CAP_PROP_FRAME_WIDTH),
|
||||||
|
// (int)cap->get(cv::CAP_PROP_FRAME_HEIGHT));
|
||||||
|
// int fps = (int)cap->get(cv::CAP_PROP_FPS);
|
||||||
|
// writer = new cv::VideoWriter("/home/evan/Workplace/project_capture/capture.avi", fourcc, fps, frameSize, true);
|
||||||
|
// }
|
||||||
|
|
||||||
|
int cnt = 1;
|
||||||
|
for (int i = 20; i > 0; i--)
|
||||||
{
|
{
|
||||||
log_error("开启摄像头 %d 失败", index);
|
if (cap->isOpened())
|
||||||
|
{
|
||||||
|
status = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
log_error("开启摄像头 %d 失败,重试中", index);
|
||||||
|
cap->open(index, cv::CAP_V4L2);
|
||||||
|
usleep(100000);
|
||||||
status = false;
|
status = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (status)
|
||||||
|
{
|
||||||
|
log_info("开启摄像头 %d 成功", index);
|
||||||
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
log_info("开启摄像头 %d 成功", index);
|
log_fatal("开启摄像头 %d 十次重试后失败", index);
|
||||||
status = true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
context = new zmq::context_t(1);
|
context = new zmq::context_t(1);
|
||||||
@@ -85,6 +126,10 @@ void capture::get(void)
|
|||||||
*cap >> dst;
|
*cap >> dst;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// if(nullptr != writer){
|
||||||
|
// writer->write(dst);
|
||||||
|
// }
|
||||||
|
|
||||||
// 确保图像是连续的
|
// 确保图像是连续的
|
||||||
if (!dst.isContinuous())
|
if (!dst.isContinuous())
|
||||||
{
|
{
|
||||||
@@ -97,6 +142,8 @@ void capture::get(void)
|
|||||||
cv::flip(dst, dst, -1);
|
cv::flip(dst, dst, -1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// adjustImageTemperature(dst, 1.2);
|
||||||
|
|
||||||
// 将图像转换为 rgb
|
// 将图像转换为 rgb
|
||||||
cv::cvtColor(dst, frame, cv::COLOR_BGR2RGB);
|
cv::cvtColor(dst, frame, cv::COLOR_BGR2RGB);
|
||||||
|
|
||||||
|
|||||||
@@ -24,6 +24,7 @@ public:
|
|||||||
cv::Mat frame;
|
cv::Mat frame;
|
||||||
zmq::context_t *context;
|
zmq::context_t *context;
|
||||||
zmq::socket_t *socket;
|
zmq::socket_t *socket;
|
||||||
|
cv::VideoWriter *writer = nullptr;
|
||||||
|
|
||||||
capture(int camera_index, int zmq_port, int width_set = 320, int height_set = 240, int fps_set = 20, bool flip = false);
|
capture(int camera_index, int zmq_port, int width_set = 320, int height_set = 240, int fps_set = 20, bool flip = false);
|
||||||
void start(void);
|
void start(void);
|
||||||
|
|||||||
2
main.cc
2
main.cc
@@ -17,7 +17,7 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
char config_path[200];
|
char config_path[200];
|
||||||
sprintf(config_path, "%s/%s", getcwd(NULL, 0), config_file_path);
|
sprintf(config_path, "%s/%s", getcwd(NULL, 0), config_file_path);
|
||||||
log_info("load config from %s", config_path);
|
log_info("[capture] 从以下路径加载配置:%s", config_path);
|
||||||
fp = fopen(config_path, "r");
|
fp = fopen(config_path, "r");
|
||||||
if (!fp)
|
if (!fp)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user