File size: 2,255 Bytes
5bb140d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import cv2
import pandas as pd
import numpy as np
from tensorflow.keras.models import load_model
from PIL import Image
import datetime

# Load File

model_imp = load_model('./src/model.keras')

def run():
    # Title
    st.title('Safe and Unsafe Working Condition')

    # Sub Header
    st.subheader('Safety Prediction of Working Condition Image ')

    # Image
    image = Image.open('./src/image12.jpg')
    st.image(image)

    # Create form
    with st.form(key='safety-prediction'):

        st.markdown('Data ID')

        date = st.date_input("Select a date", help='Input the time the photo was taken or obtained')
        time = st.time_input("Select a time")
        timestamp = datetime.datetime.combine(date, time)

        name_id = st.text_input('ID', value='---name/id---', help='Input your name or identity')

        uploaded_img = st.file_uploader("Upload an image", type=['jpg', 'jpeg', 'png'])

        submitted = st.form_submit_button('Predict')

    # Data inference
    data_inf_input = {
        'timestamp': timestamp,
        'name_id': name_id,
    }

    # Data frame
    st.markdown('Data Summary:')
    data_inference = pd.DataFrame([data_inf_input])
    st.dataframe(data_inference)

    # Preprocessing

    if uploaded_img is not None:
        img_pil = Image.open(uploaded_img).convert('RGB')
        img_arr = np.array(img_pil)
        # Resize as input model (img_height × img_width)
        img_resized = cv2.resize(img_arr, (200, 200))
        # Scaling
        img_array = np.array(img_resized) / 255.
        # Change dimension
        img_dims = np.expand_dims(img_array, axis=0)
        st.image(uploaded_img)
    else:
        st.warning("Please upload an image to continue.")

    st.markdown('Result:')
    # Prediction
    if submitted:
        prob = model_imp.predict(img_dims)

        # Convert to class (0 = safe, 1 = unsafe)
        pred = 1 if prob >= 0.5 else 0
        class_names = ['SAFE', 'UNSAFE']
        label = class_names[pred]
        percentage = prob[0][0] * 100

        # Result
        st.write(f"#### Prediction: this image consider {label} working condition.")
        st.write(f"##### Probability: {percentage:.2f} % ")

if __name__ == '__main__':
    run()