-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
197 lines (171 loc) · 5.91 KB
/
script.js
File metadata and controls
197 lines (171 loc) · 5.91 KB
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
// Get references to the canvas element and its drawing context
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
// Set the initial background color to white and fill the entire canvas
ctx.fillStyle = "#fff";
ctx.fillRect(0, 0, canvas.width, canvas.width);
// Variables to track drawing state and mouse position
let drawing = false;
let mouseX, mouseY;
// Event listener for mousedown on the canvas
canvas.addEventListener("mousedown", (e) => {
// Set drawing to true and capture initial mouse position relative to canvas
drawing = true;
mouseX = e.clientX - canvas.offsetLeft;
mouseY = e.clientY - canvas.offsetTop;
});
// Event listener for mouseup on the canvas
canvas.addEventListener("mouseup", () => {
// Set drawing to false to stop drawing lines
drawing = false;
});
// Event listener for mousemove on the canvas
canvas.addEventListener("mousemove", (e) => {
if (drawing) {
// Get new mouse position relative to canvas
const newX = e.clientX - canvas.offsetLeft;
const newY = e.clientY - canvas.offsetTop;
// Start a new path, set line width and cap style
ctx.beginPath();
ctx.lineWidth = 20;
ctx.lineCap = "round";
// Move to the previous mouse position and draw a line to the new position
ctx.moveTo(mouseX, mouseY);
ctx.lineTo(newX, newY);
ctx.stroke();
// Update current mouse position for the next move event
mouseX = newX;
mouseY = newY;
}
});
// Function to clear the entire canvas
function clearCanvas() {
const canvas = document.getElementById("myCanvas");
const ctx = canvas.getContext("2d");
// Clear the entire rectangle using canvas width and height
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Set the background color to white and fill the cleared area
ctx.fillStyle = "#fff";
ctx.fillRect(0, 0, canvas.width, canvas.width);
}
const output = document.getElementById("ouput");
function clearOutput() {
output.innerHTML = "";
clearCanvas();
input1.setAttribute("disabled",true);
input2.setAttribute("disabled",true);
labeling.setAttribute("disabled",true);
uploadbutton.setAttribute("disabled",true);
}
const input1 = document.getElementById("yes");
const input2 = document.getElementById("no");
// Event listener for the upload button click
document
.getElementById("uploadButton")
.addEventListener("click", async function (event) {
event.preventDefault(); // Prevent default form submission behavior
// Get references to output element and canvas
const output = document.getElementById("ouput");
const canvas = document.getElementById("myCanvas");
try {
// Get the image data URL from the canvas
const imageData = canvas.toDataURL("image/png");
// Call the asynchronous function to upload image and get prediction
const currentPredictedLabel = await uploadImage(imageData);
// Update output element only if a prediction exists
if (currentPredictedLabel) {
output.innerHTML = currentPredictedLabel;
}
input1.removeAttribute("disabled");
input2.removeAttribute("disabled");
} catch (error) {
console.error("Error:", error); // Log the error for debugging
}
});
// Function to upload image data and get prediction asynchronously
async function uploadImage(imageData) {
try {
// Make a POST request to the Flask server endpoint (replace URL if needed)
const response = await fetch("http://127.0.0.1:5000", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Cache-Control": "no-cache",
},
body: JSON.stringify({ imageData }),
});
if (!response.ok) {
// Throw an error with details if the response is not successful
throw new Error(`Error uploading image: ${response.statusText}`);
}
const data = await response.json();
const currentPredictedLabel = await data.predicted_label;
// clearCanvas();
return currentPredictedLabel;
} catch (err) {
// Handle upload errors (consider providing user feedback)
handle(err); // Replace with a custom error handling function
throw err; // Re-throw the error for further handling
}
}
// Functions for smooth scrolling to different sections (optional)
function home() {
const about = document.getElementById("home");
about.scrollIntoView({
behavior: "smooth",
block: "end",
inline: "nearest",
});
}
function about() {
const about = document.getElementById("about");
about.scrollIntoView({
behavior: "smooth",
block: "end",
inline: "nearest",
});
}
function contact() {
const about = document.getElementById("contact");
about.scrollIntoView({
behavior: "smooth",
block: "end",
inline: "nearest",
});
}
function more() {
const about = document.getElementById("more");
about.scrollIntoView({
behavior: "smooth",
block: "start",
inline: "nearest",
});
}
const labeling = document.getElementById("labeling");
const uploadbutton = document.getElementById("upbutton");
var store_label = "";
function fun(){
labeling.removeAttribute("disabled");
uploadbutton.removeAttribute("disabled");
}
async function upload(){
store_label = labeling.value;
const canvas = document.getElementById("myCanvas")
const imageData = canvas.toDataURL("image/png");
// console.log(form);
const response = await fetch("http://127.0.0.1:5000", {
method: "POST",
headers: {
"Content-Type": "application/json",
"Cache-Control": "no-cache",
},
body: JSON.stringify({store_label}),
});
console.log(store_label);
labeling.value = "";
input1.setAttribute("disabled",true);
input2.setAttribute("disabled",true);
labeling.setAttribute("disabled",true);
uploadbutton.setAttribute("disabled",true);
clearOutput();
}