ArthurPch commited on
Commit
be73265
·
1 Parent(s): 8936219

ajout du bouton arret

Browse files
reachy_mini_hello_world/main.py CHANGED
@@ -37,33 +37,29 @@ class ReachyMiniHelloWorld(ReachyMiniApp):
37
  "selected_video": None,
38
  "playback_state": "playing",
39
  "playback_time_sync": 0.0,
40
- "playback_sync_flag": False
 
41
  }
42
 
43
  def pipeline_analyse(filename, delai_danse=2.0):
 
44
  status["analysis_busy"] = True
45
  status["analysis_file"] = filename
46
-
47
- analyse_status = {"progress": 0, "phase": "analyzing"}
48
-
49
- def sync_status():
50
- while status["analysis_busy"]:
51
- status["analysis_progress"] = analyse_status.get("progress", 0)
52
- time.sleep(0.2)
53
-
54
- threading.Thread(target=sync_status, daemon=True).start()
55
 
56
  try:
57
  video_cible = DATA_DIR / filename
58
- analyser_video(str(video_cible), str(MODEL), status=analyse_status, delai_danse=delai_danse)
59
  except Exception as e:
60
  pass
61
  finally:
62
  status["analysis_busy"] = False
63
  status["analysis_file"] = None
64
  status["analysis_progress"] = 0
 
65
 
66
  def pipeline_replay():
 
67
  status["robot_busy"] = True
68
  status["playback_state"] = "playing"
69
  status["playback_sync_flag"] = False
@@ -76,6 +72,7 @@ class ReachyMiniHelloWorld(ReachyMiniApp):
76
  pass
77
  finally:
78
  status["robot_busy"] = False
 
79
 
80
  @self.settings_app.get("/status")
81
  async def get_status():
@@ -140,6 +137,11 @@ class ReachyMiniHelloWorld(ReachyMiniApp):
140
  status["playback_sync_flag"] = True
141
  return {"synced": True}
142
 
 
 
 
 
 
143
  @self.settings_app.post("/upload_video")
144
  async def upload_video(file: UploadFile = File(...), delai_danse_secondes: float = 2.0):
145
  if not file.filename.lower().endswith(".mp4"):
 
37
  "selected_video": None,
38
  "playback_state": "playing",
39
  "playback_time_sync": 0.0,
40
+ "playback_sync_flag": False,
41
+ "stop_requested": False
42
  }
43
 
44
  def pipeline_analyse(filename, delai_danse=2.0):
45
+ status["stop_requested"] = False
46
  status["analysis_busy"] = True
47
  status["analysis_file"] = filename
48
+ status["analysis_progress"] = 0
 
 
 
 
 
 
 
 
49
 
50
  try:
51
  video_cible = DATA_DIR / filename
52
+ analyser_video(str(video_cible), str(MODEL), status=status, delai_danse=delai_danse)
53
  except Exception as e:
54
  pass
55
  finally:
56
  status["analysis_busy"] = False
57
  status["analysis_file"] = None
58
  status["analysis_progress"] = 0
59
+ status["stop_requested"] = False
60
 
61
  def pipeline_replay():
62
+ status["stop_requested"] = False
63
  status["robot_busy"] = True
64
  status["playback_state"] = "playing"
65
  status["playback_sync_flag"] = False
 
72
  pass
73
  finally:
74
  status["robot_busy"] = False
75
+ status["stop_requested"] = False
76
 
77
  @self.settings_app.get("/status")
78
  async def get_status():
 
137
  status["playback_sync_flag"] = True
138
  return {"synced": True}
139
 
140
+ @self.settings_app.post("/stop")
141
+ async def stop_pipeline():
142
+ status["stop_requested"] = True
143
+ return {"stopped": True}
144
+
145
  @self.settings_app.post("/upload_video")
146
  async def upload_video(file: UploadFile = File(...), delai_danse_secondes: float = 2.0):
147
  if not file.filename.lower().endswith(".mp4"):
reachy_mini_hello_world/static/index.html CHANGED
@@ -55,6 +55,7 @@
55
 
56
  <div class="action-group">
57
  <button id="action-btn" class="btn-analyser" data-action="analyser">⚙ Analyser</button>
 
58
  </div>
59
 
60
  <div id="status-container">
@@ -69,7 +70,7 @@
69
  <div id="video-placeholder">
70
  <span>Prêt pour l'analyse...</span>
71
  </div>
72
- <video id="video-player" controls playsinline style="display: none"></video>
73
  </div>
74
  </div>
75
 
 
55
 
56
  <div class="action-group">
57
  <button id="action-btn" class="btn-analyser" data-action="analyser">⚙ Analyser</button>
58
+ <button id="stop-btn" class="btn-stop" style="display: none;">⏹ Stop</button>
59
  </div>
60
 
61
  <div id="status-container">
 
70
  <div id="video-placeholder">
71
  <span>Prêt pour l'analyse...</span>
72
  </div>
73
+ <video id="video-player" controls playsinline muted style="display: none"></video>
74
  </div>
75
  </div>
76
 
reachy_mini_hello_world/static/main.js CHANGED
@@ -1,5 +1,6 @@
1
  let selectedVideoReady = false;
2
  let player;
 
3
 
4
  document.addEventListener('DOMContentLoaded', () => {
5
  const savedTheme = localStorage.getItem('theme');
@@ -25,6 +26,7 @@ document.addEventListener('DOMContentLoaded', () => {
25
  });
26
 
27
  player.addEventListener("ended", () => {
 
28
  document.querySelector('.logo-img').src = "/static/image/reachy_logo.png";
29
  document.querySelector('.logo-container').classList.remove('is-happy');
30
  });
@@ -193,6 +195,25 @@ document.getElementById("action-btn").addEventListener("click", async () => {
193
  }
194
  });
195
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
196
  document.getElementById("upload-input").addEventListener("change", async (e) => {
197
  const file = e.target.files[0];
198
  if (!file) return;
@@ -255,16 +276,34 @@ setInterval(async () => {
255
  const btn = document.getElementById("action-btn");
256
  const select = document.getElementById("video-select");
257
  const blink = document.getElementById("blink");
 
 
 
 
 
 
 
 
 
 
 
 
 
 
258
  if (data.analysis_busy || data.robot_busy) {
259
  blink.className = "status-indicator active";
 
260
  } else {
261
  blink.className = "status-indicator";
 
262
  }
 
263
  if (data.analysis_busy) {
264
  setStatus(data.analysis_progress > 0 ? data.analysis_progress : null);
265
  } else {
266
  setStatus(undefined);
267
  }
 
268
  if (data.robot_busy) {
269
  btn.disabled = true;
270
  select.disabled = true;
 
1
  let selectedVideoReady = false;
2
  let player;
3
+ let isStopping = false;
4
 
5
  document.addEventListener('DOMContentLoaded', () => {
6
  const savedTheme = localStorage.getItem('theme');
 
26
  });
27
 
28
  player.addEventListener("ended", () => {
29
+ document.querySelector('.app-layout').classList.remove('hide-3d');
30
  document.querySelector('.logo-img').src = "/static/image/reachy_logo.png";
31
  document.querySelector('.logo-container').classList.remove('is-happy');
32
  });
 
195
  }
196
  });
197
 
198
+ document.getElementById("stop-btn").addEventListener("click", async () => {
199
+ isStopping = true;
200
+ const stopBtn = document.getElementById("stop-btn");
201
+ stopBtn.disabled = true;
202
+ stopBtn.textContent = "Arrêt en cours...";
203
+
204
+ if (player) {
205
+ player.pause();
206
+ player.currentTime = 0;
207
+ }
208
+
209
+ document.querySelector('.app-layout').classList.remove('hide-3d');
210
+ document.querySelector('.logo-img').src = "/static/image/reachy_logo.png";
211
+ document.querySelector('.logo-container').classList.remove('is-happy');
212
+ document.getElementById("progress-bg").style.display = "none";
213
+
214
+ await fetch("/stop", { method: "POST" });
215
+ });
216
+
217
  document.getElementById("upload-input").addEventListener("change", async (e) => {
218
  const file = e.target.files[0];
219
  if (!file) return;
 
276
  const btn = document.getElementById("action-btn");
277
  const select = document.getElementById("video-select");
278
  const blink = document.getElementById("blink");
279
+ const stopBtn = document.getElementById("stop-btn");
280
+
281
+ if (isStopping) {
282
+ if (!data.analysis_busy && !data.robot_busy) {
283
+ isStopping = false;
284
+ stopBtn.style.display = "none";
285
+ stopBtn.disabled = false;
286
+ stopBtn.textContent = "⏹ Stop";
287
+ blink.className = "status-indicator";
288
+ } else {
289
+ return;
290
+ }
291
+ }
292
+
293
  if (data.analysis_busy || data.robot_busy) {
294
  blink.className = "status-indicator active";
295
+ stopBtn.style.display = "block";
296
  } else {
297
  blink.className = "status-indicator";
298
+ stopBtn.style.display = "none";
299
  }
300
+
301
  if (data.analysis_busy) {
302
  setStatus(data.analysis_progress > 0 ? data.analysis_progress : null);
303
  } else {
304
  setStatus(undefined);
305
  }
306
+
307
  if (data.robot_busy) {
308
  btn.disabled = true;
309
  select.disabled = true;
reachy_mini_hello_world/static/style.css CHANGED
@@ -374,6 +374,31 @@ body {
374
  color: var(--text-muted);
375
  }
376
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
377
  #status-container {
378
  width: 100%;
379
  }
@@ -399,7 +424,7 @@ body {
399
  animation: progressStripes 1s linear infinite;
400
  }
401
 
402
- @keyframes progressStripes {
403
  from { background-position: 1.5rem 0; }
404
  to { background-position: 0 0; }
405
  }
 
374
  color: var(--text-muted);
375
  }
376
 
377
+ .btn-stop {
378
+ width: 100%;
379
+ padding: 16px 24px;
380
+ border: none;
381
+ border-radius: 10px;
382
+ font-size: 1.05rem;
383
+ font-weight: 700;
384
+ cursor: pointer;
385
+ transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
386
+ color: white;
387
+ background-color: #ef4444;
388
+ box-shadow: 0 6px 16px rgba(239, 68, 68, 0.3);
389
+ margin-top: 10px;
390
+ }
391
+
392
+ .btn-stop:hover {
393
+ background-color: #dc2626;
394
+ transform: translateY(-3px);
395
+ box-shadow: 0 10px 20px rgba(239, 68, 68, 0.4);
396
+ }
397
+
398
+ .btn-stop:active {
399
+ transform: scale(0.97) translateY(0);
400
+ }
401
+
402
  #status-container {
403
  width: 100%;
404
  }
 
424
  animation: progressStripes 1s linear infinite;
425
  }
426
 
427
+ @keywords progressStripes {
428
  from { background-position: 1.5rem 0; }
429
  to { background-position: 0 0; }
430
  }
scripts/play_json.py CHANGED
@@ -10,6 +10,7 @@ from mediapipe.tasks import python
10
  from mediapipe.tasks.python import vision
11
  from reachy_mini.utils import create_head_pose
12
  from moviepy import VideoFileClip
 
13
 
14
  from scripts.generation_mouvement import trouver_emotion, calculer_cibles_antennes, adoucir_mouvement, calculer_danse_antennes
15
 
@@ -78,7 +79,7 @@ def sauvegarder_fichiers(video_file, fps, frame_idx, visages_detectes, historiqu
78
  with open(fichier_mouvements, 'w', encoding='utf-8') as f:
79
  json.dump({"mouvements": historique_mouvements}, f, indent=4)
80
 
81
- def _fusionner_audio_video(chemin_video_traitee, chemin_video_source, chemin_sortie):
82
  try:
83
  video_clip = VideoFileClip(chemin_video_traitee)
84
  source_clip = VideoFileClip(chemin_video_source)
@@ -89,6 +90,7 @@ def _fusionner_audio_video(chemin_video_traitee, chemin_video_source, chemin_sor
89
  audio = audio.subclipped(0, video_clip.duration)
90
  final = video_clip.with_audio(audio)
91
  final.write_videofile(chemin_sortie, codec='libx264', audio_codec='aac', logger=None)
 
92
  final.close()
93
  else:
94
  video_clip.write_videofile(chemin_sortie, codec='libx264', logger=None)
@@ -124,10 +126,11 @@ def analyser_video(video_path, model_path, status=None, delai_danse=2.0):
124
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
125
  writer = cv2.VideoWriter(tmp_video_path, fourcc, fps, (out_w, out_h))
126
  output_video = DATA_DIR / f"{video_file.stem}_processed.mp4"
 
127
 
128
  if status:
129
  status["phase"] = "analyzing"
130
- status["progress"] = 0
131
 
132
  etat_robot = {
133
  "tete_p": 0.0, "tete_y": 0.0, "tete_r": 0.0,
@@ -148,6 +151,9 @@ def analyser_video(video_path, model_path, status=None, delai_danse=2.0):
148
 
149
  try:
150
  while cap.isOpened():
 
 
 
151
  ret, frame = cap.read()
152
  if not ret:
153
  break
@@ -227,7 +233,7 @@ def analyser_video(video_path, model_path, status=None, delai_danse=2.0):
227
  frame_idx += 1
228
 
229
  if total_frames > 0 and status:
230
- status["progress"] = int((frame_idx / total_frames) * 100)
231
 
232
  except Exception as e:
233
  if status:
@@ -237,9 +243,18 @@ def analyser_video(video_path, model_path, status=None, delai_danse=2.0):
237
  writer.release()
238
  cap.release()
239
 
 
 
 
 
 
 
 
 
 
240
  if status:
241
  status["phase"] = "encoding"
242
- _fusionner_audio_video(tmp_video_path, str(video_path), str(output_video))
243
  try:
244
  Path(tmp_video_path).unlink()
245
  except Exception:
@@ -248,7 +263,7 @@ def analyser_video(video_path, model_path, status=None, delai_danse=2.0):
248
  historique_signaux, historique_mouvements)
249
  if status:
250
  status["phase"] = "ready"
251
- status["progress"] = 100
252
 
253
  def rejouer_mouvements(chemin_mouvement, robot, status=None):
254
  with open(chemin_mouvement, 'r', encoding='utf-8') as f:
@@ -258,26 +273,56 @@ def rejouer_mouvements(chemin_mouvement, robot, status=None):
258
  if not mouvements:
259
  return
260
 
 
 
 
 
 
 
 
 
 
 
 
 
 
261
  debut = time.time()
262
  temps_ecoule = 0
263
  idx = 0
 
264
 
265
  try:
266
  while idx < len(mouvements):
 
 
 
267
  if status and status.get("playback_sync_flag"):
268
  status["playback_sync_flag"] = False
269
  temps_ecoule = status.get("playback_time_sync", temps_ecoule)
270
  debut = time.time() - temps_ecoule
271
 
 
 
 
 
 
 
272
  idx = 0
273
  while idx < len(mouvements) and mouvements[idx]["timestamp"] < temps_ecoule:
274
  idx += 1
275
  continue
276
 
277
  if status and status.get("playback_state") == "paused":
 
 
 
278
  time.sleep(0.05)
279
  debut = time.time() - temps_ecoule
280
  continue
 
 
 
 
281
 
282
  temps_ecoule = time.time() - debut
283
 
@@ -307,5 +352,16 @@ def rejouer_mouvements(chemin_mouvement, robot, status=None):
307
  idx += 1
308
 
309
  finally:
310
- robot.goto_sleep()
311
- robot.disable_motors()
 
 
 
 
 
 
 
 
 
 
 
 
10
  from mediapipe.tasks.python import vision
11
  from reachy_mini.utils import create_head_pose
12
  from moviepy import VideoFileClip
13
+ import pygame
14
 
15
  from scripts.generation_mouvement import trouver_emotion, calculer_cibles_antennes, adoucir_mouvement, calculer_danse_antennes
16
 
 
79
  with open(fichier_mouvements, 'w', encoding='utf-8') as f:
80
  json.dump({"mouvements": historique_mouvements}, f, indent=4)
81
 
82
+ def _fusionner_audio_video(chemin_video_traitee, chemin_video_source, chemin_sortie, chemin_audio):
83
  try:
84
  video_clip = VideoFileClip(chemin_video_traitee)
85
  source_clip = VideoFileClip(chemin_video_source)
 
90
  audio = audio.subclipped(0, video_clip.duration)
91
  final = video_clip.with_audio(audio)
92
  final.write_videofile(chemin_sortie, codec='libx264', audio_codec='aac', logger=None)
93
+ audio.write_audiofile(chemin_audio, logger=None)
94
  final.close()
95
  else:
96
  video_clip.write_videofile(chemin_sortie, codec='libx264', logger=None)
 
126
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
127
  writer = cv2.VideoWriter(tmp_video_path, fourcc, fps, (out_w, out_h))
128
  output_video = DATA_DIR / f"{video_file.stem}_processed.mp4"
129
+ output_audio = DATA_DIR / f"{video_file.stem}_audio.wav"
130
 
131
  if status:
132
  status["phase"] = "analyzing"
133
+ status["analysis_progress"] = 0
134
 
135
  etat_robot = {
136
  "tete_p": 0.0, "tete_y": 0.0, "tete_r": 0.0,
 
151
 
152
  try:
153
  while cap.isOpened():
154
+ if status and status.get("stop_requested"):
155
+ break
156
+
157
  ret, frame = cap.read()
158
  if not ret:
159
  break
 
233
  frame_idx += 1
234
 
235
  if total_frames > 0 and status:
236
+ status["analysis_progress"] = int((frame_idx / total_frames) * 100)
237
 
238
  except Exception as e:
239
  if status:
 
243
  writer.release()
244
  cap.release()
245
 
246
+ if status and status.get("stop_requested"):
247
+ try:
248
+ Path(tmp_video_path).unlink()
249
+ except Exception:
250
+ pass
251
+ status["phase"] = "ready"
252
+ status["analysis_progress"] = 0
253
+ return
254
+
255
  if status:
256
  status["phase"] = "encoding"
257
+ _fusionner_audio_video(tmp_video_path, str(video_path), str(output_video), str(output_audio))
258
  try:
259
  Path(tmp_video_path).unlink()
260
  except Exception:
 
263
  historique_signaux, historique_mouvements)
264
  if status:
265
  status["phase"] = "ready"
266
+ status["analysis_progress"] = 100
267
 
268
  def rejouer_mouvements(chemin_mouvement, robot, status=None):
269
  with open(chemin_mouvement, 'r', encoding='utf-8') as f:
 
273
  if not mouvements:
274
  return
275
 
276
+ if not pygame.mixer.get_init():
277
+ pygame.mixer.init()
278
+
279
+ chemin_audio = str(Path(chemin_mouvement).parent / f"{Path(chemin_mouvement).stem.replace('_movement', '_audio')}.wav")
280
+ a_du_son = Path(chemin_audio).exists()
281
+
282
+ if a_du_son:
283
+ try:
284
+ pygame.mixer.music.load(chemin_audio)
285
+ pygame.mixer.music.play()
286
+ except Exception:
287
+ a_du_son = False
288
+
289
  debut = time.time()
290
  temps_ecoule = 0
291
  idx = 0
292
+ est_en_pause = False
293
 
294
  try:
295
  while idx < len(mouvements):
296
+ if status and status.get("stop_requested"):
297
+ break
298
+
299
  if status and status.get("playback_sync_flag"):
300
  status["playback_sync_flag"] = False
301
  temps_ecoule = status.get("playback_time_sync", temps_ecoule)
302
  debut = time.time() - temps_ecoule
303
 
304
+ if a_du_son:
305
+ try:
306
+ pygame.mixer.music.set_pos(temps_ecoule)
307
+ except Exception:
308
+ pass
309
+
310
  idx = 0
311
  while idx < len(mouvements) and mouvements[idx]["timestamp"] < temps_ecoule:
312
  idx += 1
313
  continue
314
 
315
  if status and status.get("playback_state") == "paused":
316
+ if a_du_son and not est_en_pause:
317
+ pygame.mixer.music.pause()
318
+ est_en_pause = True
319
  time.sleep(0.05)
320
  debut = time.time() - temps_ecoule
321
  continue
322
+ else:
323
+ if a_du_son and est_en_pause:
324
+ pygame.mixer.music.unpause()
325
+ est_en_pause = False
326
 
327
  temps_ecoule = time.time() - debut
328
 
 
352
  idx += 1
353
 
354
  finally:
355
+ if a_du_son:
356
+ pygame.mixer.music.stop()
357
+ if status:
358
+ status["stop_requested"] = False
359
+
360
+ try:
361
+ robot.goto_sleep()
362
+ except:
363
+ pass
364
+ try:
365
+ robot.disable_motors()
366
+ except:
367
+ pass