#include "opencv2/opencv.hpp"
#include <time.h>
#include<fstream>
#include<windows.h>
#include "opencv2/highgui/highgui.hpp"
#include<String>
#include<thread>
#include "queue.h"
using namespace cv;
using namespace std;
cycleQueue<Mat> q(1000);
void getvideo(String& sc,double fps,Size framesize,bool &flag,bool &thread_exit)
{
cout << "thread:" << sc << endl;
String file = sc + ".csv";
ofstream fc(file.c_str(),ios::out);
sc = sc + String(".avi");
fc << "Frame,DateTime" << "\n";
long num = 0;
char timestamp[60] = { 0 };
VideoWriter writer(sc, VideoWriter::fourcc('M', 'J', 'P', 'G'),fps,framesize);
Mat frame;
SYSTEMTIME tt;
while (!flag)
{
if (q.isEmpty()) {
cout << "q is empty "<< flag<< endl;
continue;
}
frame=q.pop();
if (!frame.empty())
{
GetLocalTime(&tt);
snprintf(timestamp, 60, "%ld,%d-%02d-%02d %02d:%02d:%02d %06d", num++, tt.wYear, tt.wMonth, tt.wDay,
tt.wHour, tt.wMinute, tt.wSecond, tt.wMilliseconds* 1000);
fc << timestamp<<"\n";
writer << frame;
imshow("video", frame);
if (waitKey(20) == 'q')
{
thread_exit = true;
break;
}
}
}
destroyAllWindows();
destroyWindow("video");
//cout << "44444444444444" << endl;
//continue record remain video
while (!q.isEmpty())
{
frame = q.pop();
if (q.isEmpty()) continue;
if (!frame.empty())
{
GetLocalTime(&tt);
snprintf(timestamp, 60, "%ld,%d-%02d-%02d %02d:%02d:%02d %06d", num++, tt.wYear, tt.wMonth, tt.wDay,
tt.wHour, tt.wMinute, tt.wSecond, tt.wMilliseconds * 1000);
fc << timestamp << "\n";
writer << frame;
}
}
cout << "thread end" << endl;
}
int main(int argc, char** argv)
{
VideoCapture video(1);
// With webcam get(CV_CAP_PROP_FPS) does not work.
// Let's see for ourselves
double fps = video.get(CV_CAP_PROP_FPS);
video.set(CV_CAP_PROP_FRAME_WIDTH, 1920);
video.set(CV_CAP_PROP_FRAME_HEIGHT, 1080);
video.set(CV_CAP_PROP_FPS, 30);
video.set(CV_CAP_PROP_FOURCC, VideoWriter::fourcc('M', 'J', 'P', 'G'));
int width = video.get(CV_CAP_PROP_FRAME_WIDTH);
int height = video.get(CV_CAP_PROP_FRAME_HEIGHT);
// If you do not care about backward compatibility
// You can use the following instead for OpenCV 3
// double fps = video.get(CAP_PROP_FPS);
cout << " fps: " << fps
<<"width: "<<video.get(CV_CAP_PROP_FRAME_WIDTH)
<<"height: "<<video.get(CV_CAP_PROP_FRAME_HEIGHT)
<< "fourcc: "<<video.get(CV_CAP_PROP_FOURCC) << endl;
// Number of frames to capture
int num_frames = 10;
// Start and end times
time_t start, end;
// Variable for storing video frames
Mat frame;
bool flag = false, thread_exit = false;
cout << "Capturing " << num_frames << " frames" << endl;
// Start time
time(&start);
SYSTEMTIME tt;
GetLocalTime(&tt);
char filename[20] = {0};
snprintf(filename, 20, "%d%02d%02d%02d%02d%02d", tt.wYear, tt.wMonth, tt.wDay,
tt.wHour, tt.wMinute, tt.wSecond);
ofstream fs;
fs.open("total.txt");
// Grab a few frames
Size size(width, height);
String sc(filename);
thread t(getvideo, ref(sc), fps, size, ref(flag),ref(thread_exit));
char temp[60] = {0};
int key = 0;
while (video.isOpened())
{
GetLocalTime(&tt);
snprintf(temp,60 , "%d-%02d-%02d,%02d:%02d:%02d %06d", tt.wYear, tt.wMonth, tt.wDay,
tt.wHour, tt.wMinute, tt.wSecond, tt.wMilliseconds*1000);
fs << temp << "\n";
video.read(frame);
q.push(frame);
if (thread_exit) break;
// cout << "press is:" <<waitKey(1000) << endl;
}
flag = true;
// End Time
time(&end);
// Time elapsed
double seconds = difftime(end, start);
cout << "Time taken : " << seconds << " seconds" << endl;
// Calculate frames per second
fps = num_frames / seconds;
cout << "Estimated frames per second : " << fps << endl;
// Release video
t.join();
video.release();
cout << "main end " << endl;
return 0;
}
usb摄像头实时播放和录制
需积分: 0 92 浏览量
2023-03-08
19:17:00
上传
评论
收藏 89.19MB RAR 举报
小娜君
- 粉丝: 2
- 资源: 1
最新资源
- 20240420-扬州高中小学部风雨操场转换层条件图r.dwg
- 小猫咪邮件在线发送系统源码v1.1,支持添加附件
- 永磁电机铜耗估算表-Excel-v1.0
- 参考资料-人工智能对劳动力市场的影响机制研究.pdf
- 协同供应链集成产品介绍V71sp1.rar
- 上市公司-人工智能的采纳程度面板数据(2003-2021年).xlsx
- 参考资料-人工智能技术应用对就业的影响及作用机制研究-来自制造业企业的微观证据.pdf
- 第5章spring-mvc请求映射处理
- 2023-04-06-项目笔记 - 第一百十六阶段 - 4.4.2.114全局变量的作用域-114 -2024.04.27
- 协同供应链集成产品介绍V70.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈