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
af68833
commit 6b32baf
Showing
12 changed files
with
352 additions
and
7 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
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,26 @@ | ||
#include "videoconverter.h" | ||
|
||
VideoConverter::VideoConverter(QObject *parent) : QObject(parent) | ||
{ | ||
|
||
} | ||
|
||
void VideoConverter::on_convertFrame(const cv::Mat &frame) | ||
{ | ||
cv::Mat frame_cv = frame; | ||
QPixmap pixmap; | ||
|
||
cv::resize(frame_cv, frame_cv, cv::Size(), 0.5, 0.5, cv::INTER_AREA); | ||
cv::cvtColor(frame_cv, frame_cv, CV_BGR2RGB); | ||
const QImage frame_qt(frame_cv.data, | ||
frame_cv.cols, | ||
frame_cv.rows, | ||
static_cast<int>(frame_cv.step), | ||
QImage::Format_RGB888); | ||
|
||
|
||
pixmap = QPixmap::fromImage(frame_qt); | ||
|
||
//emit imageReady(frame_qt); | ||
emit pixmapReady(pixmap); | ||
} |
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,25 @@ | ||
#ifndef VIDEOCONVERTER_H | ||
#define VIDEOCONVERTER_H | ||
|
||
#include <QObject> | ||
#include <QImage> | ||
#include <QPixmap> | ||
#include <opencv2/opencv.hpp> | ||
|
||
class VideoConverter : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit VideoConverter(QObject *parent = nullptr); | ||
|
||
public slots: | ||
void on_convertFrame(const cv::Mat &frame); | ||
|
||
signals: | ||
//void imageReady(const QImage &frame); | ||
void pixmapReady(const QPixmap &pixmap); | ||
|
||
}; | ||
|
||
#endif // VIDEOCONVERTER_H |
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,69 @@ | ||
#include "videoproducer.h" | ||
|
||
VideoProducer::VideoProducer(QObject *parent) : QObject(parent), | ||
m_frameIndex(0), | ||
m_frameWidth(0), | ||
m_frameHeight(0), | ||
m_fps(0.0) | ||
{ | ||
qRegisterMetaType<cv::Mat>(); | ||
|
||
// configure state machine | ||
m_producer = new QStateMachine(this); | ||
QState *state_idle = new QState(m_producer); | ||
QState *state_acquire = new QState(m_producer); | ||
state_idle->addTransition(this, &VideoProducer::start, state_acquire); | ||
state_acquire->addTransition(this, &VideoProducer::stop, state_idle); | ||
state_acquire->addTransition(this, &VideoProducer::frameReady, state_acquire); | ||
connect(state_acquire, &QState::entered, this, &VideoProducer::on_readFrame); | ||
m_producer->setInitialState(state_idle); | ||
m_producer->start(); | ||
|
||
// configure video capture | ||
m_videoCapture = new cv::VideoCapture(0); | ||
if (!m_videoCapture->isOpened()) | ||
{ | ||
m_producer->stop(); | ||
error("VideoProducer", "failed to open video capture device"); | ||
return; | ||
} | ||
|
||
// share frame properties | ||
m_fps = m_videoCapture->get(CV_CAP_PROP_FPS); | ||
m_frameWidth = static_cast<qint32>(m_videoCapture->get(CV_CAP_PROP_FRAME_WIDTH)); | ||
m_frameHeight = static_cast<qint32>(m_videoCapture->get(CV_CAP_PROP_FRAME_HEIGHT)); | ||
connect(this, &VideoProducer::start, [this](){emit frameProperties(m_fps, m_frameWidth, m_frameHeight);}); | ||
} | ||
|
||
|
||
|
||
VideoProducer::~VideoProducer() | ||
{ | ||
if (m_producer->isRunning()) | ||
m_producer->stop(); | ||
|
||
if (m_videoCapture->isOpened()) | ||
m_videoCapture->release(); | ||
delete m_videoCapture; | ||
} | ||
|
||
|
||
|
||
void VideoProducer::on_readFrame() | ||
{ | ||
cv::Mat frame; | ||
|
||
QCoreApplication::processEvents(); | ||
|
||
if (!m_videoCapture->read(frame)) | ||
{ | ||
m_producer->stop(); | ||
error("VideoProducer", "failed to read next frame"); | ||
return; | ||
} | ||
|
||
m_frameIndex++; | ||
|
||
emit frameIndex(m_frameIndex); | ||
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,40 @@ | ||
#ifndef VIDEOPRODUCER_H | ||
#define VIDEOPRODUCER_H | ||
|
||
#include <QObject> | ||
#include <QCoreApplication> | ||
#include <QStateMachine> | ||
#include <opencv2/opencv.hpp> | ||
#include <QDebug> | ||
|
||
class VideoProducer : public QObject | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit VideoProducer(QObject *parent = nullptr); | ||
~VideoProducer(); | ||
|
||
private: | ||
quint32 m_frameIndex; | ||
qint32 m_frameWidth; | ||
qint32 m_frameHeight; | ||
double m_fps; | ||
cv::VideoCapture *m_videoCapture; | ||
QStateMachine *m_producer; | ||
|
||
private slots: | ||
void on_readFrame(); | ||
|
||
signals: | ||
void start(); | ||
void stop(); | ||
void frameIndex(quint32 index); | ||
void frameReady(const cv::Mat &frame); | ||
void frameProperties(double fps, int width, int height); | ||
void error(const QString &errorSource, const QString &errorMessage); | ||
}; | ||
|
||
Q_DECLARE_METATYPE(cv::Mat) | ||
|
||
#endif // VIDEOPRODUCER_H |
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,15 @@ | ||
#include "videoviewer.h" | ||
|
||
VideoViewer::VideoViewer(QWidget *parent) : QGraphicsView (parent) | ||
{ | ||
m_scene = new QGraphicsScene(this); | ||
m_pixmap = new QGraphicsPixmapItem(); | ||
this->setScene(m_scene); | ||
m_scene->addItem(m_pixmap); | ||
} | ||
|
||
void VideoViewer::on_pixmapReady(const QPixmap &pixmap) | ||
{ | ||
m_pixmap->setPixmap(pixmap); | ||
this->fitInView(m_pixmap, Qt::KeepAspectRatio); | ||
} |
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,26 @@ | ||
#ifndef VIDEOVIEWER_H | ||
#define VIDEOVIEWER_H | ||
|
||
#include <QObject> | ||
#include <QWidget> | ||
#include <QGraphicsView> | ||
#include <QGraphicsScene> | ||
#include <QGraphicsPixmapItem> | ||
|
||
class VideoViewer : public QGraphicsView | ||
{ | ||
Q_OBJECT | ||
|
||
public: | ||
explicit VideoViewer(QWidget *parent = nullptr); | ||
|
||
private: | ||
QGraphicsScene *m_scene; | ||
QGraphicsPixmapItem *m_pixmap; | ||
|
||
public slots: | ||
void on_pixmapReady(const QPixmap &image); | ||
|
||
}; | ||
|
||
#endif // VIDEOVIEWER_H |
Oops, something went wrong.