source: pacpussensors/trunk/OpencvVideo/CVWebcamComponent.cpp@ 67

Last change on this file since 67 was 60, checked in by DHERBOMEZ Gérald, 10 years ago

Add the module opencv video acquisition (compatible with 0.1.x version of the framework).

File size: 3.9 KB
Line 
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
17using namespace pacpus;
18
19// Declare the logger and register the new component.
20DECLARE_STATIC_LOGGER("pacpus.component.cvwebcam");
21REGISTER_COMPONENT(CVWebcamComponent, "CVWebcam");
22
23const int DEFAULT_DEVICE = -1;
24const int DEFAULT_WIDTH = 640;
25const int DEFAULT_HEIGHT = 480;
26const int DEFAULT_FRAMERATE = 15;
27const int MAX_LENGTH_NAME = 512;
28
29CVWebcamComponent::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
45CVWebcamComponent::~CVWebcamComponent()
46{
47}
48
49void
50CVWebcamComponent::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
61void
62CVWebcamComponent::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
83ComponentBase::COMPONENT_CONFIGURATION
84CVWebcamComponent::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
111void
112CVWebcamComponent::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
128void
129CVWebcamComponent::displayFrame(cv::Mat const& frame)
130{
131 cv::imshow("WebcamComponent - Frame", frame);
132}
133
134void
135CVWebcamComponent::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}
Note: See TracBrowser for help on using the repository browser.