Create app.js
Browse files
app.js
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
const video = document.getElementById('video');
|
2 |
+
const canvas = document.getElementById('canvas');
|
3 |
+
const resultP = document.getElementById('result');
|
4 |
+
const captureButton = document.getElementById('capture');
|
5 |
+
let classifier;
|
6 |
+
|
7 |
+
// Load the pre-trained model from Huggingface
|
8 |
+
classifier = ml5.imageClassifier('https://huggingface.co/models/your-model-url', modelReady);
|
9 |
+
|
10 |
+
function modelReady() {
|
11 |
+
console.log('Model Loaded!');
|
12 |
+
}
|
13 |
+
|
14 |
+
// Access the camera
|
15 |
+
navigator.mediaDevices.getUserMedia({ video: true })
|
16 |
+
.then(stream => {
|
17 |
+
video.srcObject = stream;
|
18 |
+
})
|
19 |
+
.catch(err => {
|
20 |
+
console.error('Error accessing the camera: ', err);
|
21 |
+
});
|
22 |
+
|
23 |
+
captureButton.addEventListener('click', () => {
|
24 |
+
const context = canvas.getContext('2d');
|
25 |
+
context.drawImage(video, 0, 0, canvas.width, canvas.height);
|
26 |
+
classifyImage();
|
27 |
+
});
|
28 |
+
|
29 |
+
function classifyImage() {
|
30 |
+
classifier.classify(canvas, (err, results) => {
|
31 |
+
if (err) {
|
32 |
+
console.error(err);
|
33 |
+
return;
|
34 |
+
}
|
35 |
+
const jellyType = results[0].label;
|
36 |
+
const sugarLevel = getSugarLevel(jellyType);
|
37 |
+
const hazard = getHazardLevel(sugarLevel);
|
38 |
+
resultP.textContent = `Jelly Type: ${jellyType}, Sugar Level: ${sugarLevel}, Hazard: ${hazard}`;
|
39 |
+
});
|
40 |
+
}
|
41 |
+
|
42 |
+
function getSugarLevel(jellyType) {
|
43 |
+
// Dummy data for demonstration purposes
|
44 |
+
const sugarData = {
|
45 |
+
'jellyA': 10,
|
46 |
+
'jellyB': 20,
|
47 |
+
'jellyC': 30
|
48 |
+
};
|
49 |
+
return sugarData[jellyType] || 0;
|
50 |
+
}
|
51 |
+
|
52 |
+
function getHazardLevel(sugarLevel) {
|
53 |
+
if (sugarLevel > 25) {
|
54 |
+
return 'Red (High Hazard)';
|
55 |
+
} else if (sugarLevel > 15) {
|
56 |
+
return 'Yellow (Moderate Hazard)';
|
57 |
+
} else {
|
58 |
+
return 'Green (Low Hazard)';
|
59 |
+
}
|
60 |
+
}
|