Permalink
Browse files

mainly new sliding window code less reliant on canvas

  • Loading branch information...
1 parent 1fce20e commit 20ce1196ca8c59e3b5376d98a1203c5d5297df45 @harthur committed Jun 6, 2012
View
@@ -1,9 +1,6 @@
$(document).ready(function() {
var viewer = $("#viewer");
-
- var anno = $("#annotations");
-
viewer.on('dragover', function(e) {
e.stopPropagation();
e.preventDefault();
@@ -28,10 +25,8 @@ $(document).ready(function() {
var files = e.originalEvent.dataTransfer.files;
handleFiles(files);
-
return false;
});
-
});
function handleFiles(files) {
@@ -47,18 +42,18 @@ function handleFiles(files) {
var reader = new FileReader();
reader.onload = function(e) {
img.src = e.target.result;
- drawCanvas(img);
- detectCats();
+ drawToCanvas(img);
+
+ detector.detectCats();
};
reader.readAsDataURL(file);
}
-function drawCanvas(img) {
+function drawToCanvas(img) {
var width = img.width;
var height = img.height;
- var min = Math.min(width, height);
var max = Math.max(width, height)
var scale = Math.min(max, 420) / max;
@@ -81,23 +76,56 @@ function drawCanvas(img) {
0, 0, width, height);
}
-
-function detectCats() {
- var canvas = $("#annotations").get(0);
- var ctx = canvas.getContext("2d");
-
- ctx.lineWidth = 2;
- ctx.strokeStyle = "red";
- ctx.shadowOffsetX = 2;
- ctx.shadowOffsetY = 2;
- ctx.shadowBlur = 2;
- ctx.shadowColor = "rgba(1, 1, 1, 1)";
-
- var cats = [{x: 20, y: 20, width: 30, height: 30}]; //kittydar.detectCats(canvas);
-
- for (var i = 0; i < cats.length; i++) {
- var cat = cats[i];
- ctx.strokeRect(cat.x, cat.y, cat.width, cat.height);
- //context.strokeRect(cat.x - 1, cat.y - 1, cat.width + 1, cat.height + 1);
+var detector = {
+ detectCats: function() {
+ var canvas = $("#preview").get(0);
+
+ if (window.Worker) {
+ var worker = new Worker("detection-worker.js");
+ worker.onmessage = this.onMessage;
+ worker.onerror = this.onError;
+
+ var imagedata = canvas.getImageData(0, 0, canvas.width, canvas.height);
+ worker.postMessage(imagedata);
+ }
+ else {
+ var rects = kittydar.detectCats(canvas);
+ this.paintRects(rects);
+ }
+ },
+
+ paintRects : function(rects) {
+ var canvas = $("#annotations").get(0);
+ var ctx = canvas.getContext("2d");
+
+ ctx.lineWidth = 2;
+ ctx.strokeStyle = "red";
+
+ for (var i = 0; i < rects.length; i++) {
+ var rect = rects[i];
+ ctx.strokeRect(rect.x, rect.y, rect.width, rect.height);
+ }
+ },
+
+ onMessage : function(event) {
+ var data = JSON.parse(event.data);
+
+ if (data.type == 'progress') {
+ this.showProgress(data);
+ }
+ else if (data.type == 'result') {
+ this.paintRects(data.rects);
+ }
+ },
+
+ onError : function(event) {
+ console.log("Error from detection Worker:", event.message)
+ },
+
+ showProgress : function(progress) {
+ /*
+ var completed = progress.iterations / trainer.iterations * 100;
+ $("#progress-completed").css("width", completed + "%");
+ */
}
-}
+}
View
@@ -67,13 +67,6 @@ body {
}
@font-face {
- font-family: Catastrophe;
- src: url(catastrophe.ttf);
- font-weight: 400;
-}
-
-
-@font-face {
font-family: CatsAlphabet;
src: url(CatsAlphabet.ttf);
font-weight: 400;
View
@@ -1,5 +1,6 @@
var Canvas = require("canvas"),
brain = require("brain"),
+ hog = require("hog-descriptor"),
features = require("./features");
var network = require("./network.json");
@@ -8,70 +9,129 @@ var net = new brain.NeuralNetwork().fromJSON(network);
var threshold = 0.9;
exports.detectCats = function detectCats(canvas) {
- var width = canvas.width,
- height = canvas.height;
+ var imagedata = canvas;
+ if (imagedata.data && imagedata.width && imagedata.height) {
+ // it's actually an imagedata object
+ canvas = new Canvas(imagedata.width, imagedata.height);
+ var ctx = canvas.getContext("2d");
+ ctx.putImageData(imagedata, 0, 0);
+ }
+
+ var width = canvas.width;
+ var height = canvas.height;
- // scale to reduce computation time
- var scale = 360 / Math.max(width, height);
- width = width * scale;
- height = height * scale;
+ var max = Math.max(width, height)
+ var scale = Math.min(max, 360) / max;
+
+ width *= scale;
+ height *= scale;
canvas = resizeCanvas(canvas, width, height);
- var min = 48;
- var max = Math.min(width, height);
+ var min = 48; // starting window size
var cats = [];
- var total = 0;
for (var size = min; size < max; size += 12) {
- var info = detectAtScale(canvas, size, min);
- cats = cats.concat(info.cats);
- total += info.total;
+ cats = cats.concat(detectAtSize(canvas, size, min));
}
- console.log(cats[0])
- console.log(scale)
cats = cats.map(function(cat) {
return {
- x: cat.x / scale,
- y: cat.y / scale,
- width: cat.width / scale,
- height: cat.height / scale,
+ x: Math.floor(cat.x / scale),
+ y: Math.floor(cat.y / scale),
+ width: Math.floor(cat.width / scale),
+ height: Math.floor(cat.height / scale),
prob: cat.prob
}
});
- console.log(cats[0]);
+ return cats;
+}
- return {cats: cats, total: total};
+function detectAtSize(canvas, size, fixed) {
+ var scale = fixed / size;
+
+ var width = Math.floor(canvas.width * scale);
+ var height = Math.floor(canvas.height * scale);
+
+ canvas = resizeCanvas(canvas, width, height);
+ var ctx = canvas.getContext("2d");
+ var imagedata = ctx.getImageData(0, 0, width, height);
+
+ var cats = detectAtFixed(imagedata, fixed, scale);
+ return cats;
}
-function isCat(canvas) {
- var fts = features.extractFeatures(canvas);
+function isCat(intensities) {
+ var options = {
+ "cellSize": 4,
+ "blockSize": 2,
+ "blockStride": 1,
+ "bins": 6,
+ "norm": "L2"
+ };
+
+ var fts = hog.extractHOGFromIntensities(intensities, options);
var prob = net.run(fts)[0];
return prob;
}
-function detectAtScale(canvas, scale, resizeTo) {
- var shift = Math.floor(scale / 9);
+function detectAtFixed(imagedata, fixed, scale) {
+ console.log("detect at fixed", fixed, scale);
+ var shift = 6;
- var cats = [];
- var count = 0;
+ var intensities = hog.intensities(imagedata);
- for (var y = 0; y + scale < canvas.height; y += shift) {
- for (var x = 0; x + scale < canvas.width; x += shift) {
- count++;
- var win = cropAndResize(canvas, x, y, scale, resizeTo);
+ var cats = [];
+ for (var y = 0; y + fixed < imagedata.height; y += shift) {
+ for (var x = 0; x + fixed < imagedata.width; x += shift) {
+ var win = getRect(intensities, x, y, fixed, fixed);
var prob = isCat(win);
+
if (prob > threshold) {
- cats.push({ x: x, y: y, width: scale, height: scale, prob: prob });
+ cats.push({
+ x: Math.floor(x / scale),
+ y: Math.floor(y / scale),
+ width: Math.floor(fixed / scale),
+ height: Math.floor(fixed / scale),
+ prob: prob
+ });
}
}
}
- return {cats: cats, total: count};
+ return cats;
+}
+
+function getRect(matrix, x, y, width, height) {
+ var square = new Array(height);
+
+ for (var i = 0; i < height; i++) {
+ square[i] = new Array(width);
+
+ for (var j = 0; j < width; j++) {
+ square[i][j] = matrix[y + i][x + j];
+ }
+ }
+ return square;
}
+function getIntensities(elements, x, y, size, bins) {
+ var histogram = zeros(bins);
+
+ for (var i = 0; i < size; i++) {
+ for (var j = 0; j < size; j++) {
+ var vector = elements[y + i][x + j];
+ var bin = binFor(vector.orient, bins);
+ histogram[bin] += vector.mag;
+ }
+ }
+ return histogram;
+}
+
+
+
+
function resizeCanvas(canvas, width, height) {
var resizeCanvas = new Canvas(width, height);
var ctx = resizeCanvas.getContext('2d');
View
@@ -42,13 +42,11 @@ function runTest() {
height: vals[3]
};
- utils.drawImgToCanvas(file, function(canvas) {
+ utils.drawImgToCanvas(file, function(err, canvas) {
console.time("detecting")
console.log(canvas)
- var info = kittydar.detectCats(canvas);
- var cats = info.cats;
+ var cats = kittydar.detectCats(canvas);
console.timeEnd("detecting")
- total += info.total;
var missed = true;
@@ -78,7 +76,6 @@ function runTest() {
function() {
console.log("misses", misses)
console.log("truePos", truePos, "falsePos", falsePos);
- console.log("total", total);
var precision = truePos / (truePos + falsePos);
console.log("precision", precision);
@@ -6,7 +6,6 @@
FlickRaw.shared_secret="5f76a97053f99673"
$count = 0
-$start = 4100
$fetched = Hash.new
@@ -19,7 +18,12 @@ def getPage(page)
if $fetched[url] != 1
$fetched[url] = 1
- file = "NEGS_FLICKR_TEST/#{$count + $start}.jpg"
+ name = rand(100000000000)
+
+ file = "NEGS_FLICKR2/#{name}.jpg"
+
+ puts file
+
open(file, 'wb') do |file|
file << open(url).read
end
@@ -29,6 +33,6 @@ def getPage(page)
end
end
-20.times do |i|
- getPage(i * 3)
+120.times do |i|
+ getPage((i * 3))
end
@@ -8,12 +8,12 @@ var http = require("http"),
utils = require("../../utils");
var dir = __dirname + "/NEGS_FLICKR/";
-var outdir = __dirname + "/NEGS_RAW_SAMPLED/";
+var outdir = __dirname + "/NEGS_SAMPLED/";
var count = 0;
-var start = 218652;
+var start = 18002;
-var perFile = 9;
+var perFile = 1;
fs.readdir(dir, function(err, files) {
if (err) throw err;
@@ -22,7 +22,7 @@ fs.readdir(dir, function(err, files) {
return path.extname(file) == ".jpg";
});
- images = images.slice(1000 * 25, 1000 * 26);
+ images = images.slice(9000 * 2, 9000 * 3);
console.log(images.length)
images.forEach(function(image) {
@@ -33,6 +33,7 @@ fs.readdir(dir, function(err, files) {
}
catch(e) {
console.log(e, dir + image);
+ return;
}
var canvases = generateFromRaw(canvas);
Oops, something went wrong.

0 comments on commit 20ce119

Please sign in to comment.