-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
143 lines (111 loc) · 4.33 KB
/
Copy pathscript.js
File metadata and controls
143 lines (111 loc) · 4.33 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
// Get the video element and canvas
const video = document.getElementById('video');
const canvas = document.getElementById('canvas');
const context = canvas.getContext('2d');
const outputImage = document.getElementById('output-image'); // Assume you have an img element with id 'output-image'
// Get the start and capture buttons
const startButton = document.getElementById('start-btn');
const captureButton = document.getElementById('capture-btn');
// Access the webcam
let stream;
function startWebcam() {
navigator.mediaDevices.getUserMedia({ video: true })
.then((streamObj) => {
stream = streamObj;
video.srcObject = stream;
video.play();
// Show the capture button and hide the start button
captureButton.style.display = 'inline-block';
startButton.style.display = 'none';
})
.catch((error) => {
console.error('Error accessing the webcam:', error);
});
}
// Start the webcam when the start button is clicked
startButton.addEventListener('click', () => {
startWebcam();
});
// Capture image when the capture button is clicked
captureButton.addEventListener('click', () => {
// Pause the video stream
video.pause();
video.style.transform = 'scaleX(-1)';
video.classList.add('mirrored');
// Draw the current video frame onto the canvas
context.drawImage(video, 0, 0, canvas.width, canvas.height);
video.style.transform = 'none';
// Convert the canvas image to a base64 string
const imageDataURL = canvas.toDataURL('image/png');
const base64Image = imageDataURL.split('base64,')[1];
//outputImage.src = imageDataURL;
sendToClarifaiAPI(base64Image);
// Clear the canvas
//context.clearRect(0, 0, canvas.width, canvas.height);
video.classList.remove('mirrored');
// Resume the video stream
video.play();
});
// Function to send the captured image to Clarifai API
function sendToClarifaiAPI(base64Image) {
// URL of image to use. Change this to your image.
const IMAGE_URL = base64Image;
const raw = JSON.stringify({
"user_app_id": {
"user_id": "yuchen",
"app_id": "workflow-test"
},
"inputs": [
{
"data": {
"image": {
"base64": IMAGE_URL
}
}
}
]
});
const requestOptions = {
method: 'POST',
headers: {
'Accept': 'application/json',
'Authorization': 'Key ' + '36ac13c8a57c4b768af0d129ed924cb9'
},
body: raw
};
// NOTE: MODEL_VERSION_ID is optional, you can also call prediction with the MODEL_ID only
// https://api.clarifai.com/v2/models/{YOUR_MODEL_ID}/outputs
// this will default to the latest version_id
fetch(`https://api.clarifai.com/v2/models/BARCODE-QRCODE-Reader/versions/47850e63a4c3436d9527cdb86dda8c6b/outputs`, requestOptions)
.then(response => response.json())
.then(result => {
let code = result.outputs[0].data.regions[0].data.text.raw;
const url = 'https://world.openfoodfacts.org/api/v0/product/' + code;
fetch(url)
.then(response => response.json())
.then(result => {
console.log(result)
console.log(result.product.product_name_en)
console.log(result.product.allergens);
let newtext = result.product.allergens_hierarchy;
for (var i = 0; i < newtext.length; i++) {
newtext[i] = newtext[i].substring(3);
}
console.log(newtext);
let output = result.product.product_name_en + " is not in our database";;
if (newtext != ""){
output = result.product.product_name_en + "containts:" + newtext + ", which are common allergens";
}
let utterance = new SpeechSynthesisUtterance(output);
utterance.rate = 0.8;
window.speechSynthesis.speak(utterance);
})
})
.catch(error => {
output = "Cannot find a code try again";
let utterance = new SpeechSynthesisUtterance(output);
utterance.rate = 0.8;
window.speechSynthesis.speak(utterance);
document.getElementById("codevalue").innerHTML = "Cannot find a code try again";
});
}