如何使用OpenCV在2分钟的间隔内从网络摄像头捕获视频
How to capture the video from webcam in the interval of 2 minutes using OpenCV
我正在尝试从网络摄像头(罗技C170)捕获视频。到目前为止,我能够捕获视频并将其保存在.avi容器中。
我的 c++ 代码是这样的
using namespace std;
using namespace cv;
int main(int, char**)
{
VideoCapture capture(0);
.
.
.
.
VideoWriter video("record.avi", CV_FOURCC('M', 'J', 'P', 'G'), frame_rate, Size(frame_width,frame_height), true);
.
.
.
.
Mat frame;
.
.
.
.
for(;;)
{
capture >> frame;
video.write(frame);
}
return 0;
}
我的下一个目标是
Record Sleep Record
|<------------>|<------------>|<------------>| Goes for infinite loop...
3 Minutes 2 Minutes 3 Minutes
| | |
| | |
v v v
save to Cam goes to sleep save to
/records/1.avi /records/2.avi
我正在使用opencv-2.4.3,代码是c ++。
如果需要我提供任何其他信息,请告诉我。
我怎样才能做到这一点?请您提出宝贵的建议?
提前谢谢。
我建议使用时钟来测量时间。
在循环运行时继续检查时钟,一旦达到 3 分钟,就切换到睡眠模式功能,一旦达到 2 分钟,时间再次切换回来。
下面是一些代码,用于对从此 SO 答案中部分蚕食的函数进行计时。
我相信您可以对其进行编辑以保持录制时间,并允许您在录制和睡眠之间切换。
希望这有帮助。
#include <iostream>
#include <cstdio>
#include <ctime>
int main()
{
std::clock_t start;
double duration;
start = std::clock();
// Run your frame grabbing code here //
duration = ( std::clock() - start ) / (double) CLOCKS_PER_SEC;
}
所以,这是解决方案。
#include<iostream>
#include<unistd.h>
#include<sstream>
#include<string>
#include<ctime>
#include<opencv/cv.h>
#include<opencv2/opencv.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/highgui/highgui.hpp>
using namespace std;
using namespace cv;
string currentDateTime() {
time_t now = time(0);
struct tm tstruct;
char buf[80];
tstruct = *localtime(&now);
strftime(buf, sizeof(buf), "%X", &tstruct);
return buf;
}
int main(int, char**)
{
VideoCapture capture(0);
.
.
.
.
int frame_rate = 10;
.
.
.
.
for(int i = 0; i<4 ; i++) // for infinite loop remove iteration
{
time_t timer_start, timer_stop;
Mat frame;
stringstream recordstr;
recordstr << "records/" << i << ".avi";
string recordfile = recordstr.str();
VideoWriter video(recordfile, CV_FOURCC('M', 'J', 'P', 'G'), frame_rate, Size(frame_width,frame_height), true);
cout<<"video "<<i<<".avi started capturing at "<< currentDateTime() <<endl;
time (&timer_start);
for(int j = 0; j < numFrames; j++){
capture >> frame;
if(frame.empty()){
cout << "Failed to capture an image" << endl;
return -1;
}
video.write(frame);
}
time (&timer_stop);
double timeDifference = difftime (timer_stop, timer_start);
cout<<"video "<<i<<".avi stopped capturing at "<< currentDateTime() <<" & took "<<timeDifference<<" seconds"<<endl;
sleep(60);
}
return 0;
}
输出为:
video 0.avi started capturing at 12:09:18
video 0.avi stopped capturing at 12:09:55 & took 37 seconds
video 1.avi started capturing at 12:10:55
video 1.avi stopped capturing at 12:11:32 & took 37 seconds
video 2.avi started capturing at 12:12:32
video 2.avi stopped capturing at 12:13:09 & took 37 seconds
我将不胜感激任何其他解决方案。 所以请发帖,如果有的话。
谢谢
相关文章:
- OpenCV 4.1.2 - 从网络摄像头获取帧并将其拆分
- 使用OpenCV和覆盆子上的多个网络摄像头拍摄延时摄影,出现多个V4L错误
- 使用源读取器从网络摄像头源获取 H264 样本
- 为什么我不能让 3 个网络摄像头与 pthreads 并行运行?
- 从 OpenCV 3 切换到 OpenCV 4 会导致网络摄像头以最大 5 fps 的速度录制,而不是通常的 30 f
- 媒体基金会可以允许多个客户端同时访问单个网络摄像头设备吗?
- 从C++调用网络摄像头并在 Python 中显示图像
- 从 WinPE 下的网络摄像头捕获?
- OpenCV没有显示来自Macbook网络摄像头的视频
- 使用OpenCV和C++在按键上截取网络摄像头源的屏幕截图
- 我正在尝试在树莓派中连接两个网络摄像头
- 使用 FFmpeg 编码和带有网络摄像头的 UDP
- TensorFlow OpenCV网络摄像头悬挂
- 使用来自视频文件或网络摄像头的 Kinect 面部跟踪
- OpenCV 将剪影与网络摄像头流隔离
- OpenCV-从C/C++中的默认网络摄像头获取图片-GTK问题
- 在 OpenCV 中,设置曝光会导致我的网络摄像头软件无法自动调整曝光
- 无法使用OpenCV从辅助网络摄像头读取VideoCapture中的帧
- 使用Opencv直接从网络摄像头获取灰度图像
- 使用Qt显示来自OpenCV的网络摄像头流