practisebook commited on
Commit
b277987
·
verified ·
1 Parent(s): 39fe937

Update assets/detection.js

Browse files
Files changed (1) hide show
  1. assets/detection.js +4 -8
assets/detection.js CHANGED
@@ -3,7 +3,6 @@ const canvas = document.getElementById("canvas");
3
  const ctx = canvas.getContext("2d");
4
  const button = document.getElementById("start");
5
 
6
- // Text-to-Speech for audio feedback
7
  function speak(text) {
8
  const synth = window.speechSynthesis;
9
  const utterance = new SpeechSynthesisUtterance(text);
@@ -21,7 +20,7 @@ async function setupCamera() {
21
  }
22
 
23
  async function detectObjects() {
24
- // Load YOLO model using TensorFlow.js or another library
25
  const model = await tf.loadGraphModel("https://path-to-your-yolo-model/model.json");
26
 
27
  canvas.width = video.videoWidth;
@@ -30,27 +29,24 @@ async function detectObjects() {
30
  async function processFrame() {
31
  ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
32
 
33
- // Preprocess the video frame for YOLO model
34
  const inputTensor = tf.browser.fromPixels(video).resizeBilinear([640, 480]).expandDims(0);
35
  const predictions = await model.executeAsync(inputTensor);
36
 
37
- // Parse predictions and draw bounding boxes
 
38
  predictions.forEach((prediction) => {
39
  const [x, y, width, height] = prediction.bbox;
40
  const label = prediction.class;
41
  const confidence = prediction.score;
42
 
43
- // Draw the bounding box
44
  ctx.strokeStyle = "red";
45
  ctx.lineWidth = 2;
46
  ctx.strokeRect(x, y, width, height);
47
 
48
- // Draw the label
49
  ctx.fillStyle = "red";
50
  ctx.font = "18px Arial";
51
  ctx.fillText(`${label} (${(confidence * 100).toFixed(2)}%)`, x, y - 10);
52
 
53
- // Provide audio feedback
54
  speak(`${label} detected with ${(confidence * 100).toFixed(2)}% confidence.`);
55
  });
56
 
@@ -61,7 +57,7 @@ async function detectObjects() {
61
  }
62
 
63
  button.addEventListener("click", async () => {
64
- button.disabled = true; // Disable the button after starting
65
  await setupCamera();
66
  video.play();
67
  detectObjects();
 
3
  const ctx = canvas.getContext("2d");
4
  const button = document.getElementById("start");
5
 
 
6
  function speak(text) {
7
  const synth = window.speechSynthesis;
8
  const utterance = new SpeechSynthesisUtterance(text);
 
20
  }
21
 
22
  async function detectObjects() {
23
+ console.log("Loading model...");
24
  const model = await tf.loadGraphModel("https://path-to-your-yolo-model/model.json");
25
 
26
  canvas.width = video.videoWidth;
 
29
  async function processFrame() {
30
  ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
31
 
 
32
  const inputTensor = tf.browser.fromPixels(video).resizeBilinear([640, 480]).expandDims(0);
33
  const predictions = await model.executeAsync(inputTensor);
34
 
35
+ console.log("Predictions:", predictions);
36
+
37
  predictions.forEach((prediction) => {
38
  const [x, y, width, height] = prediction.bbox;
39
  const label = prediction.class;
40
  const confidence = prediction.score;
41
 
 
42
  ctx.strokeStyle = "red";
43
  ctx.lineWidth = 2;
44
  ctx.strokeRect(x, y, width, height);
45
 
 
46
  ctx.fillStyle = "red";
47
  ctx.font = "18px Arial";
48
  ctx.fillText(`${label} (${(confidence * 100).toFixed(2)}%)`, x, y - 10);
49
 
 
50
  speak(`${label} detected with ${(confidence * 100).toFixed(2)}% confidence.`);
51
  });
52
 
 
57
  }
58
 
59
  button.addEventListener("click", async () => {
60
+ button.disabled = true;
61
  await setupCamera();
62
  video.play();
63
  detectObjects();