Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,4 +1,5 @@
|
|
| 1 |
-
from fastapi import FastAPI
|
|
|
|
| 2 |
import easyocr
|
| 3 |
import cv2
|
| 4 |
import numpy as np
|
|
@@ -13,13 +14,23 @@ app = FastAPI()
|
|
| 13 |
|
| 14 |
reader = easyocr.Reader(['en'])
|
| 15 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
# =========================
|
| 17 |
# IMAGE QUALITY CHECKS
|
| 18 |
# =========================
|
| 19 |
|
| 20 |
def is_blurry(image):
|
| 21 |
|
| 22 |
-
gray = cv2.cvtColor(
|
|
|
|
|
|
|
|
|
|
| 23 |
|
| 24 |
variance = cv2.Laplacian(
|
| 25 |
gray,
|
|
@@ -62,7 +73,7 @@ def detect_document(text):
|
|
| 62 |
|
| 63 |
# REMOVE EXTRA SYMBOLS
|
| 64 |
cleaned_text = re.sub(
|
| 65 |
-
r'[^a-zA-Z0-9\
|
| 66 |
' ',
|
| 67 |
text
|
| 68 |
)
|
|
@@ -200,7 +211,7 @@ def detect_document(text):
|
|
| 200 |
}
|
| 201 |
|
| 202 |
# =========================
|
| 203 |
-
# ELECTRICITY
|
| 204 |
# =========================
|
| 205 |
|
| 206 |
electricity_keywords = [
|
|
@@ -235,7 +246,7 @@ def detect_document(text):
|
|
| 235 |
"yedc",
|
| 236 |
"yola electricity",
|
| 237 |
|
| 238 |
-
# Common
|
| 239 |
"prepaid",
|
| 240 |
"postpaid",
|
| 241 |
"disco",
|
|
@@ -421,9 +432,9 @@ def detect_document(text):
|
|
| 421 |
|
| 422 |
plate_patterns = [
|
| 423 |
|
| 424 |
-
r"[A-Z]{3}-?\
|
| 425 |
-
r"[A-Z]{2}\
|
| 426 |
-
r"[A-Z]{3}\
|
| 427 |
]
|
| 428 |
|
| 429 |
detected_plate = None
|
|
@@ -470,6 +481,7 @@ def detect_document(text):
|
|
| 470 |
|
| 471 |
return None
|
| 472 |
|
|
|
|
| 473 |
# =========================
|
| 474 |
# HOME ROUTE
|
| 475 |
# =========================
|
|
@@ -499,19 +511,27 @@ def home():
|
|
| 499 |
|
| 500 |
@app.post("/validate")
|
| 501 |
async def validate_document(
|
| 502 |
-
|
| 503 |
):
|
| 504 |
|
| 505 |
try:
|
| 506 |
|
|
|
|
|
|
|
| 507 |
# =========================
|
| 508 |
-
#
|
| 509 |
# =========================
|
| 510 |
|
| 511 |
-
|
| 512 |
|
| 513 |
-
|
| 514 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 515 |
|
| 516 |
# =========================
|
| 517 |
# READ IMAGE
|
|
@@ -524,8 +544,13 @@ async def validate_document(
|
|
| 524 |
return {
|
| 525 |
"success": False,
|
| 526 |
"message": "Invalid image",
|
| 527 |
-
"reason":
|
| 528 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 529 |
}
|
| 530 |
|
| 531 |
# =========================
|
|
@@ -537,8 +562,11 @@ async def validate_document(
|
|
| 537 |
return {
|
| 538 |
"success": False,
|
| 539 |
"message": "Image rejected",
|
| 540 |
-
"reason": "The
|
| 541 |
-
"suggestion":
|
|
|
|
|
|
|
|
|
|
| 542 |
}
|
| 543 |
|
| 544 |
# =========================
|
|
@@ -550,8 +578,10 @@ async def validate_document(
|
|
| 550 |
return {
|
| 551 |
"success": False,
|
| 552 |
"message": "Image rejected",
|
| 553 |
-
"reason": "The
|
| 554 |
-
"suggestion":
|
|
|
|
|
|
|
| 555 |
}
|
| 556 |
|
| 557 |
# =========================
|
|
@@ -569,8 +599,13 @@ async def validate_document(
|
|
| 569 |
return {
|
| 570 |
"success": False,
|
| 571 |
"message": "Document rejected",
|
| 572 |
-
"reason":
|
| 573 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 574 |
}
|
| 575 |
|
| 576 |
# =========================
|
|
@@ -589,8 +624,8 @@ async def validate_document(
|
|
| 589 |
"success": False,
|
| 590 |
"message": "Document rejected",
|
| 591 |
"reason": (
|
| 592 |
-
"The uploaded image does not
|
| 593 |
-
"supported document type."
|
| 594 |
),
|
| 595 |
"supported_documents": [
|
| 596 |
"National ID (NIN)",
|
|
@@ -621,8 +656,12 @@ async def validate_document(
|
|
| 621 |
return {
|
| 622 |
"success": True,
|
| 623 |
"message": "Document verified successfully",
|
| 624 |
-
"document_type":
|
| 625 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 626 |
"matched_keywords": (
|
| 627 |
document_result["matched_keywords"]
|
| 628 |
),
|
|
@@ -637,11 +676,17 @@ async def validate_document(
|
|
| 637 |
"reason": str(e)
|
| 638 |
}
|
| 639 |
|
| 640 |
-
finally:
|
| 641 |
|
| 642 |
-
|
| 643 |
-
|
| 644 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 645 |
|
| 646 |
-
|
| 647 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from fastapi import FastAPI
|
| 2 |
+
from pydantic import BaseModel
|
| 3 |
import easyocr
|
| 4 |
import cv2
|
| 5 |
import numpy as np
|
|
|
|
| 14 |
|
| 15 |
reader = easyocr.Reader(['en'])
|
| 16 |
|
| 17 |
+
# =========================
|
| 18 |
+
# REQUEST MODEL
|
| 19 |
+
# =========================
|
| 20 |
+
|
| 21 |
+
class ImageRequest(BaseModel):
|
| 22 |
+
image_path: str
|
| 23 |
+
|
| 24 |
# =========================
|
| 25 |
# IMAGE QUALITY CHECKS
|
| 26 |
# =========================
|
| 27 |
|
| 28 |
def is_blurry(image):
|
| 29 |
|
| 30 |
+
gray = cv2.cvtColor(
|
| 31 |
+
image,
|
| 32 |
+
cv2.COLOR_BGR2GRAY
|
| 33 |
+
)
|
| 34 |
|
| 35 |
variance = cv2.Laplacian(
|
| 36 |
gray,
|
|
|
|
| 73 |
|
| 74 |
# REMOVE EXTRA SYMBOLS
|
| 75 |
cleaned_text = re.sub(
|
| 76 |
+
r'[^a-zA-Z0-9\s-]',
|
| 77 |
' ',
|
| 78 |
text
|
| 79 |
)
|
|
|
|
| 211 |
}
|
| 212 |
|
| 213 |
# =========================
|
| 214 |
+
# ELECTRICITY / UTILITY BILL
|
| 215 |
# =========================
|
| 216 |
|
| 217 |
electricity_keywords = [
|
|
|
|
| 246 |
"yedc",
|
| 247 |
"yola electricity",
|
| 248 |
|
| 249 |
+
# Common terms
|
| 250 |
"prepaid",
|
| 251 |
"postpaid",
|
| 252 |
"disco",
|
|
|
|
| 432 |
|
| 433 |
plate_patterns = [
|
| 434 |
|
| 435 |
+
r"[A-Z]{3}-?\d{3}[A-Z]{2}",
|
| 436 |
+
r"[A-Z]{2}\d{3}[A-Z]{3}",
|
| 437 |
+
r"[A-Z]{3}\s\d{3}\s[A-Z]{2}"
|
| 438 |
]
|
| 439 |
|
| 440 |
detected_plate = None
|
|
|
|
| 481 |
|
| 482 |
return None
|
| 483 |
|
| 484 |
+
|
| 485 |
# =========================
|
| 486 |
# HOME ROUTE
|
| 487 |
# =========================
|
|
|
|
| 511 |
|
| 512 |
@app.post("/validate")
|
| 513 |
async def validate_document(
|
| 514 |
+
request: ImageRequest
|
| 515 |
):
|
| 516 |
|
| 517 |
try:
|
| 518 |
|
| 519 |
+
image_path = request.image_path
|
| 520 |
+
|
| 521 |
# =========================
|
| 522 |
+
# CHECK FILE EXISTS
|
| 523 |
# =========================
|
| 524 |
|
| 525 |
+
if not os.path.exists(image_path):
|
| 526 |
|
| 527 |
+
return {
|
| 528 |
+
"success": False,
|
| 529 |
+
"message": "Image not found",
|
| 530 |
+
"reason": (
|
| 531 |
+
"The provided image path "
|
| 532 |
+
"does not exist."
|
| 533 |
+
)
|
| 534 |
+
}
|
| 535 |
|
| 536 |
# =========================
|
| 537 |
# READ IMAGE
|
|
|
|
| 544 |
return {
|
| 545 |
"success": False,
|
| 546 |
"message": "Invalid image",
|
| 547 |
+
"reason": (
|
| 548 |
+
"The file could not be "
|
| 549 |
+
"read as an image."
|
| 550 |
+
),
|
| 551 |
+
"suggestion": (
|
| 552 |
+
"Provide a valid JPG or PNG image."
|
| 553 |
+
)
|
| 554 |
}
|
| 555 |
|
| 556 |
# =========================
|
|
|
|
| 562 |
return {
|
| 563 |
"success": False,
|
| 564 |
"message": "Image rejected",
|
| 565 |
+
"reason": "The image is blurry.",
|
| 566 |
+
"suggestion": (
|
| 567 |
+
"Retake the photo with "
|
| 568 |
+
"better focus."
|
| 569 |
+
)
|
| 570 |
}
|
| 571 |
|
| 572 |
# =========================
|
|
|
|
| 578 |
return {
|
| 579 |
"success": False,
|
| 580 |
"message": "Image rejected",
|
| 581 |
+
"reason": "The image is too dark.",
|
| 582 |
+
"suggestion": (
|
| 583 |
+
"Use better lighting."
|
| 584 |
+
)
|
| 585 |
}
|
| 586 |
|
| 587 |
# =========================
|
|
|
|
| 599 |
return {
|
| 600 |
"success": False,
|
| 601 |
"message": "Document rejected",
|
| 602 |
+
"reason": (
|
| 603 |
+
"No readable text was detected."
|
| 604 |
+
),
|
| 605 |
+
"suggestion": (
|
| 606 |
+
"Ensure the document is "
|
| 607 |
+
"clear and fully visible."
|
| 608 |
+
)
|
| 609 |
}
|
| 610 |
|
| 611 |
# =========================
|
|
|
|
| 624 |
"success": False,
|
| 625 |
"message": "Document rejected",
|
| 626 |
"reason": (
|
| 627 |
+
"The uploaded image does not "
|
| 628 |
+
"match any supported document type."
|
| 629 |
),
|
| 630 |
"supported_documents": [
|
| 631 |
"National ID (NIN)",
|
|
|
|
| 656 |
return {
|
| 657 |
"success": True,
|
| 658 |
"message": "Document verified successfully",
|
| 659 |
+
"document_type": (
|
| 660 |
+
document_result["document_type"]
|
| 661 |
+
),
|
| 662 |
+
"confidence": (
|
| 663 |
+
document_result["confidence"]
|
| 664 |
+
),
|
| 665 |
"matched_keywords": (
|
| 666 |
document_result["matched_keywords"]
|
| 667 |
),
|
|
|
|
| 676 |
"reason": str(e)
|
| 677 |
}
|
| 678 |
|
|
|
|
| 679 |
|
| 680 |
+
# =========================
|
| 681 |
+
# RUN SERVER
|
| 682 |
+
# =========================
|
| 683 |
+
|
| 684 |
+
if __name__ == "__main__":
|
| 685 |
+
|
| 686 |
+
import uvicorn
|
| 687 |
|
| 688 |
+
uvicorn.run(
|
| 689 |
+
app,
|
| 690 |
+
host="0.0.0.0",
|
| 691 |
+
port=7860
|
| 692 |
+
)
|