Spaces:
Sleeping
Sleeping
Update utils.py
Browse files
utils.py
CHANGED
|
@@ -1,40 +1,40 @@
|
|
| 1 |
-
import
|
| 2 |
-
from PIL import Image
|
| 3 |
import re
|
| 4 |
|
|
|
|
|
|
|
|
|
|
| 5 |
def extract_kyc_fields(file_path):
|
| 6 |
try:
|
| 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 |
-
# Return structured KYC data
|
| 32 |
return {
|
| 33 |
"aadhaar_number": aadhaar_match.group(0) if aadhaar_match else "Not found",
|
| 34 |
"dob": dob_match.group(0) if dob_match else "Not found",
|
| 35 |
-
"name": name
|
| 36 |
}
|
| 37 |
|
| 38 |
except Exception as e:
|
| 39 |
-
|
| 40 |
-
return {"error": f"OCR failed: {str(e)}"}
|
|
|
|
| 1 |
+
from paddleocr import PaddleOCR
|
|
|
|
| 2 |
import re
|
| 3 |
|
| 4 |
+
# Initialize OCR model only once
|
| 5 |
+
ocr = PaddleOCR(use_angle_cls=True, lang='en') # lang='en' for English documents
|
| 6 |
+
|
| 7 |
def extract_kyc_fields(file_path):
|
| 8 |
try:
|
| 9 |
+
# Run OCR
|
| 10 |
+
result = ocr.ocr(file_path, cls=True)
|
| 11 |
+
|
| 12 |
+
all_text = ""
|
| 13 |
+
for line_group in result:
|
| 14 |
+
for line in line_group:
|
| 15 |
+
all_text += line[1][0] + "\n"
|
| 16 |
+
|
| 17 |
+
# Aadhaar number (format with or without space/dash)
|
| 18 |
+
aadhaar_match = re.search(r'\b\d{4}[\s\-]?\d{4}[\s\-]?\d{4}\b', all_text)
|
| 19 |
+
|
| 20 |
+
# DOB (any DD/MM/YYYY or similar)
|
| 21 |
+
dob_match = re.search(r'\b\d{2}[\/\-]\d{2}[\/\-]\d{4}\b', all_text)
|
| 22 |
+
|
| 23 |
+
# Name: try to detect a line with 'Name' or fallback to top line
|
| 24 |
+
name = "Not found"
|
| 25 |
+
for line in all_text.split("\n"):
|
| 26 |
+
if re.search(r'\b(name|naam|namf)\b', line, re.IGNORECASE):
|
| 27 |
+
name = line.split(":")[-1].strip() if ":" in line else line.strip()
|
| 28 |
+
break
|
| 29 |
+
|
| 30 |
+
if name == "Not found":
|
| 31 |
+
name = all_text.split("\n")[0].strip()
|
| 32 |
+
|
|
|
|
| 33 |
return {
|
| 34 |
"aadhaar_number": aadhaar_match.group(0) if aadhaar_match else "Not found",
|
| 35 |
"dob": dob_match.group(0) if dob_match else "Not found",
|
| 36 |
+
"name": name
|
| 37 |
}
|
| 38 |
|
| 39 |
except Exception as e:
|
| 40 |
+
return {"error": f"PaddleOCR failed: {str(e)}"}
|
|
|