Spaces:
Build error
Build error
| 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...") | |