usmanabbasi commited on
Commit
6233336
1 Parent(s): 565aa7d

Uploaded Projec Files

Browse files
Files changed (6) hide show
  1. DataExtraction.py +75 -0
  2. Home.py +9 -0
  3. app.py +16 -0
  4. compare_faces.py +5 -0
  5. face_comparison.py +22 -0
  6. requirements.txt +7 -0
DataExtraction.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ import numpy as np
4
+ import easyocr
5
+ import pandas as pd
6
+ import base64
7
+ import re
8
+ from datetime import datetime, timedelta
9
+
10
+ def process_image(image):
11
+ reader = easyocr.Reader(['en'], gpu=False)
12
+ img_np = np.array(image)
13
+ result = reader.readtext(img_np)
14
+
15
+ extracted_data = {
16
+ "Name": None,
17
+ "Father Name": None,
18
+ "Gender": None,
19
+ "Country of Stay": "Pakistan",
20
+ "Identity Number": None,
21
+ "Date of Birth": None,
22
+ "Date of Issue": None,
23
+ "Date of Expiry": None
24
+ }
25
+
26
+ for i, detection in enumerate(result):
27
+ text = detection[1].strip()
28
+ if "name" in text.lower() and not "father" in text.lower():
29
+ extracted_data["Name"] = result[i+1][1].strip() if i+1 < len(result) else None
30
+ elif "father" in text.lower():
31
+ extracted_data["Father Name"] = result[i+1][1].strip() if i+1 < len(result) else None
32
+ elif text.lower() in ["m", "f"]:
33
+ extracted_data["Gender"] = text.upper()
34
+ elif re.match(r'\d{5}-\d{7}-\d', text):
35
+ extracted_data["Identity Number"] = text
36
+ elif re.match(r'\d{2}\.\d{2}\.\d{4}', text):
37
+ if extracted_data["Date of Birth"] is None:
38
+ extracted_data["Date of Birth"] = text
39
+ elif extracted_data["Date of Issue"] is None:
40
+ extracted_data["Date of Issue"] = text
41
+
42
+ if extracted_data["Date of Issue"] and not extracted_data["Date of Expiry"]:
43
+ try:
44
+ date_of_issue = datetime.strptime(extracted_data["Date of Issue"], "%d.%m.%Y")
45
+ date_of_expiry = date_of_issue.replace(year=date_of_issue.year + 10)
46
+ extracted_data["Date of Expiry"] = date_of_expiry.strftime("%d.%m.%Y")
47
+ except ValueError:
48
+ pass
49
+
50
+ return extracted_data
51
+
52
+ def display_table(extracted_data):
53
+ fields = ["Name", "Father Name", "Gender", "Country of Stay", "Identity Number", "Date of Birth", "Date of Issue", "Date of Expiry"]
54
+ values = [extracted_data[field] if extracted_data[field] else "" for field in fields]
55
+ df = pd.DataFrame(list(zip(fields, values)), columns=['Field', 'Value'])
56
+ st.dataframe(df)
57
+
58
+ def get_csv_download_link(df):
59
+ csv = df.to_csv(index=False)
60
+ b64 = base64.b64encode(csv.encode()).decode()
61
+ href = f'<a href="data:file/csv;base64,{b64}" download="extracted_data.csv">Download CSV File</a>'
62
+ return href
63
+
64
+ def data_extraction_page():
65
+ st.title('ID Card Text Extraction')
66
+
67
+ uploaded_file = st.file_uploader("Upload an image of your ID card", type=["jpg", "jpeg", "png"])
68
+
69
+ if uploaded_file is not None:
70
+ image = Image.open(uploaded_file)
71
+ st.image(image, caption='Uploaded Image', use_column_width=True)
72
+ extracted_data = process_image(image)
73
+ display_table(extracted_data)
74
+
75
+ st.markdown(get_csv_download_link(pd.DataFrame(list(extracted_data.items()), columns=['Field', 'Value'])), unsafe_allow_html=True)
Home.py ADDED
@@ -0,0 +1,9 @@
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+
3
+ def home_page():
4
+ st.title("eKYC using Computer Vision")
5
+ st.write("""
6
+ Welcome to the eKYC application! This project consists of two main modules:
7
+ 1. **Data Extraction**: Upload an image of a CNIC card to extract data.
8
+ 2. **Face Comparison**: Upload an image of a CNIC card and another image of the person to verify if they match.
9
+ """)
app.py ADDED
@@ -0,0 +1,16 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from Home import home_page
3
+ from DataExtraction import data_extraction_page
4
+ from FaceComparison import face_comparison_page
5
+
6
+ PAGES = {
7
+ "Home": home_page,
8
+ "Data Extraction": data_extraction_page,
9
+ "Face Comparison": face_comparison_page
10
+ }
11
+
12
+ st.sidebar.title('Navigation')
13
+ selection = st.sidebar.radio("Go to", list(PAGES.keys()))
14
+
15
+ page = PAGES[selection]
16
+ page()
compare_faces.py ADDED
@@ -0,0 +1,5 @@
 
 
 
 
 
 
1
+ from deepface import DeepFace
2
+
3
+ def compare_faces(id_card_image_path, face_image_path):
4
+ result = DeepFace.verify(id_card_image_path, face_image_path)
5
+ return "Match" if result["verified"] else "No Match"
face_comparison.py ADDED
@@ -0,0 +1,22 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ from PIL import Image
3
+ from compare_faces import compare_faces
4
+ import tempfile
5
+
6
+ def face_comparison_page():
7
+ st.title("ID Card and Face Verification")
8
+
9
+ id_card_image = st.file_uploader("Upload your ID card image", type=["jpg", "jpeg", "png"])
10
+ face_image = st.file_uploader("Upload your face image", type=["jpg", "jpeg", "png"])
11
+
12
+ if id_card_image and face_image:
13
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as id_temp:
14
+ id_temp.write(id_card_image.read())
15
+ id_temp_path = id_temp.name
16
+
17
+ with tempfile.NamedTemporaryFile(delete=False, suffix=".jpg") as face_temp:
18
+ face_temp.write(face_image.read())
19
+ face_temp_path = face_temp.name
20
+
21
+ result = compare_faces(id_temp_path, face_temp_path)
22
+ st.write(result)
requirements.txt ADDED
@@ -0,0 +1,7 @@
 
 
 
 
 
 
 
 
1
+ streamlit
2
+ deepface
3
+ easyocr
4
+ pillow
5
+ numpy
6
+ pandas
7
+ mysql-connector-python