Skip to content
Snippets Groups Projects
Select Git revision
  • f5573fdc5f13f2292180d8ac3fb41b9ffdd4c183
  • master default protected
  • cali
  • dev protected
4 results

frameprocessor.cpp

Blame
  • user avatar
    am0ebe authored
    speedup: 	use threadpool and sublass frameprocessor from qrunnable -> reuse thread and frameprocessor objects for recording
    8f15e7ad
    History
    Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    frameprocessor.cpp 2.19 KiB
    #include "frameprocessor.h"
    #include "frameobserver.h"
    #include "utils.h"
    
    #include <opencv2/imgcodecs.hpp>
    #include <opencv2/opencv.hpp>
    
    #include <VmbC/VmbCommonTypes.h>
    #include <VmbCPP/Frame.h>
    
    #include <QDebug>
    #include <QThread>
    #include <QImage>
    #include <QDir>
    
    FrameProcessor::FrameProcessor(const QString& outDir) : QRunnable(),
    // _pframe(pframe),
    _outDir( outDir ), //xx make class static
    _pixelFormatStr("RGB8"),
    _params({cv::IMWRITE_JPEG_QUALITY, 99, cv::IMWRITE_JPEG_OPTIMIZE, 1, cv::IMWRITE_JPEG_RST_INTERVAL,4}),
    _width(0),
    _height(0),
    _timestamp(0)
    {
    	setAutoDelete(false);
    }
    
    FrameProcessor::~FrameProcessor()
    {
    	static VmbUint64_t count = 0;
    	qDebug() << __PRETTY_FUNCTION__ << count++;
    }
    
    void FrameProcessor::setFrame(const FramePtr pframe)
    {
    	_pframe = pframe;
    }
    
    // Process the received frame asynchronously
    // pre: Frame needs to be valid!
    // post: frame needs to be requeued
    void FrameProcessor::run()
    {
    	static VmbUint64_t _count = 0; //all cams, all processors, totalframes
    	++_count;
    
    	unsigned char* pdata = nullptr;
    	_pframe->GetBuffer(pdata);
    	_pframe->GetTimestamp(_timestamp);
    	_pframe->GetWidth(_width);
    	_pframe->GetHeight(_height);
    	_pframe->GetPixelFormat(_pixelFormat);
    
    	if (_pixelFormat != VmbPixelFormatRgb8)
    		qWarning() << "⚠️  Pixel format is not RGB8, 2D: implement convert func ... ";
    
    
    	cv::Mat frameMat(_height, _width, CV_8UC3, pdata);
    
    	QString filename = _outDir + QString::number(_timestamp) + ".jpg";
    	bool ret = cv::imwrite(filename.toStdString(), frameMat, _params);
    	if (!ret)
    		qWarning() << "⚠️  Could not write frame to file: " << filename;
    
    
    	// auto dur_ms = static_cast<int>(_timestamp - _prev_timestamp) / 1e6; //from ns to ms
    	// qDebug() << "rcvd Frame #" << _count << ": dur in ms" << dur_ms << ", timestamp: " << _timestamp;
    
    
    	// qDebug() << "rcvd Frame #" << _count << ": " << _width << "x" << _height << "px, format: " << _pixelFormatStr << ", timestamp: " << _timestamp << ", file: " << filename;
    // #ifdef DEBUG
    // 	QThread::msleep(2000); //simulate processing time // testing
    // #endif
    
    	_width = 0;
    	_height = 0;
    	_timestamp = 0;
    	_prev_timestamp = _timestamp;
    
    	emit frameProcessed(_pframe);
    	//need to reset pframe?
    	// _pframe = FramePtr(nullptr);
    }