| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| |
|
|
| #include <fstream> |
| #include <sstream> |
|
|
| #include <iostream> |
|
|
| #include <windows.h> |
|
|
| #include <opencv2/core/core.hpp> |
| #include <opencv2/highgui/highgui.hpp> |
|
|
| #include <stdio.h> |
| #include <time.h> |
|
|
| #include <filesystem> |
|
|
| #define INFO_STREAM( stream ) \ |
| std::cout << stream << std::endl |
|
|
| #define WARN_STREAM( stream ) \ |
| std::cout << "Warning: " << stream << std::endl |
|
|
| #define ERROR_STREAM( stream ) \ |
| std::cout << "Error: " << stream << std::endl |
|
|
| static void printErrorAndAbort( const std::string & error ) |
| { |
| std::cout << error << std::endl; |
| abort(); |
| } |
|
|
| #define FATAL_STREAM( stream ) \ |
| printErrorAndAbort( std::string( "Fatal error: " ) + stream ) |
|
|
| |
| const std::string currentDateTime() { |
| time_t now = time(0); |
| struct tm tstruct; |
| char buf[80]; |
| localtime_s(&tstruct, &now); |
| |
| |
| strftime(buf, sizeof(buf), "%Y-%m-%d-%H-%M", &tstruct); |
|
|
| return buf; |
| } |
|
|
| std::vector<std::string> get_arguments(int argc, char **argv) |
| { |
|
|
| std::vector<std::string> arguments; |
|
|
| for(int i = 1; i < argc; ++i) |
| { |
| arguments.push_back(std::string(argv[i])); |
| } |
| return arguments; |
| } |
|
|
| int main (int argc, char **argv) |
| { |
|
|
| std::vector<std::string> arguments = get_arguments(argc, argv); |
|
|
| |
| std::string outroot, outfile; |
|
|
| TCHAR NPath[200]; |
| GetCurrentDirectory(200, NPath); |
|
|
| |
| outroot = NPath; |
| outroot = outroot + "/recording/"; |
| outfile = currentDateTime() + ".avi"; |
|
|
| |
| int device = 0; |
|
|
| for (size_t i = 0; i < arguments.size(); i++) |
| { |
| if( strcmp( arguments[i].c_str(), "-dev") == 0 ) |
| { |
| std::stringstream ss; |
| ss << arguments[i+1].c_str(); |
| ss >> device; |
| } |
| else if (strcmp(arguments[i].c_str(), "-r") == 0) |
| { |
| outroot = arguments[i+1]; |
| } |
| else if (strcmp(arguments[i].c_str(), "-of") == 0) |
| { |
| outroot = arguments[i+1]; |
| } |
| else |
| { |
| WARN_STREAM( "invalid argument" ); |
| } |
| i++; |
| } |
|
|
| |
| cv::VideoCapture vCap; |
| INFO_STREAM( "Attempting to capture from device: " << device ); |
| vCap = cv::VideoCapture( device ); |
|
|
| if (!vCap.isOpened()) { |
| FATAL_STREAM("Failed to open video source"); |
| return 1; |
| } |
| |
| cv::Mat img; |
| vCap >> img; |
| |
| std::filesystem::path dir(outroot); |
| std::filesystem::create_directory(dir); |
|
|
| std::string out_file = outroot + outfile; |
| |
| cv::VideoWriter video_writer(out_file, cv::VideoWriter::fourcc('D','I','V','X'), 30, img.size(), true); |
|
|
| std::ofstream outlog; |
| outlog.open((outroot + outfile + ".log").c_str(), std::ios_base::out); |
| outlog << "frame, time(ms)" << std::endl; |
|
|
| double freq = cv::getTickFrequency(); |
|
|
| double init_time = (double)cv::getTickCount(); |
|
|
| int frameProc = 0; |
| while(!img.empty()) |
| { |
| |
| cv::namedWindow("rec",1); |
| |
| vCap >> img; |
| double curr_time = (cv::getTickCount() - init_time) / freq; |
| curr_time *= 1000; |
|
|
| video_writer << img; |
|
|
| outlog << frameProc + 1 << " " << curr_time; |
| outlog << std::endl; |
| |
| |
| cv::imshow("rec", img); |
|
|
| frameProc++; |
| |
| |
| char c = cv::waitKey(1); |
| |
| |
| if(c=='q') |
| { |
| outlog.close(); |
| |
| return(0); |
| } |
|
|
|
|
| } |
| |
| return 0; |
| } |
|
|
|
|