-
Notifications
You must be signed in to change notification settings - Fork 1
/
WebcamPassthrough.hpp
93 lines (75 loc) · 3.18 KB
/
WebcamPassthrough.hpp
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
#include<opencv2/opencv.hpp>
#include <thread>
#include "PriDrawable.hpp"
#include <X11/Xlib.h>
#include <queue>
class WebcamPassthrough : public PriDrawable {
private:
cv::VideoCapture cap;
sf::Image image;
int width, height;
int counter;
std::queue<cv::Mat> imgqueue;
int buffer_length;
int displayedTextureIndex;
public:
WebcamPassthrough(int width, int height) : PriDrawable(0) {
cap = cv::VideoCapture(-1);
this->width = width;
buffer_length = 20;
this->height = height;
counter = 0;
displayedTextureIndex = 0;
currentTextureIndex = 0;
if(!cap.isOpened()) {
return;
}
cap.set(CV_CAP_PROP_FRAME_WIDTH,100);
cap.set(CV_CAP_PROP_FRAME_HEIGHT,75);
auto newthread = std::thread(&WebcamPassthrough::frameGrabberThread, this);
newthread.detach();
}
void frameGrabberThread() {
cv::Mat capture, capture2;
counter = 0;
while(textures.size() < buffer_length) {
sf::Texture * vidframe = new sf::Texture();
cap >> capture;
cv::cvtColor(capture,capture2,cv::COLOR_BGR2RGBA);
image.create(capture2.cols, capture2.rows, capture2.ptr());
vidframe->loadFromImage(image);
textures.push_back(vidframe);
//std::cout << "added frame to buffer. size is now " << textures.size() << std::endl;
}
currentTextureIndex = 0;
while(1) {
//for(int i = 0; i < 4; i++) {
cap >> capture;
//}
cv::cvtColor(capture,capture2,cv::COLOR_BGR2RGBA);
//image.create(capture2.cols, capture2.rows, capture2.ptr());
image.create(capture2.cols, capture2.rows, capture2.ptr());
//std::cout << "Gonna try and update..." << ((currentTextureIndex - 3) + buffer_length) % buffer_length << std::endl;
textures[((currentTextureIndex + 1) + buffer_length) % buffer_length]->update(image);
//std::cout << "updated buffer at index " << ((currentTextureIndex - 3) + buffer_length) % buffer_length << std::endl;
currentTextureIndex = (currentTextureIndex + 1) % buffer_length;
}
}
void animate() {
// auto thing = sf::seconds(1);
// sf::sleep(thing);
//cv::resize(frameRGBA, frameRGBA, cv::Size(), 1, 1);
if(textures.size() >= buffer_length) {
std::cout << "Set texture at " << currentTextureIndex << std::endl;
//std::this_thread::sleep_for(std::chrono::milliseconds(10));
setTexture(*(textures[(currentTextureIndex) % buffer_length]));
//displayedTextureIndex = currentTextureIndex;
float xscale = (float)width / image.getSize().x;
float yscale = (float)height / image.getSize().y;
setScale(xscale, yscale);
setPosition(width / 2.0, height / 2.0);
setOrigin(textures[currentTextureIndex]->getSize().x / 2, textures[currentTextureIndex]->getSize().y / 2);
scale(-1, 1);
}
}
};