multicamera handling

This commit is contained in:
Jan Grewe 2023-11-01 11:14:06 +01:00
parent 31d5cf22dd
commit 5e958e710f
4 changed files with 131 additions and 50 deletions

View File

@ -17,9 +17,11 @@
#include <QStandardPaths>
#include <QStatusBar>
#include <QToolBar>
#include <QErrorMessage>
#include <iostream>
#include <chrono>
#include <cmath>
#include "camconfig.h"
#if defined(QT_PRINTSUPPORT_LIB)
# include <QtPrintSupport/qtprintsupportglobal.h>
@ -42,23 +44,33 @@ PylonRecorder::PylonRecorder(QWidget *parent)
scrollArea->setWidget(imageLabel);
scrollArea->setVisible(false);
setCentralWidget(scrollArea);
pylon = new PylonWrapper();
buffer = new ImageBuffer(defaultBufferSize);
grabber = new Grabber(pylon, buffer, defaultFrameRate);
writer = new Writer(buffer);
connect(writer, &Writer::writingDone, this, &PylonRecorder::writerDone);
createActions();
updateActions();
std::cerr << "PylonRecorder::constructor" << std::endl;
// Pylon::DeviceInfoList_t deviceList = detectCameras();
// for (auto dev : deviceList) {
// std::cout << dev.GetFullName() << " " << dev.GetFriendlyName() << std::endl;
// }
// std::cout << "peng\n";
// std::cout << deviceList.size() << std::endl;
// std::cout << "peng\n";
// if (deviceList.size() == 0) {
// QErrorMessage message(this);
// message.showMessage("No camera detected!");
// } else {
// std::cout << "peng\n";
// buffer = new ImageBuffer(defaultBufferSize);
// grabber = new Grabber(pyloncam, buffer, defaultFrameRate);
// writer = new Writer(buffer);
// connect(writer, &Writer::writingDone, this, &PylonRecorder::writerDone);
// }
std::cerr << "PylonRecorder::constructor 1" << std::endl;
frameTimer = new QTimer(this);
connect(frameTimer, &QTimer::timeout, this, &PylonRecorder::displaySingleFrame);
preassureTimer = new QTimer(this);
connect(preassureTimer, &QTimer::timeout, this, &PylonRecorder::displayBufferPreassure);
labelTimer = new QTimer(this);
connect(labelTimer, &QTimer::timeout, this, &PylonRecorder::displayActivity);
std::cerr << "PylonRecorder::constructor 2" << std::endl;
preassureBar = new QProgressBar(this);
preassureBar->setRange(0, 100);
preassureBar->setTextVisible(true);
@ -74,7 +86,7 @@ PylonRecorder::PylonRecorder(QWidget *parent)
loadBar->setFixedSize(200, 25);
QLabel *loadLabel = new QLabel("Load:", this);
loadLabel->setStyleSheet("QLabel{font-size: 11pt;font-family: Arial;font-weight: Bold}");
std::cerr << "PylonRecorder::constructor 3" << std::endl;
writingLabel = new QLabel("writing");
writingLabel->setEnabled(false);
writingLabel->setStyleSheet("QLabel{font-size: 11pt;font-family: Arial;}");
@ -85,7 +97,7 @@ PylonRecorder::PylonRecorder(QWidget *parent)
cameraConnectedLabel = new QLabel("not connected");
cameraConnectedLabel->setStyleSheet("QLabel { color : red; }");
cameraConnectedLabel->setStyleSheet("QLabel{font-size: 11pt;font-family: Arial;}");
std::cerr << "PylonRecorder::constructor 4" << std::endl;
fileLabel = new QLabel();
fileLabel->setStyleSheet("QLabel{font-size: 11pt;font-family: Arial;}");
@ -95,7 +107,7 @@ PylonRecorder::PylonRecorder(QWidget *parent)
statusHeader->setStyleSheet("QLabel{font-size: 11pt;font-family: Arial; font-weight: Bold}");
QLabel *fileHeader = new QLabel("Output file:");
fileHeader->setStyleSheet("QLabel{font-size: 11pt;font-family: Arial; font-weight: Bold}");
std::cerr << "PylonRecorder::constructor 5" << std::endl;
statusBar()->addWidget(camHeader);
statusBar()->addWidget(cameraConnectedLabel);
statusBar()->addWidget(preassureLabel);
@ -108,9 +120,45 @@ PylonRecorder::PylonRecorder(QWidget *parent)
statusBar()->addWidget(fileHeader);
statusBar()->addWidget(fileLabel);
resize(QGuiApplication::primaryScreen()->availableSize() * 3 / 5);
std::cerr << "PylonRecorder::constructor 6" << std::endl;
detectCameras();
std::cerr << "PylonRecorder::constructor 7" << std::endl;
createActions();
std::cerr << "PylonRecorder::constructor 8 " << std::endl;
updateActions();
std::cerr << "PylonRecorder::constructor 9" << std::endl;
applySettings();
std::cerr << "PylonRecorder::constructor done" << std::endl;
}
void PylonRecorder::setupCameras(){
for (auto dev : deviceList) {
std::cout << dev.GetFullName() << " " << dev.GetFriendlyName() << std::endl;
}
std::cout << "peng\n";
std::cout << deviceList.size() << std::endl;
std::cout << "peng\n";
if (deviceList.size() == 0) {
std::cout << "device list is empty!" << std::endl;
QErrorMessage message(this);
message.showMessage("No camera detected!");
} else {
std::cout << "peng\n";
// std::string cname = (std::string)deviceList[0].GetFullName();
// pyloncam = new PylonWrapper(cname);
buffer = new ImageBuffer(defaultBufferSize);
grabber = new Grabber(pyloncam, buffer, defaultFrameRate);
writer = new Writer(buffer);
connect(writer, &Writer::writingDone, this, &PylonRecorder::writerDone);
}
}
void PylonRecorder::detectCameras() {
Pylon::PylonAutoInitTerm autoInitTerm;
Pylon::CTlFactory& TlFactory = Pylon::CTlFactory::GetInstance();
TlFactory.EnumerateDevices(deviceList);
}
PylonRecorder::~PylonRecorder(){
if (grabber->isRunning()) {
@ -122,8 +170,9 @@ PylonRecorder::~PylonRecorder(){
writer->wait(1000);
}
storeSettings();
delete pylon;
std::cerr << "recorder::destructor" << std::endl;
delete pyloncam;
std::cerr << "recorder::destructor" << std::endl;
delete buffer;
delete grabber;
delete writer;
@ -504,10 +553,10 @@ void PylonRecorder::updateActions() {
zoomInAct->setEnabled(!fitToWindowAct->isChecked());
zoomOutAct->setEnabled(!fitToWindowAct->isChecked());
normalSizeAct->setEnabled(!fitToWindowAct->isChecked());
disconnect_camera_action->setEnabled(pylon->isOpen());
connect_camera_action->setEnabled(!pylon->isOpen());
grab_still_action->setEnabled(pylon->isOpen());
grab_continuous_action->setEnabled(pylon->isOpen() && !grabbing);
disconnect_camera_action->setEnabled(deviceList.size() > 0);
connect_camera_action->setEnabled(true);
grab_still_action->setEnabled(deviceList.size() > 0);
grab_continuous_action->setEnabled(deviceList.size() > 0 && pyloncam->isOpen() && !grabbing);
grab_stop_action->setEnabled(grabbing);
}
@ -533,12 +582,14 @@ void PylonRecorder::applyScaling(){
void PylonRecorder::quitApplication() {
std::cerr << "Quit Application!" << std::endl;
if (pylon->isOpen()) {
if (pyloncam->isOpen()) {
std::cerr << "Cam is open!" << std::endl;
if (grabbing) {
stopRecording();
buffer->clear();
}
pylon->closeCamera();
pyloncam->closeCamera();
}
this->close();
}
@ -549,38 +600,57 @@ void PylonRecorder::adjustScrollBar(QScrollBar *scrollBar, double factor) {
+ ((factor - 1) * scrollBar->pageStep()/2)));
}
void PylonRecorder::cameraConfiguration(){
CamConfigurator d(this);
// QObject::connect(&d, SIGNAL(recent_file_changed(QStringList)), this, SLOT(recent_file_update(QStringList)));
// QObject::connect(&d, SIGNAL(column_visibility_changed(QString, QString,bool)), this, SLOT(visible_columns_update(QString, QString,bool)));
d.exec();
}
void PylonRecorder::connectCamera() {
std::string message;
bool success = pylon->openCamera(message);
if (success) {
cameraConnectedLabel->setText("connected");
cameraConnectedLabel->setStyleSheet("QLabel { font-size: 10px;font-family: Arial;color: green;}");
}
else {
QMessageBox msgBox;
QString msg = "<p><b>Could not open camera device!</b><p><p>" + QString::fromStdString(message) + "</p>";
msgBox.setText(msg);
msgBox.exec();
if (deviceList.size() == 0) {
detectCameras();
QMessageBox msgBox;
QString msg = "<p><b>No camera device found!</b></p><br><p>Connect camera and try again!</p>";
msgBox.setText(msg);
msgBox.exec();
} else if (deviceList.size() == 1) {
std::string cname = (std::string)deviceList[0].GetFullName();
pyloncam = new PylonWrapper(cname);
std::string message;
bool success = pyloncam->openCamera(message);
if (success) {
cameraConnectedLabel->setText("connected");
cameraConnectedLabel->setStyleSheet("QLabel { font-size: 10px;font-family: Arial;color: green;}");
} else {
QMessageBox msgBox;
QString msg = "<p><b>Could not open camera device!</b><p><p>" + QString::fromStdString(message) + "</p>";
msgBox.setText(msg);
msgBox.exec();
}
statusBar()->showMessage(QString::fromStdString(message));
updateActions();
} else { // more than 1 camera
cameraConfiguration();
}
statusBar()->showMessage(QString::fromStdString(message));
updateActions();
}
void PylonRecorder::disconnectCamera() {
pylon->closeCamera();
statusBar()->showMessage(tr("Camera closed!"));
cameraConnectedLabel->setText("not connected");
cameraConnectedLabel->setStyleSheet("QLabel { font-size: 10px;font-family: Arial;color: red;}");
updateActions();
if (pyloncam->isOpen()) {
pyloncam->closeCamera();
statusBar()->showMessage(tr("Camera closed!"));
cameraConnectedLabel->setText("not connected");
cameraConnectedLabel->setStyleSheet("QLabel { font-size: 10px;font-family: Arial;color: red;}");
updateActions();
}
}
void PylonRecorder::startRecording() {
std::string filename = createFilename();
fileLabel->setText(QString::fromStdString(filename));
ImageSettings settings = pylon->getImageSettings();
ImageSettings settings = pyloncam->getImageSettings();
VideoSpecs specs;
specs.fps = framerateSpinner->value();
@ -713,9 +783,9 @@ void PylonRecorder::displayBufferPreassure() {
void PylonRecorder::grabStillFromPylon() {
if (pylon->isOpen()) {
if (pyloncam->isOpen()) {
MyImage img;
bool valid = pylon->grabFrame(img);
bool valid = pyloncam->grabFrame(img);
if (valid) {
QImage qimg(static_cast<uchar *>(img.data()), img.width(), img.height(),
QImage::Format::Format_Grayscale8);

View File

@ -6,7 +6,7 @@
#include <QSpinBox>
#include <QCheckBox>
#include <QSettings>
#include <pylon/PylonIncludes.h>
#include "pylonwrapper.h"
#include "imagebuffer.h"
#include "grabber.h"
@ -76,13 +76,15 @@ private:
void setSubjectID();
QColor progressColor(int value);
std::string createFilename();
void cameraConfiguration();
bool saveFile(const QString &fileName);
void setImage(const QImage &newImage);
void scaleImage(double factor);
void applyScaling();
void adjustScrollBar(QScrollBar *scrollBar, double factor);
void detectCameras();
void setupCameras();
int defaultBufferSize = 3000, defaultFrameRate = 30, movieCount = 0, defaultExposureTime = 6000, defaultGain=13;
QSettings *settings = new QSettings;
QImage image;
@ -93,7 +95,7 @@ private:
QProgressBar *loadBar;
QScrollArea *scrollArea;
double scaleFactor = 1;
PylonWrapper *pylon;
PylonWrapper *pyloncam;
ImageBuffer *buffer;
Grabber *grabber;
Writer *writer;
@ -107,6 +109,7 @@ private:
QAction *projectSettingsAction;
QString storageLocation = "";
ProjectMetadata mdata;
Pylon::DeviceInfoList_t deviceList;
#if defined(QT_PRINTSUPPORT_LIB) && QT_CONFIG(printer)
QPrinter printer;

View File

@ -1,9 +1,11 @@
#include "pylonwrapper.h"
PylonWrapper::PylonWrapper():
valid(false) {
PylonWrapper::PylonWrapper(const std::string &fullName):
valid(false), fullName(fullName) {
Pylon::PylonInitialize();
camera = new Pylon::CInstantCamera();
// std::cerr << "Wrapper:camera is open" << camera->IsOpen() << std::endl;
// std::cerr << "Wrapper:is valid" << valid << std::endl;
}
PylonWrapper::~PylonWrapper() {
@ -134,11 +136,15 @@ bool PylonWrapper::grabFrame(MyImage &img) {
}
bool PylonWrapper::openCamera(std::string &message) {
std::cerr << "open Camera" << std::endl;
try {
// Pylon::CInstantCamera camera( pTl->CreateDevice( lstDevices[0] );
camera->Attach(Pylon::CTlFactory::GetInstance().CreateFirstDevice());
camera->Open();
valid = true;
valid = camera->IsOpen();
message = "Successfully opened camera!";
std::cerr << message << std::endl;
} catch (const Pylon::GenericException &e) {
message = e.GetDescription();
std::cerr << "An exception occurred." << std::endl << e.GetDescription() << std::endl;
@ -148,6 +154,7 @@ bool PylonWrapper::openCamera(std::string &message) {
}
void PylonWrapper::closeCamera() {
std::cerr << "camera close !" << std::endl;
if (camera->IsOpen()) {
try {
camera->Close();

View File

@ -15,7 +15,7 @@ struct ImageSettings {
class PylonWrapper
{
public:
PylonWrapper();
PylonWrapper(const std::string &fullName);
~PylonWrapper();
ImageSettings getImageSettings();
@ -36,6 +36,7 @@ public:
private:
Pylon::CInstantCamera *camera;
bool valid;
std::string fullName;