Update app.py
Browse files
app.py
CHANGED
|
@@ -29,6 +29,81 @@ from dotenv import load_dotenv
|
|
| 29 |
|
| 30 |
load_dotenv()
|
| 31 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 32 |
# Initializing Firebase
|
| 33 |
try:
|
| 34 |
# Check for the Hugging Face secret first
|
|
@@ -55,6 +130,7 @@ except Exception as e:
|
|
| 55 |
db = None
|
| 56 |
|
| 57 |
|
|
|
|
| 58 |
# Initializing tesseract
|
| 59 |
tesseract_path = os.getenv("TESSERACT_PATH", r"C:\Program Files\Tesseract-OCR\tesseract.exe")
|
| 60 |
pytesseract.pytesseract.tesseract_cmd = tesseract_path
|
|
@@ -148,20 +224,7 @@ def get_exif_data(image_path):
|
|
| 148 |
except Exception as e:
|
| 149 |
return {"error": str(e)}
|
| 150 |
|
| 151 |
-
|
| 152 |
-
# Loading general object detection model (YOLO v8)
|
| 153 |
-
try:
|
| 154 |
-
general_model = YOLO("yolov8n.pt")
|
| 155 |
-
except:
|
| 156 |
-
general_model = None
|
| 157 |
-
print("Warning: General YOLO model not loaded. Install ultralytics and download yolov8n.pt")
|
| 158 |
|
| 159 |
-
# Loading the pre-trained Aadhaar-specific YOLO model
|
| 160 |
-
repo_config = dict(
|
| 161 |
-
repo_id="arnabdhar/YOLOv8-nano-aadhar-card",
|
| 162 |
-
filename="model.pt",
|
| 163 |
-
local_dir="./models"
|
| 164 |
-
)
|
| 165 |
|
| 166 |
def detect_objects_yolo(image_path):
|
| 167 |
#Detecting objects in image using YOLO
|
|
@@ -182,12 +245,7 @@ def detect_objects_yolo(image_path):
|
|
| 182 |
except Exception as e:
|
| 183 |
return {"error": str(e)}
|
| 184 |
|
| 185 |
-
|
| 186 |
-
# Loading the pre-trained YOLO Aadhar model
|
| 187 |
-
aadhaar_model = YOLO(hf_hub_download(**repo_config))
|
| 188 |
-
id2label = aadhaar_model.names
|
| 189 |
-
print(id2label)
|
| 190 |
-
|
| 191 |
|
| 192 |
# Verifying if the image is of frudulent Aadhar card or not using object detection
|
| 193 |
def run_object_verification(image_path, object_model_raw_results):
|
|
@@ -349,7 +407,10 @@ def create_annotated_image(image_path, text_model_results, object_model_results)
|
|
| 349 |
print(f"Error creating annotated image: {e}")
|
| 350 |
return None
|
| 351 |
|
| 352 |
-
|
|
|
|
|
|
|
|
|
|
| 353 |
def analyze_aadhar_pair(front_path, back_path):
|
| 354 |
# running the text extaction model on both front and back images
|
| 355 |
text_model_raw_results_front = aadhaar_model.predict(front_path, verbose=False)[0]
|
|
@@ -518,7 +579,7 @@ def analyze_aadhar_pair(front_path, back_path):
|
|
| 518 |
|
| 519 |
return results
|
| 520 |
|
| 521 |
-
|
| 522 |
def transform_results_for_template(results):
|
| 523 |
# --- Overall Assessment ---
|
| 524 |
risk_level = results.get('assessment', 'UNKNOWN').replace(" FRAUD RISK", "")
|
|
@@ -593,7 +654,7 @@ def transform_results_for_template(results):
|
|
| 593 |
}
|
| 594 |
return transformed_data
|
| 595 |
|
| 596 |
-
|
| 597 |
@app.route('/')
|
| 598 |
def home():
|
| 599 |
return render_template('upload.html')
|
|
|
|
| 29 |
|
| 30 |
load_dotenv()
|
| 31 |
|
| 32 |
+
|
| 33 |
+
#===========================================================================
|
| 34 |
+
# This block clears caches on startup
|
| 35 |
+
print("Attempting to clear cache directories...")
|
| 36 |
+
# Define the standard cache paths
|
| 37 |
+
hf_cache_path = Path.home() / ".cache/huggingface"
|
| 38 |
+
ul_cache_path = Path.home() / ".config/Ultralytics"
|
| 39 |
+
|
| 40 |
+
# Safely delete the directories if they exist
|
| 41 |
+
if hf_cache_path.exists() and hf_cache_path.is_dir():
|
| 42 |
+
try:
|
| 43 |
+
shutil.rmtree(hf_cache_path)
|
| 44 |
+
print(f"Successfully cleared Hugging Face cache at: {hf_cache_path}")
|
| 45 |
+
except OSError as e:
|
| 46 |
+
print(f"Error clearing Hugging Face cache: {e}")
|
| 47 |
+
|
| 48 |
+
if ul_cache_path.exists() and ul_cache_path.is_dir():
|
| 49 |
+
try:
|
| 50 |
+
shutil.rmtree(ul_cache_path)
|
| 51 |
+
print(f"Successfully cleared Ultralytics cache at: {ul_cache_path}")
|
| 52 |
+
except OSError as e:
|
| 53 |
+
print(f"Error clearing Ultralytics cache: {e}")
|
| 54 |
+
#===============================================================================
|
| 55 |
+
|
| 56 |
+
#===============================================================================
|
| 57 |
+
|
| 58 |
+
# Loading general object detection model (YOLO v8)
|
| 59 |
+
try:
|
| 60 |
+
general_model = YOLO("yolov8n.pt")
|
| 61 |
+
except:
|
| 62 |
+
general_model = None
|
| 63 |
+
print("Warning: General YOLO model not loaded. Install ultralytics and download yolov8n.pt")
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
# Loading the custom YOLO Object Detection model from the Hub
|
| 67 |
+
try:
|
| 68 |
+
# Define where to find your model file on the Hub
|
| 69 |
+
REPO_ID = "ConiferousYogi/Weights_for_Aadhar_Card_Fraud_Detection" # <-- Replace with your repo ID
|
| 70 |
+
FILENAME = "models/best.pt"
|
| 71 |
+
COMMIT_SHA = "8e491271abe6e223322b307f6a5f33892a0914b6"
|
| 72 |
+
|
| 73 |
+
print("Downloading custom object detection model from the Hub...")
|
| 74 |
+
local_model_path = hf_hub_download(
|
| 75 |
+
repo_id=REPO_ID,
|
| 76 |
+
filename=FILENAME,
|
| 77 |
+
revision=COMMIT_SHA
|
| 78 |
+
)
|
| 79 |
+
|
| 80 |
+
object_detection_model = YOLO(local_model_path)
|
| 81 |
+
print("Custom object detection model loaded successfully.")
|
| 82 |
+
print(f"Object Detection Model classes: {object_detection_model.names}")
|
| 83 |
+
|
| 84 |
+
except Exception as e:
|
| 85 |
+
object_detection_model = None
|
| 86 |
+
print(f"Warning: Custom Object Detection model could not be loaded. Error: {e}")
|
| 87 |
+
|
| 88 |
+
|
| 89 |
+
#==============================================================================================
|
| 90 |
+
# Loading the pre-trained Aadhaar-specific YOLO model
|
| 91 |
+
try:
|
| 92 |
+
repo_config = dict(
|
| 93 |
+
repo_id="arnabdhar/YOLOv8-nano-aadhar-card",
|
| 94 |
+
filename="model.pt"
|
| 95 |
+
)
|
| 96 |
+
# Loading the pre-trained YOLO Aadhar model
|
| 97 |
+
aadhaar_model = YOLO(hf_hub_download(**repo_config))
|
| 98 |
+
id2label = aadhaar_model.names
|
| 99 |
+
print(f"Text extraction model loaded successfully from Hugging Face.")
|
| 100 |
+
print(f"Text model classes: {id2label}")
|
| 101 |
+
except Exception as e:
|
| 102 |
+
aadhaar_model=None
|
| 103 |
+
print(f"Warning: Text extraction model could not be loaded. Error: {e}")
|
| 104 |
+
|
| 105 |
+
#=====================================================================================
|
| 106 |
+
|
| 107 |
# Initializing Firebase
|
| 108 |
try:
|
| 109 |
# Check for the Hugging Face secret first
|
|
|
|
| 130 |
db = None
|
| 131 |
|
| 132 |
|
| 133 |
+
#==================================================================================================
|
| 134 |
# Initializing tesseract
|
| 135 |
tesseract_path = os.getenv("TESSERACT_PATH", r"C:\Program Files\Tesseract-OCR\tesseract.exe")
|
| 136 |
pytesseract.pytesseract.tesseract_cmd = tesseract_path
|
|
|
|
| 224 |
except Exception as e:
|
| 225 |
return {"error": str(e)}
|
| 226 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 227 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 228 |
|
| 229 |
def detect_objects_yolo(image_path):
|
| 230 |
#Detecting objects in image using YOLO
|
|
|
|
| 245 |
except Exception as e:
|
| 246 |
return {"error": str(e)}
|
| 247 |
|
| 248 |
+
#=========================================================================================================
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 249 |
|
| 250 |
# Verifying if the image is of frudulent Aadhar card or not using object detection
|
| 251 |
def run_object_verification(image_path, object_model_raw_results):
|
|
|
|
| 407 |
print(f"Error creating annotated image: {e}")
|
| 408 |
return None
|
| 409 |
|
| 410 |
+
|
| 411 |
+
|
| 412 |
+
|
| 413 |
+
#================================================================================================================
|
| 414 |
def analyze_aadhar_pair(front_path, back_path):
|
| 415 |
# running the text extaction model on both front and back images
|
| 416 |
text_model_raw_results_front = aadhaar_model.predict(front_path, verbose=False)[0]
|
|
|
|
| 579 |
|
| 580 |
return results
|
| 581 |
|
| 582 |
+
#=======================================================================================================
|
| 583 |
def transform_results_for_template(results):
|
| 584 |
# --- Overall Assessment ---
|
| 585 |
risk_level = results.get('assessment', 'UNKNOWN').replace(" FRAUD RISK", "")
|
|
|
|
| 654 |
}
|
| 655 |
return transformed_data
|
| 656 |
|
| 657 |
+
#=====================================================================================================================================
|
| 658 |
@app.route('/')
|
| 659 |
def home():
|
| 660 |
return render_template('upload.html')
|