File size: 4,865 Bytes
7291333
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
156
157
158
159
160
161
162
163
164
165
166
167
"use strict";

const checkPredictionBtn = document.getElementById("checkPredictionBtn");
checkPredictionBtn.addEventListener("click", checkPrediction);
const clearCanvasBtn = document.getElementById("clearCanvasBtn");
clearCanvasBtn.addEventListener("click", clearCanvas);
const showModalBtn = document.getElementById("showModalBtn");
const modal = document.getElementById("modal");
const closeModalBtn = document.getElementById("closeModalBtn");
const clearSavesBtn = document.getElementById("clearSavesBtn");
clearSavesBtn.addEventListener("click", () => {
	localStorage.removeItem("predictions");
	loadPredictions();
	// clear the colors of the boxes
	const hiraganaBoxes = document.querySelectorAll(".hiraganaBox");
	hiraganaBoxes.forEach(box => {
		box.style.backgroundColor = "";
	});
});

showModalBtn.addEventListener("click", () => modal.showModal());
closeModalBtn.addEventListener("click", () => modal.close());

loadHiraganaData();

// load hiragana data from json file
async function loadHiraganaData() {
	let hiraganaData = {};
	await fetch("/static/hiragana.json")
		.then((response) => response.json())
		.then((data) => {
			hiraganaData = data;
			const hiraganaBoxes = document.getElementById("hiraganaBoxes");
			for (const letter in hiraganaData) {
				const box = document.createElement("div");
				box.classList.add("hiraganaBox");
				box.textContent = letter;
				box.dataset.label = hiraganaData[letter];
				hiraganaBoxes.appendChild(box);
			}
		})
		.catch((error) => {
			console.error("Hiragana data request error:", error);
		});

	loadPredictions();
}



async function sendPrediction(label, image) {

	try {
		const response = await fetch("/api/predict", {
			method: "POST",
			headers: { "Content-Type": "application/json" },
			body: JSON.stringify({
				image: { image: image },
				label: label,
			}),
		});
		const data = await response.json();
		handlePrediction(label, data.prediction);
		savePrediction(label, data.prediction);
	} catch (error) {
		console.error("Prediction request error:", error);
	}

	loadPredictions();
}


function handlePrediction(label, prediction) {
	const predictionResultElement = document.getElementById("predictionResult");
	const selectedBox = document.querySelector(`.hiraganaBox[data-label="${label}"]`);
	if (prediction) {
		predictionResultElement.textContent = "Correct prediction!";
		selectedBox.style.backgroundColor = "green";
	} else {
		predictionResultElement.textContent = "Incorrect prediction!";
		selectedBox.style.backgroundColor = "red";
	}
}

const canvas = document.getElementById("canvas");
if (canvas) {
	canvas.addEventListener("mousedown", function (e) {
		startDrawing(e);
	});
}

function startDrawing(e) {
	canvas.addEventListener("mousemove", handleDrawing);
	canvas.addEventListener("mouseup", function () {
		stopDrawing();
	});
}

function stopDrawing() {
	canvas.removeEventListener("mousemove", handleDrawing);
}

function handleDrawing(e) {
	const ctx = canvas.getContext("2d");
	ctx.lineWidth = 25;
	ctx.lineCap = "round"
	ctx.lineJoin = "round"
	ctx.strokeStyle = "#000"
	const rect = canvas.getBoundingClientRect();
	const x = e.clientX - rect.left;
	const y = e.clientY - rect.top;

	ctx.fillStyle = "#000";
	ctx.beginPath();
	ctx.arc(x, y, 5, 0, Math.PI * 2);
	ctx.fill();
}

function clearCanvas() {
	const ctx = canvas.getContext("2d");
	ctx.clearRect(0, 0, canvas.width, canvas.height);
}

function checkPrediction() {
	const canvas = document.getElementById("canvas");
	if (!canvas) return;
	const image = canvas.toDataURL("image/png");
	const selectedCharacter = document.querySelector(".hiraganaBox.selected").dataset.label;

	sendPrediction(selectedCharacter, image);
}

// Event delegation for selecting hiragana boxes
document.addEventListener("click", (e) => {
	const hiraganaBoxes = document.querySelectorAll(".hiraganaBox");
	hiraganaBoxes.forEach(box => {
		box.classList.remove("selected");
	});
	if (e.target.classList.contains("hiraganaBox")) {
		e.target.classList.add("selected");
	}
});

function savePrediction(label, prediction) {
	const predictions = JSON.parse(localStorage.getItem("predictions")) || [];
	// remove the old prediction if it exists
	const updatedPredictions = predictions.filter(p => p.label !== label);
	updatedPredictions.push({ label, prediction });
	localStorage.setItem("predictions", JSON.stringify(updatedPredictions));
}

function loadPredictions() {
	const predictions = JSON.parse(localStorage.getItem("predictions")) || [];
	// color the boxes based on the predictions
	const hiraganaBoxes = document.querySelectorAll(".hiraganaBox");
	hiraganaBoxes.forEach(box => {
		const prediction = predictions.find(p => p.label === box.dataset.label);
		if (prediction) {
			if (prediction.prediction) {
				box.style.backgroundColor = "green";
			} else {
				box.style.backgroundColor = "red";
			}
		}
	});
}