Spaces:
Sleeping
Sleeping
File size: 12,003 Bytes
4d2f9e8 | 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 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 | <!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>SPaCial AI - Live Scanner</title>
<script src="https://telegram.org/js/telegram-web-app.js"></script>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Arial, sans-serif;
background: #000;
overflow: hidden;
position: fixed;
width: 100%;
height: 100%;
}
#container {
position: relative;
width: 100%;
height: 100%;
display: flex;
flex-direction: column;
}
#videoContainer {
position: relative;
flex: 1;
overflow: hidden;
background: #000;
}
#video {
width: 100%;
height: 100%;
object-fit: cover;
}
#canvas {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
pointer-events: none;
}
#overlay {
position: absolute;
top: 20px;
left: 20px;
right: 20px;
background: rgba(0, 0, 0, 0.7);
color: white;
padding: 15px;
border-radius: 10px;
font-size: 14px;
z-index: 10;
}
#status {
font-weight: bold;
margin-bottom: 5px;
}
.zone-info {
font-size: 12px;
margin: 5px 0;
color: #0f0;
}
#controls {
background: rgba(0, 0, 0, 0.9);
padding: 20px;
display: flex;
justify-content: space-around;
align-items: center;
}
button {
background: #0088cc;
color: white;
border: none;
padding: 15px 30px;
border-radius: 25px;
font-size: 16px;
font-weight: bold;
cursor: pointer;
transition: all 0.3s;
min-width: 120px;
}
button:active {
transform: scale(0.95);
}
button:disabled {
background: #666;
cursor: not-allowed;
}
#captureBtn {
background: #28a745;
font-size: 18px;
padding: 20px 40px;
}
#toggleScanBtn {
background: #ffc107;
}
#toggleScanBtn.scanning {
background: #dc3545;
}
.error {
color: #ff4444;
background: rgba(255, 68, 68, 0.2);
padding: 10px;
border-radius: 5px;
margin-top: 10px;
}
.loading {
display: inline-block;
width: 12px;
height: 12px;
border: 2px solid #fff;
border-radius: 50%;
border-top-color: transparent;
animation: spin 0.6s linear infinite;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
#zoneCount {
font-size: 24px;
color: #0f0;
font-weight: bold;
}
</style>
</head>
<body>
<div id="container">
<div id="videoContainer">
<video id="video" autoplay playsinline></video>
<canvas id="canvas"></canvas>
<div id="overlay">
<div id="status">πΈ Camera Ready</div>
<div id="zoneCount">0 zones detected</div>
<div id="details"></div>
</div>
</div>
<div id="controls">
<button id="toggleScanBtn" onclick="toggleScanning()">Start Scan</button>
<button id="captureBtn" onclick="captureResults()" disabled>Capture</button>
</div>
</div>
<script>
const tg = window.Telegram.WebApp;
tg.ready();
tg.expand();
const video = document.getElementById('video');
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
const statusEl = document.getElementById('status');
const zoneCountEl = document.getElementById('zoneCount');
const detailsEl = document.getElementById('details');
const toggleBtn = document.getElementById('toggleScanBtn');
const captureBtn = document.getElementById('captureBtn');
let isScanning = false;
let scanInterval = null;
let lastResults = null;
let stream = null;
// Get API URL (same host as mini app)
const API_URL = window.location.origin;
// Initialize camera
async function initCamera() {
try {
stream = await navigator.mediaDevices.getUserMedia({
video: {
facingMode: 'environment',
width: { ideal: 1920 },
height: { ideal: 1080 }
}
});
video.srcObject = stream;
video.addEventListener('loadedmetadata', () => {
canvas.width = video.videoWidth;
canvas.height = video.videoHeight;
statusEl.textContent = 'β
Camera Ready';
});
} catch (error) {
statusEl.innerHTML = `β Camera Error: ${error.message}`;
console.error('Camera error:', error);
}
}
// Toggle scanning
function toggleScanning() {
if (isScanning) {
stopScanning();
} else {
startScanning();
}
}
// Start continuous scanning
function startScanning() {
isScanning = true;
toggleBtn.textContent = 'Stop Scan';
toggleBtn.classList.add('scanning');
captureBtn.disabled = true;
// Scan every 2 seconds
scanInterval = setInterval(scanFrame, 2000);
scanFrame(); // First scan immediately
}
// Stop scanning
function stopScanning() {
isScanning = false;
toggleBtn.textContent = 'Start Scan';
toggleBtn.classList.remove('scanning');
captureBtn.disabled = false;
if (scanInterval) {
clearInterval(scanInterval);
scanInterval = null;
}
}
// Scan current frame
async function scanFrame() {
try {
statusEl.innerHTML = 'β³ Processing... <span class="loading"></span>';
// Draw video frame to canvas
ctx.drawImage(video, 0, 0, canvas.width, canvas.height);
// Get image as blob
const blob = await new Promise(resolve => canvas.toBlob(resolve, 'image/jpeg', 0.95));
// Send to OCR API
const formData = new FormData();
formData.append('file', blob, 'frame.jpg');
formData.append('mode', 'fast');
const response = await fetch(`${API_URL}/ocr/process`, {
method: 'POST',
body: formData
});
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const result = await response.json();
lastResults = result;
// Draw results
drawResults(result);
} catch (error) {
statusEl.innerHTML = `β Error: ${error.message}`;
console.error('Scan error:', error);
}
}
// Draw OCR results on canvas
function drawResults(result) {
const zones = result.zones || [];
// Clear previous drawings
ctx.clearRect(0, 0, canvas.width, canvas.height);
// Update status
statusEl.textContent = 'β
Scanning...';
zoneCountEl.textContent = `${zones.length} zones detected`;
// Draw zones
zones.forEach((zone, index) => {
const x = zone.x;
const y = zone.y;
const w = zone.width;
const h = zone.height;
// Draw green box
ctx.strokeStyle = '#00ff00';
ctx.lineWidth = 3;
ctx.strokeRect(x, y, w, h);
// Draw text label with background
const text = zone.text;
const fontSize = Math.max(16, Math.min(w / text.length * 1.5, 24));
ctx.font = `bold ${fontSize}px Arial`;
const textMetrics = ctx.measureText(text);
const textWidth = textMetrics.width;
const textHeight = fontSize;
// Background for text
ctx.fillStyle = 'rgba(0, 255, 0, 0.8)';
ctx.fillRect(x, y - textHeight - 8, textWidth + 10, textHeight + 8);
// Text
ctx.fillStyle = '#000';
ctx.fillText(text, x + 5, y - 8);
});
// Show first few zones in details
if (zones.length > 0) {
const preview = zones.slice(0, 3).map((z, i) =>
`${i + 1}. ${z.text} (${Math.round(z.confidence * 100)}%)`
).join('<br>');
detailsEl.innerHTML = `<div class="zone-info">${preview}${zones.length > 3 ? '<br>...' : ''}</div>`;
} else {
detailsEl.innerHTML = '';
}
}
// Capture and send results
async function captureResults() {
if (!lastResults) {
tg.showAlert('No results to capture! Start scanning first.');
return;
}
try {
stopScanning();
statusEl.textContent = 'π€ Sending results...';
// Send results back to Telegram chat
const zones = lastResults.zones || [];
const message = `π OCR Results Captured!\n\nβ
Found ${zones.length} text zones\n\nProcessing complete!`;
// You can send data back to bot via bot API if needed
// For now, just show confirmation
tg.showAlert(`β
Captured ${zones.length} zones!\n\nResults saved.`, () => {
tg.close();
});
} catch (error) {
statusEl.innerHTML = `<div class="error">Error: ${error.message}</div>`;
}
}
// Handle Telegram back button
tg.BackButton.show();
tg.BackButton.onClick(() => {
if (stream) {
stream.getTracks().forEach(track => track.stop());
}
tg.close();
});
// Initialize on load
initCamera();
</script>
</body>
</html>
|