Repository navigation
Expand file tree
/
Copy pathloopplayer.cpp
More file actions
108 lines (92 loc) · 2.28 KB
/
Copy pathloopplayer.cpp
File metadata and controls
108 lines (92 loc) · 2.28 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 "loopplayer.h"
#include <iostream>
#include <chrono>
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())
{
fps = static_cast<int>(capture.get(CV_CAP_PROP_FPS));
frameCount = static_cast<int>(capture.get(CV_CAP_PROP_FRAME_COUNT));
duration = static_cast<long>(double(frameCount) / double(fps) * 1000);
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::setPlaybackRate(double rate) {
playbackRate = rate;
}
long LoopPlayer::getDuration() const
{
return duration;
}
bool LoopPlayer::isStopped() const
{
return stop;
}
void LoopPlayer::play()
{
stop = stop ? false : true;
if (!isRunning()) {
start(LowPriority);
}
}
void LoopPlayer::pause()
{
stop = true;
}
void LoopPlayer::run()
{
while (true)
{
if (!stop)
{
if (currentFrameIndex >= frameCount) {
currentFrameIndex = 0;
}
auto it = frames.begin() + currentFrameIndex;
cv::Mat frame = *it;
if (frame.channels() == 3)
{
img = QImage(const_cast<const unsigned char*>(frame.data),
frame.cols,
frame.rows,
QImage::Format_RGB888);
}
else
{
img = QImage(const_cast<const unsigned char*>(frame.data),
frame.cols,
frame.rows,
QImage::Format_Indexed8);
}
emit processedImage(img);
currentFrameIndex++;
}
int delay = int(1000.0 / fps * playbackRate);
std::this_thread::sleep_for(std::chrono::milliseconds(delay));
}
}