-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathold_app.py
150 lines (132 loc) · 5.7 KB
/
old_app.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import cv2
import streamlit as st
from sqlalchemy import text
from preprocess import read_image, extract_id_card, save_image
from ocr_engine import extract_text
from postprocess import extract_information
from face_verification import detect_and_extract_face, face_comparison, get_face_embeddings
from mysqldb_operations import insert_records, fetch_records, check_duplicacy
# {
# "ID": "CCNPA",
# "Name": "BIBEK RAUTH",
# "Father's Name": "AJAY RAUTH",
# "DOB": "14/09/1994",
# "ID Type": "PAN"
# }
# Set wider page layout
def wider_page():
max_width_str = f"max-width: 1200px;"
st.markdown(
f"""
<style>
.reportview-container .main .block-container{{ {max_width_str} }}
</style>
""",
unsafe_allow_html=True,
)
# Customized Streamlit theme
def set_custom_theme():
st.markdown(
"""
<style>
body {
background-color: #f0f2f6; /* Set background color */
color: #333333; /* Set text color */
}
.sidebar .sidebar-content {
background-color: #ffffff; /* Set sidebar background color */
}
</style>
""",
unsafe_allow_html=True,
)
# Sidebar
def sidebar_section():
st.sidebar.title("Select ID Card Type")
option = st.sidebar.selectbox("", ("PAN", " "))
return option
# Header
def header_section(option):
if option == "Aadhar":
st.title("Registration Using Aadhar Card")
elif option == "PAN":
st.title("Registration Using PAN Card")
# Main content
def main_content(image_file, face_image_file, conn):
if image_file is not None:
face_image = read_image(face_image_file, is_uploaded=True)
if face_image is not None:
image = read_image(image_file, is_uploaded=True)
image_roi, _ = extract_id_card(image)
face_image_path2 = detect_and_extract_face(img=image_roi)
face_image_path1 = save_image(face_image, "face_image.jpg", path="data\\02_intermediate_data")
is_face_verified = face_comparison(image1_path=face_image_path1, image2_path=face_image_path2)
if is_face_verified:
extracted_text = extract_text(image_roi)
text_info = extract_information(extracted_text)
records = fetch_records(text_info)
if records.shape[0] > 0:
st.write(records.shape)
st.write(records)
is_duplicate = check_duplicacy(text_info)
if is_duplicate:
st.write(f"User already present with ID {text_info['ID']}")
else:
st.write(text_info)
# Convert the DOB to string in format MySQL expects
text_info['DOB'] = text_info['DOB'].strftime('%Y-%m-%d')
text_info['Embedding'] = get_face_embeddings(face_image_path1)
st.write(text_info)
insert_records(text_info)
# DB Operations
# with conn.session as s:
# s.execute(
# 'INSERT INTO users (id, name, father_name, dob, id_type, embedding) VALUES (:id, :name, :father_name, :dob,:id_type, :embedding );',
# params=dict(id=text_info['ID'], name=text_info['Name'], father_name=text_info["Father's Name"],
# dob=text_info['DOB'], id_type=text_info['ID Type'])
# )
# s.execute(
# text('INSERT INTO users (id, name, father_name, dob, id_type, embedding) VALUES (:id, :name, :father_name, :dob, :id_type, :embedding);'),
# {
# 'id': text_info['ID'],
# 'name': text_info['Name'],
# 'father_name': text_info["Father's Name"],
# 'dob': text_info['DOB'], # Make sure this is formatted as a string 'YYYY-MM-DD'
# 'id_type': text_info['ID Type'],
# 'embedding': str(text_info['Embedding'])
# }
# )
# s.commit()
# col1, col2 = st.columns(2)
# # Display ID card image
# with col1:
# st.header("ID Card Image")
# image_roi = cv2.cvtColor(image_roi, cv2.COLOR_BGR2RGB)
# st.image(image_roi, use_column_width=True, caption="ID card")
# # Display uploaded face image
# with col2:
# st.header("Uploaded Face Image")
# face_image = cv2.cvtColor(face_image, cv2.COLOR_BGR2RGB)
# st.image(face_image, use_column_width=True, caption="Uploaded Face")
# # Display extracted information
# st.header("Extracted Information")
# st.dataframe(text_df)
else:
st.error("Face verification failed. Please try again.")
else:
st.error("Face image not uploaded. Please upload a face image.")
else:
st.warning("Please upload an ID card image.")
def main():
# Initialize connection.
conn = st.connection('mysql', type='sql')
wider_page()
set_custom_theme()
option = sidebar_section()
header_section(option)
image_file = st.file_uploader("Upload ID Card")
if image_file is not None:
face_image_file = st.file_uploader("Upload Face Image")
main_content(image_file, face_image_file, conn)
if __name__ == "__main__":
main()