Skip to content

Commit

Permalink
Added the possibility to change the input camera
Browse files Browse the repository at this point in the history
  • Loading branch information
xavierdechamps committed Jun 20, 2021
1 parent 825b284 commit b84ad01
Show file tree
Hide file tree
Showing 9 changed files with 246 additions and 127 deletions.
52 changes: 27 additions & 25 deletions SRC/Video.pro
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
#
unix {
# Path to global OpenCV installation directory
MY_OPENCV_DIR = /Users/dechamps/Documents/Codes/Cpp/Images/Libraries/opencv-4.3.0/install
MY_OPENCV_DIR = /Users/dechamps/Documents/Codes/Libraries/opencv-4.5.2/install
#
# If you don't have the OpenCV object detect library, comment the following 2 lines
CONFIG += objdetect
# (Macro) Path to the directory containing the cascade classifier files for face detection
DEFINES += OPENCV_HAARCASCADES_DIR=\\\"/Users/dechamps/Documents/Codes/Cpp/Images/Libraries/opencv-4.3.0/install/share/opencv4/haarcascades/\\\"
DEFINES += OPENCV_HAARCASCADES_DIR=\\\"/Users/dechamps/Documents/Codes/Libraries/opencv-4.5.2/install/share/opencv4/haarcascades/\\\"
#
# If you don't have the OpenCV extra module Face, comment the following line
CONFIG += face
# https://github.com/kurnianggoro/GSOC2017/blob/master/data/lbfmodel.yaml
# (Macro) Path to the directory containing the facemark file "lbfmodel.yaml"
DEFINES += OPENCV_FACEMARK_DIR=\\\"/Users/dechamps/Documents/Codes/Cpp/Images/Libraries/opencv-4.3.0/install/share/opencv4/lbfmodel/\\\"
DEFINES += OPENCV_FACEMARK_DIR=\\\"/Users/dechamps/Documents/Codes/Libraries/opencv-4.5.2/install/share/opencv4/lbfmodel/\\\"
#
# If you don't have the ZBar library, comment the following line
CONFIG += zbar
Expand All @@ -24,33 +24,33 @@ unix {
CONFIG += tesseract
MY_TESSERACT_DIR = /Users/dechamps/Documents/Codes/Libraries/tesseract-4.1.1/install
DEFINES += TESSERACT_DATA=\\\"/Users/dechamps/Documents/Codes/Libraries/tesseract-4.1.1/tessdata-master/\\\"
DEFINES += TESSERACT_DNN=\\\"/Users/dechamps/Documents/Codes/Cpp/Images/Libraries/opencv-4.3.0/install/share/opencv4/dnn/frozen_east_text_detection.pb\\\"
DEFINES += TESSERACT_DNN=\\\"/Users/dechamps/Documents/Codes/Libraries/opencv-4.5.2/install/share/opencv4/dnn/frozen_east_text_detection.pb\\\"
#
}

win32 {
# Path to global OpenCV installation directory
MY_OPENCV_DIR = D:\Libraries\opencv-4.3.0\build\install
MY_OPENCV_DIR = D:\Libraries\opencv-4.5.2\build\install
#
# If you don't have the OpenCV object detect library, comment the following 2 lines
CONFIG += objdetect
# (Macro) Path to the directory containing the cascade classifier files for face detection
DEFINES += OPENCV_HAARCASCADES_DIR=\\\"D:/Libraries/opencv-4.3.0/build/install/etc/haarcascades/\\\"
DEFINES += OPENCV_HAARCASCADES_DIR=\\\"D:/Libraries/opencv-4.5.2/build/install/etc/haarcascades/\\\"
#
# If you don't have the OpenCV extra module Face, comment the following line
CONFIG += face
# https://github.com/kurnianggoro/GSOC2017/blob/master/data/lbfmodel.yaml
# (Macro) Path to the directory containing the facemark file "lbfmodel.yaml"
DEFINES += OPENCV_FACEMARK_DIR=\\\"D:/Libraries/opencv-4.3.0/build/install/etc/facemark/\\\"
DEFINES += OPENCV_FACEMARK_DIR=\\\"D:/Libraries/opencv-4.5.2/build/install/etc/facemark/\\\"
#
# If you don't have the ZBar library, comment the following line
#CONFIG += zbar
#MY_ZBAR_DIR =
#MY_ZBAR_DIR =
}

# If you don't have the OpenCV stiching library, comment the following line
CONFIG += stitching
#
#
# If you don't have the OpenCV extra module Xphoto, comment the following line
CONFIG += xphoto
#
Expand All @@ -73,6 +73,7 @@ TEMPLATE = app
RESOURCES = images.qrc

SOURCES = main.cpp \
dialog_choose_camera.cpp \
myimage.cpp \
mainwindow.cpp \
dialog_blur.cpp \
Expand All @@ -84,11 +85,12 @@ SOURCES = main.cpp \
dialog_motion_detection.cpp \
secondarywindow.cpp \
dialog_photo.cpp \
capturevideo.cpp
capturevideo.cpp
stitching: SOURCES+=dialog_panorama.cpp
tesseract: SOURCES += window_tesseract.cpp

HEADERS += myimage.h \
dialog_choose_camera.h \
mainwindow.h \
dialog_blur.h \
dialog_edge.h \
Expand All @@ -99,7 +101,7 @@ HEADERS += myimage.h \
dialog_motion_detection.h \
secondarywindow.h \
dialog_photo.h \
capturevideo.h
capturevideo.h
stitching: HEADERS += dialog_panorama.h
tesseract: HEADERS += window_tesseract.h

Expand All @@ -114,19 +116,19 @@ win32 {
INCLUDEPATH += $${MY_OPENCV_DIR}\include

# Required OpenCV libraries
LIBS = $${MY_OPENCV_DIR}\x64\vc15\lib\opencv_imgcodecs430.lib \
$${MY_OPENCV_DIR}\x64\vc15\lib\opencv_core430.lib \
$${MY_OPENCV_DIR}\x64\vc15\lib\opencv_highgui430.lib \
$${MY_OPENCV_DIR}\x64\vc15\lib\opencv_videoio430.lib \
$${MY_OPENCV_DIR}\x64\vc15\lib\opencv_imgproc430.lib \
$${MY_OPENCV_DIR}\x64\vc15\lib\opencv_video430.lib \
$${MY_OPENCV_DIR}\x64\vc15\lib\opencv_photo430.lib
stitching: LIBS += $${MY_OPENCV_DIR}\x64\vc15\lib\opencv_stitching430.lib
objdetect: LIBS += $${MY_OPENCV_DIR}\x64\vc15\lib\opencv_objdetect430.lib
xphoto: LIBS += $${MY_OPENCV_DIR}\x64\vc15\lib\opencv_xphoto430.lib
face: LIBS += $${MY_OPENCV_DIR}\x64\vc15\lib\opencv_face430.lib
LIBS = $${MY_OPENCV_DIR}\x64\vc15\lib\opencv_imgcodecs452.lib \
$${MY_OPENCV_DIR}\x64\vc15\lib\opencv_core452.lib \
$${MY_OPENCV_DIR}\x64\vc15\lib\opencv_highgui452.lib \
$${MY_OPENCV_DIR}\x64\vc15\lib\opencv_videoio452.lib \
$${MY_OPENCV_DIR}\x64\vc15\lib\opencv_imgproc452.lib \
$${MY_OPENCV_DIR}\x64\vc15\lib\opencv_video452.lib \
$${MY_OPENCV_DIR}\x64\vc15\lib\opencv_photo452.lib
stitching: LIBS += $${MY_OPENCV_DIR}\x64\vc15\lib\opencv_stitching452.lib
objdetect: LIBS += $${MY_OPENCV_DIR}\x64\vc15\lib\opencv_objdetect452.lib
xphoto: LIBS += $${MY_OPENCV_DIR}\x64\vc15\lib\opencv_xphoto452.lib
face: LIBS += $${MY_OPENCV_DIR}\x64\vc15\lib\opencv_face452.lib
zbar: LIBS += $${MY_ZBAR_DIR}\lib\libzbar-0.lib
tesseract: LIBS += $${MY_TESSERACT_DIR}\lib\tesseract41.lib $${MY_OPENCV_DIR}\x64\vc15\lib\opencv_dnn430.lib
tesseract: LIBS += $${MY_TESSERACT_DIR}\lib\tesseract41.lib $${MY_OPENCV_DIR}\x64\vc15\lib\opencv_dnn452.lib
}

unix {
Expand All @@ -137,14 +139,14 @@ unix {

# Compilator flags
QMAKE_CXXFLAGS += -std=c++11

# Linker flags
QMAKE_LFLAGS += -Wl,-rpath,$${MY_OPENCV_DIR}/lib
# Include libraries from ZBar
zbar: QMAKE_LFLAGS += -Wl,-rpath,$${MY_ZBAR_DIR}/lib
# Include libraries from Tesseract
zbar: QMAKE_LFLAGS += -Wl,-rpath,$${MY_TESSERACT_DIR}/lib

# Required OpenCV libraries
LIBS = -L$${MY_OPENCV_DIR}/lib
LIBS += -lopencv_imgcodecs \
Expand Down
55 changes: 27 additions & 28 deletions SRC/capturevideo.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
* Copyright (C) 2019-2020 Xavier Dechamps
*
* PURPOSE
* Management of thread that takes care of capturing the camera, applying image
* Management of the Qthread that takes care of capturing the camera, applying image
* processes on it and sending the treated image to the GUI MainWindow through signals
*
* Multithreaded implementation. The class captureVideo is solely dedicated to the treatment
Expand Down Expand Up @@ -70,11 +70,12 @@ void captureVideo::setCamera(int camera){

/**
* @brief captureVideo::openCamera
* @param integers width and height passed as parameters are set when the camera is opened
* @return true if the camera is open. False if the camera ID is not set yet
*
* Open the camera anad initialize a new object of class MyImage
*/
bool captureVideo::openCamera(){
bool captureVideo::openCamera(int &width, int &height){
if (this->camID<0) {
qDebug() << "captureVideo::openCamera - Cannot open camera";
return false;
Expand Down Expand Up @@ -107,7 +108,16 @@ bool captureVideo::openCamera(){
return false;
}

return this->capture.open(this->camID);
// Open the camera in OpenCV
bool test = this->capture.open(this->camID);

// Get the dimensions of the camera frame
if (test) {
width = capture.get(cv::CAP_PROP_FRAME_WIDTH);
height = capture.get(cv::CAP_PROP_FRAME_HEIGHT);
}

return test;
}

/**
Expand All @@ -127,7 +137,7 @@ bool captureVideo::closeCamera() {
}

// Stop saving the movie
file_save_movie(false);
if (this->recording) file_save_movie(false);
}
return true;
}
Expand Down Expand Up @@ -155,8 +165,9 @@ bool captureVideo::getQRcodedata(std::string &qrdata, std::string &qrtype){
/**
* @brief captureVideo::run
*
* The core of the class captureVideo. It loops indefenitely to capture frames from the camera and
* to apply image processings on them. The treated images are sent to the GUI through signals.
* The core of the class captureVideo. It is called by mainWindow through this->worker->start()
* It loops indefenitely to capture frames from the camera and to apply image processings on them.
* The treated images are sent to the GUI through signals.
*/
void captureVideo::run() {
this->running = true;
Expand All @@ -166,13 +177,16 @@ void captureVideo::run() {
int current_count = 0;
bool first_time = true;

// While loop as long as running is true
while (this->running) {
if(cameraIsOpen()) {
if (first_time){
timer.start();
first_time = false;
}
// capture the camera frame and save it in imageMat
this->capture >> imageMat;
// Stay in the while loop as long as the camera has not started
while (imageMat.empty()) {
this->capture >> imageMat;
qDebug() << "captureVideo::run() : Empty image";
Expand Down Expand Up @@ -204,19 +218,22 @@ void captureVideo::run() {
// emit this->setFPSrate(this->cameraFPS);
}
}
else
{
else {
qDebug() << "captureVideo::run() : Camera is not open";
imageMat = cv::Mat::zeros(480, 640, CV_8UC1);
myFrame->set_image_content(imageMat);
imageMat = myFrame->get_image_content();
}

// Convert the opencv image to a QImage that will be displayed on the main window
// The first step is to convert the colour from BGR to RGB
cv::cvtColor(imageMat, imageMat, cv::COLOR_BGR2RGB);

// Lock mechanism to ensure that the data is not overwritten by another thread
this->data_lock->lock();
// Create the QImage on the basis of the data from the OpenCV image
this->myQimage = QImage(imageMat.data, imageMat.cols, imageMat.rows, imageMat.cols*3, QImage::Format_RGB888);
// Unlock the data
this->data_lock->unlock();

// Launch the motion detection algorithm and send the result to the Qt manager
Expand Down Expand Up @@ -259,7 +276,7 @@ void captureVideo::run() {
* @param state: boolean the new status
*
* Change the status of the thread. If state=true, the thread is activated.
* If false, the while loop in run() is escaped and
* If false, the while loop in run() is terminated
*/
void captureVideo::setThreadStatus(bool state){
this->running = state;
Expand Down Expand Up @@ -360,22 +377,6 @@ void captureVideo::toggleMotionDetection(bool state) {
* Activate/desactivate the face detection algorithm. A background image is fetch from a local directory
*/
void captureVideo::toggleFaceDetection(bool state) {
/*
if (!this->myFrame->getFace_Status()) {
this->file_background = this->main_directory + "cartoon_background.jpg";
bool test = this->myFrame->set_background_image(this->file_background);
while (!test){
QString QfileNameLocal = QFileDialog::getOpenFileName(this->mainWindowParent,
tr("Select a background image"),
QString::fromStdString(this->main_directory),
tr("Images (*.bmp *.png *.jpg *.jpeg *.jpe *.jp2 *.webp *.pbm *.pgm *.ppm *.pnm *.pfm *.src *.tiff *.tif *.exr *.hdr *.pic)") );
if ( ! QfileNameLocal.isEmpty() ) {
this->file_background = QfileNameLocal.toStdString() ;
test = this->myFrame->set_background_image(this->file_background);
}
}
}
*/
this->myFrame->toggleFace_Recon(state);
if (state)
emit changeInfo("Face detection activated");
Expand Down Expand Up @@ -773,7 +774,6 @@ bool captureVideo::setCascadeFile(){
}
else
return false;

}
}
else {
Expand Down Expand Up @@ -843,11 +843,10 @@ bool captureVideo::setFacemarkFile(){
tr("Images (*.yaml)") );
if ( ! QfileNameLocal.isEmpty() ) {
this->file_facemark = QfileNameLocal.toStdString() ;
testCascade = this->myFrame->set_Face_Cascade_Name(this->file_facemark);
testCascade = this->myFrame->set_Face_Facemark_Name(this->file_facemark);
}
else
return false;

}
}
else {
Expand Down
3 changes: 1 addition & 2 deletions SRC/capturevideo.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class captureVideo : public QThread
public:
explicit captureVideo(QMainWindow *parent=nullptr,QMutex* data_lock=nullptr);
void setCamera(int camID);
bool openCamera();
bool openCamera(int &width, int &height);
bool closeCamera();
bool cameraIsOpen();
void setThreadStatus(bool);
Expand Down Expand Up @@ -115,7 +115,6 @@ public slots:

void file_save_image();


protected:
void run() override;

Expand Down
40 changes: 40 additions & 0 deletions SRC/dialog_choose_camera.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
#include "dialog_choose_camera.h"

Dialog_choose_camera::Dialog_choose_camera(QList<QCameraInfo> cameras,QWidget *parent): QDialog(parent){
this->list_cameras = cameras;

QList< QPushButton* > PB;
QButtonGroup* group = new QButtonGroup(this);
QGridLayout *grid = new QGridLayout;

QLabel* label = new QLabel("Please choose one of the following cameras:");
grid->addWidget(label, 0, 0);
int i=0;
foreach (const QCameraInfo &cameraInfo, cameras) {
// Retrieve camera name
QString info = cameraInfo.description() ;

// Create new button with textValue == camera name
QPushButton *button = new QPushButton(info, this);

// Store button in an array
PB << button;
group->addButton(button,i);
grid->addWidget(button, i+1, 0);
i++;
}

connect(group, SIGNAL(buttonClicked(int)), this, SLOT(onClick_Choose_Camera(int)));

setLayout(grid);
setWindowTitle(tr("Camera control window"));
}

void Dialog_choose_camera::onClick_Choose_Camera(int val) {
this->info_camera = this->list_cameras[val].description();
emit this->Signal_camera_chosen( val ) ;
}

QString Dialog_choose_camera::get_info_camera() {
return this->info_camera ;
}
30 changes: 30 additions & 0 deletions SRC/dialog_choose_camera.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#ifndef DIALOG_CHOOSE_CAMERA_H
#define DIALOG_CHOOSE_CAMERA_H

#include <QDialog>
#include <QLabel>
#include <QPushButton>
#include <QButtonGroup>
#include <QGridLayout>
#include <QCameraInfo>
#include <QDebug>

class Dialog_choose_camera : public QDialog
{
Q_OBJECT
public:
explicit Dialog_choose_camera(QList<QCameraInfo> cameras,QWidget *parent = nullptr);
QString get_info_camera() ;

private:
QString info_camera ;
QList<QCameraInfo> list_cameras ;

private slots:
void onClick_Choose_Camera(int);

signals:
void Signal_camera_chosen(int value);
};

#endif // DIALOG_CHOOSE_CAMERA_H
4 changes: 2 additions & 2 deletions SRC/main.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,9 @@ Generating a Makefile through qmake has never been so easy. Simply modify the fi
qmake.exe Video.pro
to generate a Makefile (MinGW) or a project for VisualStudio. Don't forget to put the required *.dll libraries in the same directory as the executable before launching it! The list of required libraries is
- OpenCV: opencv_calib3d430.dll opencv_core430.dll opencv_face430.dll opencv_features430.dll opencv_flann430.dll opencv_imgcodecs430.dll opencv_imgproc430.dll opencv_objdetect430.dll opencv_photo430.dll opencv_stitching430.dll opencv_video430.dll opencv_video_ffmpeg430_64.dll opencv_videoio430.dll opencv_xphoto430.dll
- OpenCV: opencv_calib3d452.dll opencv_core452.dll opencv_face452.dll opencv_features452.dll opencv_flann452.dll opencv_imgcodecs452.dll opencv_imgproc452.dll opencv_objdetect452.dll opencv_photo452.dll opencv_stitching452.dll opencv_video452.dll opencv_video_ffmpeg452_64.dll opencv_videoio452.dll opencv_xphoto452.dll
- ZBar: libzbar-0.dll
- Tesseract: tesseract41.dll opencv_dnn430.dll
- Tesseract: tesseract41.dll opencv_dnn452.dll
- Qt5: Qt5Core.dll Qt5Gui.dll Qt5Multimedia.dll Qt5Network.dll Qt5Widgets.dll
*/

Expand Down
Loading

0 comments on commit b84ad01

Please sign in to comment.