Update index.html
Browse files- index.html +81 -45
index.html
CHANGED
@@ -15,14 +15,14 @@
|
|
15 |
background-color: #000;
|
16 |
}
|
17 |
#video {
|
18 |
-
width:
|
19 |
-
height:
|
20 |
display: block;
|
21 |
background-color: #000;
|
22 |
}
|
23 |
#canvas {
|
24 |
-
width:
|
25 |
-
height:
|
26 |
position: absolute;
|
27 |
top: 0;
|
28 |
left: 0;
|
@@ -39,13 +39,25 @@
|
|
39 |
border-radius: 5px;
|
40 |
pointer-events: none;
|
41 |
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
</style>
|
43 |
</head>
|
44 |
<body>
|
45 |
|
46 |
-
<video id="video" width="
|
47 |
-
<canvas id="canvas" width="
|
48 |
<div id="overlay" class="overlay"></div>
|
|
|
49 |
|
50 |
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
|
51 |
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/coco-ssd"></script>
|
@@ -55,71 +67,95 @@
|
|
55 |
const canvas = document.getElementById('canvas');
|
56 |
const ctx = canvas.getContext('2d');
|
57 |
const overlay = document.getElementById('overlay');
|
|
|
|
|
|
|
|
|
|
|
58 |
|
59 |
// Initialize video stream from webcam
|
60 |
-
navigator.mediaDevices.getUserMedia({ video:
|
61 |
.then((stream) => {
|
62 |
video.srcObject = stream;
|
|
|
63 |
})
|
64 |
.catch((err) => {
|
65 |
console.error('Error accessing webcam: ', err);
|
66 |
});
|
67 |
|
68 |
// Load the COCO-SSD model for object detection
|
69 |
-
let model;
|
70 |
async function loadModel() {
|
71 |
-
|
72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
73 |
}
|
74 |
|
75 |
-
loadModel();
|
76 |
-
|
77 |
// Function to run object detection on each frame
|
78 |
async function detectFrame() {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
79 |
// Clear the canvas before drawing
|
80 |
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
81 |
|
82 |
// Draw the video frame to canvas
|
83 |
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
|
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 |
// Call detectFrame repeatedly for continuous object detection
|
118 |
requestAnimationFrame(detectFrame);
|
119 |
}
|
120 |
|
121 |
-
// Start the detection loop
|
122 |
-
|
|
|
|
|
123 |
</script>
|
124 |
|
125 |
</body>
|
|
|
15 |
background-color: #000;
|
16 |
}
|
17 |
#video {
|
18 |
+
width: 320px; /* Reduced video size */
|
19 |
+
height: 240px; /* Reduced video size */
|
20 |
display: block;
|
21 |
background-color: #000;
|
22 |
}
|
23 |
#canvas {
|
24 |
+
width: 320px; /* Reduced canvas size */
|
25 |
+
height: 240px; /* Reduced canvas size */
|
26 |
position: absolute;
|
27 |
top: 0;
|
28 |
left: 0;
|
|
|
39 |
border-radius: 5px;
|
40 |
pointer-events: none;
|
41 |
}
|
42 |
+
#loading {
|
43 |
+
position: absolute;
|
44 |
+
top: 50%;
|
45 |
+
left: 50%;
|
46 |
+
transform: translate(-50%, -50%);
|
47 |
+
color: white;
|
48 |
+
font-size: 24px;
|
49 |
+
background-color: rgba(0, 0, 0, 0.7);
|
50 |
+
padding: 20px;
|
51 |
+
border-radius: 5px;
|
52 |
+
}
|
53 |
</style>
|
54 |
</head>
|
55 |
<body>
|
56 |
|
57 |
+
<video id="video" width="320" height="240" autoplay muted></video>
|
58 |
+
<canvas id="canvas" width="320" height="240"></canvas>
|
59 |
<div id="overlay" class="overlay"></div>
|
60 |
+
<div id="loading" style="display: none;">Loading model...</div>
|
61 |
|
62 |
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
|
63 |
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/coco-ssd"></script>
|
|
|
67 |
const canvas = document.getElementById('canvas');
|
68 |
const ctx = canvas.getContext('2d');
|
69 |
const overlay = document.getElementById('overlay');
|
70 |
+
const loading = document.getElementById('loading');
|
71 |
+
|
72 |
+
let model;
|
73 |
+
let frameCount = 0;
|
74 |
+
const detectionInterval = 2; // Process every 2nd frame
|
75 |
|
76 |
// Initialize video stream from webcam
|
77 |
+
navigator.mediaDevices.getUserMedia({ video: { width: 320, height: 240 } })
|
78 |
.then((stream) => {
|
79 |
video.srcObject = stream;
|
80 |
+
loadModel();
|
81 |
})
|
82 |
.catch((err) => {
|
83 |
console.error('Error accessing webcam: ', err);
|
84 |
});
|
85 |
|
86 |
// Load the COCO-SSD model for object detection
|
|
|
87 |
async function loadModel() {
|
88 |
+
try {
|
89 |
+
loading.style.display = 'block'; // Show loading message
|
90 |
+
model = await cocoSsd.load();
|
91 |
+
console.log('COCO-SSD model loaded!');
|
92 |
+
loading.style.display = 'none'; // Hide loading message once model is ready
|
93 |
+
startDetection();
|
94 |
+
} catch (err) {
|
95 |
+
console.error('Error loading model:', err);
|
96 |
+
loading.innerHTML = 'Error loading model. Please try again.';
|
97 |
+
}
|
98 |
}
|
99 |
|
|
|
|
|
100 |
// Function to run object detection on each frame
|
101 |
async function detectFrame() {
|
102 |
+
if (!model) return; // Only proceed if model is loaded
|
103 |
+
|
104 |
+
frameCount++;
|
105 |
+
|
106 |
+
// Skip frames to improve performance
|
107 |
+
if (frameCount % detectionInterval !== 0) {
|
108 |
+
requestAnimationFrame(detectFrame); // Skip this frame and move to the next
|
109 |
+
return;
|
110 |
+
}
|
111 |
+
|
112 |
+
frameCount = 0; // Reset frame count after detection
|
113 |
+
|
114 |
// Clear the canvas before drawing
|
115 |
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
116 |
|
117 |
// Draw the video frame to canvas
|
118 |
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
|
119 |
|
120 |
+
// Run object detection
|
121 |
+
const predictions = await model.detect(video);
|
122 |
+
|
123 |
+
// Clear previous overlay content
|
124 |
+
overlay.innerHTML = '';
|
125 |
+
|
126 |
+
predictions.forEach(prediction => {
|
127 |
+
// Draw bounding box
|
128 |
+
ctx.beginPath();
|
129 |
+
ctx.rect(
|
130 |
+
prediction.bbox[0],
|
131 |
+
prediction.bbox[1],
|
132 |
+
prediction.bbox[2],
|
133 |
+
prediction.bbox[3]
|
134 |
+
);
|
135 |
+
ctx.lineWidth = 2;
|
136 |
+
ctx.strokeStyle = 'red';
|
137 |
+
ctx.fillStyle = 'red';
|
138 |
+
ctx.stroke();
|
139 |
+
|
140 |
+
// Add label for the detected object
|
141 |
+
ctx.fillText(
|
142 |
+
`${prediction.class} (${Math.round(prediction.score * 100)}%)`,
|
143 |
+
prediction.bbox[0],
|
144 |
+
prediction.bbox[1] > 10 ? prediction.bbox[1] - 5 : 10
|
145 |
+
);
|
146 |
+
|
147 |
+
// Optionally display detected class names in the overlay
|
148 |
+
overlay.innerHTML += `<p>${prediction.class} - ${Math.round(prediction.score * 100)}%</p>`;
|
149 |
+
});
|
|
|
150 |
|
151 |
// Call detectFrame repeatedly for continuous object detection
|
152 |
requestAnimationFrame(detectFrame);
|
153 |
}
|
154 |
|
155 |
+
// Start the detection loop once the model is loaded
|
156 |
+
function startDetection() {
|
157 |
+
detectFrame();
|
158 |
+
}
|
159 |
</script>
|
160 |
|
161 |
</body>
|