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

Update assets/detection.js

Browse files
Files changed (1) hide show
  1. assets/detection.js +48 -50
assets/detection.js CHANGED
@@ -1,70 +1,68 @@
1
- import * as tf from "https://cdn.jsdelivr.net/npm/@tensorflow/tfjs";
2
- import * as cocoSsd from "https://cdn.jsdelivr.net/npm/@tensorflow-models/coco-ssd";
3
-
4
- // Get references to the HTML elements
5
  const video = document.getElementById("video");
6
  const canvas = document.getElementById("canvas");
7
  const ctx = canvas.getContext("2d");
8
- const statusLabel = document.getElementById("status");
9
-
10
- let model;
11
 
12
- // Load the YOLO (COCO-SSD) model
13
- async function loadModel() {
14
- statusLabel.innerText = "Loading model...";
15
- model = await cocoSsd.load();
16
- statusLabel.innerText = "Model loaded successfully!";
17
  }
18
 
19
- // Set up the webcam feed
20
- async function startWebcam() {
21
- const stream = await navigator.mediaDevices.getUserMedia({ video: true });
 
22
  video.srcObject = stream;
23
-
24
  return new Promise((resolve) => {
25
- video.onloadedmetadata = () => {
26
- canvas.width = video.videoWidth;
27
- canvas.height = video.videoHeight;
28
- resolve(video);
29
- };
30
  });
31
  }
32
 
33
- // Perform object detection on the video feed
34
  async function detectObjects() {
35
- const predictions = await model.detect(video);
 
36
 
37
- // Clear the canvas and draw the video frame
38
- ctx.clearRect(0, 0, canvas.width, canvas.height);
39
- ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
40
 
41
- // Draw bounding boxes and labels for detected objects
42
- predictions.forEach((prediction) => {
43
- const [x, y, width, height] = prediction.bbox;
44
 
45
- // Draw bounding box
46
- ctx.strokeStyle = "red";
47
- ctx.lineWidth = 2;
48
- ctx.strokeRect(x, y, width, height);
49
 
50
- // Draw label and confidence score
51
- ctx.fillStyle = "red";
52
- ctx.font = "18px Arial";
53
- ctx.fillText(
54
- `${prediction.class} (${Math.round(prediction.score * 100)}%)`,
55
- x,
56
- y > 10 ? y - 5 : 10
57
- );
58
- });
59
 
60
- requestAnimationFrame(detectObjects); // Loop for real-time detection
61
- }
 
 
62
 
63
- // Initialize the application
64
- async function init() {
65
- await loadModel();
66
- await startWebcam();
67
- detectObjects();
 
 
 
 
 
 
 
 
68
  }
69
 
70
- init();
 
 
 
 
 
 
 
 
 
 
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
+ // Text-to-Speech for audio feedback
7
+ function speak(text) {
8
+ const synth = window.speechSynthesis;
9
+ const utterance = new SpeechSynthesisUtterance(text);
10
+ synth.speak(utterance);
11
  }
12
 
13
+ async function setupCamera() {
14
+ const stream = await navigator.mediaDevices.getUserMedia({
15
+ video: { width: 640, height: 480 },
16
+ });
17
  video.srcObject = stream;
 
18
  return new Promise((resolve) => {
19
+ video.onloadedmetadata = () => resolve(video);
 
 
 
 
20
  });
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;
28
+ canvas.height = video.videoHeight;
 
29
 
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
+
57
+ requestAnimationFrame(processFrame);
58
+ }
59
+
60
+ processFrame();
61
  }
62
 
63
+ button.addEventListener("click", async () => {
64
+ button.disabled = true; // Disable the button after starting
65
+ await setupCamera();
66
+ video.play();
67
+ detectObjects();
68
+ });