File size: 8,285 Bytes
1f72938
 
 
 
 
 
 
 
 
 
 
 
 
9312707
 
 
1f72938
 
 
9312707
1f72938
 
 
 
 
 
 
 
 
 
 
 
 
9312707
 
 
 
 
 
1f72938
9312707
1f72938
 
 
 
9312707
 
 
 
 
 
 
1f72938
 
 
 
 
9312707
 
 
1f72938
 
9312707
 
1f72938
 
 
 
 
 
 
 
 
 
 
 
 
 
9312707
 
 
 
1f72938
9312707
1f72938
9312707
 
1f72938
9312707
 
 
 
1f72938
 
 
 
 
 
 
9312707
 
1f72938
 
 
 
 
 
 
9312707
1f72938
 
9312707
 
 
1f72938
 
 
9312707
 
 
 
1f72938
9312707
1f72938
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9312707
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1f72938
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9312707
1f72938
 
 
 
 
 
 
 
 
 
9312707
1f72938
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
import streamlit as st
import similarity_check as sc
import cv2
from PIL import Image
import numpy as np
import tempfile
from streamlit_webrtc import VideoTransformerBase, webrtc_streamer
import demo
import time
import streamlit as st
import requests
import json
import request_json.sbt_request_generator as sbt
import pathlib
import os
import check_hkid_validity as chv


def main():

    # st.title("SBT Web Application")
    # today's date = get_today_date

    # global data
    html_temp = """
        <body style="background-color:red;">
        <div style="background-color:teal ;padding:10px">
        <h2 style="color:white;text-align:center;">SBT Web Application</h2>
        </div>
        </body>
        """
    st.markdown(html_temp, unsafe_allow_html=True)
    
    if 'hkid_image_validity' not in st.session_state:
        st.session_state.hkid_image_validity = False

    if 'data' not in st.session_state:
        st.session_state['data'] = {}

    st.header("I. Similarity Check")
    image_file = st.file_uploader("Upload Image", type=['jpg', 'png', 'jpeg', 'pdf'], accept_multiple_files=True)
    if len(image_file) == 1:
        image1 = Image.open(image_file[0])
        st.text("HKID card")
        st.image(image1)
        image1.save('image/hkid.jpg', 'JPEG')
        if chv.check_hkid('image/hkid.jpg'):
            st.text("Valid HKID card.")
            st.session_state.hkid_image_validity = True
        else:
            st.text("Invalid HKID card. Please upload again!")
            st.session_state.hkid_image_validity = False
    elif len(image_file) == 2:
        image1 = Image.open(image_file[0])
        st.text("HKID card")
        st.image(image1)
        image2 = Image.open(image_file[1])
        # image2 = image_file[1]
        # image2.save('image/hkid.jpg', 'JPEG')
        # file_name = image_file[1].name
        st.text("Bank statement")
        st.image(image2)

    print(f"the id is: {st.session_state.hkid_image_validity}")
    # if image_file2 is not None:
    #     image2 = Image.open(image_file)
    #     st.text("Bank statement")
    #     st.image(image2)

    # path1 = 'IMG_4495.jpg'
    # path2 = 'hangseng_page-0001.jpg'    
    # image1 = save_image(image1)
    # image2 = save_image(image2)

    data = {}
    if st.button("Recognise"):
        with st.spinner('Wait for it...'):
            # global data
            data = sc.get_data(image1, image2)
            # data = ed.get_info_from_bank('hsbc_one_account.pdf')
            # with open('data1.txt', 'w') as f:
            #     f.write(json.dumps(data))
            # data.update(sc.get_data(image1, image2, file_name))
            # print(f'data inside {data}')
            # sbt.split_data(data)
        if 'data' in st.session_state:
            st.session_state['data'] = data
        st.success('Done!')
        # if "similarity_score" not in data.keys():
        #     data["similarity_score"] = "0"
        score = int(st.session_state['data']['similarity_score'])
        # score = int(data["similarity_score"])
        #print(score)
        st.text(f'score: {score}')
        if (score>85):
            st.text(f'matched')
        else:
            st.text(f'unmatched')
        

        data = st.session_state['data']
        st.header("IIa. HKID Data Extraction")
        st.text(f'Name: {data["name_on_id"]}') # name is without space
        st.text(f'HKID: {data["hkid"]} and validity: {data["validity"]}')
        st.text(f'Date of issue: {data["issue_date"]}')
    
        st.header("IIb. Bank Statement Data Extraction")
        # st.write('------------From bank statement------------')
        st.text(f'Name: {data["nameStatement"]}')
        st.text(f'Address: {data["address"]}')
        st.text(f'Bank: {data["bank"]}')
        st.text(f'Date: {data["statementDate"]}')
        st.text(f'Asset: {data["totalAsset"]} hkd')
        st.text(f'Liabilities: {data["totalLiability"]} hkd')
        # result_img= detect_faces(our_image)
        # st.image(result_img)
    # print(f'data outside 1 {data}')

    if 'data' in st.session_state:
        tempout = st.session_state['data']
        print(f'hello: {tempout}')
    

    st.header("II. Facial Recognition")
    run = st.checkbox('Run')

    # webrtc_streamer(key="example")
    # 1. Web Rtc
    # webrtc_streamer(key="jhv", video_frame_callback=video_frame_callback)


    # # init the camera
    face_locations = []
    # face_encodings = []
    face_names = []
    process_this_frame = True

    score = []

    faces = 0

    FRAME_WINDOW = st.image([])
    camera = cv2.VideoCapture(0)

    while run:

        # Capture frame-by-frame
        # Grab a single frame of video
        ret, frame = camera.read()
        
        result, process_this_frame, face_locations, faces, face_names, score = demo.process_frame(frame, process_this_frame, face_locations, faces, face_names, score)
        # Display the resulting image
        FRAME_WINDOW.image(result)

        print(score)
        if len(score) > 20:
            avg_score =  sum(score) / len(score)
            st.write(avg_score)
            # st.write(f'{demo.convert_distance_to_percentage(avg_score, 0.45)}')
            camera.release()
            run = not run
            st.session_state['data']['avg_score'] = str(avg_score)
            # with open('data1.txt', 'r') as f:
            #     if f is not None:
            #         data_raw = f.read()
            #         data = json.loads(data_raw)
            #         data['avg_score'] = str(avg_score)
            #     else:
            #         data = {}

            
            # with open('data1.txt', 'w') as f:
            #         f.write(json.dumps(data))
                

        # update_text(f'{demo.convert_distance_to_percentage(score, 0.45)}')
    else:
        st.write('Stopped')
    
    # print(f'the data is {data}')

    # st.header("IIIa. HKID Data Extraction")
    # st.text(f'Name: {data["name_on_id"]}') # name is without space
    # st.text(f'HKID: {data["hkid"]} and validity: {data["validity"]}')
    # st.text(f'Date of issue: {data["issue_date"]}')

    # st.header("IIIb. Bank Statement Data Extraction")
    # # st.write('------------From bank statement------------')
    # st.text(f'Name: {data["nameStatement"]}')
    # st.text(f'Address: {data["address"]}')
    # st.text(f'Bank: {data["bank"]}')
    # st.text(f'Date: {data["date"]}')
    # st.text(f'Asset: {data["asset"]} hkd')
    # st.text(f'Liabilities: {data["liabilities"]} hkd')
    
    # print(f'data outside 2 {data}')
    if st.button("Confirm"):
        # print(f'data outside 3 {data}')
        with st.spinner('Sending data...'):
            sbt.split_data(st.session_state['data'])
        st.success('Done!')

if __name__ == '__main__':
    main()



    # def save_image(image):
#     try:
#         temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='.jpg')
#         Image.save(temp_file.name)
#         return temp_file.name
#     except IOError:
#         print("Unable to save image to temporary file")
#         return None

    # json_file = 'request json\request_legalDocument.json'
    # file = open(json_file, 'r')
    # data = json.load(file)
    # file.close()
    # # Update data
    # data.update(new_data)
    # file = open(json_file, 'w')
    # for item in data['request']['body']['formdata']:
    #     if item["key"] == "requestId":
    #         item["value"] = ""
    #     elif item["key"] == "userId":
    #         item["value"] = generate_token_id(2048)
    #     elif item["key"] == "endpoint":
    #         item["value"] = ""
    #     elif item["key"] == "apiType":
    #         item["value"] = ""
    #     elif item["key"] == "docType":
    #         item["value"] = "HKID"
    #     elif item["key"] == "nameDoc":
    #         item["value"] = new_data["name_on_id"]
    #     elif item["key"] == "docID":
    #         item["value"] = new_data["name_on_id"]
    #     elif item["key"] == "docValidity":
    #         item["value"] = new_data["validity"]
    #     elif item["key"] == "dateOfIssue":
    #         item["value"] = new_data["date_issue"]
    #     elif item["key"] == "matchingScore":
    #         item["value"] = new_data["similarity_score"]
    # json.dump(data, file)
    # file.close()