File size: 1,929 Bytes
47315cd
 
 
 
 
 
 
60914bf
47315cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ef73aca
 
 
 
 
 
47315cd
ef73aca
 
 
 
47315cd
ef73aca
47315cd
ef73aca
 
 
 
 
 
 
 
 
47315cd
ef73aca
 
 
 
47315cd
ef73aca
 
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
import streamlit as st
import json
import os
import requests
import socket

def start_server():   
    os.system("uvicorn server:app --port 8080 --host 0.0.0.0 --workers 2")
    st.session_state['server_started'] = True
    
def is_port_in_use(port):
    import socket
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        return s.connect_ex(('0.0.0.0', port)) == 0

def recognize_passport(image_path):
    files = {'image': open(image_path, 'rb')}
    response = requests.post("http://0.0.0.0:8080/recognize_passport", files=files)
    return response.json()
 
if 'server_started' not in st.session_state:
    st.session_state['server_started'] = False

if not st.session_state['server_started']:
  start_server()              

st.sidebar.title("Server Status")
st.sidebar.write("🟢 Server is running" if st.session_state['server_started'] else "🔴 Server is not running")

st.title('Passport Recognition with passportEYE, paddleocr and a LLM')

upload_option = st.radio("Select Image Source", ("Upload Image", "Load Example Image"), index=0)

if upload_option == "Load Example Image":
    image_path = "sample.jpg"
else:
    image_path = st.file_uploader("Upload Passport Image", type=["jpg", "jpeg", "png"], key="image_uploader")

if image_path is not None and st.button("Read Passport"):

    with st.spinner("Running..."):
        if upload_option == "Upload Image":
            with open("temp_image.jpg", "wb") as f:
                f.write(image_path.read())
        else:
            passport_info = recognize_passport(image_path)
            
    if upload_option == "Upload Image":
        passport_info = recognize_passport("temp_image.jpg")

    col1, col2 = st.columns(2)
    with col1:
        st.markdown(f'## Passport Recognition Results')
        st.json(json.dumps(passport_info, indent=2))

    with col2:
        st.image(image_path, caption="Uploaded Image.", use_column_width=True)