Skip to content

Commit

Permalink
Create ComputerVision.js
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 4, 2024
1 parent 23f40cb commit a7fbdb1
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions blockchain_integration/pi_network/cognita/ComputerVision.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import * as cv from 'opencv-js';

class ComputerVision {
constructor() {
this.video = document.getElementById('video');
this.canvas = document.getElementById('canvas');
this.ctx = this.canvas.getContext('2d');
this.faceCascade = new cv.CascadeClassifier(cv.FACE_CASCADE);

this.video.addEventListener('play', () => {
this.interval = setInterval(() => {
this.detectFaces();
}, 100);
});
}

detectFaces() {
this.ctx.drawImage(this.video, 0, 0, this.canvas.width, this.canvas.height);
const gray = new cv.Mat();
cv.cvtColor(this.ctx, gray, cv.COLOR_RGBA2GRAY);
const faces = new cv.RectVector();
this.faceCascade.detectMultiScale(gray, faces);
for (let i = 0; i < faces.size(); i++) {
const face = faces.get(i);
this.ctx.beginPath();
this.ctx.rect(face.x, face.y, face.width, face.height);
this.ctx.strokeStyle = 'green';
this.ctx.stroke();
}
}
}

export default ComputerVision;

0 comments on commit a7fbdb1

Please sign in to comment.