File size: 6,046 Bytes
340c2c0 590961f 340c2c0 590961f 340c2c0 590961f 340c2c0 9b45932 590961f 340c2c0 9b45932 340c2c0 9b45932 b365c72 340c2c0 b365c72 340c2c0 b365c72 9b45932 340c2c0 b365c72 340c2c0 590961f 340c2c0 b365c72 340c2c0 590961f 9b45932 340c2c0 b365c72 9b45932 b365c72 340c2c0 b365c72 340c2c0 b365c72 340c2c0 b365c72 cefa0f3 9b45932 b365c72 340c2c0 b365c72 340c2c0 b365c72 590961f 340c2c0 590961f 340c2c0 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 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 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 | <!DOCTYPE html>
<html lang="tr">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Shape Recognition Model</title>
<!-- ONNX Runtime Web kütüphanesini tarayıcıya yüklüyoruz -->
<script src="https://cdn.jsdelivr.net/npm/onnxruntime-web/dist/ort.min.js"></script>
<style>
body { font-family: Arial, sans-serif; display: flex; flex-direction: column; align-items: center; background-color: #f3f4f6; margin-top: 50px; }
canvas { border: 2px solid #333; background-color: white; cursor: crosshair; touch-action: none; }
.controls { margin-top: 20px; display: flex; gap: 10px; }
button { padding: 10px 20px; font-size: 16px; cursor: pointer; background-color: #4CAF50; color: white; border: none; border-radius: 5px; }
button.clear { background-color: #f44336; }
#result { margin-top: 20px; font-size: 24px; font-weight: bold; color: #333; }
</style>
</head>
<body>
<h2>Draw a Shape</h2>
<canvas id="canvas" width="500" height="500"></canvas>
<div class="controls">
<button onclick="predict()">Predict</button>
<button class="clear" onclick="clearCanvas()">Clear</button>
</div>
<div id="result">Waiting...</div>
<!-- Modelin beklediği 30x30 boyutuna uygun gizli tuval -->
<canvas id="hiddenCanvas" width="30" height="30" style="display:none;"></canvas>
<script>
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let isDrawing = false;
const classes = ["Circle", "Triangle", "Rectangle", "Pentagon", "Parallelogram", "Line", "Hexagon"];
ctx.lineWidth = 20;
ctx.lineCap = 'round';
ctx.strokeStyle = 'black';
// Fare (Mouse) Eventleri
canvas.addEventListener('mousedown', startDrawing);
canvas.addEventListener('mousemove', draw);
canvas.addEventListener('mouseup', stopDrawing);
canvas.addEventListener('mouseout', stopDrawing);
// Dokunmatik (Touch) Eventleri
canvas.addEventListener('touchstart', handleTouchStart, { passive: false });
canvas.addEventListener('touchmove', handleTouchMove, { passive: false });
canvas.addEventListener('touchend', stopDrawing);
canvas.addEventListener('touchcancel', stopDrawing);
// Fare için çizim fonksiyonları
function startDrawing(e) {
isDrawing = true;
draw(e);
}
function stopDrawing() {
isDrawing = false;
ctx.beginPath();
}
function draw(e) {
if (!isDrawing) return;
const rect = canvas.getBoundingClientRect();
ctx.lineTo(e.clientX - rect.left, e.clientY - rect.top);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(e.clientX - rect.left, e.clientY - rect.top);
}
// Dokunmatik ekran için özel fonksiyonlar
function getTouchPos(e) {
const rect = canvas.getBoundingClientRect();
return {
x: e.touches[0].clientX - rect.left,
y: e.touches[0].clientY - rect.top
};
}
function handleTouchStart(e) {
e.preventDefault(); // Sayfanın kaymasını engelle
isDrawing = true;
const pos = getTouchPos(e);
ctx.beginPath();
ctx.moveTo(pos.x, pos.y);
}
function handleTouchMove(e) {
if (!isDrawing) return;
e.preventDefault(); // Sayfanın kaymasını engelle
const pos = getTouchPos(e);
ctx.lineTo(pos.x, pos.y);
ctx.stroke();
ctx.beginPath();
ctx.moveTo(pos.x, pos.y);
}
function clearCanvas() {
ctx.clearRect(0, 0, canvas.width, canvas.height);
document.getElementById('result').innerText = "Waiting...";
}
async function predict() {
document.getElementById('result').innerText = "Calculating...";
const hiddenCanvas = document.getElementById('hiddenCanvas');
const hiddenCtx = hiddenCanvas.getContext('2d');
hiddenCtx.fillStyle = "white";
hiddenCtx.fillRect(0, 0, 30, 30);
hiddenCtx.drawImage(canvas, 0, 0, 30, 30);
const imgData = hiddenCtx.getImageData(0, 0, 30, 30).data;
const floatArray = new Float32Array(30 * 30);
for (let i = 0; i < imgData.length; i += 4) {
const r = imgData[i], g = imgData[i+1], b = imgData[i+2];
const gray = 0.299 * r + 0.587 * g + 0.114 * b;
const val = gray < 128 ? 1.0 : 0.0;
floatArray[i/4] = val;
}
try {
const session = await ort.InferenceSession.create('./model.onnx');
// Tensor boyutunu 3'e düşürdük [1, 30, 30]
const tensor = new ort.Tensor('float32', floatArray, [1, 30, 30]);
const results = await session.run({ input: tensor });
const output = results.output.data;
let maxIndex = 0;
let maxValue = output[0];
for (let i = 1; i < output.length; i++) {
if (output[i] > maxValue) {
maxValue = output[i];
maxIndex = i;
}
}
document.getElementById('result').innerText = "Prediction: " + classes[maxIndex];
} catch (err) {
console.error(err);
document.getElementById('result').innerText = "Error loading model!";
}
}
</script>
</body>
</html> |