switched video capture to QCamera and removed opencv dependency

This commit is contained in:
csoler 2021-05-12 09:40:42 +02:00
parent 4b34c73cf8
commit 0de9c877b8
4 changed files with 98 additions and 104 deletions

View File

@ -53,7 +53,6 @@ linux-* {
PKGCONFIG += libavcodec libavutil
PKGCONFIG += speex speexdsp
PKGCONFIG += opencv4
} else {
LIBS += -lspeex -lspeexdsp -lavcodec -lavutil
}
@ -66,37 +65,10 @@ win32 {
INCLUDEPATH += . $$INC_DIR
USE_PRECOMPILED_LIBS =
for(lib, RS_LIB_DIR) {
#message(Scanning $$lib)
isEmpty(USE_PRECOMPILED_LIBS) {
exists($$lib/opencv/libopencv_core.a) {
message(Get pre-compiled opencv libraries here:)
message($$lib/opencv)
LIBS += -L"$$lib/opencv"
USE_PRECOMPILED_LIBS = 1
}
exists($$lib/libopencv_core.dll.a) {
message(Get pre-compiled opencv libraries here:)
message($$lib)
LIBS += -L"$$lib"
USE_PRECOMPILED_LIBS = 1
}
}
}
isEmpty(USE_PRECOMPILED_LIBS) {
message(Use system opencv libraries.)
}
LIBS += -lopencv_core -lopencv_highgui -lopencv_imgproc -lopencv_videoio -lopencv_imgcodecs -llibwebp -llibtiff -llibpng -llibopenjp2 -lIlmImf
# Should we keep these after removing opencv??
LIBS += -lole32 -loleaut32 -luuid -lvfw32
# Check for msys2
!isEmpty(PREFIX_MSYS2) {
message(Use msys2 opencv4.)
INCLUDEPATH += "$${PREFIX_MSYS2}/include/opencv4"
} else {
LIBS += -llibjpeg-turbo -lzlib
}
LIBS += -llibjpeg-turbo -lzlib
}
#################################### MacOSX #####################################
@ -105,30 +77,6 @@ macx {
DEPENDPATH += . $$INC_DIR
INCLUDEPATH += . $$INC_DIR
#OPENCV_VERSION = "249"
USE_PRECOMPILED_LIBS =
for(lib, LIB_DIR) {
#message(Scanning $$lib)
exists( $$lib/opencv/libopencv_core*.dylib) {
isEmpty(USE_PRECOMPILED_LIBS) {
message(Get pre-compiled opencv libraries here:)
message($$lib)
LIBS += -L"$$lib/opencv"
LIBS += -lopencv_core -lopencv_highgui -lopencv_imgproc
USE_PRECOMPILED_LIBS = 1
}
}
exists( $$lib/libopencv_videoio*.dylib) {
message(videoio found in opencv libraries.)
message($$lib)
LIBS += -lopencv_videoio
}
}
isEmpty(USE_PRECOMPILED_LIBS) {
message(Use system opencv libraries.)
LIBS += -lopencv_core -lopencv_highgui -lopencv_imgproc
}
}

View File

@ -38,7 +38,6 @@
#include "gui/SoundManager.h"
#include "gui/chat/ChatWidget.h"
#include <opencv2/opencv.hpp>
#include <speex/speex.h>
#define IMAGE_VOIP ":/images/talking_on.svg"
@ -188,8 +187,6 @@ std::string VOIPPlugin::getPluginName() const
void VOIPPlugin::getLibraries(std::list<RsLibraryInfo> &libraries)
{
libraries.push_back(RsLibraryInfo("OpenCV", CV_VERSION));
const char *speexVersion = NULL;
if (speex_lib_ctl(SPEEX_LIB_GET_VERSION_STRING, &speexVersion) == 0 && speexVersion) {
libraries.push_back(RsLibraryInfo("Speex", speexVersion));

View File

@ -18,12 +18,13 @@
* *
*******************************************************************************/
#include <opencv2/opencv.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc/types_c.h>
#include <QTimer>
#include <QPainter>
#include <QImageReader>
#include <QBuffer>
#include <QCamera>
#include <QCameraInfo>
#include <QCameraImageCapture>
#include "QVideoDevice.h"
#include "VideoProcessor.h"
@ -36,6 +37,16 @@ QVideoInputDevice::QVideoInputDevice(QWidget *parent)
_echo_output_device = NULL ;
}
QVideoInputDevice::~QVideoInputDevice()
{
stop() ;
_video_processor = NULL ;
delete _image_capture;
delete _capture_device;
delete _timer;
}
bool QVideoInputDevice::stopped()
{
return _timer == NULL ;
@ -45,7 +56,7 @@ void QVideoInputDevice::stop()
{
if(_timer != NULL)
{
QObject::disconnect(_timer,SIGNAL(timeout()),this,SLOT(grabFrame())) ;
_capture_device->stop();
_timer->stop() ;
delete _timer ;
_timer = NULL ;
@ -53,10 +64,12 @@ void QVideoInputDevice::stop()
if(_capture_device != NULL)
{
// the camera will be deinitialized automatically in VideoCapture destructor
_capture_device->release();
delete _capture_device ;
delete _image_capture ;
delete _capture_device ;
_capture_device = NULL ;
}
_image_capture = NULL ;
}
}
void QVideoInputDevice::start()
{
@ -65,45 +78,78 @@ void QVideoInputDevice::start()
stop() ;
// Initialise la capture
static const int cam_id = 0 ;
_capture_device = new cv::VideoCapture(cam_id);
QCameraInfo caminfo = QCameraInfo::defaultCamera();
if(!_capture_device->isOpened())
{
std::cerr << "Cannot initialise camera. Something's wrong." << std::endl;
return ;
}
if(caminfo.isNull())
{
std::cerr << "No video camera available in this system!" << std::endl;
return ;
}
_capture_device = new QCamera(caminfo);
_timer = new QTimer ;
QObject::connect(_timer,SIGNAL(timeout()),this,SLOT(grabFrame())) ;
if(_capture_device->error() != QCamera::NoError)
{
emit cameraCaptureInfo(CANNOT_INITIALIZE_CAMERA,_capture_device->error());
std::cerr << "Cannot initialise camera. Something's wrong." << std::endl;
return;
}
_capture_device->setCaptureMode(QCamera::CaptureStillImage);
_timer->start(50) ; // 10 images per second.
if(_capture_device->error() == QCamera::NoError)
emit cameraCaptureInfo(CAMERA_IS_READY,QCamera::NoError);
_image_capture = new QCameraImageCapture(_capture_device);
if(!_image_capture->isCaptureDestinationSupported(QCameraImageCapture::CaptureToBuffer))
{
emit cameraCaptureInfo(CAMERA_IS_READY,QCamera::NoError);
delete _capture_device;
delete _image_capture;
return;
}
_image_capture->setCaptureDestination(QCameraImageCapture::CaptureToBuffer);
QObject::connect(_image_capture,SIGNAL(imageAvailable(int,QVideoFrame)),this,SLOT(grabFrame(int,QVideoFrame)));
QObject::connect(this,SIGNAL(cameraCaptureInfo(CameraStatus,QCamera::Error)),this,SLOT(errorHandling(CameraStatus,QCamera::Error)));
_timer = new QTimer ;
QObject::connect(_timer,SIGNAL(timeout()),_image_capture,SLOT(capture())) ;
_timer->start(50) ; // 10 images per second.
_capture_device->start();
}
void QVideoInputDevice::grabFrame()
void QVideoInputDevice::errorHandling(CameraStatus status,QCamera::Error error)
{
if(!_timer)
return ;
std::cerr << "Received msg from camera capture: status=" << (int)status << " error=" << (int)error << std::endl;
cv::Mat frame;
if(!_capture_device->read(frame))
if(status == CANNOT_INITIALIZE_CAMERA)
{
std::cerr << "(EE) Cannot capture image from camera. Something's wrong." << std::endl;
return ;
std::cerr << "Cannot initialize camera. Make sure to install package libqt5multimedia5-plugins, as this is a common cause for camera not being found." << std::endl;
}
}
void QVideoInputDevice::grabFrame(int id,QVideoFrame frame)
{
if(frame.size().isEmpty())
{
std::cerr << "Empty frame!" ;
return;
}
// get the image data
frame.map(QAbstractVideoBuffer::ReadOnly);
QByteArray data((const char *)frame.bits(), frame.mappedBytes());
QBuffer buffer;
buffer.setData(data);
buffer.open(QIODevice::ReadOnly);
QImageReader reader(&buffer, "JPG");
reader.setScaledSize(QSize(640,480));
QImage image(reader.read());
if(frame.channels() != 3)
{
std::cerr << "(EE) expected 3 channels. Got " << frame.channels() << std::endl;
return ;
}
// convert to RGB and copy to new buffer, because cvQueryFrame tells us to not modify the buffer
cv::Mat img_rgb;
cv::cvtColor(frame, img_rgb, CV_BGR2RGB);
QImage image = QImage(img_rgb.data,img_rgb.cols,img_rgb.rows,QImage::Format_RGB888);
std::cerr << "Frame " << id << ". Pixel format: " << frame.pixelFormat() << ". Size: " << image.size().width() << " x " << image.size().height() << std::endl; // if(frame.pixelFormat() != QVideoFrame::Format_Jpeg)
if(_video_processor != NULL)
{
@ -131,13 +177,6 @@ uint32_t QVideoInputDevice::currentBandwidth() const
return _video_processor->currentBandwidthOut() ;
}
QVideoInputDevice::~QVideoInputDevice()
{
stop() ;
_video_processor = NULL ;
}
QVideoOutputDevice::QVideoOutputDevice(QWidget *parent)
: QLabel(parent)
{

View File

@ -21,13 +21,13 @@
#pragma once
#include <QLabel>
#include <QCamera>
#include "interface/rsVOIP.h"
#include "opencv2/opencv.hpp"
#include "gui/VideoProcessor.h"
class VideoEncoder ;
class QCameraImageCapture;
// Responsible from displaying the video. The source of the video is
// a VideoDecoder object, which uses a codec.
@ -74,16 +74,26 @@ class QVideoInputDevice: public QObject
void start() ;
void stop() ;
bool stopped();
enum CameraStatus {
CAMERA_IS_READY = 0x00,
CANNOT_INITIALIZE_CAMERA = 0x01,
CAMERA_CANNOT_GRAB_FRAMES = 0x02
};
protected slots:
void grabFrame() ;
void grabFrame(int id, QVideoFrame f) ;
void errorHandling(CameraStatus status,QCamera::Error error);
signals:
void networkPacketReady() ;
void cameraCaptureInfo(CameraStatus status,QCamera::Error qt_cam_err_code);
private:
VideoProcessor *_video_processor ;
QTimer *_timer ;
cv::VideoCapture *_capture_device ;
QCamera *_capture_device;
QCameraImageCapture *_image_capture;
QVideoOutputDevice *_echo_output_device ;