Rahul-Samedavar commited on
Commit
0f71496
·
1 Parent(s): 966aa1e
Files changed (1) hide show
  1. static/script.js +179 -62
static/script.js CHANGED
@@ -1,10 +1,185 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  document.addEventListener('DOMContentLoaded', function () {
2
  const canvas = document.getElementById('drawingCanvas');
3
  const ctx = canvas.getContext('2d');
4
  const clearButton = document.getElementById('clearButton');
5
  const resultsBody = document.getElementById('resultsBody');
6
 
7
- const canvasSize = { width: 280, height: 280 };
 
8
  canvas.width = canvasSize.width;
9
  canvas.height = canvasSize.height;
10
 
@@ -14,7 +189,7 @@ document.addEventListener('DOMContentLoaded', function () {
14
 
15
  ctx.lineJoin = 'round';
16
  ctx.lineCap = 'round';
17
- ctx.lineWidth = 18;
18
  ctx.strokeStyle = 'black';
19
 
20
  function clearCanvas() {
@@ -29,67 +204,9 @@ document.addEventListener('DOMContentLoaded', function () {
29
 
30
  clearCanvas();
31
 
32
- function getBoundingBox(imageData) {
33
- const { data, width, height } = imageData;
34
- let minX = width, minY = height, maxX = 0, maxY = 0;
35
- let found = false;
36
-
37
- for (let y = 0; y < height; y++) {
38
- for (let x = 0; x < width; x++) {
39
- const idx = (y * width + x) * 4;
40
- const alpha = data[idx + 3];
41
- const isBlack = data[idx] < 128 && alpha > 0;
42
-
43
- if (isBlack) {
44
- if (x < minX) minX = x;
45
- if (x > maxX) maxX = x;
46
- if (y < minY) minY = y;
47
- if (y > maxY) maxY = y;
48
- found = true;
49
- }
50
- }
51
- }
52
-
53
- if (!found) return null;
54
-
55
- return {
56
- x: minX,
57
- y: minY,
58
- width: maxX - minX,
59
- height: maxY - minY,
60
- };
61
- }
62
-
63
  function canvasToArray() {
64
  const imageData = ctx.getImageData(0, 0, canvasSize.width, canvasSize.height);
65
- const box = getBoundingBox(imageData);
66
-
67
- const tempCanvas = document.createElement('canvas');
68
- const tempCtx = tempCanvas.getContext('2d');
69
- tempCanvas.width = canvasSize.width;
70
- tempCanvas.height = canvasSize.height;
71
-
72
- // Fill with white background
73
- tempCtx.fillStyle = 'white';
74
- tempCtx.fillRect(0, 0, canvasSize.width, canvasSize.height);
75
-
76
- if (box) {
77
- const offsetX = (canvasSize.width - box.width) / 2;
78
- const offsetY = (canvasSize.height - box.height) / 2;
79
-
80
- // Draw only the centered portion onto the temp canvas
81
- tempCtx.drawImage(
82
- canvas,
83
- box.x, box.y, box.width, box.height,
84
- offsetX, offsetY, box.width, box.height
85
- );
86
- } else {
87
- // If nothing is drawn, use the original canvas
88
- tempCtx.drawImage(canvas, 0, 0);
89
- }
90
-
91
- const centeredData = tempCtx.getImageData(0, 0, canvasSize.width, canvasSize.height);
92
- const data = centeredData.data;
93
 
94
  const result = Array(gridSize).fill(0).map(() => Array(gridSize).fill(0));
95
 
@@ -134,7 +251,7 @@ document.addEventListener('DOMContentLoaded', function () {
134
  const response = await fetch('/classify', {
135
  method: 'POST',
136
  headers: { 'Content-Type': 'application/json' },
137
- body: JSON.stringify({ drawing: doodleData }),
138
  });
139
 
140
  if (!response.ok) {
 
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
  document.addEventListener('DOMContentLoaded', function () {
176
  const canvas = document.getElementById('drawingCanvas');
177
  const ctx = canvas.getContext('2d');
178
  const clearButton = document.getElementById('clearButton');
179
  const resultsBody = document.getElementById('resultsBody');
180
 
181
+
182
+ const canvasSize = { width: 280, height: 280 };
183
  canvas.width = canvasSize.width;
184
  canvas.height = canvasSize.height;
185
 
 
189
 
190
  ctx.lineJoin = 'round';
191
  ctx.lineCap = 'round';
192
+ ctx.lineWidth = 15;
193
  ctx.strokeStyle = 'black';
194
 
195
  function clearCanvas() {
 
204
 
205
  clearCanvas();
206
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207
  function canvasToArray() {
208
  const imageData = ctx.getImageData(0, 0, canvasSize.width, canvasSize.height);
209
+ const data = imageData.data;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
210
 
211
  const result = Array(gridSize).fill(0).map(() => Array(gridSize).fill(0));
212
 
 
251
  const response = await fetch('/classify', {
252
  method: 'POST',
253
  headers: { 'Content-Type': 'application/json' },
254
+ body: JSON.stringify({ doodle: doodleData }),
255
  });
256
 
257
  if (!response.ok) {