Created
October 31, 2015 18:34
-
-
Save coleww/cbd3a877ffba60c3abcb to your computer and use it in GitHub Desktop.
isProbablyAMeme.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function isProbablyAMeme(ctx2, w, h) { | |
// given a ctx, get it's pixel data | |
var pixels = ctx2.getImageData(0, 0, w, h) | |
// threshhold filter the pixels. looking for bright white a la bold impact meme text | |
for (var i = 0; i < pixels.data.length; i += 4) { | |
var avg = (pixels.data[i] + pixels.data[i + 1] + pixels.data[i + 2]) / 3 | |
var ne = avg > 240 ? 0 : 255 | |
pixels.data[i] = ne | |
pixels.data[i + 1] = ne | |
pixels.data[i + 2] = ne | |
} | |
var canvas = new Canvas(w, h) | |
var ctx = canvas.getContext('2d') | |
ctx.putImageData(pixels, 0, 0) | |
// run OCR over the threshholded canvas, remove garbage from the detected text | |
var ocr = Ocrad(canvas).replace(/\W|\_/g, '') | |
// a handful of misidentified letters/numbers is ok, but not a dozen. | |
return ocr.length > 12 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment