-
Notifications
You must be signed in to change notification settings - Fork 0
/
provide.cpp
76 lines (64 loc) · 1.84 KB
/
provide.cpp
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
/**
* Copyright (c) 2012 Andreas Heider, Julius Adorf, Markus Grimm
*
* MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
#include "tpofinder/provide.h"
#include <iostream>
using namespace cv;
using namespace std;
namespace tpofinder {
void WebcamImageProvider::capture_loop() {
cv::Mat buffer;
while (!stop_) {
capture_ >> buffer;
image_mutex_.lock();
image_ = buffer;
ready_.notify_one();
image_mutex_.unlock();
}
}
WebcamImageProvider::WebcamImageProvider() : capture_(0), image_mutex_(),
image_(), ready_(), stop_(false) {
if (!capture_.isOpened()) {
cerr << "Could not open default camera." << endl;
exit(-1);
}
worker_ = new thread(&WebcamImageProvider::capture_loop, std::ref(*this));
}
WebcamImageProvider::~WebcamImageProvider() {
stop_ = true;
worker_->join();
delete worker_;
}
bool WebcamImageProvider::next(cv::Mat &image) {
std::unique_lock<std::mutex> lock(image_mutex_);
ready_.wait(lock);
image = image_;
return true;
}
bool StdinFilenameImageProvider::next(Mat &image) {
string s;
getline(cin, s);
if (s.empty()) {
return false;
} else {
image = imread(s);
}
if (image.empty()) {
return false;
} else {
return true;
}
}
ListFilenameImageProvider::ListFilenameImageProvider(const vector<string>
&files) : num_(0), files_(files) {}
bool ListFilenameImageProvider::next(Mat &image) {
if (num_ < files_.size()) {
string f = files_[num_++];
image = imread(f);
} else {
return false;
}
}
}