Added signal detection option

Former-commit-id: 1bd70d02814d6238c488db765d9d25af655c1872
This commit is contained in:
johan 2014-01-26 15:51:02 +01:00
parent f5317bc2d9
commit 305a2e856a
6 changed files with 140 additions and 58 deletions

View File

@ -120,7 +120,7 @@ void OptionsParser::usage() const {
for(i = parameters.parameters.begin();
i != parameters.parameters.end(); i++)
{
cerr.width(31);
cerr.width(33);
cerr << std::left << " " + (*i)->usageLine();
cerr.width(40);

View File

@ -17,12 +17,15 @@ include_directories(
set(Hyperion_V4L2_HEADERS
V4L2Grabber.h
ProtoConnection.h
ImageHandler.h
VideoStandardParameter.h
)
set(Hyperion_V4L2_SOURCES
hyperion-v4l2.cpp
V4L2Grabber.cpp
ProtoConnection.cpp
ImageHandler.cpp
)
set(Hyperion_V4L2_PROTOS
@ -42,6 +45,7 @@ add_executable(hyperion-v4l2
target_link_libraries(hyperion-v4l2
getoptPlusPlus
blackborder
hyperion-utils
${PROTOBUF_LIBRARIES}
pthread

View File

@ -0,0 +1,41 @@
// hyperion-v4l2 includes
#include "ImageHandler.h"
ImageHandler::ImageHandler(const std::string &address, int priority, double signalThreshold, bool skipProtoReply) :
_priority(priority),
_connection(address),
_signalThreshold(signalThreshold),
_signalProcessor(100, 50, 0, uint8_t(std::min(255, std::max(0, int(255*signalThreshold)))))
{
_connection.setSkipReply(skipProtoReply);
}
void ImageHandler::receiveImage(const Image<ColorRgb> &image)
{
// check if we should do signal detection
if (_signalThreshold < 0)
{
_connection.setImage(image, _priority, 200);
}
else
{
if (_signalProcessor.process(image))
{
std::cout << "Signal state = " << (_signalProcessor.getCurrentBorder().unknown ? "off" : "on") << std::endl;
}
// consider an unknown border as no signal
// send the image to Hyperion if we have a signal
if (!_signalProcessor.getCurrentBorder().unknown)
{
_connection.setImage(image, _priority, 200);
}
}
}
void ImageHandler::imageCallback(void *arg, const Image<ColorRgb> &image)
{
ImageHandler * handler = static_cast<ImageHandler *>(arg);
handler->receiveImage(image);
}

View File

@ -0,0 +1,34 @@
// blackborder includes
#include <blackborder/BlackBorderProcessor.h>
// hyperion-v4l includes
#include "ProtoConnection.h"
/// This class handles callbacks from the V4L2 grabber
class ImageHandler
{
public:
ImageHandler(const std::string & address, int priority, double signalThreshold, bool skipProtoReply);
/// Handle a single image
/// @param image The image to process
void receiveImage(const Image<ColorRgb> & image);
/// static function used to direct callbacks to a ImageHandler object
/// @param arg This should be an ImageHandler instance
/// @param image The image to process
static void imageCallback(void * arg, const Image<ColorRgb> & image);
private:
/// Priority for calls to Hyperion
const int _priority;
/// Hyperion proto connection object
ProtoConnection _connection;
/// Threshold used for signal detection
double _signalThreshold;
/// Blackborder detector which is used as a signal detector (unknown border = no signal)
hyperion::BlackBorderProcessor _signalProcessor;
};

View File

@ -0,0 +1,36 @@
// getoptPlusPLus includes
#include <getoptPlusPlus/getoptpp.h>
using namespace vlofgren;
/// Data parameter for the video standard
typedef vlofgren::PODParameter<V4L2Grabber::VideoStandard> VideoStandardParameter;
namespace vlofgren {
/// Translates a string (as passed on the commandline) to a color standard
///
/// @param[in] s The string (as passed on the commandline)
/// @return The color standard
/// @throws Parameter::ParameterRejected If the string did not result in a video standard
template<>
V4L2Grabber::VideoStandard VideoStandardParameter::validate(const std::string& s) throw (Parameter::ParameterRejected)
{
QString input = QString::fromStdString(s).toLower();
if (input == "pal")
{
return V4L2Grabber::PAL;
}
else if (input == "ntsc")
{
return V4L2Grabber::NTSC;
}
else if (input == "no-change")
{
return V4L2Grabber::NO_CHANGE;
}
throw Parameter::ParameterRejected("Invalid value for video standard. Valid values are: PAL, NTSC, and NO-CHANGE");
return V4L2Grabber::NO_CHANGE;
}
}

View File

@ -9,43 +9,17 @@
// getoptPlusPLus includes
#include <getoptPlusPlus/getoptpp.h>
// blackborder includes
#include <blackborder/BlackBorderProcessor.h>
// hyperion-v4l2 includes
#include "V4L2Grabber.h"
#include "ProtoConnection.h"
#include "VideoStandardParameter.h"
#include "ImageHandler.h"
using namespace vlofgren;
/// Data parameter for the video standard
typedef vlofgren::PODParameter<V4L2Grabber::VideoStandard> VideoStandardParameter;
namespace vlofgren {
/// Translates a string (as passed on the commandline) to a color standard
///
/// @param[in] s The string (as passed on the commandline)
/// @return The color standard
/// @throws Parameter::ParameterRejected If the string did not result in a video standard
template<>
V4L2Grabber::VideoStandard VideoStandardParameter::validate(const std::string& s) throw (Parameter::ParameterRejected)
{
QString input = QString::fromStdString(s).toLower();
if (input == "pal")
{
return V4L2Grabber::PAL;
}
else if (input == "ntsc")
{
return V4L2Grabber::NTSC;
}
else if (input == "no-change")
{
return V4L2Grabber::NO_CHANGE;
}
throw Parameter::ParameterRejected("Invalid value for video standard. Valid values are: PAL, NTSC, and NO-CHANGE");
return V4L2Grabber::NO_CHANGE;
}
}
// save the image as screenshot
void saveScreenshot(void *, const Image<ColorRgb> & image)
{
@ -54,13 +28,6 @@ void saveScreenshot(void *, const Image<ColorRgb> & image)
pngImage.save("screenshot.png");
}
// send the image to Hyperion
void sendImage(void * arg, const Image<ColorRgb> & image)
{
ProtoConnection * connection = static_cast<ProtoConnection *>(arg);
connection->setImage(image, 50, 200);
}
int main(int argc, char** argv)
{
try
@ -79,6 +46,7 @@ int main(int argc, char** argv)
IntParameter & argSizeDecimation = parameters.add<IntParameter> ('s', "size-decimator", "Decimation factor for the output size [default=1]");
IntParameter & argFrameDecimation = parameters.add<IntParameter> ('f', "frame-decimator", "Decimation factor for the video frames [default=1]");
SwitchParameter<> & argScreenshot = parameters.add<SwitchParameter<>> (0x0, "screenshot", "Take a single screenshot, save it to file and quit");
DoubleParameter & argSignalThreshold = parameters.add<DoubleParameter> ('t', "signal-threshold", "The signal threshold for detecting the presence of a signal. Value should be between 0.0 and 1.0.");
StringParameter & argAddress = parameters.add<StringParameter> ('a', "address", "Set the address of the hyperion server [default: 127.0.0.1:19445]");
IntParameter & argPriority = parameters.add<IntParameter> ('p', "priority", "Use the provided priority channel (the lower the number, the higher the priority) [default: 800]");
SwitchParameter<> & argSkipReply = parameters.add<SwitchParameter<>> (0x0, "skip-reply", "Do not receive and check reply messages from Hyperion");
@ -96,6 +64,7 @@ int main(int argc, char** argv)
argFrameDecimation.setDefault(1);
argAddress.setDefault("127.0.0.1:19445");
argPriority.setDefault(800);
argSignalThreshold.setDefault(-1);
// parse all options
optionParser.parse(argc, const_cast<const char **>(argv));
@ -126,10 +95,8 @@ int main(int argc, char** argv)
}
else
{
ProtoConnection connection(argAddress.getValue());
connection.setSkipReply(argSkipReply.isSet());
grabber.setCallback(&sendImage, &connection);
ImageHandler handler(argAddress.getValue(), argPriority.getValue(), argSignalThreshold.getValue(), argSkipReply.isSet());
grabber.setCallback(&ImageHandler::imageCallback, &handler);
grabber.capture();
}
grabber.stop();