ericjeon commited on
Commit
b2644cb
·
verified ·
1 Parent(s): 23a236c

create a website so that

Browse files

- if i move the cursor with left click applied, i can draw(actually write) on canvas.
- once i stop drawing for more than 0.5 seconds, the website immediately performs ocr, and pops out what i wrote in text.
- target orthography can be anything, including roman alphabets, emojis, numbers, chinese characters, and hangul.

Files changed (2) hide show
  1. README.md +8 -5
  2. index.html +188 -18
README.md CHANGED
@@ -1,10 +1,13 @@
1
  ---
2
- title: Scribblesense Ocr Wizard
3
- emoji: 🐢
4
- colorFrom: red
5
- colorTo: green
6
  sdk: static
7
  pinned: false
 
 
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
1
  ---
2
+ title: ScribbleSense OCR Wizard ✍️
3
+ colorFrom: purple
4
+ colorTo: red
5
+ emoji: 🐳
6
  sdk: static
7
  pinned: false
8
+ tags:
9
+ - deepsite-v3
10
  ---
11
 
12
+ # Welcome to your new DeepSite project!
13
+ This project was created with [DeepSite](https://deepsite.hf.co).
index.html CHANGED
@@ -1,19 +1,189 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19
  </html>
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>ScribbleSense - Draw and Recognize Text</title>
7
+ <script src="https://cdn.tailwindcss.com"></script>
8
+ <script src="https://unpkg.com/feather-icons"></script>
9
+ <script src="https://cdn.jsdelivr.net/npm/tesseract.js@4/dist/tesseract.min.js"></script>
10
+ </head>
11
+ <body class="bg-gradient-to-br from-indigo-100 to-purple-50 min-h-screen flex flex-col items-center justify-center p-4">
12
+ <div class="max-w-4xl w-full mx-auto text-center">
13
+ <h1 class="text-4xl md:text-5xl font-bold text-indigo-800 mb-2">ScribbleSense ✍️</h1>
14
+ <p class="text-lg text-indigo-600 mb-8">Draw anything and watch it transform into text!</p>
15
+
16
+ <div class="bg-white rounded-xl shadow-xl p-6 mb-8">
17
+ <div class="relative">
18
+ <canvas id="drawingCanvas" class="border-2 border-gray-300 rounded-lg w-full h-64 md:h-96 touch-none bg-white"></canvas>
19
+ <div id="clearBtn" class="absolute top-4 right-4 p-2 bg-white rounded-full shadow-md cursor-pointer hover:bg-gray-100 transition">
20
+ <i data-feather="x" class="text-gray-600"></i>
21
+ </div>
22
+ </div>
23
+ </div>
24
+
25
+ <div id="resultContainer" class="bg-white rounded-xl shadow-xl p-6 hidden">
26
+ <h2 class="text-2xl font-semibold text-indigo-700 mb-4">Recognized Text</h2>
27
+ <div id="resultText" class="text-3xl md:text-4xl font-mono p-4 bg-gray-50 rounded-lg min-h-20 break-all">
28
+ <!-- OCR result will appear here -->
29
+ </div>
30
+ <button id="copyBtn" class="mt-4 px-4 py-2 bg-indigo-600 text-white rounded-lg hover:bg-indigo-700 transition flex items-center justify-center gap-2 mx-auto">
31
+ <i data-feather="copy" class="w-5 h-5"></i> Copy Text
32
+ </button>
33
+ </div>
34
+
35
+ <div class="mt-8 text-sm text-gray-500">
36
+ <p>Draw with left mouse button pressed. The system will automatically recognize your writing after a pause.</p>
37
+ <p class="mt-1">Supports: Roman alphabets, numbers, emojis, Chinese, Hangul, and more!</p>
38
+ </div>
39
+ </div>
40
+
41
+ <script>
42
+ document.addEventListener('DOMContentLoaded', function() {
43
+ feather.replace();
44
+
45
+ const canvas = document.getElementById('drawingCanvas');
46
+ const ctx = canvas.getContext('2d');
47
+ const resultContainer = document.getElementById('resultContainer');
48
+ const resultText = document.getElementById('resultText');
49
+ const clearBtn = document.getElementById('clearBtn');
50
+ const copyBtn = document.getElementById('copyBtn');
51
+
52
+ // Set canvas size
53
+ function resizeCanvas() {
54
+ const containerWidth = canvas.parentElement.clientWidth;
55
+ canvas.width = containerWidth - 32; // accounting for padding
56
+ canvas.height = Math.min(window.innerHeight * 0.6, 600);
57
+ ctx.lineWidth = 4;
58
+ ctx.lineCap = 'round';
59
+ ctx.strokeStyle = '#4f46e5'; // indigo-600
60
+ }
61
+
62
+ window.addEventListener('resize', resizeCanvas);
63
+ resizeCanvas();
64
+
65
+ // Drawing variables
66
+ let isDrawing = false;
67
+ let lastX = 0;
68
+ let lastY = 0;
69
+ let inactivityTimer;
70
+ let lastDrawTime = 0;
71
+
72
+ // Drawing functions
73
+ function startDrawing(e) {
74
+ isDrawing = true;
75
+ [lastX, lastY] = getPosition(e);
76
+ clearTimeout(inactivityTimer);
77
+ }
78
+
79
+ function draw(e) {
80
+ if (!isDrawing) return;
81
+
82
+ const [x, y] = getPosition(e);
83
+
84
+ ctx.beginPath();
85
+ ctx.moveTo(lastX, lastY);
86
+ ctx.lineTo(x, y);
87
+ ctx.stroke();
88
+
89
+ [lastX, lastY] = [x, y];
90
+ lastDrawTime = Date.now();
91
+
92
+ // Reset inactivity timer
93
+ clearTimeout(inactivityTimer);
94
+ inactivityTimer = setTimeout(performOCR, 500);
95
+ }
96
+
97
+ function stopDrawing() {
98
+ isDrawing = false;
99
+ }
100
+
101
+ function getPosition(e) {
102
+ let x, y;
103
+ if (e.type.includes('touch')) {
104
+ const rect = canvas.getBoundingClientRect();
105
+ x = e.touches[0].clientX - rect.left;
106
+ y = e.touches[0].clientY - rect.top;
107
+ } else {
108
+ x = e.offsetX;
109
+ y = e.offsetY;
110
+ }
111
+ return [x, y];
112
+ }
113
+
114
+ // OCR function
115
+ function performOCR() {
116
+ if (Date.now() - lastDrawTime < 500) return;
117
+
118
+ Tesseract.recognize(
119
+ canvas,
120
+ 'eng+chi_sim+chi_tra+kor+jpn+equ',
121
+ {
122
+ logger: m => console.log(m)
123
+ }
124
+ ).then(({ data: { text } }) => {
125
+ resultText.textContent = text.trim() || "Couldn't recognize any text";
126
+ resultContainer.classList.remove('hidden');
127
+
128
+ // Scroll to result if needed
129
+ resultContainer.scrollIntoView({ behavior: 'smooth', block: 'nearest' });
130
+ });
131
+ }
132
+
133
+ // Clear canvas
134
+ function clearCanvas() {
135
+ ctx.clearRect(0, 0, canvas.width, canvas.height);
136
+ resultContainer.classList.add('hidden');
137
+ clearTimeout(inactivityTimer);
138
+ }
139
+
140
+ // Copy text function
141
+ function copyText() {
142
+ navigator.clipboard.writeText(resultText.textContent).then(() => {
143
+ const originalText = copyBtn.innerHTML;
144
+ copyBtn.innerHTML = '<i data-feather="check" class="w-5 h-5"></i> Copied!';
145
+ setTimeout(() => {
146
+ copyBtn.innerHTML = originalText;
147
+ feather.replace();
148
+ }, 2000);
149
+ });
150
+ }
151
+
152
+ // Event listeners
153
+ canvas.addEventListener('mousedown', startDrawing);
154
+ canvas.addEventListener('mousemove', draw);
155
+ canvas.addEventListener('mouseup', stopDrawing);
156
+ canvas.addEventListener('mouseout', stopDrawing);
157
+
158
+ // Touch support
159
+ canvas.addEventListener('touchstart', (e) => {
160
+ e.preventDefault();
161
+ startDrawing(e);
162
+ });
163
+ canvas.addEventListener('touchmove', (e) => {
164
+ e.preventDefault();
165
+ draw(e);
166
+ });
167
+ canvas.addEventListener('touchend', stopDrawing);
168
+
169
+ clearBtn.addEventListener('click', clearCanvas);
170
+ copyBtn.addEventListener('click', copyText);
171
+
172
+ // Instructions tooltip
173
+ setTimeout(() => {
174
+ const tooltip = document.createElement('div');
175
+ tooltip.className = 'absolute bg-indigo-600 text-white px-4 py-2 rounded-lg shadow-lg z-10 animate-bounce';
176
+ tooltip.style.top = '20px';
177
+ tooltip.style.left = '50%';
178
+ tooltip.style.transform = 'translateX(-50%)';
179
+ tooltip.textContent = 'Draw here with your mouse or finger!';
180
+ canvas.parentElement.appendChild(tooltip);
181
+
182
+ setTimeout(() => {
183
+ tooltip.remove();
184
+ }, 3000);
185
+ }, 1000);
186
+ });
187
+ </script>
188
+ </body>
189
  </html>