-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimagescan.py
More file actions
341 lines (277 loc) · 12.5 KB
/
imagescan.py
File metadata and controls
341 lines (277 loc) · 12.5 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
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
import cv2
from azure.ai.vision.imageanalysis import ImageAnalysisClient
from azure.ai.vision.imageanalysis.models import VisualFeatures
from azure.core.credentials import AzureKeyCredential
from imutils.perspective import four_point_transform
from pymongo import MongoClient
import numpy as np
import os
import uuid
import json
from concurrent.futures import ThreadPoolExecutor
from glob import glob
from pathlib import Path
from dotenv import load_dotenv
import fitz
import docx
import io
from PIL import Image
import zipfile
from google.cloud import storage
from google.oauth2 import service_account
import datetime
import shutil
load_dotenv()
# MongoDB setup
DATABASE_NAME = os.getenv("db")
COLLECTION_NAME = os.getenv("documentcollection")
mongouri = os.getenv("client")
client = MongoClient(mongouri)
db = client[DATABASE_NAME]
collection = db[COLLECTION_NAME]
IMAGE_FOLDER = Path(r"C:\path\to\storage")
PROCESSED_FOLDER = Path(r"C:\path\to\output")
os.makedirs(PROCESSED_FOLDER, exist_ok=True)
# this loads the GCP service account info from .env
# Define the path where the GCP_key.json will be saved
SERVICE_KEY_PATH = Path(r"C:\path\to\GCP_key.json")
# List of environment variables corresponding to the service account .env fields
service_account_fields = [
"type",
"project_id",
"private_key_id",
"private_key",
"client_email",
"client_id",
"auth_uri",
"token_uri",
"auth_provider_x509_cert_url",
"client_x509_cert_url",
"universe_domain",
]
# Populate the GCP_key.json using environment variables
service_account_data = {field: os.getenv(field.upper()) for field in service_account_fields}
# Ensure all required environment variables are provided
missing_fields = [field for field, value in service_account_data.items() if not value]
if missing_fields:
raise ValueError(f"Missing required environment variables: {', '.join(missing_fields)}")
# Write the JSON data to the GCP_key.json file
SERVICE_KEY_PATH.parent.mkdir(parents=True, exist_ok=True) # Ensure the directory exists
with SERVICE_KEY_PATH.open("w") as f:
json.dump(service_account_data, f, indent=4)
# Load the service account credentials
credentials = service_account.Credentials.from_service_account_file(SERVICE_KEY_PATH)
# Azure Computer Vision setup
VISION_ENDPOINT = os.getenv("AZURE_VISION_ENDPOINT")
VISION_KEY = os.getenv("AZURE_VISION_KEY")
# Initialize Google Cloud Storage client with the explicit credentials, add your bucket!
storage_client = storage.Client(credentials=credentials)
bucket = storage_client.bucket("UR_BUCKET_HERE")
def extract_text_with_azure(image_path):
#Extract text from image using Azure Computer Vision
try:
# Create client
client = ImageAnalysisClient(
endpoint=VISION_ENDPOINT,
credential=AzureKeyCredential(VISION_KEY)
)
# Open image file
with open(image_path, "rb") as image_file:
# Analyze image
result = client.analyze(
image_data=image_file,
visual_features=[VisualFeatures.READ]
)
# Extract text from result
if result.read is not None:
text_lines = []
for block in result.read.blocks:
for line in block.lines:
text_lines.append(line.text)
return '\n'.join(text_lines)
return ""
except Exception as e:
print(f"Azure OCR Error: {e}")
return ""
def extract_text_from_pdf_with_azure(pdf_path):
#Extract text from PDF using Azure OCR
try:
# Convert PDF pages to images
doc = fitz.open(pdf_path)
extracted_text = []
for page in doc:
# Convert page to image
pix = page.get_pixmap()
img = Image.frombytes("RGB", [pix.width, pix.height], pix.samples)
# Save temporary image for Azure OCR
temp_image_path = PROCESSED_FOLDER / f"temp_page_{uuid.uuid4()}.jpg"
img.save(temp_image_path)
# Extract text using Azure
page_text = extract_text_with_azure(str(temp_image_path))
extracted_text.append(page_text)
# Remove temporary image
os.unlink(temp_image_path)
doc.close()
return '\n'.join(extracted_text)
except Exception as e:
print(f"Azure PDF processing error: {e}")
return ""
def extract_text_from_docx(docx_path):
#Extract text from DOCX file.
try:
doc = docx.Document(docx_path)
return '\n'.join([paragraph.text for paragraph in doc.paragraphs])
except Exception as e:
print(f"Error processing DOCX: {e}")
return ""
def zip_processed_files(file_path):
#Create a zip archive containing the original file.
file_path = Path(file_path)
base_name = file_path.stem
zip_path = PROCESSED_FOLDER / f"{base_name}.zip"
try:
with zipfile.ZipFile(zip_path, 'w') as zipf:
# Add the original file
zipf.write(file_path, file_path.name)
# Add any processed files if they exist
for processed_file in PROCESSED_FOLDER.iterdir():
if processed_file.is_file() and processed_file.name.startswith(('cropped_', 'binary_', 'edges_')):
zipf.write(processed_file, processed_file.name)
if os.path.exists(zip_path) and os.path.getsize(zip_path) > 0:
print(f"Successfully created zip file: {zip_path}")
return zip_path
else:
print("Warning: Created zip file is empty")
return None
except Exception as e:
print(f"Error creating zip file: {e}")
return None
def upload_to_gcp(file_path):
#Upload file to GCP and generate signed URL.
blob_name = f"{file_path.name}"
blob = bucket.blob(blob_name)
blob.upload_from_filename(str(file_path)) # Upload the file directly to the bucket
url = blob.generate_signed_url(
version="v4",
expiration=datetime.timedelta(days=7),
method="GET"
)
return url
def process_image(file_path):
#Process files of various types with fallback for unsupported/unprocessable files.
print(f"Processing: {file_path}")
file_path = Path(file_path)
extracted_text = ""
cropped_path = binary_path = edges_path = None
try:
supported_image_formats = (".jpg", ".jpeg", ".png", ".bmp", ".tiff")
file_type = file_path.suffix.lower()
# Try to process the file based on type
try:
if file_type in supported_image_formats:
image = cv2.imread(str(file_path))
if image is not None:
# Convert to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Enhanced preprocessing
# Add bilateral filtering to reduce noise while preserving edges
denoised = cv2.bilateralFilter(gray, 9, 75, 75)
# Adaptive thresholding instead of Otsu's method
binary = cv2.adaptiveThreshold(denoised, 255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 11, 2)
edges = cv2.Canny(denoised, 30, 200)
# Dilates edges to connect fragmented contours
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3, 3))
edges = cv2.dilate(edges, kernel, iterations=2)
# Save intermediate results
base_name = file_path.stem
binary_path = PROCESSED_FOLDER / f"binary_{base_name}.jpg"
edges_path = PROCESSED_FOLDER / f"edges_{base_name}.jpg"
cv2.imwrite(str(binary_path), binary)
cv2.imwrite(str(edges_path), edges)
# Find contours with different approach
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
# Sort contours by area, filter out very small contours
contours = [c for c in contours if cv2.contourArea(c) > 100] # Adjust area threshold as needed
contours = sorted(contours, key=cv2.contourArea, reverse=True)
# Find largest rectangular contour
for c in contours:
peri = cv2.arcLength(c, True)
approx = cv2.approxPolyDP(c, 0.02 * peri, True)
if len(approx) == 4: # Quadrilateral detected
warped = four_point_transform(image, approx.reshape(4, 2))
unique_id = str(uuid.uuid4())
cropped_path = PROCESSED_FOLDER / f"cropped_{unique_id}.jpg"
cv2.imwrite(str(cropped_path), warped)
# Extect with Azure OCR
extracted_text = extract_text_with_azure(str(cropped_path))
break
if cropped_path:
extracted_text = extract_text_with_azure(str(cropped_path))
if not extracted_text:
# Fallback to full image OCR if crop fails
extracted_text = extract_text_with_azure(str(file_path))
elif file_type == '.pdf':
extracted_text = extract_text_from_pdf_with_azure(str(file_path))
elif file_type == '.docx':
extracted_text = extract_text_from_docx(str(file_path))
except Exception as e:
print(f"Error processing file contents: {e}")
# Create zip file
zip_path = zip_processed_files(file_path)
if not zip_path:
print("Failed to create zip file, uploading original file")
zip_path = file_path
# Upload to GCP directly to the bucket
gcp_url = upload_to_gcp(zip_path)
# Save to MongoDB
record = {
"original_file_path": str(file_path),
"file_type": file_type,
"cropped_image_path": gcp_url,
"file_name": file_path.name,
"extracted_text": extracted_text,
"processing_status": "fully_processed" if extracted_text else "uploaded_only"
}
result = collection.insert_one(record)
print(f"Document processed and saved with ID: {result.inserted_id}")
# Cleanup processed files, this clears storage and output
cleanup_processed_files()
except Exception as e:
print(f"Error processing file {file_path}: {e}")
raise
def cleanup_processed_files():
#Clean up processed files after successful upload.
try:
for file_path in PROCESSED_FOLDER.iterdir():
if file_path.is_file():
print(f"Cleaning up processed file: {file_path}")
file_path.unlink()
except Exception as e:
print(f"Error during cleanup of processed files: {e}")
def process_files_in_folder(folder_path):
#Process all files in a folder.
folder_path = Path(folder_path)
try:
with ThreadPoolExecutor() as executor:
for file_path in folder_path.iterdir():
if file_path.is_file():
executor.submit(process_image, str(file_path))
cleanup_input_files()
except Exception as e:
print(f"Error in folder processing: {e}")
def cleanup_input_files():
#Clean up storage files after processing.
try:
for file_path in IMAGE_FOLDER.iterdir():
if file_path.is_file():
print(f"Cleaning up input file: {file_path}")
file_path.unlink()
except Exception as e:
print(f"Error during cleanup of input files: {e}")
if __name__ == "__main__":
try:
process_files_in_folder(IMAGE_FOLDER)
except Exception as e:
print(f"Error in main process: {e}")