File size: 2,113 Bytes
ff4541c
e4eed50
 
 
 
 
 
 
55b0336
 
 
3e05f3a
e4eed50
 
55b0336
 
cb54161
55b0336
 
 
 
 
 
600beba
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()