Skip to content

Instantly share code, notes, and snippets.

@ukyo
Created September 9, 2018 08:49
Show Gist options
  • Save ukyo/578e54d2c0cdf2bf9bf75175e8f45499 to your computer and use it in GitHub Desktop.
Save ukyo/578e54d2c0cdf2bf9bf75175e8f45499 to your computer and use it in GitHub Desktop.
顔認識するやつ
<canvas id="canvas" width="640" height="480"></canvas>
<script>
const img = new Image;
img.src = "lena.png";
let ctx;
let arr;
img.onload = () => {
ctx = canvas.getContext("2d");
ctx.drawImage(img, 0, 0);
arr = ctx.getImageData(0, 0, 640, 480).data;
};
const fugafuga = new Uint8Array(640 * 480);
</script>
<script src="opencv.js"></script>
<script>
cv.onRuntimeInitialized = async () => {
const fileName = "haarcascade_frontalface_default.xml";
const frontalface = new Uint8Array(await fetch(fileName).then(res => res.arrayBuffer()));
cv.FS_createDataFile("/", fileName, frontalface, true, true);
const classifier = new cv.CascadeClassifier();
classifier.load(fileName);
const frame = cv.imread("canvas");
const grayFrame = new cv.Mat();
cv.cvtColor(frame, grayFrame, cv.COLOR_RGBA2GRAY);
cv.equalizeHist(grayFrame, grayFrame);
const faces = new cv.RectVector();
classifier.detectMultiScale(grayFrame, faces, 1.1, 3);
const box = faces.get(0);
ctx.rect(box.x, box.y, box.width, box.height);
ctx.stroke();
};
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment