Permalink
Browse files

add missing files for features

  • Loading branch information...
1 parent 7e43722 commit 108b736583d842afb1ae2b1f5c450ac779246b35 @harthur committed May 31, 2012
Showing with 400 additions and 159 deletions.
  1. +14 −0 features.js
  2. +7 −0 hog-params.json
  3. +19 −40 kittydar.js
  4. +1 −5 package.json
  5. +55 −6 test.js
  6. +104 −0 testing/test.js
  7. +0 −16 training/features.js
  8. +172 −0 training/findparams.js
  9. +0 −45 training/test.js
  10. +4 −5 training/train.js
  11. +10 −10 training/upload.js
  12. +14 −32 {training → }/utils.js
View
@@ -0,0 +1,14 @@
+var hog = require("hog-descriptor"),
+ utils = require("./utils");
+
+var defaultParams = require("./hog-params.json");
+
+var size = 48;
+
+exports.extractFeatures = function(canvas, params) {
+ canvas = utils.resizeCanvas(canvas, size, size);
+
+ var descriptor = hog.extractHOG(canvas, params || defaultParams);
+
+ return descriptor;
+}
View
@@ -0,0 +1,7 @@
+{
+ "cellSize": 6,
+ "blockSize": 2,
+ "blockStride": 1,
+ "bins": 9,
+ "norm": "L2"
+}
View
@@ -1,73 +1,52 @@
-var hog = require("hog-descriptor"),
- Canvas = require("canvas"),
+var Canvas = require("canvas"),
brain = require("brain"),
- utils = require("./utils"),
- network = require("./cv-network.json");
-
-var threshold = 0.95;
-
-var cropsdir = "/CROPS/"
+ features = require("./features");
+var network = require("./network.json");
var net = new brain.NeuralNetwork().fromJSON(network);
+var threshold = 0.9;
+
exports.detectCats = function detectCats(canvas) {
var found = [];
var minScale = 48;
var maxScale = Math.min(canvas.width, canvas.height);
- var step = 10;
+ var step = 14;
var cats = [];
+ var total = 0;
for (var scale = minScale; scale < maxScale; scale += step) {
- var detected = detectAtScale(canvas, scale, minScale);
- cats = cats.concat(detected)
+ var info = detectAtScale(canvas, scale, minScale);
+ cats = cats.concat(info.cats);
+ total += info.total;
}
-
- return cats;
+ return {cats: cats, total: total};
}
-function catProb(canvas) {
- var descriptor = hog.extractHOG(canvas, {
- cellSize: 6,
- blockSize: 2,
- bins: 6,
- norm: "L2"
- });
-
- var prob = net.run(descriptor)[0];
+function isCat(canvas) {
+ var fts = features.extractFeatures(canvas);
+ var prob = net.run(fts)[0];
return prob;
}
function detectAtScale(canvas, scale, resizeTo) {
- console.log("\n\ndetecting at", scale, "px");
-
- var shift = Math.floor(scale / 10); // 8
+ var shift = Math.floor(scale / 9);
var cats = [];
var count = 0;
+
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 prob = catProb(win);
- //console.log("testing at", x, y, "prob:", prob, "scale:", scale);
+ var prob = isCat(win);
if (prob > threshold) {
- saveCrop(win, x, y, scale, prob);
-
- console.log("prob", prob, "detected at", scale, "px", "x:", x, "y:", y);
- cats.push({ x: x, y: y, width: scale, height: scale });
+ cats.push({ x: x, y: y, width: scale, height: scale, prob: prob });
}
}
}
- console.log(count)
- return cats;
-}
-
-function saveCrop(canvas, x, y, scale, prob) {
- var file = __dirname + cropsdir + x + "_" + y + "_" + scale + "_" + prob.toFixed(2) + ".jpg"
- utils.writeCanvasToFile(canvas, file, function(err) {
- //console.log(err)
- });
+ return {cats: cats, total: count};
}
function cropAndResize(canvas, x, y, size, resizeTo) {
View
@@ -1,6 +1,6 @@
{
"name": "kittydar",
- "description": "Cat detection in images",
+ "description": "Cat detector",
"version": "0.1.0",
"author": "Heather Arthur <fayearthur@gmail.com>",
"repository": {
@@ -10,9 +10,5 @@
"dependencies" : {
"canvas" : ">=0.10.0",
},
- "devDependencies": {
- "mocha" : ">=1.0.0",
- "should" : ">=0.6.0",
- },
"main": "./kittydar"
}
View
61 test.js
@@ -1,7 +1,56 @@
-var kittydar = require("./kittydar"),
- utils = require("./utils");
+var assert = require("assert");
-utils.drawImgToCanvas(__dirname + "/test8.jpg", function(canvas) {
- var cats = kittydar.detectCats(canvas);
- console.log(cats);
-})
+
+function test(r1, r2, expected) {
+ var overlaps = doesOverlap(r1, r2);
+
+ assert.equal(overlaps, expected, JSON.stringify(r1) + JSON.stringify(r2));
+}
+
+var r1 = {x: 0, y: 0, width: 10, height: 10};
+var r2 = {x: 0, y: 20, width: 10, height: 10};
+
+test(r1, r2, false);
+test(r2, r1, false);
+
+r1 = {x: 0, y: 0, width: 10, height: 10};
+r2 = {x: 20, y: 0, width: 10, height: 10};
+
+test(r1, r2, false);
+test(r2, r1, false);
+
+r1 = {x: 0, y: 0, width: 10, height: 10};
+r2 = {x: 5, y: 5, width: 10, height: 10};
+
+test(r1, r2, false);
+test(r2, r1, false);
+
+r1 = {x: 0, y: 0, width: 10, height: 10};
+r2 = {x: 2, y: 2, width: 10, height: 10};
+
+test(r1, r2, true);
+test(r2, r1, true);
+
+
+function doesOverlap(cat, rect) {
+ var overlapW, overlapH;
+
+ if (cat.x > rect.x) {
+ overlapW = (rect.x + rect.width) - cat.x;
+ }
+ else {
+ overlapW = (cat.x + cat.width) - rect.x;
+ }
+
+ if (cat.y > rect.y) {
+ overlapH = (rect.y + rect.height) - cat.y;
+ }
+ else {
+ overlapH = (cat.y + cat.height) - rect.y;
+ }
+
+ if (overlapW > 0 && overlapH > 0) {
+ return (overlapH * overlapW) > (cat.width * cat.height * 0.5);
+ }
+ return false;
+}
View
@@ -0,0 +1,104 @@
+var brain = require("brain"),
+ fs = require("fs"),
+ path = require("path"),
+ async = require("async"),
+ utils = require("../utils"),
+ kittydar = require("../kittydar");
+
+var dir = __dirname + "/TEST/";
+
+runTest();
+
+function runTest() {
+ var truePos = 0;
+ var falsePos = 0;
+ var misses = 0;
+ var total = 0;
+
+ fs.readdir(dir, function(err, files) {
+ if (err) throw err;
+
+ var images = files.filter(function(file) {
+ return path.extname(file) == ".jpg";
+ })
+
+ images = images.slice(0, 1);
+
+ async.forEach(images, function(file, done) {
+ file = dir + file;
+
+ fs.readFile(file + ".rect", "utf-8", function(err, text) {
+ if (err) throw err;
+
+ var vals = text.split(" ");
+ var rect = {
+ x: vals[0],
+ y: vals[1],
+ width: vals[2],
+ height: vals[3]
+ };
+
+ utils.drawImgToCanvas(file, function(canvas) {
+ console.time("detecting")
+ var info = kittydar.detectCats(canvas);
+ var cats = info.cats;
+ console.timeEnd("detecting")
+ total += info.total;
+
+ var missed = true;
+
+ console.log("testing", file);
+
+ cats.forEach(function(cat) {
+ if (doesOverlap(cat, rect)) {
+ missed = false;
+ truePos++;
+ }
+ else {
+ falsePos++;
+ }
+ });
+
+ if (missed) {
+ misses++;
+ }
+
+ done();
+ });
+ })
+ },
+ function() {
+ console.log("misses", misses)
+ console.log("truePos", truePos, "falsePos", falsePos);
+ console.log("total", total);
+
+ var precision = truePos / (truePos + falsePos);
+ console.log("precision", precision);
+
+ var fpr = falsePos / (total - truePos);
+ });
+ });
+}
+
+function doesOverlap(cat, rect) {
+ var overlapW, overlapH;
+
+ if (cat.x > rect.x) {
+ overlapW = (rect.x + rect.width) - cat.x;
+ }
+ else {
+ overlapW = (cat.x + cat.width) - rect.x;
+ }
+
+ if (cat.y > rect.y) {
+ overlapH = (rect.y + rect.height) - cat.y;
+ }
+ else {
+ overlapH = (cat.y + cat.height) - rect.y;
+ }
+
+ if (overlapW > 0 && overlapH > 0) {
+ return (overlapH * overlapW) > (cat.width * cat.height * 0.5);
+ }
+ return false;
+}
View
@@ -1,16 +0,0 @@
-var hog = require("hog-descriptor"),
- utils = require("./utils");
-
-var resize = 48;
-
-exports.extractFeatures = function(file, callback) {
- utils.drawImgToCanvas(file, resize, function(canvas) {
- var descriptor = hog.extractHOG(canvas, {
- cellSize: 6,
- blockSize: 2,
- bins: 6,
- norm: "L2"
- });
- callback(descriptor);
- });
-}
Oops, something went wrong.

0 comments on commit 108b736

Please sign in to comment.