File size: 1,993 Bytes
b035cb5
 
 
 
 
 
 
 
142281a
 
 
b035cb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import sys
import cv2
import numpy as np
import pandas as pd
import streamlit as st
import joblib

SCALER_PATH = "scaler.pkl"
ENCODER_PATH = "le.pkl"
MODEL_PATH = "randomforest.pkl"

scaler = joblib.load(SCALER_PATH)
encoder = joblib.load(ENCODER_PATH)
model = joblib.load(MODEL_PATH)
pred = None
def live_data_pred(cam_input):
    bytes_data = cam_input.getvalue()

    cv2_img = cv2.imdecode(np.frombuffer(bytes_data, np.uint8), 0)
    resize_img = cv2.resize(cv2_img, (256, 256), interpolation=cv2.INTER_CUBIC)
    flattened_img = resize_img.flatten()
    flattened_img = np.reshape(flattened_img, (1, -1))
    
    scaled_input = scaler.transform(flattened_img)
    prediction = model.predict(scaled_input)
    prediction = encoder.inverse_transform(prediction)

    return prediction

def pick_gif(prediction):
    pass




st.set_page_config(page_title="Driver Posture Alert System", layout='wide')
st.markdown("## Driver Posture Detection and Alert System")
st.markdown(">A user friendly platform Indepedent application to detect the real time posture of the driver and provides appropriate actions")

with st.sidebar:
    st.markdown("#### Choose the Input type")
    opt = st.selectbox(
        "Select the type of input",
        ("Live camera input", "Captured input"),
        index = None
    )
    
    if opt == "Live camera input":
        cam_input = st.camera_input("Live input")
        if cam_input is not None:
            pred = live_data_pred(cam_input)
            st.write(f"{pred}")
            #gif_path = pick_gif(pred)

    elif opt == "Captured input":
        cam_input = st.file_uploader(
            label="Upload captured image",
            type= ["png", "jpg", "jpeg"]
        )
        if cam_input is not None:
            pred = live_data_pred(cam_input)
            

if pred is not None:
    st.markdown("### Image Captured - Prediction")
    st.write(f"{pred[0].capitalize()}")
else:
    st.markdown("#### Waiting for Input...")