forked from KorkanaRahul/OCR_interface
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
130 lines (110 loc) · 4.89 KB
/
app.js
File metadata and controls
130 lines (110 loc) · 4.89 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
const dragDropZone = document.getElementById('dragDropZone');
const fileInput = document.getElementById('fileInput');
const uploadedImagePreview = document.getElementById('uploadedImagePreview');
const resultColumn = document.getElementById('resultColumn');
const detectedInfo = document.getElementById('detectedInfo');
const detectedTextSpan = document.getElementById('detectedText');
const progressBar = document.getElementById('progressBar');
const progress = progressBar.querySelector('.progress');
const sampleSelect = document.getElementById('sampleSelect');
dragDropZone.addEventListener('click', () => fileInput.click());
dragDropZone.addEventListener('dragover', (e) => e.preventDefault());
dragDropZone.addEventListener('drop', (e) => {
e.preventDefault();
const file = e.dataTransfer.files[0];
if (file) showPreview(file);
});
fileInput.addEventListener('change', () => {
const file = fileInput.files[0];
if (file) showPreview(file);
});
// Handle sample image selection from dropdown
sampleSelect.addEventListener('change', async (e) => {
const imageUrl = e.target.value;
if (imageUrl) {
try {
// Fetch the selected sample image
const response = await fetch(imageUrl);
if (!response.ok) {
throw new Error('Failed to fetch image.');
}
const blob = await response.blob();
// Create a file from the blob and pass it to showPreview function
const file = new File([blob], imageUrl.split('/').pop(), { type: blob.type });
showPreview(file);
} catch (error) {
console.error('Error fetching sample image:', error);
}
}
});
function showPreview(file) {
const reader = new FileReader();
reader.onload = (e) => {
uploadedImagePreview.src = e.target.result;
uploadedImagePreview.style.display = 'block';
handleUpload(file);
};
reader.readAsDataURL(file);
}
async function handleUpload(file) {
const formData = new FormData();
formData.append('image', file);
resultColumn.innerHTML = ""; // Clear previous results
detectedInfo.style.display = "none";
progressBar.style.display = "block";
progress.style.width = "0%";
const loadingSpinner = document.createElement('div');
loadingSpinner.className = 'loading-spinner';
resultColumn.appendChild(loadingSpinner); // Show loading spinner
try {
const response = await fetch('http://192.168.137.37:5001/detect_sendimg', {
method: 'POST',
body: formData
});
if (!response.ok) {
throw new Error('Error uploading the image.');
}
const data = await response.json();
loadingSpinner.style.display = 'none'; // Hide spinner
progress.style.width = "25%";
// Display stages with meaningful hover descriptions
data.stages.forEach((stage, index) => {
setTimeout(() => {
const stageDiv = document.createElement('div');
stageDiv.className = 'stage-card';
const stageImage = document.createElement('img');
stageImage.src = `data:image/png;base64,${stage.image}`;
stageImage.alt = stage.title;
const stageTitle = document.createElement('h4');
stageTitle.textContent = stage.title;
const hoverDescription = document.createElement('div');
hoverDescription.className = 'hover-description';
hoverDescription.textContent = stage.description;
stageDiv.appendChild(stageImage);
stageDiv.appendChild(stageTitle);
stageDiv.appendChild(hoverDescription);
resultColumn.appendChild(stageDiv);
if (index < data.stages.length - 1) {
const arrow = document.createElement('div');
arrow.className = 'arrow';
arrow.textContent = '⬇';
resultColumn.appendChild(arrow);
}
progress.style.width = `${(50 + (index + 1) * (50 / data.stages.length))}%`;
}, index * 1000);
});
// Show detected text after stages
setTimeout(() => {
detectedTextSpan.textContent = data.text;
detectedInfo.style.display = "block";
progressBar.style.display = "none";
}, data.stages.length * 1000);
} catch (error) {
console.error('Error processing the image:', error);
const errorDiv = document.createElement('div');
errorDiv.className = 'error-message';
errorDiv.textContent = 'Error processing the image. Please try again later.';
resultColumn.appendChild(errorDiv);
progressBar.style.display = "none";
}
}