-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathfileaction.cpp
More file actions
106 lines (89 loc) · 2.55 KB
/
Copy pathfileaction.cpp
File metadata and controls
106 lines (89 loc) · 2.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <thread>
#include <list>
#include <vector>
#include <iostream>
#include <chrono>
#include <ctime>
#include <string>
#include <mutex>
#include <condition_variable>
#include <opencv2/opencv.hpp>
#include "fileaction.h"
#include "settings.h"
using namespace std;
using namespace cv;
FileAction::FileAction():
abort(false)
{
actionThread = new thread(launcher, this);
}
FileAction::~FileAction()
{
list<Mat*>::const_iterator it;
abort = true;
actionThread->join();
delete actionThread;
for (it = imageFIFO.begin(); it != imageFIFO.end(); ++it) {
delete *it;
}
}
void FileAction::handler(const Mat& image)
{
lock_guard<mutex> lock(mtx); // hold the mutex during the execution of handler() {}
// Limit the number of images in the file writer waiting list
if (imageFIFO.size() >= Settings::instance().getMaxFrames()) {
cout << "Warning: Image Fifo overrun, skipping frame.\n";
return;
}
Mat* newImage = new Mat();
image.copyTo(*newImage); // save the image locally
imageFIFO.push_back(newImage); // save its address in the local FIFO
syncCV.notify_one(); // notify the run() thread a new image is ready
}
// Private stuff
void FileAction::launcher(void * instance)
{
static_cast<FileAction*>(instance)->run();
}
void FileAction::run()
{
list<Mat*>::const_iterator pict;
Mat* currentPicture;
time_t tt;
vector<int> jpgParams;
jpgParams.push_back(CV_IMWRITE_JPEG_QUALITY);
jpgParams.push_back(90);
string genericName = "capture_";
string extension = ".jpg";
unsigned fileID = 1;
string dateAndTime;
string filename;
string directory = Settings::instance().getPath();
cout << "FileAction thread started...\n";
while (!abort) {
{
unique_lock<mutex> lock(mtx);
syncCV.wait(lock); // wait until handler() call notify_one()
}
while(!imageFIFO.empty()) {
tt = chrono::system_clock::to_time_t ( chrono::system_clock::now() );
dateAndTime = ctime(&tt);
if (fileID >= Settings::instance().getFPS()) fileID = 1; // Cannot have more than "FPS" image in the same second...
filename = directory +
genericName +
dateAndTime.substr(0, dateAndTime.length()-1) +
"_" +
to_string(fileID++) +
extension;
{
lock_guard<mutex> fifoLock(mtx); // lock the mutex for the current {} section (FIFO access)
currentPicture = imageFIFO.front();
imwrite(filename, *currentPicture, jpgParams);
delete currentPicture;
imageFIFO.pop_front();
}
}
cout << "All image in the FIFO have been written to the disk.\n";
}
cout << "FileAction thread stopped...\n";
}