File size: 1,264 Bytes
47315cd
 
 
 
 
 
 
 
60914bf
47315cd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5dac815
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
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.title('Passport Recognition Demo')

image_path = st.file_uploader("Upload Passport Image", type=["jpg", "jpeg", "png"])

if image_path is not None:
    st.image(image_path, caption="Uploaded Image.", use_column_width=True)
    st.write("")
    st.write("Classifying...")

    with open("temp_image.jpg", "wb") as f:
        f.write(image_path.read())

    passport_info = recognize_passport("temp_image.jpg")

    st.markdown(f'## Passport Recognition Results')
    st.json(json.dumps(passport_info, indent=2))