Spaces:
Running
Running
import gradio as gr | |
import pytesseract | |
import cv2 | |
import numpy as np | |
import datetime | |
import uuid | |
import re | |
# Ensure Tesseract is properly configured | |
TESSERACT_PATH = '/usr/bin/tesseract' # Path to Tesseract binary (for Linux) | |
pytesseract.pytesseract.tesseract_cmd = TESSERACT_PATH | |
# Function to process the ID image | |
def process_id(image): | |
if image is None: | |
return "No image provided. Please upload a valid image.", None, None | |
# Extract text using OCR | |
extracted_text = pytesseract.image_to_string(image) | |
# Display all found text | |
all_text_output = f"Extracted Text:\n{extracted_text}" | |
# Locate and extract the date of birth in "D.O.B MM-DD-YYYY" format | |
dob = None | |
dob_pattern = r"D\.O\.B\s(\d{2}-\d{2}-\d{4})" | |
match = re.search(dob_pattern, extracted_text) | |
if match: | |
dob = match.group(1) | |
if not dob: | |
return "Date of Birth not found. Please upload a clearer image.", None, all_text_output | |
# Convert to datetime and calculate age | |
try: | |
birth_date = datetime.datetime.strptime(dob, "%m-%d-%Y") | |
age = (datetime.datetime.now() - birth_date).days // 365 | |
except ValueError: | |
return "Could not determine age. Ensure the ID format is correct.", None, all_text_output | |
# Generate token if age is valid | |
if age >= 18: | |
token = str(uuid.uuid4()) # Generate a unique token | |
return f"Age Verified: {age} years\nAccess Granted! Token: {token}", token, all_text_output | |
else: | |
return f"Age Verified: {age} years\nAccess Denied. Must be 18 or older.", None, all_text_output | |
# Gradio Interface | |
iface = gr.Interface( | |
fn=process_id, | |
inputs=gr.Image(type="numpy"), | |
outputs=[ | |
gr.Textbox(label="Result"), | |
gr.Textbox(label="Token", type="text"), | |
gr.Textbox(label="Found Text", lines=10) | |
], | |
title="Driver's License Age Verification", | |
description="Upload an image of your driver's license. The system will verify if the holder is 18 or older, display all found text, and provide a token if eligible." | |
) | |
if __name__ == "__main__": | |
iface.launch() |