pref: 增加开启摄像头失败重试功能
This commit is contained in:
57
capture.cc
57
capture.cc
@@ -9,6 +9,21 @@
|
||||
#include "logc/log.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)
|
||||
{
|
||||
index = camera_index;
|
||||
@@ -18,22 +33,48 @@ capture::capture(int camera_index, int zmq_port, int width_set, int height_set,
|
||||
fps = fps_set;
|
||||
flip = _flip;
|
||||
|
||||
log_info("trying open camera %d", index);
|
||||
log_info("尝试开启摄像头 %d", index);
|
||||
|
||||
cap = new cv::VideoCapture(index, cv::CAP_V4L2);
|
||||
|
||||
sleep(2); // 等待两秒进行构造
|
||||
|
||||
cap->set(cv::CAP_PROP_FRAME_WIDTH, width);
|
||||
cap->set(cv::CAP_PROP_FRAME_HEIGHT, height);
|
||||
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;
|
||||
}
|
||||
|
||||
if (status)
|
||||
{
|
||||
log_info("开启摄像头 %d 成功", index);
|
||||
}
|
||||
else
|
||||
{
|
||||
log_info("开启摄像头 %d 成功", index);
|
||||
status = true;
|
||||
log_fatal("开启摄像头 %d 十次重试后失败", index);
|
||||
}
|
||||
|
||||
context = new zmq::context_t(1);
|
||||
@@ -85,6 +126,10 @@ void capture::get(void)
|
||||
*cap >> dst;
|
||||
}
|
||||
|
||||
// if(nullptr != writer){
|
||||
// writer->write(dst);
|
||||
// }
|
||||
|
||||
// 确保图像是连续的
|
||||
if (!dst.isContinuous())
|
||||
{
|
||||
@@ -97,6 +142,8 @@ void capture::get(void)
|
||||
cv::flip(dst, dst, -1);
|
||||
}
|
||||
|
||||
// adjustImageTemperature(dst, 1.2);
|
||||
|
||||
// 将图像转换为 rgb
|
||||
cv::cvtColor(dst, frame, cv::COLOR_BGR2RGB);
|
||||
|
||||
|
||||
@@ -24,6 +24,7 @@ public:
|
||||
cv::Mat frame;
|
||||
zmq::context_t *context;
|
||||
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);
|
||||
void start(void);
|
||||
|
||||
Reference in New Issue
Block a user