-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript2.js
More file actions
84 lines (72 loc) · 2.07 KB
/
Copy pathscript2.js
File metadata and controls
84 lines (72 loc) · 2.07 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
76
77
78
79
80
81
82
83
84
let net;
let imgEl = document.getElementById("img");
let descEl = document.getElementById("descripcion_imagen");
const webCamElement = document.getElementById("webcam");
const classifier = knnClassifier.create();
const captureImg = async () => {
try {
const cam = await tf.data.webcam(webCamElement);
return await cam.capture();
} catch (error) {
console.log(error);
}
};
const app = async () => {
try {
console.log("Loading Model...");
net = await mobilenet.load();
console.log(net, "Modelo cargado correctamente");
if (imgEl.complete) {
const result = await net.classify(imgEl);
displayImagePrediction(result);
}
imgEl.onload = async () => {
const result = await net.classify(imgEl);
displayImagePrediction(result);
};
while (true) {
const img = await captureImg();
const result = await net.classify(img);
const activation = net.infer(img, "conv_preds");
let result2;
try {
result2 = await classifier.predictClass(activation);
const classes = ["Gatos", "Dino", "Elena", "Rock"];
document.getElementById("console2:" + classes[result2.label]);
} catch (error) {
console.error(error);
}
document.getElementById("console").innerHTML =
"prediction:" +
result[0].className +
" probability:" +
result[0].probability;
img.dispose();
await tf.nextFrame();
}
} catch (err) {
console.error("Error al cargar el modelo:", err);
}
};
const addExample = async (id) => {
const img = await captureImg();
const activation = net.infer(img, true);
classifier.addExample(activation, id);
img.dispose();
};
const displayImagePrediction = (result) => {
if (result) {
descEl.innerHTML = JSON.stringify(result, null, 2);
console.log(result);
} else {
console.log(
"El modelo no se cargó o el método classify no está disponible"
);
}
};
let count = 0;
const cambiarImagen = async () => {
count = count + 1;
imgEl.src = "https://picsum.photos/200/300?random=" + count;
};
app();