-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
86 lines (74 loc) · 2.46 KB
/
demo.py
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
82
83
84
85
86
import pandas as pd
import asyncio
from argparse import ArgumentParser
import streamlit as st
import altair as alt
from etoile.subscriber import TrafficSubscriber
def create_layout():
st.title("Real-Time Traffic Analytics")
st.header("Live Video Stream")
img_stream = st.empty()
st.header("Traffic Rate")
rate_chart = st.empty()
st.header("Daily Traffic")
daily_chart = st.empty()
return img_stream, rate_chart, daily_chart
async def update_video_stream(img, subscriber):
async for frame in subscriber.frames():
img.image(frame, channels="BGR")
async def update_rate_chart(chart, subscriber):
asyncio.create_task(subscriber.updates())
x, y = ("time", "vehicles per second")
df = pd.DataFrame(columns=[x, y])
while True:
now, rate = subscriber.vehicle_rate()
df = pd.concat([df, pd.DataFrame([[now, rate]], columns=[x, y])])
c = alt.Chart(df).mark_line().encode(
x=x,
y=y,
).properties(
width=800,
height=400,
)
chart.altair_chart(c, use_container_width=True)
await asyncio.sleep(5)
async def update_daily_chart(chart, subscriber):
while True:
days = await subscriber.daily_counts()
x,y = ("day", "total vehicles")
df = pd.DataFrame(days, columns=[x, y])
c = alt.Chart(df).mark_bar().encode(
x=x,
y=y,
).properties(
width=800,
height=400,
)
chart.altair_chart(c, use_container_width=True)
await asyncio.sleep(60)
async def app(subscriber):
img, rate, daily = create_layout()
tasks = []
tasks.append(asyncio.create_task(update_video_stream(img, subscriber)))
tasks.append(asyncio.create_task(update_rate_chart(rate, subscriber)))
tasks.append(asyncio.create_task(update_daily_chart(daily, subscriber)))
await asyncio.gather(*tasks)
if __name__ == "__main__":
parser = ArgumentParser(
description="Demo app that subscribes to real-time traffic updates and displays a real-time dashboard."
)
parser.add_argument(
"-t",
"--topic",
default="figure-updates-json",
help="Ensign topic to subscribe to",
)
parser.add_argument(
"-c",
"--cred-path",
default="",
help="Path to Ensign credentials",
required=True,
)
subscriber = TrafficSubscriber(**vars(parser.parse_args()))
asyncio.run(app(subscriber))