// +-------------------------------- DISCLAIMER ---------------------------------+ // | | // | This program is provided to you free of charge "as such". | // | You should not assume that this program is error-free or suitable for any | // | purpose whatsoever. | // | | // | EURESYS does not give any representation, warranty or undertaking that this | // | program is free of any defect or error or suitable for any purpose. EURESYS | // | shall not be liable, in contract, in torts or otherwise, for any damages, | // | loss, costs, expenses or other claims for compensation, including those | // | asserted by third parties, arising out of or in connection with the use of | // | this program. | // +-----------------------------------------------------------------------------+ #include #include #include #include #include #include #include "easylocateinferencewidget.h" #include "QtDrawAdapter.h" #include #include #include QTimer *timer; EImageC24 src_; Mat frame; Mat tmpframe; VideoCapture cap; cv::VideoWriter *video; QString model_path="/home/adlink/model.ecl"; #undef min int deviceID = 0; int apiID = cv::CAP_V4L2; EasyLocateInferenceWidget::EasyLocateInferenceWidget(QWidget *parent) : QWidget(parent), locator_(NULL) { cap.set(cv::CAP_PROP_FRAME_WIDTH,1920); cap.set(cv::CAP_PROP_FRAME_HEIGHT,1080); cap.open(deviceID, apiID); if (cap.isOpened() == false){ std::cout<< "Camera can't open"<Load(model_path.toStdString()); // Run on GPU if available if (locator_->GetNumGPUs() > 0) { // Use the first GPU locator_->SetEnableGPU(true); std::vector gpuIndex = {0}; locator_->SetGPUIndexes(gpuIndex); } ComputeResult(); } timer = new QTimer(this); connect(timer, SIGNAL(timeout()), this, SLOT(update())); timer->start(1); } EasyLocateInferenceWidget::~EasyLocateInferenceWidget() { delete locator_; cap.release(); timer->stop(); this->close(); } void EasyLocateInferenceWidget::loadLocator(const QString &filename) { try { if (locator_) { delete locator_; locator_ = NULL; } // Create the EasyLocate tool locator_ = new ELocator; // Load the EasyLocate tool locator_->Load(filename.toStdString()); // Run on GPU if available if (locator_->GetNumGPUs() > 0) { // Use the first GPU locator_->SetEnableGPU(true); std::vector gpuIndex = {0}; locator_->SetGPUIndexes(gpuIndex); } ComputeResult(); } catch(const EException &e) { printMessageInformation("Error", e.what()); } } void EasyLocateInferenceWidget::paintEvent(QPaintEvent *) { float zoom = 0.8; // Create a QT painter cv::Mat tmpImage; cv::Mat image; cap.read(tmpImage); if (tmpImage.empty() == true){ std::cout << "EMPTY!"; return; } cv::cvtColor(tmpImage, image, cv::COLOR_BGR2RGB); QImage img((const unsigned char*)(image.data), image.cols, image.rows, QImage::Format_RGB888); //Euresys cv to img_ void * imageptr= tmpImage.data; src_.SetImagePtr(1920,1080,imageptr,1920*8*3); ComputeResult(); QPixmap pixmap = QPixmap::fromImage(img); QPainter painter(this); /* float comprimento = 1.0*width()/pixmap.width(); float altura = 1.0*height()/pixmap.height(); float ratio = 0.; if (comprimento<=altura) ratio = comprimento; else ratio = altura; QSize size = ratio*pixmap.size(); size.setHeight(size.height()-10); QPoint p; p.setX(0 + (width()-size.width())/2); p.setY(5); //painter.drawPixmap(QRect(p, size), pixmap.scaled(size, Qt::KeepAspectRatio)); */ // Draw the image using the Qt draw adapter QtDrawAdapter drawAdapter(&painter); src_.Draw(&drawAdapter, zoom, zoom, 0, 0); // Build a text message from inference result QString message; if (result_.IsValid()) { result_.Draw(&drawAdapter, zoom, zoom, 0, 0); message = tr("%1 objects detected in %2ms").arg(result_.GetNumDetectedObjects()).arg(timing_); } else { message = tr("No object detected"); } // Display the text QRect boundingRect = painter.boundingRect(10, 10, 400, 100, 0, message); painter.fillRect(boundingRect, QWidget::palette().color(QWidget::backgroundRole())); painter.drawText(boundingRect, 0, message); } void EasyLocateInferenceWidget::mousePressEvent(QMouseEvent *e) { if (e->button() == Qt::LeftButton) emit click(); } void EasyLocateInferenceWidget::printMessageInformation(const QString &title, const QString &message) { QMessageBox::information(this, title, message); } void EasyLocateInferenceWidget::ComputeResult() { if (src_.IsVoid() || locator_ == NULL || !locator_->IsTrained()) return; try { // Apply the locator tool and keep the processing time Easy::StartTiming(); result_ = locator_->Apply(src_); timing_ = Easy::StopTiming(1000); } catch(const EException &e) { printMessageInformation("Error", e.what()); } }