[60] | 1 | // Includes, project.
|
---|
| 2 | #include "CVWebcamComponent.hpp"
|
---|
| 3 |
|
---|
| 4 | // Includes, pacpus.
|
---|
| 5 | #include <Pacpus/kernel/ComponentFactory.h>
|
---|
| 6 | #include <Pacpus/kernel/Log.h>
|
---|
| 7 | #include <Pacpus/kernel/DbiteFileTypes.h>
|
---|
| 8 | #include <Pacpus/kernel/DbiteException.h>
|
---|
| 9 |
|
---|
| 10 | // Includes, qt.
|
---|
| 11 | #include <QDir>
|
---|
| 12 | #include <QMetaType>
|
---|
| 13 |
|
---|
| 14 | // Includes, standard.
|
---|
| 15 | #include <stdexcept>
|
---|
| 16 |
|
---|
| 17 | using namespace pacpus;
|
---|
| 18 |
|
---|
| 19 | // Declare the logger and register the new component.
|
---|
| 20 | DECLARE_STATIC_LOGGER("pacpus.component.cvwebcam");
|
---|
| 21 | REGISTER_COMPONENT(CVWebcamComponent, "CVWebcam");
|
---|
| 22 |
|
---|
| 23 | const int DEFAULT_DEVICE = -1;
|
---|
| 24 | const int DEFAULT_WIDTH = 640;
|
---|
| 25 | const int DEFAULT_HEIGHT = 480;
|
---|
| 26 | const int DEFAULT_FRAMERATE = 15;
|
---|
| 27 | const int MAX_LENGTH_NAME = 512;
|
---|
| 28 |
|
---|
| 29 | CVWebcamComponent::CVWebcamComponent(QString const& name)
|
---|
| 30 | : ComponentBase(name)
|
---|
| 31 | , mDevice(DEFAULT_DEVICE)
|
---|
| 32 | , mWidth(DEFAULT_WIDTH)
|
---|
| 33 | , mHeight(DEFAULT_HEIGHT)
|
---|
| 34 | , mHasUI(false)
|
---|
| 35 | , mFramerate(DEFAULT_FRAMERATE)
|
---|
| 36 | , mIsRecording(false)
|
---|
| 37 | , mImageCounter(0)
|
---|
| 38 | , firstTime (true)
|
---|
| 39 | , mShMem (NULL)
|
---|
| 40 | {
|
---|
| 41 | // Register new types for the signal/slot system.
|
---|
| 42 | qRegisterMetaType<cv::Mat>("cv::Mat");
|
---|
| 43 | }
|
---|
| 44 |
|
---|
| 45 | CVWebcamComponent::~CVWebcamComponent()
|
---|
| 46 | {
|
---|
| 47 | }
|
---|
| 48 |
|
---|
| 49 | void
|
---|
| 50 | CVWebcamComponent::stopActivity()
|
---|
| 51 | {
|
---|
| 52 | mWebcam.close();
|
---|
| 53 | disconnect(&mWebcam, SIGNAL(gotFrame(cv::Mat)), this, SLOT(receiveFrame(cv::Mat)));
|
---|
| 54 | mDBTFile.close();
|
---|
| 55 | if (mShMem) {
|
---|
| 56 | delete mShMem;
|
---|
| 57 | mShMem = NULL;
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | void
|
---|
| 62 | CVWebcamComponent::startActivity()
|
---|
| 63 | {
|
---|
| 64 | firstTime = true;
|
---|
| 65 | connect(&mWebcam, SIGNAL(gotFrame(cv::Mat)), this, SLOT(receiveFrame(cv::Mat)));
|
---|
| 66 |
|
---|
| 67 | try
|
---|
| 68 | {
|
---|
| 69 | QString filename = name() + ".dbt";
|
---|
| 70 | mDBTFile.open(filename.toStdString(), WriteMode, FILE_JPEG, MAX_LENGTH_NAME);
|
---|
| 71 |
|
---|
| 72 | int periodMSec = static_cast<int>((1.f / mFramerate) * 1E3);
|
---|
| 73 | LOG_INFO("period of the acquisition in msec: " << periodMSec);
|
---|
| 74 | mWebcam.open(mDevice, mWidth, mHeight, periodMSec);
|
---|
| 75 | mWebcam.start();
|
---|
| 76 | }
|
---|
| 77 | catch (std::exception const& e)
|
---|
| 78 | {
|
---|
| 79 | LOG_ERROR(e.what());
|
---|
| 80 | }
|
---|
| 81 | }
|
---|
| 82 |
|
---|
| 83 | ComponentBase::COMPONENT_CONFIGURATION
|
---|
| 84 | CVWebcamComponent::configureComponent(XmlComponentConfig config)
|
---|
| 85 | {
|
---|
| 86 | mDevice = config.getIntProperty("device", DEFAULT_DEVICE);
|
---|
| 87 | mWidth = config.getIntProperty("width", DEFAULT_WIDTH);
|
---|
| 88 | mHeight = config.getIntProperty("height", DEFAULT_HEIGHT);
|
---|
| 89 | mFramerate = config.getIntProperty("framerate", DEFAULT_FRAMERATE);
|
---|
| 90 | mHasUI = config.getBoolProperty("ui", false);
|
---|
| 91 | mIsRecording = config.getBoolProperty("recording", false);
|
---|
| 92 |
|
---|
| 93 | try
|
---|
| 94 | {
|
---|
| 95 | QDir directory;
|
---|
| 96 | if (!directory.exists(name().toLatin1()) && !directory.mkdir(name().toLatin1()))
|
---|
| 97 | throw std::runtime_error("cannot create the webcam directory");
|
---|
| 98 |
|
---|
| 99 | mFilenameTemplate = QString("%1/image-%2.jpg").arg(name());
|
---|
| 100 | LOG_INFO("template filename: " << mFilenameTemplate);
|
---|
| 101 | }
|
---|
| 102 | catch (std::exception const& e)
|
---|
| 103 | {
|
---|
| 104 | LOG_ERROR(e.what());
|
---|
| 105 | return ComponentBase::CONFIGURED_FAILED;
|
---|
| 106 | }
|
---|
| 107 |
|
---|
| 108 | return ComponentBase::CONFIGURED_OK;
|
---|
| 109 | }
|
---|
| 110 |
|
---|
| 111 | void
|
---|
| 112 | CVWebcamComponent::receiveFrame(cv::Mat frame)
|
---|
| 113 | {
|
---|
| 114 | if (mHasUI)
|
---|
| 115 | displayFrame(frame);
|
---|
| 116 | if (mIsRecording)
|
---|
| 117 | saveFrame(frame);
|
---|
| 118 | // create the shared memory
|
---|
| 119 | if (firstTime) {
|
---|
| 120 | mShMem = new ShMem("IMAGE", frame.step * frame.rows );
|
---|
| 121 | LOG_INFO(frame.step * frame.cols);
|
---|
| 122 | firstTime = false;
|
---|
| 123 | }
|
---|
| 124 | // send image in shared memory
|
---|
| 125 | mShMem->write(frame.data, frame.step * frame.rows );
|
---|
| 126 | }
|
---|
| 127 |
|
---|
| 128 | void
|
---|
| 129 | CVWebcamComponent::displayFrame(cv::Mat const& frame)
|
---|
| 130 | {
|
---|
| 131 | cv::imshow("WebcamComponent - Frame", frame);
|
---|
| 132 | }
|
---|
| 133 |
|
---|
| 134 | void
|
---|
| 135 | CVWebcamComponent::saveFrame(cv::Mat const& frame)
|
---|
| 136 | {
|
---|
| 137 | try
|
---|
| 138 | {
|
---|
| 139 | QString filename = mFilenameTemplate.arg(mImageCounter);
|
---|
| 140 | ++mImageCounter;
|
---|
| 141 |
|
---|
| 142 | if (!cv::imwrite(filename.toStdString(), frame))
|
---|
| 143 | throw std::runtime_error("cannot save the following frame: " + filename.toStdString());
|
---|
| 144 |
|
---|
| 145 | mDBTFile.writeRecord(road_time(), 0, filename.toStdString().c_str(), MAX_LENGTH_NAME);
|
---|
| 146 | }
|
---|
| 147 | catch (DbiteException & e)
|
---|
| 148 | {
|
---|
| 149 | LOG_ERROR("error writing data: " << e.what());
|
---|
| 150 | }
|
---|
| 151 | } |
---|