forked from taeyeobyeo/Capstone
-
Notifications
You must be signed in to change notification settings - Fork 1
/
content.js
71 lines (65 loc) · 1.82 KB
/
content.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import * as tf from '@tensorflow/tfjs';
import * as posenet from '@tensorflow-models/posenet';
import Stats from 'stats.js';
import { CHROME_ENVS } from '@tensorflow/tfjs-core/dist/test_util';
//global variables
const stats = new Stats();
let isDetecting = true;
/*
* 프로그램이 실행되면 실행되는 코드
*/
async function setup(){
const video = await loadVideo();
// console.log(video);
const model = await posenet.load();
// console.log(model);
setupFPS();
animate(video, model);
}
/**
* 카메라가 활성화된 HTMLVideoElement를 생성후 리턴
*/
async function loadVideo(){
const stream = await navigator.mediaDevices.getUserMedia({ video: true, audio: false })
let video = document.createElement('video');
video.height = 480;
video.width = 640;
video.srcObject = stream;
video.play();
return video;
}
/**
* 매초 마다 pose를 detect합니다.
* @param {*} video // 카메라로 찍는 화면이 활성화된 HTMLVideoElement
* @param {*} model // posenet.PoseNet 모델
*/
function animate(video, model){
async function detect(){
stats.begin();
if(isDetecting === true){
const pose = await model.estimateSinglePose(video);
console.log(pose);
}
stats.end();
requestAnimationFrame(detect);
}
detect();
}
/**
* 카메라로 1초에 몇번 인식하고 있는지 왼쪽 상단에 표시
*/
function setupFPS() {
stats.showPanel(0); // 0: fps, 1: ms, 2: mb, 3+: custom
document.body.appendChild(stats.dom);
}
setup();
chrome.runtime.onMessage.addListener(gotMessage);
function gotMessage(message, sender, sendResponse){
console.log(message.txt);
if(message.txt === "OFF") {
isDetecting = false;
}
else if (message.txt ==="ON"){
isDetecting = true;
}
}