Update index.html
Browse files- index.html +121 -19
index.html
CHANGED
|
@@ -1,19 +1,121 @@
|
|
| 1 |
-
<!
|
| 2 |
-
<html>
|
| 3 |
-
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<!DOCTYPE html>
|
| 2 |
+
<html lang="tr">
|
| 3 |
+
<head>
|
| 4 |
+
<meta charset="UTF-8">
|
| 5 |
+
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
+
<title>Şekil Tanıma Modeli</title>
|
| 7 |
+
<!-- ONNX Runtime Web kütüphanesini tarayıcıya yüklüyoruz -->
|
| 8 |
+
<script src="https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/ort.min.js"></script>
|
| 9 |
+
<style>
|
| 10 |
+
body { font-family: Arial, sans-serif; display: flex; flex-direction: column; align-items: center; background-color: #f3f4f6; margin-top: 50px; }
|
| 11 |
+
canvas { border: 2px solid #333; background-color: white; cursor: crosshair; touch-action: none; }
|
| 12 |
+
.controls { margin-top: 20px; display: flex; gap: 10px; }
|
| 13 |
+
button { padding: 10px 20px; font-size: 16px; cursor: pointer; background-color: #4CAF50; color: white; border: none; border-radius: 5px; }
|
| 14 |
+
button.clear { background-color: #f44336; }
|
| 15 |
+
#result { margin-top: 20px; font-size: 24px; font-weight: bold; color: #333; }
|
| 16 |
+
</style>
|
| 17 |
+
</head>
|
| 18 |
+
<body>
|
| 19 |
+
|
| 20 |
+
<h2>Bir Şekil Çizin</h2>
|
| 21 |
+
<!-- Kullanıcının çizeceği 500x500 tuval -->
|
| 22 |
+
<canvas id="canvas" width="500" height="500"></canvas>
|
| 23 |
+
|
| 24 |
+
<div class="controls">
|
| 25 |
+
<button onclick="predict()">Tahmin Et</button>
|
| 26 |
+
<button class="clear" onclick="clearCanvas()">Temizle</button>
|
| 27 |
+
</div>
|
| 28 |
+
|
| 29 |
+
<div id="result">Bekleniyor...</div>
|
| 30 |
+
|
| 31 |
+
<!-- Arka planda resmi 50x50'ye küçültmek için gizli tuval -->
|
| 32 |
+
<canvas id="hiddenCanvas" width="50" height="50" style="display:none;"></canvas>
|
| 33 |
+
|
| 34 |
+
<script>
|
| 35 |
+
const canvas = document.getElementById('canvas');
|
| 36 |
+
const ctx = canvas.getContext('2d');
|
| 37 |
+
let isDrawing = false;
|
| 38 |
+
|
| 39 |
+
const classes = ["Çember", "Üçgen", "Dikdörtgen", "Beşgen", "Paralelkenar", "Çizgi"];
|
| 40 |
+
|
| 41 |
+
// Çizim Ayarları (Kalın fırça)
|
| 42 |
+
ctx.lineWidth = 20;
|
| 43 |
+
ctx.lineCap = 'round';
|
| 44 |
+
ctx.strokeStyle = 'black';
|
| 45 |
+
|
| 46 |
+
// Fare/Dokunmatik olayları
|
| 47 |
+
canvas.addEventListener('mousedown', startDrawing);
|
| 48 |
+
canvas.addEventListener('mousemove', draw);
|
| 49 |
+
canvas.addEventListener('mouseup', stopDrawing);
|
| 50 |
+
canvas.addEventListener('mouseout', stopDrawing);
|
| 51 |
+
|
| 52 |
+
function startDrawing(e) { isDrawing = true; draw(e); }
|
| 53 |
+
function stopDrawing() { isDrawing = false; ctx.beginPath(); }
|
| 54 |
+
|
| 55 |
+
function draw(e) {
|
| 56 |
+
if (!isDrawing) return;
|
| 57 |
+
const rect = canvas.getBoundingClientRect();
|
| 58 |
+
ctx.lineTo(e.clientX - rect.left, e.clientY - rect.top);
|
| 59 |
+
ctx.stroke();
|
| 60 |
+
ctx.beginPath();
|
| 61 |
+
ctx.moveTo(e.clientX - rect.left, e.clientY - rect.top);
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
function clearCanvas() {
|
| 65 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
| 66 |
+
document.getElementById('result').innerText = "Bekleniyor...";
|
| 67 |
+
}
|
| 68 |
+
|
| 69 |
+
async function predict() {
|
| 70 |
+
document.getElementById('result').innerText = "Hesaplanıyor...";
|
| 71 |
+
|
| 72 |
+
// 1. 500x500'lük çizimi 50x50'ye küçült
|
| 73 |
+
const hiddenCanvas = document.getElementById('hiddenCanvas');
|
| 74 |
+
const hiddenCtx = hiddenCanvas.getContext('2d');
|
| 75 |
+
|
| 76 |
+
// Arka planı beyaz yap, üstüne çizimi kopyala
|
| 77 |
+
hiddenCtx.fillStyle = "white";
|
| 78 |
+
hiddenCtx.fillRect(0, 0, 50, 50);
|
| 79 |
+
hiddenCtx.drawImage(canvas, 0, 0, 50, 50);
|
| 80 |
+
|
| 81 |
+
// 2. Piksel verilerini al (RGBA)
|
| 82 |
+
const imgData = hiddenCtx.getImageData(0, 0, 50, 50).data;
|
| 83 |
+
const floatArray = new Float32Array(50 * 50);
|
| 84 |
+
|
| 85 |
+
// 3. RGBA'yı modele uygun Float'a çevir (Siyah=1.0, Beyaz=0.0)
|
| 86 |
+
for (let i = 0; i < imgData.length; i += 4) {
|
| 87 |
+
const r = imgData[i], g = imgData[i+1], b = imgData[i+2];
|
| 88 |
+
// Gri tonlama
|
| 89 |
+
const gray = 0.299 * r + 0.587 * g + 0.114 * b;
|
| 90 |
+
// Eşik değeri uygulayıp (Siyah çizimler 1.0, beyazlar 0.0)
|
| 91 |
+
const val = gray < 128 ? 1.0 : 0.0;
|
| 92 |
+
floatArray[i/4] = val;
|
| 93 |
+
}
|
| 94 |
+
|
| 95 |
+
try {
|
| 96 |
+
// 4. ONNX Modelini Çalıştır
|
| 97 |
+
const session = await ort.InferenceSession.create('./model.onnx');
|
| 98 |
+
const tensor = new ort.Tensor('float32', floatArray, [1, 1, 50, 50]);
|
| 99 |
+
const results = await session.run({ input: tensor });
|
| 100 |
+
|
| 101 |
+
// 5. Çıktıları Al ve En Yüksek Olasılığı Bul
|
| 102 |
+
const output = results.output.data;
|
| 103 |
+
let maxIndex = 0;
|
| 104 |
+
let maxValue = output[0];
|
| 105 |
+
|
| 106 |
+
for (let i = 1; i < output.length; i++) {
|
| 107 |
+
if (output[i] > maxValue) {
|
| 108 |
+
maxValue = output[i];
|
| 109 |
+
maxIndex = i;
|
| 110 |
+
}
|
| 111 |
+
}
|
| 112 |
+
|
| 113 |
+
document.getElementById('result').innerText = "Tahmin: " + classes[maxIndex];
|
| 114 |
+
} catch (err) {
|
| 115 |
+
console.error(err);
|
| 116 |
+
document.getElementById('result').innerText = "Model yüklenirken hata oluştu!";
|
| 117 |
+
}
|
| 118 |
+
}
|
| 119 |
+
</script>
|
| 120 |
+
</body>
|
| 121 |
+
</html>
|