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|
| 3 |
+
import joblib
|
| 4 |
+
import pandas as pd
|
| 5 |
+
import numpy as np
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
import warnings
|
| 8 |
+
import hopsworks
|
| 9 |
+
import streamlit as st
|
| 10 |
+
import json
|
| 11 |
+
import os
|
| 12 |
+
import seaborn as sns
|
| 13 |
+
|
| 14 |
+
# Configuring the web page and setting the page title and icon
|
| 15 |
+
st.set_page_config(
|
| 16 |
+
page_title='Parking Occupacy Detection',
|
| 17 |
+
page_icon='🅿️',
|
| 18 |
+
initial_sidebar_state='expanded')
|
| 19 |
+
|
| 20 |
+
# Ignoring filtering warnings
|
| 21 |
+
warnings.filterwarnings("ignore")
|
| 22 |
+
|
| 23 |
+
def color_prediction(val):
|
| 24 |
+
color = 'red' if val=='detection' else 'green' if val=='no_detection' else 'yellow'
|
| 25 |
+
return f'background-color: {color}'
|
| 26 |
+
|
| 27 |
+
# Setting the title and adding text
|
| 28 |
+
st.title('Parking Occupancy Detection')
|
| 29 |
+
|
| 30 |
+
# Creating tabs for the different features of the application
|
| 31 |
+
tab1,tab2,tab3,tab4 = st.tabs(['Parking lot status', 'About', 'Dataset and visualisations', 'Model performance'])
|
| 32 |
+
|
| 33 |
+
with tab1:
|
| 34 |
+
# st.image('parking_lot.jpg', width = 460)
|
| 35 |
+
|
| 36 |
+
# Logging in to Hopsworks and loading the feature store
|
| 37 |
+
project = hopsworks.login(project = "miknie20", api_key_value=os.environ['HOPSWORKS_API_KEY'])
|
| 38 |
+
fs = project.get_feature_store()
|
| 39 |
+
|
| 40 |
+
# Function to load the bikelane model
|
| 41 |
+
@st.cache_data()
|
| 42 |
+
def get_model(project=project):
|
| 43 |
+
mr = project.get_model_registry()
|
| 44 |
+
model = mr.get_model("bikelane_hist_model", version = 1)
|
| 45 |
+
model_dir = model.download()
|
| 46 |
+
return joblib.load(model_dir + "/bikelane_hist_model.pkl")
|
| 47 |
+
|
| 48 |
+
# Retrieving model
|
| 49 |
+
bikelane_hist_model = get_model()
|
| 50 |
+
|
| 51 |
+
# Function to load the building model
|
| 52 |
+
@st.cache_data()
|
| 53 |
+
def get_model(project=project):
|
| 54 |
+
mr = project.get_model_registry()
|
| 55 |
+
model = mr.get_model("building_hist_model", version = 2)
|
| 56 |
+
model_dir = model.download()
|
| 57 |
+
return joblib.load(model_dir + "/building_hist_model.pkl")
|
| 58 |
+
|
| 59 |
+
# Retrieving model
|
| 60 |
+
building_hist_model = get_model()
|
| 61 |
+
|
| 62 |
+
# Loading the feature view with latest data for building
|
| 63 |
+
building_new_fv = fs.get_feature_view(
|
| 64 |
+
name = 'building_new_fv',
|
| 65 |
+
version = 1)
|
| 66 |
+
|
| 67 |
+
# Function to loading the feature view with latest data for building as a dataset
|
| 68 |
+
@st.cache_data()
|
| 69 |
+
def retrieve_building(feature_view=building_new_fv):
|
| 70 |
+
building_new_fv = feature_view.get_batch_data()
|
| 71 |
+
return building_new_fv
|
| 72 |
+
|
| 73 |
+
# Retrieving el1 data
|
| 74 |
+
building_new = retrieve_building()
|
| 75 |
+
|
| 76 |
+
st.markdown('Parking Space near Building:')
|
| 77 |
+
building_most_recent_prediction = building[['x', 'y', 'z']]
|
| 78 |
+
building_most_recent_prediction = building_hist_model.predict(building_most_recent_prediction)
|
| 79 |
+
building_new['prediction'] = building_most_recent_prediction
|
| 80 |
+
building_new = building_new.set_index(['time'])
|
| 81 |
+
st.dataframe(building_new[['prediction']].tail(5).style.applymap(color_prediction, subset=['prediction']))
|
| 82 |
+
|
| 83 |
+
# Loading the feature view with latest data for bikelane
|
| 84 |
+
bikelane_new_fv = fs.get_feature_view(
|
| 85 |
+
name = 'bikelane_new_fv',
|
| 86 |
+
version = 1)
|
| 87 |
+
|
| 88 |
+
# Function to loading the feature view with latest data for bikelane as a dataset
|
| 89 |
+
@st.cache_data()
|
| 90 |
+
def retrieve_bikelane(feature_view=bikelane_new_fv):
|
| 91 |
+
bikelane_new_fv = feature_view.get_batch_data()
|
| 92 |
+
return bikelane_new_fv
|
| 93 |
+
|
| 94 |
+
# Retrieving bikelane data
|
| 95 |
+
bikelane_new = retrieve_bikelane()
|
| 96 |
+
|
| 97 |
+
st.markdown('Parking Space near Bikelane:')
|
| 98 |
+
bikelane_most_recent_prediction = bikelane[['x', 'y', 'z']]
|
| 99 |
+
bikelane_most_recent_prediction = bikelane_hist_model.predict(bikelane_most_recent_prediction)
|
| 100 |
+
bikelane_new['prediction'] = bikelane_most_recent_prediction
|
| 101 |
+
bikelane_new = bikelane_new.set_index(['time'])
|
| 102 |
+
st.dataframe(bikelane_new[['prediction']].tail(5).style.applymap(color_prediction, subset=['prediction']))
|
| 103 |
+
|
| 104 |
+
if st.button("Update status"):
|
| 105 |
+
st.rerun()
|
| 106 |
+
|
| 107 |
+
with tab2:
|
| 108 |
+
st.markdown('This application is made as part of the module "Data Engineering and Machine Learning Operations in Business - F2024" in Business Data Science 2nd Semester at Aalborg University Business School.')
|
| 109 |
+
st.markdown('The application is made by Annika and Mikkel and is divided into 4 tabs:')
|
| 110 |
+
st.markdown('* **Parking lot status:** The first tab includes the actual interface, where the goal has been to make a simple UI which shows if 3 parking spaces are occupied or available.')
|
| 111 |
+
st.markdown('* **About:** In the second tab (the current tab) you can get some information about the interface.')
|
| 112 |
+
st.markdown('* **Dataset and visualisations:** The third tab contains an overview of the training data and also includes EDAs for each individual parking space. The goal with these EDAs is to give you some information about when the parking spaces usually are occupied.')
|
| 113 |
+
st.markdown('* **Model Performance:** The fourth tab explains how the underlying Machine Learning Model performs and how the predictor works.')
|
| 114 |
+
|
| 115 |
+
with tab3:
|
| 116 |
+
st.markdown('...')
|
| 117 |
+
|
| 118 |
+
# Loading the parking detection feature view
|
| 119 |
+
#parking_detection_fv = fs.get_feature_view(
|
| 120 |
+
# name = 'parking_detection_fv',
|
| 121 |
+
# version = 1)
|
| 122 |
+
|
| 123 |
+
# Function to loading the parking detection feature view as a dataset
|
| 124 |
+
#@st.cache_data()
|
| 125 |
+
#def retrieve_batch_data(feature_view=parking_detection_fv):
|
| 126 |
+
# batch_data = feature_view.get_batch_data()
|
| 127 |
+
# return batch_data
|
| 128 |
+
|
| 129 |
+
# Retrieving batch data
|
| 130 |
+
#batch_data = retrieve_batch_data()
|
| 131 |
+
|
| 132 |
+
# Display dataset overview
|
| 133 |
+
#st.subheader("Dataset Overview")
|
| 134 |
+
#st.dataframe(batch_data.head())
|
| 135 |
+
|
| 136 |
+
with tab4:
|
| 137 |
+
st.markdown('The predictions are made on the basis of a KNearestNeighbours model.')
|
| 138 |
+
#st.write(model)
|
| 139 |
+
|
| 140 |
+
|
| 141 |
+
# Making a countplot of the predictions
|
| 142 |
+
#predictions = model.predict(batch_data)
|
| 143 |
+
#df_test = batch_data.copy()
|
| 144 |
+
#df_test['predictions'] = predictions
|
| 145 |
+
#st.dataframe(df_test.head())
|
| 146 |
+
|
| 147 |
+
#plt.figure(figsize=(10, 6))
|
| 148 |
+
#sns.set_style("darkgrid")
|
| 149 |
+
#sns.countplot(data=df_test, x="predictions")
|
| 150 |
+
#plt.title('Distribution of Predictions')
|
| 151 |
+
#st.pyplot(plt)
|
| 152 |
+
|
| 153 |
+
# Confusion Matrix
|
| 154 |
+
st.subheader("Confusion Matrix")
|
| 155 |
+
st.markdown('In this confusion matrix...')
|