Spaces:
Running
Running
1. Application must have access to the user storage to upload documents or images, 2. Application must have camera access for user the capture images
Browse files- index.html +8 -6
- script.js +112 -60
index.html
CHANGED
|
@@ -4,7 +4,7 @@
|
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
<title>ClarifAI - Smart Image Recognition</title>
|
| 7 |
-
<link rel="stylesheet" href="style.css">
|
| 8 |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
|
| 9 |
<link rel="preconnect" href="https://fonts.googleapis.com">
|
| 10 |
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
@@ -29,8 +29,8 @@
|
|
| 29 |
|
| 30 |
<div class="tab-content active" id="home">
|
| 31 |
<div class="scan-area">
|
| 32 |
-
|
| 33 |
-
<div class="placeholder-image">
|
| 34 |
<div class="camera-overlay">
|
| 35 |
<div class="camera-icon">
|
| 36 |
<i class="fas fa-brain"></i>
|
|
@@ -61,7 +61,9 @@
|
|
| 61 |
</div>
|
| 62 |
</div>
|
| 63 |
</div>
|
| 64 |
-
<
|
|
|
|
|
|
|
| 65 |
<h4>Original Image</h4>
|
| 66 |
<div class="image-preview">
|
| 67 |
<img id="preview-image" src="" alt="Captured image">
|
|
@@ -72,10 +74,10 @@
|
|
| 72 |
</div>
|
| 73 |
</div>
|
| 74 |
<div class="scan-buttons">
|
| 75 |
-
<button class="scan-button upload-button">
|
| 76 |
<i class="fas fa-upload"></i>
|
| 77 |
</button>
|
| 78 |
-
<button class="scan-button capture-button">
|
| 79 |
<i class="fas fa-camera"></i>
|
| 80 |
</button>
|
| 81 |
</div>
|
|
|
|
| 4 |
<meta charset="UTF-8">
|
| 5 |
<meta name="viewport" content="width=device-width, initial-scale=1.0">
|
| 6 |
<title>ClarifAI - Smart Image Recognition</title>
|
| 7 |
+
<link rel="stylesheet" href="style.css">
|
| 8 |
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0/css/all.min.css">
|
| 9 |
<link rel="preconnect" href="https://fonts.googleapis.com">
|
| 10 |
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
|
|
|
|
| 29 |
|
| 30 |
<div class="tab-content active" id="home">
|
| 31 |
<div class="scan-area">
|
| 32 |
+
<div class="camera-view">
|
| 33 |
+
<div class="placeholder-image" id="placeholderImage">
|
| 34 |
<div class="camera-overlay">
|
| 35 |
<div class="camera-icon">
|
| 36 |
<i class="fas fa-brain"></i>
|
|
|
|
| 61 |
</div>
|
| 62 |
</div>
|
| 63 |
</div>
|
| 64 |
+
<video id="cameraStream" style="display: none; width: 100%; height: 100%; object-fit: cover;"></video>
|
| 65 |
+
<canvas id="captureCanvas" style="display: none;"></canvas>
|
| 66 |
+
<div class="image-preview-container" style="display: none;">
|
| 67 |
<h4>Original Image</h4>
|
| 68 |
<div class="image-preview">
|
| 69 |
<img id="preview-image" src="" alt="Captured image">
|
|
|
|
| 74 |
</div>
|
| 75 |
</div>
|
| 76 |
<div class="scan-buttons">
|
| 77 |
+
<button class="scan-button upload-button" id="uploadButton">
|
| 78 |
<i class="fas fa-upload"></i>
|
| 79 |
</button>
|
| 80 |
+
<button class="scan-button capture-button" id="captureButton">
|
| 81 |
<i class="fas fa-camera"></i>
|
| 82 |
</button>
|
| 83 |
</div>
|
script.js
CHANGED
|
@@ -2,6 +2,14 @@
|
|
| 2 |
document.addEventListener('DOMContentLoaded', function() {
|
| 3 |
// Initialize empty results array
|
| 4 |
const scanResults = [];
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
// Tab switching functionality
|
| 6 |
const tabButtons = document.querySelectorAll('.tab-button');
|
| 7 |
const tabContents = document.querySelectorAll('.tab-content');
|
|
@@ -24,6 +32,103 @@ const tabButtons = document.querySelectorAll('.tab-button');
|
|
| 24 |
const uploadButton = document.querySelector('.upload-button');
|
| 25 |
const captureButton = document.querySelector('.capture-button');
|
| 26 |
const textPreview = document.querySelector('.text-preview');
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
uploadButton.addEventListener('click', function() {
|
| 28 |
// Create file input for upload
|
| 29 |
const fileInput = document.createElement('input');
|
|
@@ -134,67 +239,14 @@ Due Date: 12/15/2023
|
|
| 134 |
`;
|
| 135 |
document.head.appendChild(uploadStyle);
|
| 136 |
});
|
| 137 |
-
captureButton.addEventListener('click', function() {
|
| 138 |
-
//
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
<p><i class="fas fa-spinner fa-spin"></i> Analyzing text...</p>
|
| 144 |
-
</div>
|
| 145 |
-
`;
|
| 146 |
-
|
| 147 |
-
setTimeout(() => {
|
| 148 |
-
// Store and simulate OCR results with better sample data
|
| 149 |
-
const result = {
|
| 150 |
-
type: 'scan',
|
| 151 |
-
content: 'The quick brown fox jumps over the lazy dog\n1234567890',
|
| 152 |
-
confidence: '98.7%',
|
| 153 |
-
timestamp: new Date().toISOString(),
|
| 154 |
-
imageData: 'http://static.photos/office/375x320/78'
|
| 155 |
-
};
|
| 156 |
-
scanResults.push(result);
|
| 157 |
-
document.querySelector('.camera-view').classList.remove('active-scan');
|
| 158 |
-
|
| 159 |
-
// Show image preview
|
| 160 |
-
const imagePreviewContainer = document.querySelector('.image-preview-container');
|
| 161 |
-
const previewImage = document.getElementById('preview-image');
|
| 162 |
-
previewImage.src = result.imageData;
|
| 163 |
-
imagePreviewContainer.style.display = 'block';
|
| 164 |
-
|
| 165 |
-
textPreview.innerHTML = `
|
| 166 |
-
<div class="result-header">
|
| 167 |
-
<div class="result-icon">
|
| 168 |
-
<i class="fas fa-camera"></i>
|
| 169 |
-
</div>
|
| 170 |
-
<h4>Scan Results</h4>
|
| 171 |
-
</div>
|
| 172 |
-
<div class="result-content">
|
| 173 |
-
<p class="ocr-text">The quick brown fox jumps over the lazy dog</p>
|
| 174 |
-
<p class="ocr-text">1234567890</p>
|
| 175 |
-
</div>
|
| 176 |
-
<div class="result-meta">
|
| 177 |
-
<span class="confidence">Confidence: 98.7%</span>
|
| 178 |
-
</div>
|
| 179 |
-
<p class="timestamp">Processed at ${new Date().toLocaleTimeString()}</p>
|
| 180 |
-
`;
|
| 181 |
-
|
| 182 |
-
// Add processed text section after results
|
| 183 |
-
const processedTextSection = document.createElement('div');
|
| 184 |
-
processedTextSection.className = 'processed-text-section';
|
| 185 |
-
processedTextSection.innerHTML = `
|
| 186 |
-
<h4>Processed Text</h4>
|
| 187 |
-
<div class="processed-text-content">
|
| 188 |
-
The quick brown fox jumps over the lazy dog
|
| 189 |
-
1234567890
|
| 190 |
-
</div>
|
| 191 |
-
`;
|
| 192 |
-
|
| 193 |
-
textPreview.appendChild(processedTextSection);
|
| 194 |
-
}, 3000);
|
| 195 |
});
|
| 196 |
-
|
| 197 |
-
// Function to close image preview
|
| 198 |
window.closeImagePreview = function() {
|
| 199 |
const imagePreviewContainer = document.querySelector('.image-preview-container');
|
| 200 |
imagePreviewContainer.style.display = 'none';
|
|
|
|
| 2 |
document.addEventListener('DOMContentLoaded', function() {
|
| 3 |
// Initialize empty results array
|
| 4 |
const scanResults = [];
|
| 5 |
+
|
| 6 |
+
// Camera and storage access variables
|
| 7 |
+
let cameraStream = null;
|
| 8 |
+
const cameraVideo = document.getElementById('cameraStream');
|
| 9 |
+
const captureCanvas = document.getElementById('captureCanvas');
|
| 10 |
+
const placeholderImage = document.getElementById('placeholderImage');
|
| 11 |
+
const uploadButton = document.getElementById('uploadButton');
|
| 12 |
+
const captureButton = document.getElementById('captureButton');
|
| 13 |
// Tab switching functionality
|
| 14 |
const tabButtons = document.querySelectorAll('.tab-button');
|
| 15 |
const tabContents = document.querySelectorAll('.tab-content');
|
|
|
|
| 32 |
const uploadButton = document.querySelector('.upload-button');
|
| 33 |
const captureButton = document.querySelector('.capture-button');
|
| 34 |
const textPreview = document.querySelector('.text-preview');
|
| 35 |
+
// Request camera permission and initialize camera
|
| 36 |
+
async function initializeCamera() {
|
| 37 |
+
try {
|
| 38 |
+
cameraStream = await navigator.mediaDevices.getUserMedia({
|
| 39 |
+
video: {
|
| 40 |
+
facingMode: 'environment',
|
| 41 |
+
width: { ideal: 1920 },
|
| 42 |
+
height: { ideal: 1080 }
|
| 43 |
+
});
|
| 44 |
+
cameraVideo.srcObject = cameraStream;
|
| 45 |
+
return true;
|
| 46 |
+
} catch (error) {
|
| 47 |
+
console.error('Error accessing camera:', error);
|
| 48 |
+
alert('Camera access is required for capturing images. Please allow camera permissions.');
|
| 49 |
+
return false;
|
| 50 |
+
}
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
// Capture image from camera
|
| 54 |
+
function captureImage() {
|
| 55 |
+
if (!cameraStream) {
|
| 56 |
+
alert('Please allow camera access first');
|
| 57 |
+
return;
|
| 58 |
+
}
|
| 59 |
+
|
| 60 |
+
// Show camera stream and hide placeholder
|
| 61 |
+
placeholderImage.style.display = 'none';
|
| 62 |
+
cameraVideo.style.display = 'block';
|
| 63 |
+
|
| 64 |
+
// Set canvas dimensions to match video
|
| 65 |
+
captureCanvas.width = cameraVideo.videoWidth;
|
| 66 |
+
captureCanvas.height = cameraVideo.videoHeight;
|
| 67 |
+
|
| 68 |
+
const context = captureCanvas.getContext('2d');
|
| 69 |
+
context.drawImage(cameraVideo, 0, 0);
|
| 70 |
+
|
| 71 |
+
// Get image data URL
|
| 72 |
+
const imageData = captureCanvas.toDataURL('image/jpeg');
|
| 73 |
+
|
| 74 |
+
// Process the captured image
|
| 75 |
+
processCapturedImage(imageData);
|
| 76 |
+
}
|
| 77 |
+
|
| 78 |
+
// Process captured image and show results
|
| 79 |
+
function processCapturedImage(imageData) {
|
| 80 |
+
// Show image preview
|
| 81 |
+
const imagePreviewContainer = document.querySelector('.image-preview-container');
|
| 82 |
+
const previewImage = document.getElementById('preview-image');
|
| 83 |
+
previewImage.src = imageData;
|
| 84 |
+
imagePreviewContainer.style.display = 'block';
|
| 85 |
+
|
| 86 |
+
// Store and simulate OCR results with sample data
|
| 87 |
+
const result = {
|
| 88 |
+
type: 'scan',
|
| 89 |
+
content: 'The quick brown fox jumps over the lazy dog\n1234567890',
|
| 90 |
+
confidence: '98.7%',
|
| 91 |
+
timestamp: new Date().toISOString(),
|
| 92 |
+
imageData: imageData
|
| 93 |
+
};
|
| 94 |
+
scanResults.push(result);
|
| 95 |
+
|
| 96 |
+
// Show results with image preview and processed text
|
| 97 |
+
textPreview.innerHTML = `
|
| 98 |
+
<div class="result-header">
|
| 99 |
+
<div class="result-icon">
|
| 100 |
+
<i class="fas fa-camera"></i>
|
| 101 |
+
</div>
|
| 102 |
+
<h4>Scan Results</h4>
|
| 103 |
+
</div>
|
| 104 |
+
<div class="result-content">
|
| 105 |
+
<p class="ocr-text">The quick brown fox jumps over the lazy dog</p>
|
| 106 |
+
<p class="ocr-text">1234567890</p>
|
| 107 |
+
</div>
|
| 108 |
+
<div class="result-meta">
|
| 109 |
+
<span class="confidence">Confidence: 98.7%</span>
|
| 110 |
+
</div>
|
| 111 |
+
<p class="timestamp">Processed at ${new Date().toLocaleTimeString()}</p>
|
| 112 |
+
`;
|
| 113 |
+
|
| 114 |
+
// Add processed text section after results
|
| 115 |
+
const processedTextSection = document.createElement('div');
|
| 116 |
+
processedTextSection.className = 'processed-text-section';
|
| 117 |
+
processedTextSection.innerHTML = `
|
| 118 |
+
<h4>Processed Text</h4>
|
| 119 |
+
<div class="processed-text-content">
|
| 120 |
+
The quick brown fox jumps over the lazy dog
|
| 121 |
+
1234567890
|
| 122 |
+
</div>
|
| 123 |
+
`;
|
| 124 |
+
|
| 125 |
+
textPreview.appendChild(processedTextSection);
|
| 126 |
+
|
| 127 |
+
// Hide camera and show placeholder again
|
| 128 |
+
cameraVideo.style.display = 'none';
|
| 129 |
+
placeholderImage.style.display = 'block';
|
| 130 |
+
}
|
| 131 |
+
|
| 132 |
uploadButton.addEventListener('click', function() {
|
| 133 |
// Create file input for upload
|
| 134 |
const fileInput = document.createElement('input');
|
|
|
|
| 239 |
`;
|
| 240 |
document.head.appendChild(uploadStyle);
|
| 241 |
});
|
| 242 |
+
captureButton.addEventListener('click', async function() {
|
| 243 |
+
// Request camera permission if not already granted
|
| 244 |
+
const cameraInitialized = await initializeCamera();
|
| 245 |
+
if (cameraInitialized) {
|
| 246 |
+
captureImage();
|
| 247 |
+
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 248 |
});
|
| 249 |
+
// Function to close image preview
|
|
|
|
| 250 |
window.closeImagePreview = function() {
|
| 251 |
const imagePreviewContainer = document.querySelector('.image-preview-container');
|
| 252 |
imagePreviewContainer.style.display = 'none';
|