Skip to content

Commit

Permalink
Added real fire detector
Browse files Browse the repository at this point in the history
  • Loading branch information
aahz committed May 29, 2015
1 parent b1c86ff commit e943332
Showing 1 changed file with 119 additions and 48 deletions.
167 changes: 119 additions & 48 deletions app/detector.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
var cv = require('opencv');
var cv = require('opencv'),
extend = require('extend');

function Detector () {
var _this = this;
Expand Down Expand Up @@ -33,94 +34,164 @@ _proto._getMovementImage = function (image) {
var diff1 = new cv.Matrix(image.width(), image.height()),
diff2 = new cv.Matrix(image.width(), image.height());

diff1.absDiff(_this.frames.previous.copy(), image);
diff2.absDiff(_this.frames.current.copy(), image);
diff1.absDiff(_this.frames.current.copy(), image);
diff2.absDiff(_this.frames.previous.copy(), image);

processedImage.bitwiseAnd(diff1, diff2);
}

return processedImage;
};

_proto.detectMotion = function (image) {
var _this = this,
result = {};

if ( _this._isDetectionAllowed() ) {
var processedImage = _this._getMovementImage(image),
contours;
_proto._getRawContours = function (image) {
image.convertGrayscale();
image = image.threshold(60, 255, "Binary");
image.dilate(1.5);

processedImage.convertGrayscale();
processedImage = processedImage.threshold(60, 255, "Binary");
processedImage.dilate(1.5);
return image.findContours();
};

contours = processedImage.findContours();
_proto._getPreparedContoursData = function (contours) {
var result = {};

for (var c = 0; c < contours.size(); ++c) {
result['contour' + c] = [];
for (var c = 0; c < contours.size(); c++) {
result['contour' + c] = [];

for (var i = 0; i < contours.cornerCount(c); ++i) {
var point = contours.point(c, i);
for (var i = 0; i < contours.cornerCount(c); ++i) {
var point = contours.point(c, i);

result['contour' + c].push([point.x, point.y]);
}
result['contour' + c].push([point.x, point.y]);
}
}

return result;
};

_proto.detectFlame = function (image) {
var _this = this,
result = {};
_proto._getContoursData = function (image) {
var _this = this;

if ( _this._isDetectionAllowed() ) {
var processedImage = _this._getMovementImage(image),
contours;
return _this._getPreparedContoursData(_this._getRawContours(image));
};

processedImage.convertGrayscale();
processedImage = processedImage.threshold(100, 250, "Binary");
processedImage.dilate(1.1);
_proto._isPossibleFireColor = function (point) {
var breakLine = {
red: 180,
saturation: 65,
value: 150
},
fireColorMask = (point.red > breakLine.red && point.red > point.green && point.green > point.blue),
saturation = (point.value > breakLine.value && point.saturation > breakLine.saturation && fireColorMask ? point.saturation : 0);

contours = processedImage.findContours();
return saturation;
};

for (var c = 0; c < contours.size(); ++c) {
result['contour' + c] = [];
_proto.detectMotion = function (image) {
var _this = this,
result = {};

for (var i = 0; i < contours.cornerCount(c); ++i) {
var point = contours.point(c, i);
if ( _this._isDetectionAllowed() ) {
var processedImage = _this._getMovementImage(image);

result['contour' + c].push([point.x, point.y]);
}
}
result = extend({}, result, _this._getContoursData(processedImage));
}

return result;
};

_proto.detectSmoke = function (image) {
_proto.detectFlame = function (image, detectionBase) {
var _this = this,
result = {};

if ( !detectionBase || typeof detectionBase !== 'string' ) {
detectionBase = 'common';
}

if ( _this._isDetectionAllowed() ) {
var processedImage = _this._getMovementImage(image),
var originalImageSize = image.size(),

resizedImage = image.copy(),
resizedImageHSV,
resizedImageSize,

movementImage,
movementImageContours,

resultMatrix,
contours;

processedImage.convertGrayscale();
processedImage = processedImage.threshold(150, 255, "Binary");
processedImage.dilate(0.1);
if ( originalImageSize[0] < 1 || originalImageSize[1] < 1 ) {
throw new Error('Got a blank image');
}

movementImage = _this._getMovementImage(image);

movementImage.resize(Math.floor(originalImageSize[1] / 5), Math.floor(originalImageSize[0] / 5));
resizedImage.resize(Math.floor(originalImageSize[1] / 5), Math.floor(originalImageSize[0] / 5));

contours = processedImage.findContours();
resizedImageHSV = resizedImage.copy();
resizedImageHSV.convertHSVscale();

for (var c = 0; c < contours.size(); ++c) {
result['contour' + c] = [];
resultMatrix = resizedImage.copy();
resizedImageSize = resizedImage.size();

for (var i = 0; i < contours.cornerCount(c); ++i) {
var point = contours.point(c, i);
movementImage.dilate(5);
movementImageContours = _this._getRawContours(movementImage);

result['contour' + c].push([point.x, point.y]);
for (var x = 0, xc = resizedImageSize[0]; x < xc; x++) {
for (var y = 0, yc = resizedImageSize[1]; y < yc; y++) {
resultMatrix.pixel(x, y, [0, 0, 0]);
}
}

if ( detectionBase === 'movement' ) {
for (var i = 0, ic = movementImageContours.size(); i < ic; i++) {
var boundingArea = movementImageContours.boundingRect(i);

for (var x = boundingArea.y, xc = boundingArea.y + boundingArea.height; x < xc; x++) {
for (var y = boundingArea.x, yc = boundingArea.x + boundingArea.width; y < yc; y++) {
var pointRGB = resizedImage.pixel(x, y),
pointHSV = resizedImageHSV.pixel(x, y),
fireColor = _this._isPossibleFireColor({
red: pointRGB[2],
green: pointRGB[1],
blue: pointRGB[0],
hue: pointHSV[0],
saturation: pointHSV[1],
value: pointHSV[2]
});

resultMatrix.pixel(x, y, [fireColor, fireColor, fireColor]);
}
}
}
}
else {
for (var x = 0, xc = resizedImageSize[0]; x < xc; x++) {
for (var y = 0, yc = resizedImageSize[1]; y < yc; y++) {
var pointRGB = resizedImage.pixel(x, y),
pointHSV = resizedImageHSV.pixel(x, y),
fireColor = _this._isPossibleFireColor({
red: pointRGB[2],
green: pointRGB[1],
blue: pointRGB[0],
hue: pointHSV[0],
saturation: pointHSV[1],
value: pointHSV[2]
});

resultMatrix.pixel(x, y, [fireColor, fireColor, fireColor]);
}
}
}

// Reduce noise:
resultMatrix.dilate(0.5);

resultMatrix.resize(originalImageSize[1], originalImageSize[0]);

contours = _this._getRawContours(resultMatrix);

result = extend({}, result, _this._getPreparedContoursData(contours));
}

return result;
Expand Down

0 comments on commit e943332

Please sign in to comment.