Spaces:
Sleeping
Sleeping
Commit
·
8f76218
1
Parent(s):
cca1555
Upload 5 files
Browse files- __init__.py +3 -0
- index.py +42 -0
- my_dashboard.py +313 -0
- videostream.py +112 -0
- videostream_utils.py +303 -0
__init__.py
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .index import app
|
| 2 |
+
|
| 3 |
+
|
index.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from .my_dashboard import dashboard as my_dashboard
|
| 2 |
+
from typing import Dict, Callable
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
app_routes = {
|
| 6 |
+
"/": main_layout,
|
| 7 |
+
"/my_dashboard": my_dashboard,
|
| 8 |
+
}
|
| 9 |
+
|
| 10 |
+
def app() -> Dict[str, Callable]:
|
| 11 |
+
return app_routes
|
| 12 |
+
|
| 13 |
+
|
| 14 |
+
pn.extension(sizing_mode="stretch_width")
|
| 15 |
+
|
| 16 |
+
nav_markdown = """
|
| 17 |
+
# Navigation
|
| 18 |
+
- [My Dashboard](/my_dashboard)
|
| 19 |
+
"""
|
| 20 |
+
|
| 21 |
+
navigation = pn.Markdown(nav_markdown)
|
| 22 |
+
|
| 23 |
+
main_layout = pn.Column(
|
| 24 |
+
navigation,
|
| 25 |
+
# other components and layouts
|
| 26 |
+
)
|
| 27 |
+
|
| 28 |
+
INTRO = """
|
| 29 |
+
# Awesome Panel on Hugging Face Spaces
|
| 30 |
+
... (rest of the content)
|
| 31 |
+
"""
|
| 32 |
+
|
| 33 |
+
some_component = pn.panel(INTRO)
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
template = pn.template.FastListTemplate(
|
| 38 |
+
site="Awesome Panel 🤗", title="Hello Hugging Face World", main=[some_component],
|
| 39 |
+
favicon="https://sharing.awesome-panel.org/favicon.ico", accent="#fef3c7", header_color="#4b5563"
|
| 40 |
+
)
|
| 41 |
+
|
| 42 |
+
app = pn.serve({'/': template, '/my_dashboard': my_dashboard_app}, return_views=True)
|
my_dashboard.py
ADDED
|
@@ -0,0 +1,313 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import altair as alt
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import panel as pn
|
| 4 |
+
|
| 5 |
+
# Load the Panel extension
|
| 6 |
+
|
| 7 |
+
pn.extension('vega')
|
| 8 |
+
|
| 9 |
+
|
| 10 |
+
# Load the dataset
|
| 11 |
+
data = pd.read_csv("/Users/kenzabaddou/Downloads/archive/marketing_campaign.csv", sep=";")
|
| 12 |
+
|
| 13 |
+
# Data cleaning
|
| 14 |
+
data = data.rename(columns=lambda x: x.strip()) # Remove leading and trailing spaces from column names
|
| 15 |
+
data = data.dropna(subset=['Income'])
|
| 16 |
+
mean_income = data['Income'].mean()
|
| 17 |
+
data['Income'] = data['Income'].fillna(mean_income)
|
| 18 |
+
data['total_spent'] = data[['MntWines', 'MntFruits', 'MntMeatProducts', 'MntFishProducts', 'MntSweetProducts', 'MntGoldProds']].sum(axis=1)
|
| 19 |
+
|
| 20 |
+
# Define widgets
|
| 21 |
+
education_dropdown = alt.binding_select(options=sorted(data['Education'].unique()), name='Education Level:')
|
| 22 |
+
education_select = alt.selection_single(fields=['Education'], bind=education_dropdown, name='Select')
|
| 23 |
+
|
| 24 |
+
# New widget: marital status dropdown
|
| 25 |
+
marital_status_dropdown = alt.binding_select(options=sorted(data['Marital_Status'].unique()), name='Marital Status:')
|
| 26 |
+
marital_status_select = alt.selection_single(fields=['Marital_Status'], bind=marital_status_dropdown, name='Select')
|
| 27 |
+
|
| 28 |
+
# New widget: range slider for number of web visits
|
| 29 |
+
num_web_visits_slider = alt.binding_range(min=0, max=data['NumWebVisitsMonth'].max(), step=1, name='Web Visits per Month:')
|
| 30 |
+
num_web_visits_select = alt.selection_single(fields=['NumWebVisitsMonth'], bind=num_web_visits_slider, name='Select')
|
| 31 |
+
|
| 32 |
+
# Add new widgets for product selection
|
| 33 |
+
products = ['MntWines', 'MntFruits', 'MntMeatProducts', 'MntFishProducts', 'MntSweetProducts', 'MntGoldProds']
|
| 34 |
+
product_x_dropdown = alt.binding_select(options=products, name='Product X:')
|
| 35 |
+
product_x_select = alt.selection_single(fields=['x_product'], bind=product_x_dropdown, init={'x_product': 'MntWines'})
|
| 36 |
+
product_y_dropdown = alt.binding_select(options=products, name='Product Y:')
|
| 37 |
+
product_y_select = alt.selection_single(fields=['y_product'], bind=product_y_dropdown, init={'y_product': 'MntMeatProducts'})
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
# Define views
|
| 41 |
+
|
| 42 |
+
# View 1: Bar chart of customer distribution by education level
|
| 43 |
+
education_chart = alt.Chart(data).mark_bar().encode(
|
| 44 |
+
x=alt.X('Education:N', title='Education Level'),
|
| 45 |
+
y=alt.Y('count():Q', title='Number of Customers'),
|
| 46 |
+
color='Education:N'
|
| 47 |
+
).properties(title='Customer Distribution by Education Level')
|
| 48 |
+
|
| 49 |
+
|
| 50 |
+
# Define widgets
|
| 51 |
+
education_dropdown = alt.binding_select(options=sorted(data['Education'].unique()), name='Education Level:')
|
| 52 |
+
education_select = alt.selection_single(fields=['Education'], bind=education_dropdown, name='Select')
|
| 53 |
+
income_slider = alt.binding_range(min=0, max=data['Income'].max(), step=1000, name='Annual Income:')
|
| 54 |
+
income_select = alt.selection_single(fields=['Income'], bind=income_slider, name='Select')
|
| 55 |
+
|
| 56 |
+
# View 2: Scatter plot of customer annual income and total amount spent on products (with interaction)
|
| 57 |
+
scatter_chart = alt.Chart(data).mark_circle().encode(
|
| 58 |
+
x=alt.X('Income:Q', title='Annual Income'),
|
| 59 |
+
y=alt.Y('total_spent:Q', title='Total Amount Spent on Products'),
|
| 60 |
+
color='Education:N',
|
| 61 |
+
tooltip=[
|
| 62 |
+
alt.Tooltip('Education'),
|
| 63 |
+
alt.Tooltip('Marital_Status'),
|
| 64 |
+
alt.Tooltip('Income', format='$,.0f'),
|
| 65 |
+
alt.Tooltip('total_spent', format='$,.0f')
|
| 66 |
+
]
|
| 67 |
+
).properties(title='Customer Annual Income vs Amount Spent on Products').add_selection(
|
| 68 |
+
education_select, income_select, marital_status_select, num_web_visits_select
|
| 69 |
+
).transform_filter(
|
| 70 |
+
education_select
|
| 71 |
+
).transform_filter(
|
| 72 |
+
income_select
|
| 73 |
+
).transform_filter(
|
| 74 |
+
marital_status_select
|
| 75 |
+
).transform_filter(
|
| 76 |
+
num_web_visits_select
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
|
| 80 |
+
# Define linked selection
|
| 81 |
+
brush = alt.selection(type='interval')
|
| 82 |
+
|
| 83 |
+
# View 5: Bar chart - Average Total Spending per Education Level (linked to scatter chart)
|
| 84 |
+
avg_spending_by_education = alt.Chart(data).mark_bar().encode(
|
| 85 |
+
x=alt.X('Education:N', title='Education Level'),
|
| 86 |
+
y=alt.Y('mean(total_spent):Q', title='Average Total Spending'),
|
| 87 |
+
tooltip=['Education', 'mean(total_spent):Q']
|
| 88 |
+
).properties(title='Average Total Spending per Education Level').add_selection(
|
| 89 |
+
brush
|
| 90 |
+
).transform_filter(
|
| 91 |
+
education_select
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
# View 6: Yearly Total Amount Spent on Products (linked to enrollment chart)
|
| 95 |
+
yearly_total_spent_chart = alt.Chart(data).mark_line().encode(
|
| 96 |
+
x=alt.X('year(Dt_Customer):O', title='Year'),
|
| 97 |
+
y=alt.Y('sum(total_spent):Q', title='Total Amount Spent'),
|
| 98 |
+
tooltip=['year(Dt_Customer):O', 'sum(total_spent):Q']
|
| 99 |
+
).transform_filter(
|
| 100 |
+
education_select
|
| 101 |
+
)
|
| 102 |
+
|
| 103 |
+
# View 3: Timeline chart of new customer enrollments (with linked highlighting)
|
| 104 |
+
enrollment_chart = alt.Chart(data).mark_line().encode(
|
| 105 |
+
x=alt.X('year(Dt_Customer):T', title='Year of Enrollment'),
|
| 106 |
+
y=alt.Y('count():Q', title='Number of New Enrollments'),
|
| 107 |
+
color='Marital_Status:N').properties(title='New Customer Enrollments Over Time').add_selection(
|
| 108 |
+
brush, education_select, income_select, marital_status_select, num_web_visits_select).transform_filter(
|
| 109 |
+
education_select).transform_filter(income_select).transform_filter(marital_status_select).transform_filter(
|
| 110 |
+
num_web_visits_select)
|
| 111 |
+
|
| 112 |
+
# New View: Average spending per marital status (with linked highlighting)
|
| 113 |
+
|
| 114 |
+
avg_spending_by_marital_status = alt.Chart(data).mark_bar().encode(
|
| 115 |
+
x=alt.X('Marital_Status:N', title='Marital Status'),
|
| 116 |
+
y=alt.Y('mean(total_spent):Q', title='Average Total Spending'),
|
| 117 |
+
color='Marital_Status:N',
|
| 118 |
+
tooltip=['Marital_Status', 'mean(total_spent):Q']).properties(title='Average Total Spending per Marital Status').add_selection(brush)
|
| 119 |
+
|
| 120 |
+
import pandas as pd
|
| 121 |
+
import altair as alt
|
| 122 |
+
|
| 123 |
+
|
| 124 |
+
# Convert the 'Dt_Customer' column to datetime format
|
| 125 |
+
data['Dt_Customer'] = pd.to_datetime(data['Dt_Customer'])
|
| 126 |
+
|
| 127 |
+
# Create 'total_spent' column by summing up spending in different product categories
|
| 128 |
+
data['total_spent'] = data[['MntWines', 'MntFruits', 'MntMeatProducts', 'MntFishProducts', 'MntSweetProducts', 'MntGoldProds']].sum(axis=1)
|
| 129 |
+
|
| 130 |
+
# Calculate the average spending for each combination of Teenhome, Kidhome, and Marital_Status
|
| 131 |
+
avg_spending = data.groupby(['Teenhome', 'Kidhome', 'Marital_Status'])['total_spent'].mean().reset_index()
|
| 132 |
+
|
| 133 |
+
# Create a hover selection
|
| 134 |
+
hover = alt.selection_single(on='mouseover', nearest=True, empty='none')
|
| 135 |
+
|
| 136 |
+
# Create a selection for the dropdown
|
| 137 |
+
marital_status_dropdown = alt.binding_select(options=sorted(data['Marital_Status'].unique()), name='Marital Status:')
|
| 138 |
+
marital_status_select = alt.selection_single(fields=['Marital_Status'], bind=marital_status_dropdown, init={'Marital_Status': 'Married'})
|
| 139 |
+
|
| 140 |
+
# Create a grouped bar chart for kids
|
| 141 |
+
chart_kids = alt.Chart(avg_spending).mark_bar().encode(
|
| 142 |
+
x=alt.X('Kidhome:O', title='Number of Kids at Home'),
|
| 143 |
+
y=alt.Y('total_spent:Q', title='Average Spending'),
|
| 144 |
+
color=alt.condition(hover, 'Kidhome:O', alt.value('lightgray'), legend=alt.Legend(title='Number of Kids at Home')),
|
| 145 |
+
tooltip=['Teenhome', 'Kidhome', 'total_spent', 'Marital_Status']
|
| 146 |
+
).properties(title='Average Spending by Number of Kids at Home (Filtered by Marital Status)').add_selection(
|
| 147 |
+
hover
|
| 148 |
+
)
|
| 149 |
+
|
| 150 |
+
# Create a grouped bar chart for teens
|
| 151 |
+
chart_teens = alt.Chart(avg_spending).mark_bar().encode(
|
| 152 |
+
x=alt.X('Teenhome:O', title='Number of Teens at Home'),
|
| 153 |
+
y=alt.Y('total_spent:Q', title='Average Spending'),
|
| 154 |
+
color=alt.condition(hover, 'Teenhome:O', alt.value('lightgray'), legend=alt.Legend(title='Number of Teens at Home')),
|
| 155 |
+
tooltip=['Teenhome', 'Kidhome', 'total_spent', 'Marital_Status']
|
| 156 |
+
).properties(title='Average Spending by Number of Teens at Home (Filtered by Marital Status)').add_selection(
|
| 157 |
+
hover
|
| 158 |
+
)
|
| 159 |
+
|
| 160 |
+
# Combine the charts
|
| 161 |
+
concat_chart = alt.hconcat(chart_kids, chart_teens).add_selection(
|
| 162 |
+
marital_status_select
|
| 163 |
+
).transform_filter(
|
| 164 |
+
marital_status_select
|
| 165 |
+
)
|
| 166 |
+
|
| 167 |
+
|
| 168 |
+
"""scatter_plot = alt.Chart(data).mark_circle().encode(
|
| 169 |
+
x=alt.X('NumWebVisitsMonth:Q', title='Number of Web Visits per Month'),
|
| 170 |
+
y=alt.Y('NumWebPurchases:Q', title='Number of Web Purchases'),
|
| 171 |
+
size=alt.Size('count():Q', scale=alt.Scale(range=[50, 500]), legend=alt.Legend(title='Number of Customers')),
|
| 172 |
+
color=alt.Color('count():Q', scale=alt.Scale(scheme='viridis'), legend=None),
|
| 173 |
+
tooltip=['NumWebVisitsMonth', 'NumWebPurchases', 'count()']
|
| 174 |
+
).properties(title='Scatter Plot of Web Visits per Month vs. Web Purchases')
|
| 175 |
+
|
| 176 |
+
scatter_plot.interactive()"""
|
| 177 |
+
|
| 178 |
+
# Define dropdown selection for marital status
|
| 179 |
+
marital_status_dropdown = alt.binding_select(options=data['Marital_Status'].unique().tolist(), name='Marital Status: ')
|
| 180 |
+
marital_status_selection = alt.selection_single(fields=['Marital_Status'], bind=marital_status_dropdown, name='Marital_Status', init={'Marital_Status': data['Marital_Status'].iloc[0]})
|
| 181 |
+
|
| 182 |
+
# Add a scatter plot and filter by marital status
|
| 183 |
+
scatter_plot_filtered = alt.Chart(data).mark_circle().encode(
|
| 184 |
+
x=alt.X('NumWebVisitsMonth:Q', title='Number of Web Visits per Month'),
|
| 185 |
+
y=alt.Y('NumWebPurchases:Q', title='Number of Web Purchases'),
|
| 186 |
+
size=alt.Size('count():Q', scale=alt.Scale(range=[50, 500]), legend=alt.Legend(title='Number of Customers')),
|
| 187 |
+
color=alt.Color('count():Q', scale=alt.Scale(scheme='viridis'), legend=None),
|
| 188 |
+
tooltip=['NumWebVisitsMonth', 'NumWebPurchases', 'count()']
|
| 189 |
+
).properties(title='Scatter Plot of Web Visits per Month vs. Web Purchases Filtered by Marital Status').transform_filter(
|
| 190 |
+
marital_status_selection
|
| 191 |
+
)
|
| 192 |
+
|
| 193 |
+
# Add interactivity and the marital status selection to the scatter plot
|
| 194 |
+
interactive_scatter_plot_filtered = scatter_plot_filtered.interactive().add_selection(marital_status_selection)
|
| 195 |
+
interactive_scatter_plot_filtered
|
| 196 |
+
|
| 197 |
+
|
| 198 |
+
# Group by 'Dt_Customer' and calculate average spending for new and returning customers
|
| 199 |
+
new_customers = data[data['NumWebPurchases'] == 0].groupby('Dt_Customer')['total_spent'].mean().reset_index()
|
| 200 |
+
returning_customers = data[data['NumWebPurchases'] > 0].groupby('Dt_Customer')['total_spent'].mean().reset_index()
|
| 201 |
+
|
| 202 |
+
|
| 203 |
+
# new_customers and returning_customers DataFrames
|
| 204 |
+
# Create line charts for new and returning customers without filtering
|
| 205 |
+
new_line = alt.Chart(new_customers).mark_line().encode(
|
| 206 |
+
x=alt.X('Dt_Customer:T', title='Date'),
|
| 207 |
+
y=alt.Y('total_spent:Q', title='Average Spending'),
|
| 208 |
+
color=alt.value('blue'),
|
| 209 |
+
tooltip=['Dt_Customer', 'total_spent']
|
| 210 |
+
).properties(title='New Customers')
|
| 211 |
+
|
| 212 |
+
returning_line = alt.Chart(returning_customers).mark_line().encode(
|
| 213 |
+
x=alt.X('Dt_Customer:T', title='Date'),
|
| 214 |
+
y=alt.Y('total_spent:Q', title='Average Spending'),
|
| 215 |
+
color=alt.value('green'),
|
| 216 |
+
tooltip=['Dt_Customer', 'total_spent']
|
| 217 |
+
).properties(title='Returning Customers')
|
| 218 |
+
|
| 219 |
+
# Display the charts side by side
|
| 220 |
+
combined_chart = alt.hconcat(new_line, returning_line)
|
| 221 |
+
combined_chart
|
| 222 |
+
|
| 223 |
+
# Merge Marital_Status to new_customers and returning_customers DataFrames
|
| 224 |
+
|
| 225 |
+
new_customers = new_customers.merge(data[['Dt_Customer', 'Marital_Status']], on='Dt_Customer', how='left')
|
| 226 |
+
returning_customers = returning_customers.merge(data[['Dt_Customer', 'Marital_Status']], on='Dt_Customer', how='left')
|
| 227 |
+
|
| 228 |
+
# Filter data by marital status using transform_filter within the charts
|
| 229 |
+
new_line_filtered = alt.Chart(new_customers).mark_line().encode(
|
| 230 |
+
x=alt.X('Dt_Customer:T', title='Date'),
|
| 231 |
+
y=alt.Y('total_spent:Q', title='Average Spending'),
|
| 232 |
+
color=alt.value('blue'),
|
| 233 |
+
tooltip=['Dt_Customer', 'total_spent']
|
| 234 |
+
).properties(title='New Customers').transform_filter(
|
| 235 |
+
marital_status_selection
|
| 236 |
+
)
|
| 237 |
+
|
| 238 |
+
returning_line_filtered = alt.Chart(returning_customers).mark_line().encode(
|
| 239 |
+
x=alt.X('Dt_Customer:T', title='Date'),
|
| 240 |
+
y=alt.Y('total_spent:Q', title='Average Spending'),
|
| 241 |
+
color=alt.value('green'),
|
| 242 |
+
tooltip=['Dt_Customer', 'total_spent']
|
| 243 |
+
).properties(title='Returning Customers').transform_filter(
|
| 244 |
+
marital_status_selection
|
| 245 |
+
)
|
| 246 |
+
|
| 247 |
+
# Add interactivity for panning and zooming
|
| 248 |
+
interactive_chart_filtered = alt.layer(new_line_filtered, returning_line_filtered).resolve_scale(y='shared').interactive().add_selection(marital_status_selection)
|
| 249 |
+
|
| 250 |
+
# Combine the interactive chart, the legend, and the marital status selection
|
| 251 |
+
# Create legend
|
| 252 |
+
legend = alt.Chart(pd.DataFrame({'legend': ['New Customers', 'Returning Customers'], 'color': ['blue', 'green']})).mark_point().encode(
|
| 253 |
+
x=alt.value(20),
|
| 254 |
+
y=alt.Y('legend', title=None),
|
| 255 |
+
color=alt.Color('color', scale=None),
|
| 256 |
+
tooltip=['legend']
|
| 257 |
+
)
|
| 258 |
+
|
| 259 |
+
# Combine the interactive chart, the legend, and the marital status selection
|
| 260 |
+
final_chart_filtered = alt.hconcat(interactive_chart_filtered, legend).properties(title='Average Spending of New vs Returning Customers by Marital Status')
|
| 261 |
+
final_chart_filtered
|
| 262 |
+
|
| 263 |
+
import panel as pn
|
| 264 |
+
pn.extension('vega')
|
| 265 |
+
|
| 266 |
+
|
| 267 |
+
# Define dashboard layout
|
| 268 |
+
dashboard = pn.Column(
|
| 269 |
+
pn.Row(
|
| 270 |
+
pn.Column(
|
| 271 |
+
education_chart,
|
| 272 |
+
avg_spending_by_education,
|
| 273 |
+
avg_spending_by_marital_status,
|
| 274 |
+
width=350
|
| 275 |
+
),
|
| 276 |
+
pn.Column(
|
| 277 |
+
scatter_chart,
|
| 278 |
+
enrollment_chart,
|
| 279 |
+
width=700
|
| 280 |
+
),
|
| 281 |
+
),
|
| 282 |
+
pn.Row(
|
| 283 |
+
concat_chart,
|
| 284 |
+
width=1000
|
| 285 |
+
),
|
| 286 |
+
pn.Row(
|
| 287 |
+
final_chart_filtered,
|
| 288 |
+
width=1000
|
| 289 |
+
),
|
| 290 |
+
)
|
| 291 |
+
|
| 292 |
+
|
| 293 |
+
# Display dashboard
|
| 294 |
+
dashboard.servable()
|
| 295 |
+
|
| 296 |
+
app = pn.serve(dashboard, return_views=True)
|
| 297 |
+
|
| 298 |
+
|
| 299 |
+
dashboard = pn.Column(
|
| 300 |
+
pn.Row(
|
| 301 |
+
pn.Column(education_chart, avg_spending_by_education, interactive_scatter_plot_filtered, width=350),
|
| 302 |
+
scatter_chart, width=700
|
| 303 |
+
),
|
| 304 |
+
|
| 305 |
+
pn.Row(
|
| 306 |
+
avg_spending_by_marital_status, concat_chart, width=1000
|
| 307 |
+
),
|
| 308 |
+
|
| 309 |
+
pn.Row(
|
| 310 |
+
enrollment_chart, final_chart_filtered, width=1000
|
| 311 |
+
)
|
| 312 |
+
)
|
| 313 |
+
|
videostream.py
ADDED
|
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""The VideoStreamInterface provides an easy way to apply transforms to a video stream"""
|
| 2 |
+
import numpy as np
|
| 3 |
+
import panel as pn
|
| 4 |
+
import param
|
| 5 |
+
import skimage
|
| 6 |
+
from PIL import Image, ImageFilter
|
| 7 |
+
from skimage import data, filters
|
| 8 |
+
from skimage.color.adapt_rgb import adapt_rgb, each_channel
|
| 9 |
+
from skimage.draw import rectangle
|
| 10 |
+
from skimage.exposure import rescale_intensity
|
| 11 |
+
from skimage.feature import Cascade
|
| 12 |
+
from videostream_utils import PILImageTransform, NumpyImageTransform, VideoStreamInterface
|
| 13 |
+
|
| 14 |
+
pn.extension("terminal", sizing_mode="stretch_width")
|
| 15 |
+
|
| 16 |
+
ACCENT = "#fef3c7"
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
class GaussianBlur(PILImageTransform):
|
| 20 |
+
"""Gaussian Blur
|
| 21 |
+
https://pillow.readthedocs.io/en/stable/reference/ImageFilter.html#PIL.ImageFilter.GaussianBlur
|
| 22 |
+
"""
|
| 23 |
+
|
| 24 |
+
radius = param.Integer(default=2, bounds=(0, 10))
|
| 25 |
+
|
| 26 |
+
def transform(self, image: Image):
|
| 27 |
+
return image.filter(ImageFilter.GaussianBlur(radius=self.radius))
|
| 28 |
+
|
| 29 |
+
|
| 30 |
+
class GrayscaleTransform(NumpyImageTransform):
|
| 31 |
+
"""GrayScale transform
|
| 32 |
+
https://scikit-image.org/docs/0.15.x/auto_examples/color_exposure/plot_rgb_to_gray.html
|
| 33 |
+
"""
|
| 34 |
+
|
| 35 |
+
def transform(self, image: np.ndarray):
|
| 36 |
+
grayscale = skimage.color.rgb2gray(image[:, :, :3])
|
| 37 |
+
return skimage.color.gray2rgb(grayscale)
|
| 38 |
+
|
| 39 |
+
|
| 40 |
+
class SobelTransform(NumpyImageTransform):
|
| 41 |
+
"""Sobel Transform
|
| 42 |
+
https://scikit-image.org/docs/0.15.x/auto_examples/color_exposure/plot_adapt_rgb.html
|
| 43 |
+
"""
|
| 44 |
+
|
| 45 |
+
def transform(self, image):
|
| 46 |
+
@adapt_rgb(each_channel)
|
| 47 |
+
def sobel_each(image):
|
| 48 |
+
return filters.sobel(image)
|
| 49 |
+
|
| 50 |
+
return rescale_intensity(1 - sobel_each(image))
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
@pn.cache()
|
| 54 |
+
def get_detector():
|
| 55 |
+
"""Returns the Cascade detector"""
|
| 56 |
+
trained_file = data.lbp_frontal_face_cascade_filename()
|
| 57 |
+
return Cascade(trained_file)
|
| 58 |
+
|
| 59 |
+
class FaceDetectionTransform(NumpyImageTransform):
|
| 60 |
+
"""Face detection using a cascade classifier.
|
| 61 |
+
https://scikit-image.org/docs/0.15.x/auto_examples/applications/plot_face_detection.html
|
| 62 |
+
"""
|
| 63 |
+
|
| 64 |
+
scale_factor = param.Number(1.4, bounds=(1.0, 2.0), step=0.1)
|
| 65 |
+
step_ratio = param.Integer(1, bounds=(1, 10))
|
| 66 |
+
size_x = param.Range(default=(60, 322), bounds=(10, 500))
|
| 67 |
+
size_y = param.Range(default=(60, 322), bounds=(10, 500))
|
| 68 |
+
|
| 69 |
+
def transform(self, image):
|
| 70 |
+
detector = get_detector()
|
| 71 |
+
detected = detector.detect_multi_scale(
|
| 72 |
+
img=image,
|
| 73 |
+
scale_factor=self.scale_factor,
|
| 74 |
+
step_ratio=self.step_ratio,
|
| 75 |
+
min_size=(self.size_x[0], self.size_y[0]),
|
| 76 |
+
max_size=(self.size_x[1], self.size_y[1]),
|
| 77 |
+
)
|
| 78 |
+
|
| 79 |
+
for patch in detected:
|
| 80 |
+
rrr, ccc = rectangle(
|
| 81 |
+
start=(patch["r"], patch["c"]),
|
| 82 |
+
extent=(patch["height"], patch["width"]),
|
| 83 |
+
shape=image.shape[:2],
|
| 84 |
+
)
|
| 85 |
+
image[rrr, ccc, 0] = 200
|
| 86 |
+
|
| 87 |
+
return image
|
| 88 |
+
|
| 89 |
+
|
| 90 |
+
component = VideoStreamInterface(
|
| 91 |
+
transforms=[
|
| 92 |
+
GaussianBlur,
|
| 93 |
+
GrayscaleTransform,
|
| 94 |
+
SobelTransform,
|
| 95 |
+
FaceDetectionTransform,
|
| 96 |
+
]
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
component.video_stream.timeout=1000
|
| 100 |
+
component.video_stream.param.timeout.bounds=(250, 2000)
|
| 101 |
+
|
| 102 |
+
pn.template.FastListTemplate(
|
| 103 |
+
site="Awesome Panel 🤗",
|
| 104 |
+
title="VideoStream with transforms",
|
| 105 |
+
sidebar=[component.settings],
|
| 106 |
+
main=[
|
| 107 |
+
"""Try a much, much faster version <a href="https://sharing.awesome-panel.org/MarcSkovMadsen/videostream-interface/app.html" target="_blank">here</a> powered by Webassembly.""",
|
| 108 |
+
component],
|
| 109 |
+
favicon="https://sharing.awesome-panel.org/favicon.ico",
|
| 110 |
+
accent=ACCENT,
|
| 111 |
+
header_color="#4b5563"
|
| 112 |
+
).servable()
|
videostream_utils.py
ADDED
|
@@ -0,0 +1,303 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""The VideoStreamInterface provides an easy way to apply transforms to a video stream"""
|
| 2 |
+
import base64
|
| 3 |
+
import io
|
| 4 |
+
import time
|
| 5 |
+
|
| 6 |
+
import numpy as np
|
| 7 |
+
import panel as pn
|
| 8 |
+
import param
|
| 9 |
+
import PIL
|
| 10 |
+
from PIL import Image
|
| 11 |
+
|
| 12 |
+
HEIGHT = 400
|
| 13 |
+
WIDTH = 400
|
| 14 |
+
TIMEOUT = 250
|
| 15 |
+
ACCENT = "#fef3c7"
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def to_instance(value, **params):
|
| 19 |
+
"""Converts the value to an instance
|
| 20 |
+
Args:
|
| 21 |
+
value: A param.Parameterized class or instance
|
| 22 |
+
Returns:
|
| 23 |
+
An instance of the param.Parameterized class
|
| 24 |
+
"""
|
| 25 |
+
if isinstance(value, param.Parameterized):
|
| 26 |
+
value.param.update(**params)
|
| 27 |
+
return value
|
| 28 |
+
return value(**params)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
class Timer(pn.viewable.Viewer):
|
| 32 |
+
"""Helper Component used to show duration trends"""
|
| 33 |
+
|
| 34 |
+
_trends = param.Dict()
|
| 35 |
+
|
| 36 |
+
def __init__(self, **params):
|
| 37 |
+
super().__init__()
|
| 38 |
+
|
| 39 |
+
self.last_updates = {}
|
| 40 |
+
self._trends = {}
|
| 41 |
+
|
| 42 |
+
self._layout = pn.Row(**params)
|
| 43 |
+
|
| 44 |
+
def time_it(self, name, func, *args, **kwargs):
|
| 45 |
+
"""Measures the duration of the execution of the func function and reports it under the
|
| 46 |
+
name specified"""
|
| 47 |
+
start = time.time()
|
| 48 |
+
result = func(*args, **kwargs)
|
| 49 |
+
end = time.time()
|
| 50 |
+
duration = round(end - start, 2)
|
| 51 |
+
self._report(name=name, duration=duration)
|
| 52 |
+
return result
|
| 53 |
+
|
| 54 |
+
def inc_it(self, name):
|
| 55 |
+
"""Measures the duration since the last time `inc_it` was called and reports it under the
|
| 56 |
+
specified name"""
|
| 57 |
+
start = self.last_updates.get(name, time.time())
|
| 58 |
+
end = time.time()
|
| 59 |
+
duration = round(end - start, 2)
|
| 60 |
+
self._report(name=name, duration=duration)
|
| 61 |
+
self.last_updates[name] = end
|
| 62 |
+
|
| 63 |
+
def _report(self, name, duration):
|
| 64 |
+
if not name in self._trends:
|
| 65 |
+
self._trends[name] = pn.indicators.Trend(
|
| 66 |
+
title=name,
|
| 67 |
+
data={"x": [1], "y": [duration]},
|
| 68 |
+
plot_color=ACCENT,
|
| 69 |
+
height=100,
|
| 70 |
+
width=150,
|
| 71 |
+
sizing_mode="fixed",
|
| 72 |
+
)
|
| 73 |
+
self.param.trigger("_trends")
|
| 74 |
+
else:
|
| 75 |
+
trend = self._trends[name]
|
| 76 |
+
next_x = max(trend.data["x"]) + 1
|
| 77 |
+
trend.stream({"x": [next_x], "y": [duration]}, rollover=10)
|
| 78 |
+
|
| 79 |
+
@pn.depends("_trends")
|
| 80 |
+
def _panel(self):
|
| 81 |
+
self._layout[:] = list(self._trends.values())
|
| 82 |
+
return self._layout
|
| 83 |
+
|
| 84 |
+
def __panel__(self):
|
| 85 |
+
return self._panel
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
class ImageTransform(pn.viewable.Viewer):
|
| 89 |
+
"""Base class for image transforms."""
|
| 90 |
+
|
| 91 |
+
def __init__(self, **params):
|
| 92 |
+
super().__init__(**params)
|
| 93 |
+
|
| 94 |
+
with param.edit_constant(self):
|
| 95 |
+
self.name = self.__class__.name.replace("Transform", "")
|
| 96 |
+
self.view = self.create_view()
|
| 97 |
+
|
| 98 |
+
def __panel__(self):
|
| 99 |
+
return self.view
|
| 100 |
+
|
| 101 |
+
def run(self, image: str, height: int = HEIGHT, width: int = WIDTH) -> str:
|
| 102 |
+
"""Transforms the base64 encoded jpg image to a base64 encoded jpg BytesIO object"""
|
| 103 |
+
raise NotImplementedError()
|
| 104 |
+
|
| 105 |
+
def create_view(self):
|
| 106 |
+
"""Creates a view of the parameters of the transform to enable the user to configure them"""
|
| 107 |
+
return pn.Param(self, name=self.name)
|
| 108 |
+
|
| 109 |
+
def transform(self, image):
|
| 110 |
+
"""Transforms the image"""
|
| 111 |
+
raise NotImplementedError()
|
| 112 |
+
|
| 113 |
+
|
| 114 |
+
class PILImageTransform(ImageTransform):
|
| 115 |
+
"""Base class for PIL image transforms"""
|
| 116 |
+
|
| 117 |
+
@staticmethod
|
| 118 |
+
def to_pil_img(value: str, height=HEIGHT, width=WIDTH):
|
| 119 |
+
"""Converts a base64 jpeg image string to a PIL.Image"""
|
| 120 |
+
encoded_data = value.split(",")[1]
|
| 121 |
+
base64_decoded = base64.b64decode(encoded_data)
|
| 122 |
+
image = Image.open(io.BytesIO(base64_decoded))
|
| 123 |
+
image.draft("RGB", (height, width))
|
| 124 |
+
return image
|
| 125 |
+
|
| 126 |
+
@staticmethod
|
| 127 |
+
def from_pil_img(image: Image):
|
| 128 |
+
"""Converts a PIL.Image to a base64 encoded JPG BytesIO object"""
|
| 129 |
+
buff = io.BytesIO()
|
| 130 |
+
image.save(buff, format="JPEG")
|
| 131 |
+
return buff
|
| 132 |
+
|
| 133 |
+
def run(self, image: str, height: int = HEIGHT, width: int = WIDTH) -> io.BytesIO:
|
| 134 |
+
pil_img = self.to_pil_img(image, height=height, width=width)
|
| 135 |
+
|
| 136 |
+
transformed_image = self.transform(pil_img)
|
| 137 |
+
|
| 138 |
+
return self.from_pil_img(transformed_image)
|
| 139 |
+
|
| 140 |
+
def transform(self, image: PIL.Image) -> PIL.Image:
|
| 141 |
+
"""Transforms the PIL.Image image"""
|
| 142 |
+
raise NotImplementedError()
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
class NumpyImageTransform(ImageTransform):
|
| 146 |
+
"""Base class for np.ndarray image transforms"""
|
| 147 |
+
|
| 148 |
+
@staticmethod
|
| 149 |
+
def to_np_ndarray(image: str, height=HEIGHT, width=WIDTH) -> np.ndarray:
|
| 150 |
+
"""Converts a base64 encoded jpeg string to a np.ndarray"""
|
| 151 |
+
pil_img = PILImageTransform.to_pil_img(image, height=height, width=width)
|
| 152 |
+
return np.array(pil_img)
|
| 153 |
+
|
| 154 |
+
@staticmethod
|
| 155 |
+
def from_np_ndarray(image: np.ndarray) -> io.BytesIO:
|
| 156 |
+
"""Converts np.ndarray jpeg image to a jpeg BytesIO instance"""
|
| 157 |
+
if image.dtype == np.dtype("float64"):
|
| 158 |
+
image = (image * 255).astype(np.uint8)
|
| 159 |
+
pil_img = PIL.Image.fromarray(image)
|
| 160 |
+
return PILImageTransform.from_pil_img(pil_img)
|
| 161 |
+
|
| 162 |
+
def run(self, image: str, height: int = HEIGHT, width: int = WIDTH) -> io.BytesIO:
|
| 163 |
+
np_array = self.to_np_ndarray(image, height=height, width=width)
|
| 164 |
+
|
| 165 |
+
transformed_image = self.transform(np_array)
|
| 166 |
+
|
| 167 |
+
return self.from_np_ndarray(transformed_image)
|
| 168 |
+
|
| 169 |
+
def transform(self, image: np.ndarray) -> np.ndarray:
|
| 170 |
+
"""Transforms the nd.array image"""
|
| 171 |
+
raise NotImplementedError()
|
| 172 |
+
|
| 173 |
+
|
| 174 |
+
class VideoStreamInterface(pn.viewable.Viewer):
|
| 175 |
+
"""An easy to use interface for a VideoStream and a set of transforms"""
|
| 176 |
+
|
| 177 |
+
video_stream = param.ClassSelector(
|
| 178 |
+
class_=pn.widgets.VideoStream, constant=True, doc="The source VideoStream"
|
| 179 |
+
)
|
| 180 |
+
|
| 181 |
+
height = param.Integer(
|
| 182 |
+
HEIGHT,
|
| 183 |
+
bounds=(10, 2000),
|
| 184 |
+
step=10,
|
| 185 |
+
doc="""The height of the image converted and shown""",
|
| 186 |
+
)
|
| 187 |
+
width = param.Integer(
|
| 188 |
+
WIDTH,
|
| 189 |
+
bounds=(10, 2000),
|
| 190 |
+
step=10,
|
| 191 |
+
doc="""The width of the image converted and shown""",
|
| 192 |
+
)
|
| 193 |
+
|
| 194 |
+
transform = param.Selector(doc="The currently selected transform")
|
| 195 |
+
|
| 196 |
+
def __init__(
|
| 197 |
+
self,
|
| 198 |
+
transforms,
|
| 199 |
+
timeout=TIMEOUT,
|
| 200 |
+
paused=False,
|
| 201 |
+
**params,
|
| 202 |
+
):
|
| 203 |
+
super().__init__(
|
| 204 |
+
video_stream=pn.widgets.VideoStream(
|
| 205 |
+
name="Video Stream",
|
| 206 |
+
timeout=timeout,
|
| 207 |
+
paused=paused,
|
| 208 |
+
height=0,
|
| 209 |
+
width=0,
|
| 210 |
+
visible=False,
|
| 211 |
+
format="jpeg",
|
| 212 |
+
),
|
| 213 |
+
**params,
|
| 214 |
+
)
|
| 215 |
+
self.image = pn.pane.JPG(
|
| 216 |
+
height=self.height, width=self.width, sizing_mode="fixed"
|
| 217 |
+
)
|
| 218 |
+
self._updating = False
|
| 219 |
+
transforms = [to_instance(transform) for transform in transforms]
|
| 220 |
+
self.param.transform.objects = transforms
|
| 221 |
+
self.transform = transforms[0]
|
| 222 |
+
self.timer = Timer(sizing_mode="stretch_width")
|
| 223 |
+
self.settings = self._create_settings()
|
| 224 |
+
self._panel = self._create_panel()
|
| 225 |
+
|
| 226 |
+
def _create_settings(self):
|
| 227 |
+
return pn.Column(
|
| 228 |
+
pn.Param(
|
| 229 |
+
self.video_stream,
|
| 230 |
+
parameters=["timeout", "paused"],
|
| 231 |
+
widgets={
|
| 232 |
+
"timeout": {
|
| 233 |
+
"widget_type": pn.widgets.IntSlider,
|
| 234 |
+
"start": 10,
|
| 235 |
+
"end": 2000,
|
| 236 |
+
"step": 10,
|
| 237 |
+
}
|
| 238 |
+
},
|
| 239 |
+
),
|
| 240 |
+
self.timer,
|
| 241 |
+
pn.Param(self, parameters=["height", "width"], name="Image"),
|
| 242 |
+
pn.Param(
|
| 243 |
+
self,
|
| 244 |
+
parameters=["transform"],
|
| 245 |
+
expand_button=False,
|
| 246 |
+
expand=False,
|
| 247 |
+
widgets={
|
| 248 |
+
"transform": {
|
| 249 |
+
"widget_type": pn.widgets.RadioButtonGroup,
|
| 250 |
+
"orientation": "vertical",
|
| 251 |
+
"button_type": "success",
|
| 252 |
+
}
|
| 253 |
+
},
|
| 254 |
+
name="Transform",
|
| 255 |
+
),
|
| 256 |
+
self._get_transform,
|
| 257 |
+
)
|
| 258 |
+
|
| 259 |
+
def _create_panel(self):
|
| 260 |
+
return pn.Row(
|
| 261 |
+
self.video_stream,
|
| 262 |
+
pn.layout.HSpacer(),
|
| 263 |
+
self.image,
|
| 264 |
+
pn.layout.HSpacer(),
|
| 265 |
+
sizing_mode="stretch_width",
|
| 266 |
+
align="center",
|
| 267 |
+
)
|
| 268 |
+
|
| 269 |
+
@pn.depends("height", "width", watch=True)
|
| 270 |
+
def _update_height_width(self):
|
| 271 |
+
self.image.height = self.height
|
| 272 |
+
self.image.width = self.width
|
| 273 |
+
|
| 274 |
+
@pn.depends("transform")
|
| 275 |
+
def _get_transform(self):
|
| 276 |
+
# Hack: returning self.transform stops working after browsing the transforms for a while
|
| 277 |
+
return self.transform.view
|
| 278 |
+
|
| 279 |
+
def __panel__(self):
|
| 280 |
+
return self._panel
|
| 281 |
+
|
| 282 |
+
@pn.depends("video_stream.value", watch=True)
|
| 283 |
+
def _handle_stream(self):
|
| 284 |
+
if self._updating:
|
| 285 |
+
return
|
| 286 |
+
|
| 287 |
+
self._updating = True
|
| 288 |
+
if self.transform and self.video_stream.value:
|
| 289 |
+
value = self.video_stream.value
|
| 290 |
+
try:
|
| 291 |
+
image = self.timer.time_it(
|
| 292 |
+
name="Transform",
|
| 293 |
+
func=self.transform.run,
|
| 294 |
+
image=value,
|
| 295 |
+
height=self.height,
|
| 296 |
+
width=self.width,
|
| 297 |
+
)
|
| 298 |
+
self.image.object = image
|
| 299 |
+
except PIL.UnidentifiedImageError:
|
| 300 |
+
print("unidentified image")
|
| 301 |
+
|
| 302 |
+
self.timer.inc_it("last update")
|
| 303 |
+
self._updating = False
|