-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
75 lines (62 loc) · 2.94 KB
/
script.js
File metadata and controls
75 lines (62 loc) · 2.94 KB
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
72
73
74
75
// Elementos HTML
const video = document.getElementById('webcam');
const liveView = document.getElementById('liveView');
const demosSection = document.getElementById('demos');
const enableWebcamButton = document.getElementById('webcamButton');
const loader = document.getElementById('loader');
const loadingMessage = document.getElementById('loadingMessage');
// Verifica se o navegador suporta o acesso à webcam
function getUserMediaSupported() {
return !!(navigator.mediaDevices && navigator.mediaDevices.getUserMedia);
}
// Habilita o botão de webcam se a webcam for suportada
if (getUserMediaSupported()) {
enableWebcamButton.addEventListener('click', enableCam);
} else {
console.warn('getUserMedia() não é suportado pelo seu navegador');
}
// Função para habilitar a webcam e iniciar a classificação
function enableCam(event) {
if (!model) return;
// Esconde o botão após o clique
event.target.classList.add('hidden');
// Parâmetros para getUserMedia para forçar vídeo sem áudio
const constraints = { video: true };
// Ativa o fluxo de vídeo da webcam
navigator.mediaDevices.getUserMedia(constraints).then(function (stream) {
video.srcObject = stream;
video.addEventListener('loadeddata', predictWebcam);
});
}
// Variáveis de controle
let model = undefined; // Modelo será carregado aqui
const children = [];
// Carrega o modelo COCO-SSD
cocoSsd.load().then(function (loadedModel) {
model = loadedModel;
demosSection.classList.remove('hidden');
loader.classList.add('hidden'); // Esconde o loader quando o modelo estiver carregado
loadingMessage.classList.add('hidden'); // Esconde a mensagem de carregamento quando o modelo estiver carregado
});
// Função que faz a previsão e desenha na tela
function predictWebcam() {
model.detect(video).then(function (predictions) {
children.forEach(child => liveView.removeChild(child));
children.length = 0;
predictions.forEach(prediction => {
if (prediction.score > 0.66) {
const p = document.createElement('p');
p.innerText = `${prediction.class} - com ${Math.round(prediction.score * 100)}% de confiança.`;
p.className = 'absolute bg-blue-800 text-white text-sm p-2 rounded-lg shadow-md';
p.style = `left: ${prediction.bbox[0]}px; top: ${prediction.bbox[1] - 10}px; width: ${prediction.bbox[2] - 10}px;`;
const highlighter = document.createElement('div');
highlighter.className = 'absolute border-2 border-white rounded-lg';
highlighter.style = `left: ${prediction.bbox[0]}px; top: ${prediction.bbox[1]}px; width: ${prediction.bbox[2]}px; height: ${prediction.bbox[3]}px;`;
liveView.appendChild(highlighter);
liveView.appendChild(p);
children.push(highlighter, p);
}
});
window.requestAnimationFrame(predictWebcam);
});
}