Skip to content

Commit 4d5f1f5

Browse files
committed
Provide controls in UI and API layer to enable/disable image processing options.
1 parent 756a990 commit 4d5f1f5

File tree

2 files changed

+49
-32
lines changed

2 files changed

+49
-32
lines changed

html/index.html

Lines changed: 41 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,35 @@ <h1 class="text-center mb-4">Extract text from documents</h1>
1818
<label for="fileInput" class="form-label">Upload an image, a scanned document or a PDF.</label>
1919
<input class="form-control" type="file" id="fileInput" accept="image/*,.pdf" required>
2020
</div>
21-
<div class="accordion mb-3" id="processingOptionsAccordion">
22-
<div class="accordion-item border-0">
23-
<h2 class="accordion-header" id="headingOptions">
24-
<button class="accordion-button collapsed py-2 small" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOptions" aria-expanded="false" aria-controls="collapseOptions">
25-
Advanced Options <span class="text-muted ms-2 small">(optional)</span>
26-
</button>
27-
</h2>
28-
<div id="collapseOptions" class="accordion-collapse collapse" aria-labelledby="headingOptions" data-bs-parent="#processingOptionsAccordion">
29-
<div class="accordion-body py-2">
30-
<div class="form-check small mb-1">
31-
<input class="form-check-input" type="checkbox" value="true" id="removeNoise" name="remove_noise">
32-
<label class="form-check-label" for="removeNoise">Remove noise and specks (Uses Bilateral Filter)</label>
33-
</div>
34-
<div class="form-check small mb-1">
35-
<input class="form-check-input" type="checkbox" value="true" id="binarization" name="binarize">
36-
<label class="form-check-label" for="binarization">Perform binarization (Uses Adaptive Thresholding)</label>
37-
</div>
38-
<div class="form-check small">
39-
<input class="form-check-input" type="checkbox" value="true" id="autoRotate" name="auto_rotate">
40-
<label class="form-check-label" for="autoRotate">Auto rotate if needed (Contour Detection and Alignment)</label>
41-
</div>
42-
</div>
43-
</div>
44-
</div>
45-
</div>
21+
<div class="accordion mb-3" id="processingOptionsAccordion">
22+
<div class="accordion-item border-0">
23+
<h2 class="accordion-header" id="headingOptions">
24+
<button class="accordion-button collapsed py-2 small" type="button" data-bs-toggle="collapse" data-bs-target="#collapseOptions" aria-expanded="false" aria-controls="collapseOptions">
25+
Advanced Options <span class="text-muted ms-2 small">(optional)</span>
26+
</button>
27+
</h2>
28+
<div id="collapseOptions" class="accordion-collapse collapse" aria-labelledby="headingOptions" data-bs-parent="#processingOptionsAccordion">
29+
<div class="accordion-body py-2">
30+
<div class="form-check small mb-1">
31+
<input class="form-check-input" type="checkbox" value="true" id="gray" name="gray" checked>
32+
<label class="form-check-label" for="gray">Perform Grayscale conversion</label>
33+
</div>
34+
<div class="form-check small mb-1">
35+
<input class="form-check-input" type="checkbox" value="true" id="denoise" name="denoise" checked>
36+
<label class="form-check-label" for="denoise">Remove noise and specks (Uses Bilateral Filter)</label>
37+
</div>
38+
<div class="form-check small mb-1">
39+
<input class="form-check-input" type="checkbox" value="true" id="binarize" name="binarize" checked>
40+
<label class="form-check-label" for="binarize">Perform binarization (Uses Adaptive Thresholding)</label>
41+
</div>
42+
<div class="form-check small">
43+
<input class="form-check-input" type="checkbox" value="true" id="autoRotate" name="auto_rotate" checked>
44+
<label class="form-check-label" for="autoRotate">Auto rotate if needed (Contour Detection and Alignment)</label>
45+
</div>
46+
</div>
47+
</div>
48+
</div>
49+
</div>
4650
<div class="d-flex justify-content-between">
4751
<button type="submit" class="btn btn-outline-success w-50 me-2 d-flex align-items-center justify-content-center gap-2" data-mode="basic">
4852
Basic OCR (Faster and Recommended)
@@ -87,20 +91,28 @@ <h4>Detected Text:</h4>
8791

8892
form.addEventListener('submit', async (event) => {
8993
event.preventDefault();
90-
// Reset, hide previous alert
91-
errorAlert.classList.add('d-none');
92-
errorAlert.textContent = '';
93-
resultText.textContent = 'OCR and Recognition is computationally expensive, and takes time. Please wait...';
9494

9595
const file = fileInput.files[0];
9696
if (!file) {
9797
alert('Please select a file first.');
9898
return;
9999
}
100100

101+
// Get checkbox states
102+
const grayChecked = document.getElementById('gray').checked;
103+
const denoiseChecked = document.getElementById('denoise').checked;
104+
const binarizeChecked = document.getElementById('binarize').checked;
105+
// Reset, hide previous alert
106+
errorAlert.classList.add('d-none');
107+
errorAlert.textContent = '';
108+
resultText.textContent = 'OCR and Recognition is computationally expensive, and takes time. Please wait...';
109+
101110
const formData = new FormData();
102111
formData.append('attachment', file);
103112
formData.append('synchronous', false);
113+
formData.append('gray', grayChecked);
114+
formData.append('denoise', denoiseChecked);
115+
formData.append('binarize', binarizeChecked);
104116

105117
resultText.textContent = 'OCR and Recognition is computationally expensive, and takes time. Please wait...';
106118
const BASE_URL = 'http://localhost:8000';

main.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from typing import List
55

66
from fastapi import FastAPI
7-
from fastapi import UploadFile
7+
from fastapi import UploadFile, Form
88
from fastapi.exceptions import HTTPException
99
from fastapi.middleware.cors import CORSMiddleware
1010

@@ -117,7 +117,7 @@ def extract_img_text(attachment: UploadFile):
117117

118118

119119
@app.post("/ocr")
120-
def ocr(attachment: UploadFile):
120+
def ocr(attachment: UploadFile, gray: bool = Form(True), denoise: bool = Form(True), binarize: bool = Form(True)):
121121
"""
122122
TODO: Support multiple attachments
123123
It could pass a PDF or an image.
@@ -133,6 +133,11 @@ def ocr(attachment: UploadFile):
133133
In all of the above cases, the processing would happen asynchronously.
134134
The task would be queued and a link would be returned to the user.
135135
"""
136+
options = {
137+
"gray": gray,
138+
"denoise": denoise,
139+
"binarize": binarize
140+
}
136141
type_details = identify_file_type(attachment.file)
137142
if not type_details.mime_type.startswith('image') and not type_details.mime_type.startswith('application/pdf'):
138143
raise HTTPException(status_code=400, detail="Provide either an image or a PDF")
@@ -145,7 +150,7 @@ def ocr(attachment: UploadFile):
145150
if type_details.mime_type.startswith('image'):
146151
# Attempt extraction through Tesseract
147152
set_object(key=path_hash, field="type", value="image")
148-
enqueue_extraction(extraction_function=extract_image_text_and_set_db, file_path=output_filename, key=path_hash)
153+
enqueue_extraction(extraction_function=extract_image_text_and_set_db, file_path=output_filename, key=path_hash, options=options)
149154
elif type_details.mime_type.startswith('application/pdf'):
150155
# Attempt extracting text using pdfminer.six or else through the image conversion -> OCR pipeline.
151156
set_object(key=path_hash, field="type", value="pdf")

0 commit comments

Comments
 (0)