Skip to content

Commit 45a1c18

Browse files
authored
Add files via upload
1 parent 411eb37 commit 45a1c18

File tree

1 file changed

+84
-24
lines changed

1 file changed

+84
-24
lines changed

index.html

+84-24
Original file line numberDiff line numberDiff line change
@@ -3,44 +3,78 @@
33
<head>
44
<meta charset="UTF-8">
55
<meta name="viewport" content="width=device-width, initial-scale=1.0">
6-
<title>YOLOv8 Object Detection with Logs</title>
6+
<title>YOLOv8 Object Detection with Sound Alert</title>
77
<style>
8-
#webcam-container {
8+
body {
9+
font-family: Arial, sans-serif;
10+
margin: 0;
11+
padding: 0;
12+
background-color: #f4f4f9;
913
display: flex;
1014
justify-content: center;
1115
align-items: center;
12-
margin-top: 20px;
16+
height: 100vh;
1317
}
18+
19+
#container {
20+
position: relative;
21+
width: 640px;
22+
height: 480px;
23+
border: 2px solid #ccc;
24+
background-color: #fff;
25+
}
26+
1427
#webcam {
15-
border: 2px solid black;
28+
width: 100%;
29+
height: 100%;
30+
object-fit: cover;
31+
position: absolute;
1632
}
33+
34+
#canvas {
35+
position: absolute;
36+
top: 0;
37+
left: 0;
38+
z-index: 10;
39+
pointer-events: none; /* 캔버스가 비디오 위에 있지만 클릭할 수 없도록 설정 */
40+
}
41+
1742
#log {
1843
margin-top: 20px;
1944
padding: 10px;
2045
width: 640px;
21-
height: 150px;
22-
border: 2px solid #000;
23-
overflow-y: scroll;
24-
background-color: #f0f0f0;
46+
max-height: 200px;
47+
overflow-y: auto;
48+
background-color: #222;
49+
color: #fff;
2550
font-family: monospace;
51+
border-radius: 5px;
52+
border: 2px solid #333;
53+
}
54+
55+
h1 {
56+
text-align: center;
57+
color: #333;
2658
}
59+
2760
</style>
2861
</head>
2962
<body>
30-
<h1>YOLOv8 Object Detection</h1>
31-
<div id="webcam-container">
32-
<video id="webcam" width="640" height="480" autoplay></video>
63+
<div id="container">
64+
<video id="webcam" autoplay></video>
65+
<canvas id="canvas" width="640" height="480"></canvas>
3366
</div>
3467
<div id="log"></div> <!-- 로그를 표시할 영역 추가 -->
35-
<script src="ort.min.js"></script> <!-- 로컬 ort.min.js 파일을 로드 -->
68+
69+
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/ort.min.js"></script>
3670
<script>
3771
// 전역 변수: 소리, 상태
3872
let alertSound = new Audio('alert_sound.mp3'); // 알림 소리 파일 경로 수정
3973
let isPlaying = false;
4074

4175
// 웹캠 초기화
4276
const video = document.getElementById('webcam');
43-
const canvas = document.createElement('canvas');
77+
const canvas = document.getElementById('canvas');
4478
const ctx = canvas.getContext('2d');
4579

4680
// 웹캠 권한 요청 및 설정
@@ -78,21 +112,28 @@ <h1>YOLOv8 Object Detection</h1>
78112

79113
// 객체 탐지 시작
80114
async function startDetection() {
81-
canvas.width = video.width;
82-
canvas.height = video.height;
83-
84115
function detectObjects() {
85116
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
86-
let frame = canvas.toDataURL('image/jpeg');
117+
let frame = canvas;
87118

88119
// 이미지 데이터를 텐서로 변환
89120
let tensor = preprocessImage(frame);
90121

122+
// 모델의 입력 이름을 확인 후, feeds 객체에 적절한 값을 추가
123+
const feeds = {};
124+
feeds['input.1'] = tensor; // 입력 이름을 정확하게 확인하고 변경하세요
125+
91126
// 추론 실행
92-
session.run([tensor]).then((output) => {
93-
const boxes = output[0].data; // 박스 데이터
94-
const confidences = output[1].data; // 신뢰도 데이터
95-
const classIds = output[2].data; // 클래스 ID 데이터
127+
session.run(feeds).then((output) => {
128+
// output 객체 출력 (디버깅을 위해 추가)
129+
console.log("Model output:", output);
130+
131+
// output의 구조를 확인한 후 아래와 같이 수정합니다.
132+
// output[0], output[1], output[2] 등은 실제 모델의 출력 형식에 따라 다를 수 있습니다.
133+
134+
const boxes = output[0] ? output[0].data : []; // 박스 데이터
135+
const confidences = output[1] ? output[1].data : []; // 신뢰도 데이터
136+
const classIds = output[2] ? output[2].data : []; // 클래스 ID 데이터
96137

97138
// 'cigarette'가 탐지된 경우
98139
let cigaretteDetected = false;
@@ -132,9 +173,23 @@ <h1>YOLOv8 Object Detection</h1>
132173

133174
// 웹캠 이미지 전처리 및 텐서로 변환
134175
function preprocessImage(frame) {
135-
// 이미지 전처리 (크기 조정, 정규화 등)
136-
// 모델에 맞는 텐서 형태로 변환
137-
let tensor = new ort.Tensor('float32', new Float32Array(frame), [1, 3, 640, 640]); // 모델의 입력 형식에 맞게 수정 필요
176+
canvas.width = 640;
177+
canvas.height = 640;
178+
ctx.drawImage(frame, 0, 0, canvas.width, canvas.height);
179+
180+
const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
181+
const data = new Float32Array(3 * 640 * 640);
182+
let index = 0;
183+
184+
// RGBA -> RGB로 변환하고 정규화
185+
for (let i = 0; i < imageData.data.length; i += 4) {
186+
data[index++] = imageData.data[i] / 255; // Red
187+
data[index++] = imageData.data[i + 1] / 255; // Green
188+
data[index++] = imageData.data[i + 2] / 255; // Blue
189+
}
190+
191+
// 모델에 맞게 텐서 생성
192+
const tensor = new ort.Tensor('float32', data, [1, 3, 640, 640]);
138193
return tensor;
139194
}
140195

@@ -146,6 +201,11 @@ <h1>YOLOv8 Object Detection</h1>
146201
ctx.strokeStyle = 'red';
147202
ctx.fillStyle = 'red';
148203
ctx.stroke();
204+
205+
// 텍스트 추가: "Cigarette" 또는 클래스명 출력
206+
ctx.font = "20px Arial";
207+
ctx.fillStyle = "red";
208+
ctx.fillText("Cigarette", x, y - 10);
149209
}
150210

151211
// 로그 메시지 추가

0 commit comments

Comments
 (0)