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

Update assets/detection.js

Browse files
Files changed (1) hide show
  1. assets/detection.js +17 -7
assets/detection.js CHANGED
@@ -1,7 +1,7 @@
1
  const video = document.getElementById("video");
2
  const canvas = document.getElementById("canvas");
3
  const ctx = canvas.getContext("2d");
4
- const button = document.getElementById("start");
5
 
6
  function speak(text) {
7
  const synth = window.speechSynthesis;
@@ -20,8 +20,10 @@ async function setupCamera() {
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;
27
  canvas.height = video.videoHeight;
@@ -29,24 +31,32 @@ async function detectObjects() {
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
 
@@ -56,8 +66,8 @@ async function detectObjects() {
56
  processFrame();
57
  }
58
 
59
- button.addEventListener("click", async () => {
60
- button.disabled = true;
61
  await setupCamera();
62
  video.play();
63
  detectObjects();
 
1
  const video = document.getElementById("video");
2
  const canvas = document.getElementById("canvas");
3
  const ctx = canvas.getContext("2d");
4
+ const startButton = document.getElementById("start");
5
 
6
  function speak(text) {
7
  const synth = window.speechSynthesis;
 
20
  }
21
 
22
  async function detectObjects() {
23
+ console.log("Loading YOLO model...");
24
+ const model = await tf.loadGraphModel(
25
+ "https://path-to-your-model/model.json"
26
+ ); // Replace with your actual model path
27
 
28
  canvas.width = video.videoWidth;
29
  canvas.height = video.videoHeight;
 
31
  async function processFrame() {
32
  ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
33
 
34
+ const inputTensor = tf.browser
35
+ .fromPixels(video)
36
+ .resizeBilinear([640, 480])
37
+ .expandDims(0);
38
+
39
  const predictions = await model.executeAsync(inputTensor);
40
 
41
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
42
+ ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
43
 
44
  predictions.forEach((prediction) => {
45
  const [x, y, width, height] = prediction.bbox;
46
  const label = prediction.class;
47
  const confidence = prediction.score;
48
 
49
+ // Draw bounding box
50
  ctx.strokeStyle = "red";
51
  ctx.lineWidth = 2;
52
  ctx.strokeRect(x, y, width, height);
53
 
54
+ // Draw label
55
  ctx.fillStyle = "red";
56
  ctx.font = "18px Arial";
57
  ctx.fillText(`${label} (${(confidence * 100).toFixed(2)}%)`, x, y - 10);
58
 
59
+ // Provide audio feedback
60
  speak(`${label} detected with ${(confidence * 100).toFixed(2)}% confidence.`);
61
  });
62
 
 
66
  processFrame();
67
  }
68
 
69
+ startButton.addEventListener("click", async () => {
70
+ startButton.disabled = true;
71
  await setupCamera();
72
  video.play();
73
  detectObjects();