-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathloopplayer.cpp
110 lines (91 loc) · 1.91 KB
/
loopplayer.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
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
107
108
109
#include "loopplayer.h"
#include <iostream>
LoopPlayer::LoopPlayer(QObject *parent) : QThread(parent)
{
stop = true;
}
LoopPlayer::~LoopPlayer()
{
mutex.lock();
stop = true;
condition.wakeOne();
mutex.unlock();
wait();
}
bool LoopPlayer::loadVideo(std::string filename)
{
cv::VideoCapture capture;
capture.open(filename);
if (capture.isOpened())
{
frameRate = (int)capture.get(CV_CAP_PROP_FPS);
cv::Mat frame;
while (capture.read(frame))
{
if (frame.channels() == 3)
{
cv::cvtColor(frame, frame, CV_BGR2RGB);
}
frames.push_back(frame.clone());
}
capture.release();
return true;
}
return false;
}
void LoopPlayer::play()
{
if (!isRunning())
{
if (isStopped())
{
stop = false;
}
start(LowPriority);
}
}
void LoopPlayer::run()
{
int delay = 1000.0 / frameRate;
auto it = frames.begin();
while (!stop) {
if (it == frames.end())
{
it = frames.begin();
}
cv::Mat frame = *it;
if (frame.channels() == 3)
{
img = QImage((const unsigned char*)(frame.data),
frame.cols,
frame.rows,
QImage::Format_RGB888);
}
else
{
img = QImage((const unsigned char*)(frame.data),
frame.cols,
frame.rows,
QImage::Format_Indexed8);
}
emit processedImage(img);
this->msleep(delay);
it++;
}
}
void LoopPlayer::pause()
{
stop = true;
}
void LoopPlayer::msleep(int ms)
{
struct timespec ts =
{
ms / 1000, (ms % 1000) * 1000 * 1000
};
nanosleep(&ts, NULL);
}
bool LoopPlayer::isStopped() const
{
return stop;
}