Skip to content
Snippets Groups Projects
Commit aaaaf3fa authored by am0ebe's avatar am0ebe
Browse files

add frameProcessor class, to process the frame data asynchronously.

parent 60f30d51
No related branches found
No related tags found
No related merge requests found
...@@ -8,12 +8,14 @@ SOURCES += \ ...@@ -8,12 +8,14 @@ SOURCES += \
../src/cmd/console.cpp \ ../src/cmd/console.cpp \
../src/cmd/core.cpp \ ../src/cmd/core.cpp \
../src/cmd/frameObserver.cpp \ ../src/cmd/frameObserver.cpp \
../src/cmd/frameProcessor.cpp \
../src/utils.cpp ../src/utils.cpp
HEADERS += \ HEADERS += \
../src/cmd/console.h \ ../src/cmd/console.h \
../src/cmd/core.h \ ../src/cmd/core.h \
../src/cmd/frameObserver.h \ ../src/cmd/frameObserver.h \
../src/cmd/frameProcessor.h \
../src/utils.h ../src/utils.h
DEFINES *= QT_USE_QSTRINGBUILDER #converts + to % when building strings DEFINES *= QT_USE_QSTRINGBUILDER #converts + to % when building strings
...@@ -29,6 +31,7 @@ LIBS += -L$${VIMBA_LIB_DIR} -lVmbC -lVmbCPP -Wl,-rpath,\'\$$ORIGIN\' ...@@ -29,6 +31,7 @@ LIBS += -L$${VIMBA_LIB_DIR} -lVmbC -lVmbCPP -Wl,-rpath,\'\$$ORIGIN\'
#-Wl,-rpath,. #-Wl,-rpath,.
QMAKE_POST_LINK += cp $${VIMBA_LIB_DIR}/lib*.so ../bin # copy vmb-libs to bin QMAKE_POST_LINK += cp $${VIMBA_LIB_DIR}/lib*.so ../bin # copy vmb-libs to bin
QMAKE_CXXFLAGS += -Wno-deprecated-enum-enum-conversion # ignore opencv warnings.
#PRE_LINK: cp vimba libs once #PRE_LINK: cp vimba libs once
# cp ../b/vimbax/api/lib/GenICam/*.so $${VIMBA_LIB_DIR} # cp ../b/vimbax/api/lib/GenICam/*.so $${VIMBA_LIB_DIR}
......
#include "core.h" #include "core.h"
#include "VmbC/VmbCommonTypes.h" #include "VmbC/VmbCommonTypes.h"
#include "VmbCPP/SharedPointerDefines.h" #include "VmbCPP/SharedPointerDefines.h"
#include "frameObserver.h" #include "frameobserver.h"
#include "../utils.h" #include "../utils.h"
#include <iostream> #include <iostream>
...@@ -203,8 +203,8 @@ void Core::record() ...@@ -203,8 +203,8 @@ void Core::record()
if ( f(curCam->GetFeatureByName ("PayloadSize", pFeature ),"get PayloadSize") ) if ( f(curCam->GetFeatureByName ("PayloadSize", pFeature ),"get PayloadSize") )
return; // emit error("Couldn't get payload size 1"); return; // emit error("Couldn't get payload size 1");
if( pFeature == nullptr ) // if( pFeature == nullptr )
return; // emit error("Couldn't get payload size 2"); // return; // emit error("Couldn't get payload size 2");
if( f(pFeature->GetValue (nPLS))) if( f(pFeature->GetValue (nPLS)))
return; return;
......
#include "frameObserver.h" #include "frameobserver.h"
#include "frameprocessor.h"
#include "qnamespace.h"
#include <VmbCPP/SharedPointerDefines.h> #include <VmbCPP/SharedPointerDefines.h>
#include <VmbCPP/Camera.h> #include <VmbCPP/Camera.h>
#include <QDebug> #include <QDebug>
#include <QThread>
#include <QObject>
using namespace VmbCPP; using namespace VmbCPP;
...@@ -14,24 +20,34 @@ FrameObserver::FrameObserver( CameraPtr pCamera ) : IFrameObserver( pCamera ) ...@@ -14,24 +20,34 @@ FrameObserver::FrameObserver( CameraPtr pCamera ) : IFrameObserver( pCamera )
} }
// Frame callback notifies about incoming frames // Frame callback notifies about incoming frames
// process data afap
// some functions are prohibited within this callback!
// Do not apply image processing within this callback ( performance )
// -> create frame processor to handle frame processing asynchronously
// When the frame has been processed , requeue it
void FrameObserver::FrameReceived ( const FramePtr pframe ) void FrameObserver::FrameReceived ( const FramePtr pframe )
{ {
VmbUint64_t timestamp;
qDebug() << __LINE__ << "-" << __PRETTY_FUNCTION__ << ""; qDebug() << __LINE__ << "-" << __PRETTY_FUNCTION__ << "";
pframe->GetTimestamp(timestamp);
qDebug() << "FrameObserver::FrameReceived():" << timestamp;
// process data afap FrameProcessor *processor = new FrameProcessor(pframe, this);
// some functions are prohibited within this callback
QThread *thread = new QThread;
processor->moveToThread(thread);
// XXX connect(thread, &QThread::started, processor, &FrameProcessor::processFrame);
// ensure framedata is valid: connect(processor, &FrameProcessor::frameProcessed, processor, &FrameProcessor::deleteLater);
// Frame::GetReceiveStatus == VmbFrameStatusComplete connect(processor, &FrameProcessor::frameProcessed, thread, &QThread::quit);
// pframe->GetReceiveStatus(VmbFrameStatusType &status) connect(processor, &FrameProcessor::frameProcessed, this, &FrameObserver::queueFrame); // Connect to a slot in FrameObserver
connect(thread, &QThread::finished, thread, &QThread::deleteLater);
// Send notification to working thread thread->start();
// Do not apply image processing within this callback ( performance )
// When the frame has been processed , requeue it
m_pCamera->QueueFrame( pframe ); m_pCamera->QueueFrame( pframe );
} }
void FrameObserver::queueFrame(FramePtr pFrame)
{
m_pCamera->QueueFrame(pFrame); // Queue the frame back to the camera
}
#pragma once #pragma once
#include <QObject>
#include <VmbCPP/IFrameObserver.h> #include <VmbCPP/IFrameObserver.h>
namespace VmbCPP{ // using VmbCPP::CameraPtr;
// using VmbCPP::FramePtr;
// using VmbCPP::IFrameObserver;
using namespace VmbCPP;
class FrameObserver : public IFrameObserver class FrameObserver : public QObject, public IFrameObserver
{ {
Q_OBJECT
public: public:
void FrameReceived( const FramePtr pFrame ); void FrameReceived( const FramePtr pFrame );
FrameObserver( CameraPtr pCamera ); FrameObserver( CameraPtr pCamera );
private slots:
void queueFrame(FramePtr pFrame);
}; };
}
#include "frameprocessor.h"
#include "opencv2/imgcodecs.hpp"
#include <opencv2/opencv.hpp>
#include <VmbC/VmbCommonTypes.h>
#include <VmbCPP/Frame.h>
#include <QDebug>
#include <QThread>
#include <QObject>
#include <QImage>
FrameProcessor::FrameProcessor(FramePtr pframe, QObject *parent) : QObject(parent), m_pframe(pframe)
{}
//2d check image format!
//2d add camera name to filename
/// Process the received frame asynchronously
void FrameProcessor::processFrame()
{
// process frames receiveStatus
VmbFrameStatusType status;
m_pframe->GetReceiveStatus(status);
if (status != VmbFrameStatusComplete)
{
qDebug() << "Frame not complete, skip processing.";
return;
}
VmbUint32_t width = 0;
VmbUint32_t height = 0;
unsigned char* pdata;
VmbPixelFormatType pixelFormat;
VmbUint64_t timestamp;
m_pframe->GetWidth(width);
m_pframe->GetHeight(height);
m_pframe->GetBuffer(pdata);
m_pframe->GetPixelFormat(pixelFormat);
m_pframe->GetTimestamp(timestamp);
cv::Mat frameMat(height, width, CV_8UC3, pdata);
if (pixelFormat == VmbPixelFormatRgb8)
{
// If the frame pixel format is RGB8, directly copy the data
// memcpy(frameMat.data, pData, width * height * 3);
qDebug() << "Pixel format is RGB8";
}
else
{
// If the pixel format is not already in BGR format, you may need to convert it
// cv::Mat tempMat(height, width, CV_8UC1, pdata); //UC1??
// cv::cvtColor(tempMat, frameMat, cv::COLOR_BayerGB2BGR); // Adjust the conversion based on the actual Bayer pattern
qDebug() << "Pixel format is not RGB8, converting to BGR";
}
std::vector<int> params = {cv::IMWRITE_JPEG_QUALITY, 99, cv::IMWRITE_JPEG_OPTIMIZE, 1, cv::IMWRITE_JPEG_RST_INTERVAL,4};
QString filename = QString::number(timestamp) + "frame.jpg";
cv::imwrite(filename.toStdString(), frameMat, params);
qDebug() << "Frame saved as JPEG: " << filename;
emit frameProcessed(m_pframe);
}
#include <QObject>
#include <VmbCPP/SharedPointerDefines.h>
using VmbCPP::FramePtr;
class FrameProcessor : public QObject
{
Q_OBJECT
public:
explicit FrameProcessor(FramePtr, QObject *parent = nullptr);
public slots:
void processFrame();
signals:
void frameProcessed(FramePtr);
private:
FramePtr m_pframe;
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment