This repository has been archived by the owner. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b6effa6
commit 8165178
Showing
5 changed files
with
165 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
#include "videoproducer.h" | ||
|
||
VideoProducer::VideoProducer(QObject *parent) : QObject(parent), | ||
m_videoCapture(nullptr), | ||
m_producer(new QStateMachine(this)), | ||
m_viewerTimer(new QTimer(this)), | ||
m_viewerFlag(false) | ||
{ | ||
qRegisterMetaType<cv::Mat>(); | ||
|
||
// configure video capture | ||
m_videoCapture = new cv::VideoCapture(0); | ||
if (!m_videoCapture->isOpened()) { | ||
emit log(Qt::red, "VideoProducer :: failed to open video capture device"); | ||
return; | ||
} | ||
emit log(Qt::black, "VideoProducer :: camera is initialized"); | ||
|
||
// configure state machine | ||
QState *stateIdle = new QState(m_producer); | ||
QState *stateAcquire = new QState(m_producer); | ||
stateIdle->addTransition(this, &VideoProducer::start, stateAcquire); | ||
stateAcquire->addTransition(this, &VideoProducer::frameReady, stateAcquire); | ||
stateAcquire->addTransition(this, &VideoProducer::stop, stateIdle); | ||
connect(stateAcquire, &QState::entered, this, &VideoProducer::on_acquire); | ||
m_producer->setInitialState(stateIdle); | ||
m_producer->start(); | ||
|
||
// configure viewer timer | ||
m_viewerTimer->setInterval(70); | ||
connect(m_viewerTimer, &QTimer::timeout, [this](){m_viewerFlag = true;}); | ||
connect(this, &VideoProducer::start, m_viewerTimer, QOverload<>::of(&QTimer::start)); | ||
connect(this, &VideoProducer::stop, m_viewerTimer, &QTimer::stop); | ||
|
||
} | ||
|
||
|
||
VideoProducer::~VideoProducer() | ||
{ | ||
if (m_viewerTimer->isActive()) | ||
m_viewerTimer->stop(); | ||
|
||
if (m_producer->isRunning()) | ||
m_producer->stop(); | ||
|
||
if (m_videoCapture != nullptr) { | ||
if (m_videoCapture->isOpened()) | ||
m_videoCapture->release(); | ||
delete m_videoCapture; | ||
} | ||
} | ||
|
||
|
||
void VideoProducer::on_acquire() | ||
{ | ||
cv::Mat frame; | ||
|
||
QCoreApplication::processEvents(); | ||
|
||
if (!m_videoCapture->read(frame)) { | ||
emit log(Qt::red, "VideoProducer :: failed to read next frame"); | ||
m_producer->stop(); | ||
return; | ||
} | ||
|
||
if (m_viewerFlag) { | ||
cv::Mat frame_cv = frame; | ||
cv::resize(frame_cv, frame_cv, cv::Size(), 0.5, 0.5, cv::INTER_AREA); | ||
cv::cvtColor(frame_cv, frame_cv, cv::COLOR_BGR2RGB); | ||
const QImage frame_qt(frame_cv.data, | ||
frame_cv.cols, | ||
frame_cv.rows, | ||
static_cast<int>(frame_cv.step), | ||
QImage::Format_RGB888); | ||
emit frameView(frame_qt); | ||
} | ||
|
||
m_viewerFlag = false; | ||
emit frameReady(frame); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
#ifndef VIDEOPRODUCER_H | ||
#define VIDEOPRODUCER_H | ||
|
||
#include <QObject> | ||
#include <QCoreApplication> | ||
#include <QStateMachine> | ||
#include <QTimer> | ||
#include <QImage> | ||
#include <opencv2/opencv.hpp> | ||
|
||
class VideoProducer : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit VideoProducer(QObject *parent = nullptr); | ||
~VideoProducer(); | ||
|
||
private: | ||
cv::VideoCapture *m_videoCapture; | ||
QStateMachine *m_producer; | ||
QTimer *m_viewerTimer; | ||
bool m_viewerFlag; | ||
|
||
private slots: | ||
void on_acquire(); | ||
|
||
signals: | ||
void log(const Qt::GlobalColor &color, const QString &message); | ||
void frameReady(const cv::Mat &frame); | ||
void frameView(const QImage &frame); | ||
void start(); | ||
void stop(); | ||
|
||
}; | ||
|
||
Q_DECLARE_METATYPE(cv::Mat); | ||
|
||
#endif // VIDEOPRODUCER_H |