Raffael-Kultyshev commited on
Commit
3403b3b
·
1 Parent(s): 99e35a0

Use exact reference structure: elem_classes plot-html + CSS + theme

Browse files
Files changed (1) hide show
  1. app.py +66 -44
app.py CHANGED
@@ -7,6 +7,21 @@ import plotly.io as pio
7
  DATA_DIR = Path(__file__).parent / "data"
8
  video_path = DATA_DIR / "video.mp4"
9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  def load_data():
11
  metadata = {}
12
  end_effector = {}
@@ -22,15 +37,15 @@ def load_data():
22
  print(f"Error loading end_effector: {e}")
23
  return metadata, end_effector
24
 
25
- def build_plot(metadata, end_effector, hand, metric):
26
  try:
27
  fps = metadata.get('fps', 60)
28
  if not end_effector:
29
- return "<div style='padding:20px;'>No data available</div>"
30
 
31
  frame_keys = sorted([int(k) for k in end_effector.keys() if str(k).isdigit()])
32
  if not frame_keys:
33
- return "<div style='padding:20px;'>No frames found</div>"
34
 
35
  times = [i/fps for i in frame_keys]
36
  values = []
@@ -53,26 +68,23 @@ def build_plot(metadata, end_effector, hand, metric):
53
  values.append(pose[4] * 57.3)
54
  elif metric == "yaw_deg":
55
  values.append(pose[5] * 57.3)
56
- else:
57
- values.append(0)
58
  else:
59
  values.append(None)
60
  else:
61
  values.append(None)
62
 
63
- # Filter out None values for plotting
64
  valid_times = [t for t, v in zip(times, values) if v is not None]
65
  valid_values = [v for v in values if v is not None]
66
 
67
  if not valid_times or not valid_values:
68
- return f"<div style='padding:20px;'>No valid data for {hand} hand {metric}</div>"
69
 
70
  fig = go.Figure()
71
  fig.add_trace(go.Scatter(
72
  x=valid_times,
73
  y=valid_values,
74
  mode="lines",
75
- name=f"{hand.title()} {metric}",
76
  line=dict(width=2)
77
  ))
78
  fig.update_layout(
@@ -82,60 +94,70 @@ def build_plot(metadata, end_effector, hand, metric):
82
  xaxis_title="Time (s)",
83
  yaxis_title=metric.replace("_", " ").title()
84
  )
85
- html_str = pio.to_html(fig, include_plotlyjs="cdn", full_html=False)
86
- return f"<div style='width:100%; height:300px;'>{html_str}</div>"
87
  except Exception as e:
88
- return f"<div style='padding:20px; color:red;'>Error: {str(e)}</div>"
89
 
90
- # Load data at module level
91
  metadata, end_effector = load_data()
92
  total_frames = len(metadata.get('poses', []))
93
  fps = metadata.get('fps', 60)
94
 
95
- # Build all plots at module level
96
- left_plots = {
97
- "x_cm": build_plot(metadata, end_effector, "left", "x_cm"),
98
- "y_cm": build_plot(metadata, end_effector, "left", "y_cm"),
99
- "z_cm": build_plot(metadata, end_effector, "left", "z_cm"),
100
- "roll_deg": build_plot(metadata, end_effector, "left", "roll_deg"),
101
- "pitch_deg": build_plot(metadata, end_effector, "left", "pitch_deg"),
102
- "yaw_deg": build_plot(metadata, end_effector, "left", "yaw_deg"),
103
  }
104
 
105
- right_plots = {
106
- "x_cm": build_plot(metadata, end_effector, "right", "x_cm"),
107
- "y_cm": build_plot(metadata, end_effector, "right", "y_cm"),
108
- "z_cm": build_plot(metadata, end_effector, "right", "z_cm"),
109
- "roll_deg": build_plot(metadata, end_effector, "right", "roll_deg"),
110
- "pitch_deg": build_plot(metadata, end_effector, "right", "pitch_deg"),
111
- "yaw_deg": build_plot(metadata, end_effector, "right", "yaw_deg"),
112
  }
113
 
114
- with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
115
  gr.Markdown("# 🤖 Dynamic Intelligence - Human Demo Visualizer")
116
  gr.Markdown(f"**Frames:** {total_frames:,} | **FPS:** {fps}")
117
 
118
  gr.Video(value=str(video_path) if video_path.exists() else None, height=360)
119
 
120
  gr.Markdown("### Left Hand Trajectories")
121
- with gr.Row():
122
- gr.HTML(left_plots["x_cm"])
123
- gr.HTML(left_plots["y_cm"])
124
- gr.HTML(left_plots["z_cm"])
125
- with gr.Row():
126
- gr.HTML(left_plots["roll_deg"])
127
- gr.HTML(left_plots["pitch_deg"])
128
- gr.HTML(left_plots["yaw_deg"])
 
129
 
130
  gr.Markdown("### Right Hand Trajectories")
131
- with gr.Row():
132
- gr.HTML(right_plots["x_cm"])
133
- gr.HTML(right_plots["y_cm"])
134
- gr.HTML(right_plots["z_cm"])
135
- with gr.Row():
136
- gr.HTML(right_plots["roll_deg"])
137
- gr.HTML(right_plots["pitch_deg"])
138
- gr.HTML(right_plots["yaw_deg"])
 
139
 
140
  if __name__ == "__main__":
141
  demo.launch()
 
7
  DATA_DIR = Path(__file__).parent / "data"
8
  video_path = DATA_DIR / "video.mp4"
9
 
10
+ CUSTOM_CSS = """
11
+ .plot-html {
12
+ background: #111a2c;
13
+ border-radius: 12px;
14
+ padding: 10px;
15
+ border: 1px solid #1f2b47;
16
+ min-height: 320px;
17
+ }
18
+ .plot-html iframe {
19
+ width: 100%;
20
+ height: 300px;
21
+ border: none;
22
+ }
23
+ """
24
+
25
  def load_data():
26
  metadata = {}
27
  end_effector = {}
 
37
  print(f"Error loading end_effector: {e}")
38
  return metadata, end_effector
39
 
40
+ def build_plot_html(metadata, end_effector, hand, metric):
41
  try:
42
  fps = metadata.get('fps', 60)
43
  if not end_effector:
44
+ return "<p>No data</p>"
45
 
46
  frame_keys = sorted([int(k) for k in end_effector.keys() if str(k).isdigit()])
47
  if not frame_keys:
48
+ return "<p>No frames</p>"
49
 
50
  times = [i/fps for i in frame_keys]
51
  values = []
 
68
  values.append(pose[4] * 57.3)
69
  elif metric == "yaw_deg":
70
  values.append(pose[5] * 57.3)
 
 
71
  else:
72
  values.append(None)
73
  else:
74
  values.append(None)
75
 
 
76
  valid_times = [t for t, v in zip(times, values) if v is not None]
77
  valid_values = [v for v in values if v is not None]
78
 
79
  if not valid_times or not valid_values:
80
+ return "<p>No valid data</p>"
81
 
82
  fig = go.Figure()
83
  fig.add_trace(go.Scatter(
84
  x=valid_times,
85
  y=valid_values,
86
  mode="lines",
87
+ name=f"{hand} {metric}",
88
  line=dict(width=2)
89
  ))
90
  fig.update_layout(
 
94
  xaxis_title="Time (s)",
95
  yaxis_title=metric.replace("_", " ").title()
96
  )
97
+ return pio.to_html(fig, include_plotlyjs="cdn", full_html=False)
 
98
  except Exception as e:
99
+ return f"<p>Error: {str(e)}</p>"
100
 
101
+ # Load data
102
  metadata, end_effector = load_data()
103
  total_frames = len(metadata.get('poses', []))
104
  fps = metadata.get('fps', 60)
105
 
106
+ # Build plots
107
+ left_figs = {
108
+ "x_cm": build_plot_html(metadata, end_effector, "left", "x_cm"),
109
+ "y_cm": build_plot_html(metadata, end_effector, "left", "y_cm"),
110
+ "z_cm": build_plot_html(metadata, end_effector, "left", "z_cm"),
111
+ "roll_deg": build_plot_html(metadata, end_effector, "left", "roll_deg"),
112
+ "pitch_deg": build_plot_html(metadata, end_effector, "left", "pitch_deg"),
113
+ "yaw_deg": build_plot_html(metadata, end_effector, "left", "yaw_deg"),
114
  }
115
 
116
+ right_figs = {
117
+ "x_cm": build_plot_html(metadata, end_effector, "right", "x_cm"),
118
+ "y_cm": build_plot_html(metadata, end_effector, "right", "y_cm"),
119
+ "z_cm": build_plot_html(metadata, end_effector, "right", "z_cm"),
120
+ "roll_deg": build_plot_html(metadata, end_effector, "right", "roll_deg"),
121
+ "pitch_deg": build_plot_html(metadata, end_effector, "right", "pitch_deg"),
122
+ "yaw_deg": build_plot_html(metadata, end_effector, "right", "yaw_deg"),
123
  }
124
 
125
+ theme = gr.themes.Soft(
126
+ primary_hue="cyan", secondary_hue="blue", neutral_hue="slate"
127
+ ).set(
128
+ body_background_fill="#0c1424",
129
+ body_text_color="#f8fafc",
130
+ block_background_fill="#111a2c",
131
+ border_color_primary="#1f2b47",
132
+ )
133
+
134
+ with gr.Blocks(theme=theme, css=CUSTOM_CSS) as demo:
135
  gr.Markdown("# 🤖 Dynamic Intelligence - Human Demo Visualizer")
136
  gr.Markdown(f"**Frames:** {total_frames:,} | **FPS:** {fps}")
137
 
138
  gr.Video(value=str(video_path) if video_path.exists() else None, height=360)
139
 
140
  gr.Markdown("### Left Hand Trajectories")
141
+ with gr.Column():
142
+ with gr.Row():
143
+ gr.HTML(value=left_figs["x_cm"], elem_classes=["plot-html"])
144
+ gr.HTML(value=left_figs["y_cm"], elem_classes=["plot-html"])
145
+ gr.HTML(value=left_figs["z_cm"], elem_classes=["plot-html"])
146
+ with gr.Row():
147
+ gr.HTML(value=left_figs["roll_deg"], elem_classes=["plot-html"])
148
+ gr.HTML(value=left_figs["pitch_deg"], elem_classes=["plot-html"])
149
+ gr.HTML(value=left_figs["yaw_deg"], elem_classes=["plot-html"])
150
 
151
  gr.Markdown("### Right Hand Trajectories")
152
+ with gr.Column():
153
+ with gr.Row():
154
+ gr.HTML(value=right_figs["x_cm"], elem_classes=["plot-html"])
155
+ gr.HTML(value=right_figs["y_cm"], elem_classes=["plot-html"])
156
+ gr.HTML(value=right_figs["z_cm"], elem_classes=["plot-html"])
157
+ with gr.Row():
158
+ gr.HTML(value=right_figs["roll_deg"], elem_classes=["plot-html"])
159
+ gr.HTML(value=right_figs["pitch_deg"], elem_classes=["plot-html"])
160
+ gr.HTML(value=right_figs["yaw_deg"], elem_classes=["plot-html"])
161
 
162
  if __name__ == "__main__":
163
  demo.launch()