#include #include "../../json_nlohmann/json.hpp" #include #include #include using namespace cv; using json = nlohmann::json; int main(int argc, char **argv) { // Read Config from json file std::string path, video_path; bool save_video; std::ifstream config_file("config_collect.json"); if (config_file.is_open()) { json data = json::parse(config_file); path = data["path"]; if (path[path.length() - 1] != '/') { path = path + "/"; } video_path = data["video_path"]; save_video = data["save_video"]; } else { path = "./"; save_video = false; } // Start Video Capture bool CollectEnabled = false; VideoCapture capture(0); if (!capture.isOpened()) { std::cout << "Can't open video device!" << std::endl; return -1; } // properties double rate = capture.get(CAP_PROP_FPS); double width = capture.get(CAP_PROP_FRAME_WIDTH); double height = capture.get(CAP_PROP_FRAME_HEIGHT); // Save video VideoWriter writer; if (save_video) { writer = VideoWriter(video_path, VideoWriter::fourcc('m', 'p', '4', 'v'), rate, Size(width, height), true); } // Information about the camera std::cout << "Frame Rate = " << rate << "width = " << width << "height = " << height << std::endl; namedWindow("Video Capture", WINDOW_NORMAL); Mat frame; while (1) { // read frame from video capture if (!capture.read(frame)) { std::cout << "no video frame" << std::endl; continue; } // Show the image of frame imshow("Video Capture", frame); // Write frame to file static int count = 0; if (CollectEnabled) { count++; std::string filename = path + std::to_string(count) + ".jpg"; imwrite(filename, frame); // write to video when it enabled if (save_video) { writer.write(frame); } } int c = waitKey(1000 / rate); if (c == 27) { break; } if (c == 99) { CollectEnabled = !CollectEnabled; } } capture.release(); writer.release(); destroyAllWindows(); return 0; }