lucasfiotti commited on
Commit
8afa347
·
1 Parent(s): a5ec907

Simulation synchrone avec le lancement de l'interface + bouton supprimer + impossible de mettre deux noms identiques à une vidéo

Browse files
coquille/Jalon2OPTI.py CHANGED
@@ -30,9 +30,10 @@ else:
30
 
31
  dossier = os.path.dirname(os.path.abspath(sys.argv[0]))
32
 
33
- output_video_path = os.path.join(dossier, f"{video_name}(landmarks){video_ext}")
34
- output_csv_path = os.path.join(dossier, f"{video_name}.csv")
35
- temp_video_path = os.path.join(dossier, f"{video_name}_temp_raw.mp4")
 
36
 
37
  print(f"Vidéo d'entrée : {video_path}")
38
  print(f"CSV de sortie : {output_csv_path}")
@@ -55,16 +56,16 @@ options = vision.FaceLandmarkerOptions(
55
  )
56
  detector = vision.FaceLandmarker.create_from_options(options)
57
 
58
- cap = cv2.VideoCapture(video_path)
59
- fps = cap.get(cv2.CAP_PROP_FPS)
60
- frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
61
  frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
62
- frame_index = 0
63
 
64
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
65
- out = cv2.VideoWriter(temp_video_path, fourcc, fps, (frame_width, frame_height))
66
 
67
- csv_file = open(output_csv_path, "w", newline="", encoding="utf-8")
68
  csv_writer = csv.writer(csv_file)
69
  csv_writer.writerow(["timestamp", "bouche", "oeil_g", "oeil_d", "pitch", "yaw", "roll", "norm_x", "norm_y"])
70
 
@@ -84,7 +85,8 @@ def compute_head_pose(landmarks, frame_w, frame_h):
84
  indices = [1, 152, 33, 263, 61, 291]
85
  image_points = np.array([[landmarks[i].x * frame_w, landmarks[i].y * frame_h] for i in indices], dtype=np.float64)
86
  camera_matrix, dist_coeffs = generic_optic(frame_w, frame_h)
87
- success, rotation_vec, translation_vec = cv2.solvePnP(model_points, image_points, camera_matrix, dist_coeffs, flags=cv2.SOLVEPNP_ITERATIVE)
 
88
  if not success:
89
  return None, None, None, None, None
90
  rotation_mat, _ = cv2.Rodrigues(rotation_vec)
@@ -106,7 +108,7 @@ def compute_face_position(landmarks, frame_w, frame_h):
106
  face_h = int((y_max - y_min) * frame_h)
107
  face_size_ratio = ((x_max - x_min) * (y_max - y_min)) ** 0.5
108
  return center_x, center_y, norm_x, norm_y, face_w, face_h, face_size_ratio, \
109
- (int(x_min * frame_w), int(y_min * frame_h), int(x_max * frame_w), int(y_max * frame_h))
110
 
111
 
112
  def draw_axes(frame, rotation_vec, translation_vec, camera_matrix, dist_coeffs, origin):
@@ -127,28 +129,29 @@ while True:
127
  if not ret:
128
  break
129
 
130
- timestamp = frame_index / fps
131
  frame_index += 1
132
- h, w, _ = frame.shape
133
 
134
  rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
135
- mp_image = Image(image_format=ImageFormat.SRGB, data=rgb_frame)
136
- results = detector.detect_for_video(mp_image, int(timestamp * 1000))
137
 
138
  if results.face_landmarks:
139
  landmarks = results.face_landmarks[0]
140
- mouth_open = abs(landmarks[13].y - landmarks[14].y)
141
- left_eye_open = abs(landmarks[159].y - landmarks[145].y)
142
  right_eye_open = abs(landmarks[386].y - landmarks[374].y)
143
  pitch, yaw, roll, rot_vec, trans_vec = compute_head_pose(landmarks, w, h)
144
- center_x, center_y, norm_x, norm_y, face_w, face_h, face_size_ratio, bbox = compute_face_position(landmarks, w, h)
 
145
 
146
  if pitch is not None:
147
- yaw = -yaw
148
  pitch = ((pitch + 180) % 360)
149
- roll = max(-40, min(40, roll))
150
  pitch = max(-40, min(40, pitch))
151
- yaw = max(-180, min(180, yaw))
152
 
153
  list_timestamp.append(round(timestamp, 2))
154
  list_mouth_open.append(round(mouth_open, 3))
@@ -167,7 +170,8 @@ while True:
167
  cv2.drawMarker(frame, (w // 2, h // 2), (128, 128, 128), cv2.MARKER_CROSS, 20, 1)
168
  if rot_vec is not None:
169
  camera_matrix, dist_coeffs = generic_optic(w, h)
170
- draw_axes(frame, rot_vec, trans_vec, camera_matrix, dist_coeffs, (int(landmarks[1].x * w), int(landmarks[1].y * h)))
 
171
  elif list_mouth_open:
172
  list_timestamp.append(round(timestamp, 2))
173
  list_mouth_open.append(list_mouth_open[-1])
@@ -200,13 +204,15 @@ if not list_mouth_open:
200
  csv_file.close()
201
  sys.exit(1)
202
 
 
203
  def norm_list(lst, mi, ma, n_mi, n_ma):
204
  return [((i - mi) / (ma - mi)) * (n_ma - n_mi) + n_mi if ma != mi else 0.0 for i in lst]
205
 
206
- list_mouth_open = norm_list(list_mouth_open, min(list_mouth_open), max(list_mouth_open), -0.01, 0.01)
207
- list_norm_x = norm_list(list_norm_x, min(list_norm_x), max(list_norm_x), -0.03, 0.03)
208
- list_norm_y = norm_list(list_norm_y, min(list_norm_y), max(list_norm_y), -0.0075, 0.0075)
209
- list_left_eye_open = norm_list(list_left_eye_open, min(list_left_eye_open), max(list_left_eye_open), 0, -50.0)
 
210
  list_right_eye_open = norm_list(list_right_eye_open, min(list_right_eye_open), max(list_right_eye_open), 0, 50.0)
211
 
212
  for i in range(len(list_timestamp)):
@@ -235,28 +241,26 @@ if not ffmpeg_cmd:
235
  break
236
 
237
  if ffmpeg_cmd:
 
 
238
  result = subprocess.run(
239
  [ffmpeg_cmd, "-y", "-i", temp_video_path,
240
  "-c:v", "libx264", "-preset", "fast", "-crf", "23",
241
  "-pix_fmt", "yuv420p", "-movflags", "+faststart",
242
- "-an", output_video_path],
243
  capture_output=True, text=True
244
  )
245
  if result.returncode == 0:
246
  os.remove(temp_video_path)
 
 
 
247
  print("[H264] Succès.")
248
  else:
249
  os.rename(temp_video_path, output_video_path)
250
  print(f"[H264] ffmpeg a échoué : {result.stderr[-300:]}")
251
- else:
252
- os.rename(temp_video_path, output_video_path)
253
- print("[H264] ffmpeg introuvable, vidéo en mp4v (illisible dans le navigateur).")
254
- print("Installez ffmpeg : winget install ffmpeg")
255
-
256
- print(f"\nTerminé : {video_name}")
257
 
258
  if ffmpeg_cmd:
259
- output_wav_path = os.path.join(dossier, f"{video_name}.wav")
260
  wav_result = subprocess.run(
261
  [ffmpeg_cmd, "-y", "-i", video_path, "-vn", "-acodec", "pcm_s16le",
262
  "-ar", "44100", "-ac", "2", output_wav_path],
 
30
 
31
  dossier = os.path.dirname(os.path.abspath(sys.argv[0]))
32
 
33
+ output_video_path = os.path.join(dossier, f"./liste/landmarks/{video_name}(landmarks){video_ext}")
34
+ output_csv_path = os.path.join(dossier, f"./liste/csv/{video_name}.csv")
35
+ temp_video_path = os.path.join(dossier, f"{video_name}_temp_raw.mp4")
36
+ output_wav_path = os.path.join(dossier, f"./liste/audio/{video_name}.wav")
37
 
38
  print(f"Vidéo d'entrée : {video_path}")
39
  print(f"CSV de sortie : {output_csv_path}")
 
56
  )
57
  detector = vision.FaceLandmarker.create_from_options(options)
58
 
59
+ cap = cv2.VideoCapture(video_path)
60
+ fps = cap.get(cv2.CAP_PROP_FPS)
61
+ frame_width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
62
  frame_height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
63
+ frame_index = 0
64
 
65
  fourcc = cv2.VideoWriter_fourcc(*'mp4v')
66
+ out = cv2.VideoWriter(temp_video_path, fourcc, fps, (frame_width, frame_height))
67
 
68
+ csv_file = open(output_csv_path, "w", newline="", encoding="utf-8")
69
  csv_writer = csv.writer(csv_file)
70
  csv_writer.writerow(["timestamp", "bouche", "oeil_g", "oeil_d", "pitch", "yaw", "roll", "norm_x", "norm_y"])
71
 
 
85
  indices = [1, 152, 33, 263, 61, 291]
86
  image_points = np.array([[landmarks[i].x * frame_w, landmarks[i].y * frame_h] for i in indices], dtype=np.float64)
87
  camera_matrix, dist_coeffs = generic_optic(frame_w, frame_h)
88
+ success, rotation_vec, translation_vec = cv2.solvePnP(model_points, image_points, camera_matrix, dist_coeffs,
89
+ flags=cv2.SOLVEPNP_ITERATIVE)
90
  if not success:
91
  return None, None, None, None, None
92
  rotation_mat, _ = cv2.Rodrigues(rotation_vec)
 
108
  face_h = int((y_max - y_min) * frame_h)
109
  face_size_ratio = ((x_max - x_min) * (y_max - y_min)) ** 0.5
110
  return center_x, center_y, norm_x, norm_y, face_w, face_h, face_size_ratio, \
111
+ (int(x_min * frame_w), int(y_min * frame_h), int(x_max * frame_w), int(y_max * frame_h))
112
 
113
 
114
  def draw_axes(frame, rotation_vec, translation_vec, camera_matrix, dist_coeffs, origin):
 
129
  if not ret:
130
  break
131
 
132
+ timestamp = frame_index / fps
133
  frame_index += 1
134
+ h, w, _ = frame.shape
135
 
136
  rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
137
+ mp_image = Image(image_format=ImageFormat.SRGB, data=rgb_frame)
138
+ results = detector.detect_for_video(mp_image, int(timestamp * 1000))
139
 
140
  if results.face_landmarks:
141
  landmarks = results.face_landmarks[0]
142
+ mouth_open = abs(landmarks[13].y - landmarks[14].y)
143
+ left_eye_open = abs(landmarks[159].y - landmarks[145].y)
144
  right_eye_open = abs(landmarks[386].y - landmarks[374].y)
145
  pitch, yaw, roll, rot_vec, trans_vec = compute_head_pose(landmarks, w, h)
146
+ center_x, center_y, norm_x, norm_y, face_w, face_h, face_size_ratio, bbox = compute_face_position(landmarks, w,
147
+ h)
148
 
149
  if pitch is not None:
150
+ yaw = -yaw
151
  pitch = ((pitch + 180) % 360)
152
+ roll = max(-40, min(40, roll))
153
  pitch = max(-40, min(40, pitch))
154
+ yaw = max(-180, min(180, yaw))
155
 
156
  list_timestamp.append(round(timestamp, 2))
157
  list_mouth_open.append(round(mouth_open, 3))
 
170
  cv2.drawMarker(frame, (w // 2, h // 2), (128, 128, 128), cv2.MARKER_CROSS, 20, 1)
171
  if rot_vec is not None:
172
  camera_matrix, dist_coeffs = generic_optic(w, h)
173
+ draw_axes(frame, rot_vec, trans_vec, camera_matrix, dist_coeffs,
174
+ (int(landmarks[1].x * w), int(landmarks[1].y * h)))
175
  elif list_mouth_open:
176
  list_timestamp.append(round(timestamp, 2))
177
  list_mouth_open.append(list_mouth_open[-1])
 
204
  csv_file.close()
205
  sys.exit(1)
206
 
207
+
208
  def norm_list(lst, mi, ma, n_mi, n_ma):
209
  return [((i - mi) / (ma - mi)) * (n_ma - n_mi) + n_mi if ma != mi else 0.0 for i in lst]
210
 
211
+
212
+ list_mouth_open = norm_list(list_mouth_open, min(list_mouth_open), max(list_mouth_open), -0.01, 0.01)
213
+ list_norm_x = norm_list(list_norm_x, min(list_norm_x), max(list_norm_x), -0.03, 0.03)
214
+ list_norm_y = norm_list(list_norm_y, min(list_norm_y), max(list_norm_y), -0.0075, 0.0075)
215
+ list_left_eye_open = norm_list(list_left_eye_open, min(list_left_eye_open), max(list_left_eye_open), 0, -50.0)
216
  list_right_eye_open = norm_list(list_right_eye_open, min(list_right_eye_open), max(list_right_eye_open), 0, 50.0)
217
 
218
  for i in range(len(list_timestamp)):
 
241
  break
242
 
243
  if ffmpeg_cmd:
244
+ safe_output = os.path.join(dossier, f"{video_name}_landmarks_h264.mp4")
245
+
246
  result = subprocess.run(
247
  [ffmpeg_cmd, "-y", "-i", temp_video_path,
248
  "-c:v", "libx264", "-preset", "fast", "-crf", "23",
249
  "-pix_fmt", "yuv420p", "-movflags", "+faststart",
250
+ "-an", safe_output],
251
  capture_output=True, text=True
252
  )
253
  if result.returncode == 0:
254
  os.remove(temp_video_path)
255
+ if os.path.exists(output_video_path):
256
+ os.remove(output_video_path)
257
+ os.rename(safe_output, output_video_path)
258
  print("[H264] Succès.")
259
  else:
260
  os.rename(temp_video_path, output_video_path)
261
  print(f"[H264] ffmpeg a échoué : {result.stderr[-300:]}")
 
 
 
 
 
 
262
 
263
  if ffmpeg_cmd:
 
264
  wav_result = subprocess.run(
265
  [ffmpeg_cmd, "-y", "-i", video_path, "-vn", "-acodec", "pcm_s16le",
266
  "-ar", "44100", "-ac", "2", output_wav_path],
coquille/Jalon3.py CHANGED
@@ -3,79 +3,84 @@ from reachy_mini import ReachyMini, ReachyMiniApp
3
  from reachy_mini.utils import create_head_pose
4
  import numpy as np
5
  import time
6
- from pydantic import BaseModel
7
  import csv
8
  import soundfile as sf
9
  import scipy.signal
10
  import sys
 
11
 
12
  if len(sys.argv) < 3:
 
13
  sys.exit(1)
14
 
15
  csv_path = sys.argv[1]
16
  wav_path = sys.argv[2]
17
 
 
 
 
18
 
19
  class Test(ReachyMiniApp):
20
- # Optional: URL to a custom configuration page for the app
21
- # eg. "http://localhost:8043"
22
- custom_app_url: str | None = "http://0.0.0.0:8043"
23
- # Optional: specify a media backend ("gstreamer", "gstreamer_no_video", "default", etc.)
24
- #  On the wireless, use gstreamer_no_video to optimise CPU usage if the app does not use video streaming
25
  request_media_backend: str | None = None
26
 
27
  def run(self, reachy_mini: ReachyMini, stop_event: threading.Event):
28
- t0 = time.time()
29
-
30
- antennas_enabled = True
31
- sound_play_requested = False
32
 
33
- # Main control loop
34
  def apply_signals(signals_way, audio_way):
35
- while not stop_event.is_set():
36
- with open(signals_way, newline='') as csvfile:
37
- reader = csv.DictReader(csvfile)
38
- rows = list(reader)
39
-
40
- last_timestamp = 0
41
- audio, samplerate_in = sf.read(audio_way, dtype="float32")
42
-
43
- if audio.ndim > 1:
44
- audio = np.mean(audio, axis=1)
45
-
46
- output_sr = reachy_mini.media.get_output_audio_samplerate()
47
- if samplerate_in != output_sr:
48
- audio = scipy.signal.resample(
49
- audio,
50
- int(len(audio) * (output_sr / samplerate_in))
51
- )
52
-
53
- reachy_mini.media.start_playing()
54
- reachy_mini.media.push_audio_sample(audio)
55
- for row in rows:
56
- x_pos = float(row["bouche"])
57
- y_pos = float(row["norm_x"])
58
- z_pos = float(row["norm_y"])
59
- pitch_deg = float(row["pitch"])
60
- yaw_deg = float(row["yaw"])
61
- roll_deg = float(row["roll"])
62
-
63
- if antennas_enabled:
64
- g = float(row["oeil_g"])
65
- d = float(row["oeil_d"])
66
- antennas_deg = np.array([g, d])
67
- else:
68
- antennas_deg = np.array([0.0, 0.0])
69
- antennas_rad = np.deg2rad(antennas_deg)
70
-
71
- head_pose = create_head_pose(x=x_pos, y=y_pos, z=z_pos, pitch=pitch_deg, yaw=yaw_deg, roll=roll_deg)
72
- reachy_mini.set_target(head=head_pose, antennas=antennas_rad)
73
-
74
- print(row["timestamp"])
75
- time.sleep(float(row['timestamp']) - last_timestamp)
76
- last_timestamp = float(row['timestamp'])
77
- reachy_mini.media.stop_playing()
78
- stop_event.set()
 
 
 
 
 
 
 
 
 
 
79
  apply_signals(csv_path, wav_path)
80
 
81
 
 
3
  from reachy_mini.utils import create_head_pose
4
  import numpy as np
5
  import time
 
6
  import csv
7
  import soundfile as sf
8
  import scipy.signal
9
  import sys
10
+ import urllib.request
11
 
12
  if len(sys.argv) < 3:
13
+ print("Usage: python Jalon3.py <fichier.csv> <fichier.wav>")
14
  sys.exit(1)
15
 
16
  csv_path = sys.argv[1]
17
  wav_path = sys.argv[2]
18
 
19
+ # URL du serveur main.py pour signaler que la lecture va demarrer
20
+ SERVER_URL = "http://127.0.0.1:8042"
21
+
22
 
23
  class Test(ReachyMiniApp):
24
+ custom_app_url: str | None = "http://0.0.0.0:8042"
 
 
 
 
25
  request_media_backend: str | None = None
26
 
27
  def run(self, reachy_mini: ReachyMini, stop_event: threading.Event):
 
 
 
 
28
 
 
29
  def apply_signals(signals_way, audio_way):
30
+ with open(signals_way, newline='') as csvfile:
31
+ reader = csv.DictReader(csvfile)
32
+ rows = list(reader)
33
+
34
+ audio, samplerate_in = sf.read(audio_way, dtype="float32")
35
+ if audio.ndim > 1:
36
+ audio = np.mean(audio, axis=1)
37
+
38
+ output_sr = reachy_mini.media.get_output_audio_samplerate()
39
+ if samplerate_in != output_sr:
40
+ audio = scipy.signal.resample(
41
+ audio,
42
+ int(len(audio) * (output_sr / samplerate_in))
43
+ )
44
+
45
+ reachy_mini.media.start_playing()
46
+ reachy_mini.media.push_audio_sample(audio)
47
+
48
+ # Tout est pret : signaler au serveur que la lecture commence
49
+ try:
50
+ req = urllib.request.Request(
51
+ f"{SERVER_URL}/ready",
52
+ data=b"{}",
53
+ headers={"Content-Type": "application/json"},
54
+ method="POST"
55
+ )
56
+ urllib.request.urlopen(req, timeout=2)
57
+ print("[READY] Signal envoye au serveur")
58
+ except Exception as e:
59
+ print(f"[READY] Echec signal : {e}")
60
+
61
+ last_timestamp = 0
62
+ for row in rows:
63
+ if stop_event.is_set():
64
+ break
65
+ x_pos = float(row["bouche"])
66
+ y_pos = float(row["norm_x"])
67
+ z_pos = float(row["norm_y"])
68
+ pitch_deg = float(row["pitch"])
69
+ yaw_deg = float(row["yaw"])
70
+ roll_deg = float(row["roll"])
71
+ g = float(row["oeil_g"])
72
+ d = float(row["oeil_d"])
73
+ antennas_rad = np.deg2rad(np.array([g, d]))
74
+
75
+ head_pose = create_head_pose(x=x_pos, y=y_pos, z=z_pos, pitch=pitch_deg, yaw=yaw_deg, roll=roll_deg)
76
+ reachy_mini.set_target(head=head_pose, antennas=antennas_rad)
77
+
78
+ time.sleep(float(row['timestamp']) - last_timestamp)
79
+ last_timestamp = float(row['timestamp'])
80
+
81
+ reachy_mini.media.stop_playing()
82
+ stop_event.set()
83
+
84
  apply_signals(csv_path, wav_path)
85
 
86
 
coquille/main.py CHANGED
@@ -1,6 +1,13 @@
 
 
 
 
 
 
 
 
1
  import threading
2
  import subprocess
3
- import sys
4
  import os
5
  import time
6
  from fastapi import Request
@@ -10,9 +17,22 @@ from reachy_mini import ReachyMini, ReachyMiniApp
10
 
11
  DOSSIER = os.path.dirname(os.path.abspath(__file__))
12
  STATIC = os.path.join(DOSSIER, "static")
 
 
 
 
 
 
 
 
 
 
 
 
 
13
 
14
  class Coquille(ReachyMiniApp):
15
- custom_app_url: str | None = "http://localhost:8043"
16
  request_media_backend: str | None = None
17
 
18
  def __init__(self, *args, **kwargs):
@@ -26,24 +46,29 @@ class Coquille(ReachyMiniApp):
26
  while not stop_event.is_set():
27
  time.sleep(0.1)
28
 
 
29
  app = Coquille()
30
 
31
  app.settings_app.mount("/static", StaticFiles(directory=STATIC), name="static")
32
 
 
33
  @app.settings_app.get("/")
34
  def index():
35
  return FileResponse(os.path.join(STATIC, "index.html"))
36
 
 
37
  @app.settings_app.get("/favicon.ico")
38
  def favicon():
39
  return Response(status_code=204)
40
 
 
41
  @app.settings_app.get("/liste")
42
  def liste():
43
- fichiers = os.listdir(DOSSIER)
44
- noms = [f.replace(".csv", "") for f in fichiers if f.endswith(".csv") and f != "resultats.csv" and "video_temp" not in f]
45
  return JSONResponse({"noms": noms})
46
 
 
47
  @app.settings_app.post("/charger")
48
  async def charger(request: Request):
49
  form = await request.form()
@@ -58,13 +83,14 @@ async def charger(request: Request):
58
  with open(video_path, "wb") as f:
59
  f.write(contents)
60
 
61
- # Étape 1 : extraction landmarks + génération CSV + vidéo mp4v brute
62
  try:
63
  result = subprocess.run(
64
  [sys.executable, os.path.join(DOSSIER, "Jalon2OPTI.py"), video_path, nom],
65
  cwd=DOSSIER,
66
  capture_output=True,
67
  text=True,
 
 
68
  check=True
69
  )
70
  print(f"[PIPELINE OK]\n{result.stdout}")
@@ -75,48 +101,88 @@ async def charger(request: Request):
75
  "message": f"Pipeline échoué : {e.stderr or e.stdout or 'erreur inconnue'}"
76
  }, status_code=500)
77
 
78
- # Étape 2 : réencodage H.264 via ffmpeg pour compatibilité navigateur
79
- raw_path = os.path.join(DOSSIER, f"{nom}(landmarks).mp4")
80
- tmp_path = os.path.join(DOSSIER, f"{nom}(landmarks)_raw.mp4")
81
 
82
- if os.path.exists(raw_path):
 
 
 
 
83
  try:
84
- os.rename(raw_path, tmp_path)
85
- ffmpeg = subprocess.run(
86
- ["ffmpeg", "-y", "-i", tmp_path,
87
- "-vcodec", "libx264", "-pix_fmt", "yuv420p",
88
- "-movflags", "+faststart",
89
- "-an", raw_path],
90
- capture_output=True, text=True
91
- )
92
- if ffmpeg.returncode == 0:
93
- os.remove(tmp_path)
94
- print("[FFMPEG] Encodage H.264 réussi")
95
- else:
96
- # ffmpeg absent ou erreur : on remet le fichier brut
97
- os.rename(tmp_path, raw_path)
98
- print(f"[FFMPEG] Échec (ffmpeg absent ?), on garde mp4v : {ffmpeg.stderr}")
99
- except Exception as e:
100
- if os.path.exists(tmp_path) and not os.path.exists(raw_path):
101
- os.rename(tmp_path, raw_path)
102
- print(f"[FFMPEG] Exception : {e}")
103
 
104
- return JSONResponse({"succes": True, "nom": nom})
105
 
106
  @app.settings_app.post("/play")
107
  async def play(request: Request):
 
108
  data = await request.json()
109
- return JSONResponse({"succes": True, "nom": data.get("nom")})
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
110
 
111
  @app.settings_app.get("/video/{nom}")
112
  def video(nom: str, request: Request):
113
- path = os.path.join(DOSSIER, f"{nom}(landmarks).mp4")
 
 
 
 
 
 
114
 
115
- print(f"[VIDEO] Recherche : {path} | Existe : {os.path.exists(path)}")
116
- if not os.path.exists(path):
117
- mp4s = [f for f in os.listdir(DOSSIER) if f.endswith(".mp4")]
118
  print(f"[VIDEO] MP4 disponibles : {mp4s}")
119
- return JSONResponse({"succes": False, "message": f"Vidéo introuvable : {path}"}, status_code=404)
120
 
121
  file_size = os.path.getsize(path)
122
  range_header = request.headers.get("range")
@@ -153,6 +219,53 @@ def video(nom: str, request: Request):
153
  else:
154
  return FileResponse(path, media_type="video/mp4", headers={"Accept-Ranges": "bytes"})
155
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
156
  if __name__ == "__main__":
157
  import uvicorn
158
- uvicorn.run(app.settings_app, host="127.0.0.1", port=8043)
 
1
+ # -*- coding: utf-8 -*-
2
+ import sys
3
+ import io
4
+
5
+ # Force la sortie UTF-8 pour éviter les crashs de décodage Unicode avec le daemon Reachy
6
+ sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace")
7
+ sys.stderr = io.TextIOWrapper(sys.stderr.buffer, encoding="utf-8", errors="replace")
8
+
9
  import threading
10
  import subprocess
 
11
  import os
12
  import time
13
  from fastapi import Request
 
17
 
18
  DOSSIER = os.path.dirname(os.path.abspath(__file__))
19
  STATIC = os.path.join(DOSSIER, "static")
20
+ DOSSIER_CSV = os.path.join(DOSSIER, "liste", "csv")
21
+ DOSSIER_LANDMARKS = os.path.join(DOSSIER, "liste", "landmarks")
22
+ DOSSIER_AUDIO = os.path.join(DOSSIER, "liste", "audio")
23
+
24
+ # Création automatique des dossiers si absents
25
+ os.makedirs(DOSSIER_CSV, exist_ok=True)
26
+ os.makedirs(DOSSIER_LANDMARKS, exist_ok=True)
27
+ os.makedirs(DOSSIER_AUDIO, exist_ok=True)
28
+
29
+ _apply_proc: subprocess.Popen | None = None
30
+ _ready_event = threading.Event()
31
+ _play_start_time: float = 0.0
32
+
33
 
34
  class Coquille(ReachyMiniApp):
35
+ custom_app_url: str | None = "http://localhost:8042"
36
  request_media_backend: str | None = None
37
 
38
  def __init__(self, *args, **kwargs):
 
46
  while not stop_event.is_set():
47
  time.sleep(0.1)
48
 
49
+
50
  app = Coquille()
51
 
52
  app.settings_app.mount("/static", StaticFiles(directory=STATIC), name="static")
53
 
54
+
55
  @app.settings_app.get("/")
56
  def index():
57
  return FileResponse(os.path.join(STATIC, "index.html"))
58
 
59
+
60
  @app.settings_app.get("/favicon.ico")
61
  def favicon():
62
  return Response(status_code=204)
63
 
64
+
65
  @app.settings_app.get("/liste")
66
  def liste():
67
+ fichiers = os.listdir(DOSSIER_CSV)
68
+ noms = [f.replace(".csv", "") for f in fichiers if f.endswith(".csv") and f != "resultats.csv"]
69
  return JSONResponse({"noms": noms})
70
 
71
+
72
  @app.settings_app.post("/charger")
73
  async def charger(request: Request):
74
  form = await request.form()
 
83
  with open(video_path, "wb") as f:
84
  f.write(contents)
85
 
 
86
  try:
87
  result = subprocess.run(
88
  [sys.executable, os.path.join(DOSSIER, "Jalon2OPTI.py"), video_path, nom],
89
  cwd=DOSSIER,
90
  capture_output=True,
91
  text=True,
92
+ encoding="utf-8",
93
+ errors="replace",
94
  check=True
95
  )
96
  print(f"[PIPELINE OK]\n{result.stdout}")
 
101
  "message": f"Pipeline échoué : {e.stderr or e.stdout or 'erreur inconnue'}"
102
  }, status_code=500)
103
 
104
+ return JSONResponse({"succes": True, "nom": nom})
 
 
105
 
106
+
107
+ def _kill_apply():
108
+ global _apply_proc
109
+ if _apply_proc and _apply_proc.poll() is None:
110
+ _apply_proc.terminate()
111
  try:
112
+ _apply_proc.wait(timeout=3)
113
+ except subprocess.TimeoutExpired:
114
+ _apply_proc.kill()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
115
 
 
116
 
117
  @app.settings_app.post("/play")
118
  async def play(request: Request):
119
+ global _apply_proc, _play_start_time
120
  data = await request.json()
121
+ nom = data.get("nom", "").strip()
122
+
123
+ if not nom:
124
+ return JSONResponse({"succes": False, "message": "Nom manquant."}, status_code=400)
125
+
126
+ csv_path = os.path.join(DOSSIER_CSV, f"{nom}.csv")
127
+ wav_path = os.path.join(DOSSIER_AUDIO, f"{nom}.wav")
128
+
129
+ if not os.path.exists(csv_path):
130
+ return JSONResponse({"succes": False, "message": f"CSV introuvable : {csv_path}"}, status_code=404)
131
+ if not os.path.exists(wav_path):
132
+ return JSONResponse({"succes": False, "message": f"WAV introuvable : {wav_path}"}, status_code=404)
133
+
134
+ # Nettoyage de l'ancien processus avant d'en ouvrir un nouveau
135
+ _kill_apply()
136
+ _ready_event.clear()
137
+ _play_start_time = time.time()
138
+
139
+ apply_script = os.path.join(DOSSIER, "Jalon3.py")
140
+ _apply_proc = subprocess.Popen(
141
+ [sys.executable, apply_script, csv_path, wav_path],
142
+ cwd=DOSSIER,
143
+ )
144
+
145
+ print(f"[PLAY] apply_signals PID {_apply_proc.pid} pour '{nom}'")
146
+ return JSONResponse({"succes": True, "nom": nom})
147
+
148
+
149
+ @app.settings_app.get("/is-ready")
150
+ async def is_ready():
151
+ ready = _ready_event.is_set()
152
+ delay_ms = int((time.time() - _play_start_time) * 1000) if ready else 0
153
+ return JSONResponse({"ready": ready, "delay_ms": delay_ms})
154
+
155
+
156
+ @app.settings_app.post("/ready")
157
+ async def ready():
158
+ elapsed = time.time() - _play_start_time
159
+ _ready_event.set()
160
+ print(f"[READY] Signal reçu après {elapsed:.2f}s")
161
+ return JSONResponse({"succes": True, "delay_ms": int(elapsed * 1000)})
162
+
163
+
164
+ @app.settings_app.post("/stop")
165
+ async def stop():
166
+ _kill_apply()
167
+ print("[STOP] apply_signals arrêté.")
168
+ return JSONResponse({"succes": True})
169
+
170
 
171
  @app.settings_app.get("/video/{nom}")
172
  def video(nom: str, request: Request):
173
+ path = None
174
+ target = f"{nom}(landmarks)"
175
+ for f in os.listdir(DOSSIER_LANDMARKS):
176
+ name_no_ext, _ = os.path.splitext(f)
177
+ if name_no_ext == target:
178
+ path = os.path.join(DOSSIER_LANDMARKS, f)
179
+ break
180
 
181
+ print(f"[VIDEO] Recherche : {path} | Existe : {path is not None and os.path.exists(path)}")
182
+ if not path or not os.path.exists(path):
183
+ mp4s = [f for f in os.listdir(DOSSIER_LANDMARKS) if f.endswith(".mp4")]
184
  print(f"[VIDEO] MP4 disponibles : {mp4s}")
185
+ return JSONResponse({"succes": False, "message": f"Vidéo introuvable pour : {nom}"}, status_code=404)
186
 
187
  file_size = os.path.getsize(path)
188
  range_header = request.headers.get("range")
 
219
  else:
220
  return FileResponse(path, media_type="video/mp4", headers={"Accept-Ranges": "bytes"})
221
 
222
+
223
+ @app.settings_app.post("/supprimer")
224
+ async def supprimer(request: Request):
225
+ try:
226
+ data = await request.json()
227
+ except Exception:
228
+ return JSONResponse({"succes": False, "message": "Body JSON invalide."}, status_code=400)
229
+
230
+ nom = (data.get("nom") or "").strip()
231
+ if not nom:
232
+ return JSONResponse({"succes": False, "message": "Nom manquant."}, status_code=400)
233
+
234
+ csv_path = os.path.join(DOSSIER_CSV, f"{nom}.csv")
235
+ audio_path = os.path.join(DOSSIER_AUDIO, f"{nom}.wav")
236
+
237
+ # Recherche dynamique de la vidéo pour s'adapter à toutes les extensions (.mp4, .avi, etc.)
238
+ video_path = None
239
+ target = f"{nom}(landmarks)"
240
+ for f in os.listdir(DOSSIER_LANDMARKS):
241
+ name_no_ext, _ = os.path.splitext(f)
242
+ if name_no_ext == target:
243
+ video_path = os.path.join(DOSSIER_LANDMARKS, f)
244
+ break
245
+
246
+ supprime = []
247
+ chemins_a_verifier = [csv_path, audio_path]
248
+ if video_path:
249
+ chemins_a_verifier.append(video_path)
250
+
251
+ for p in chemins_a_verifier:
252
+ if os.path.exists(p):
253
+ try:
254
+ os.remove(p)
255
+ supprime.append(p)
256
+ except PermissionError:
257
+ return JSONResponse({
258
+ "succes": False,
259
+ "message": f"Fichier verrouillé (fermez la vidéo ou attendez la fin de l'animation) : {os.path.basename(p)}"
260
+ }, status_code=500)
261
+
262
+ if supprime:
263
+ print(f"[DELETE] Éléments supprimés pour '{nom}': {supprime}")
264
+ return JSONResponse({"succes": True})
265
+
266
+ return JSONResponse({"succes": False, "message": f"Aucun fichier trouvé pour : {nom}"}, status_code=404)
267
+
268
+
269
  if __name__ == "__main__":
270
  import uvicorn
271
+ uvicorn.run(app.settings_app, host="127.0.0.1", port=8042)
coquille/programme1.py CHANGED
@@ -8,8 +8,8 @@ from pydantic import BaseModel
8
 
9
  class Test(ReachyMiniApp):
10
  # Optional: URL to a custom configuration page for the app
11
- # eg. "http://localhost:8043"
12
- custom_app_url: str | None = "http://0.0.0.0:8043"
13
  # Optional: specify a media backend ("gstreamer", "gstreamer_no_video", "default", etc.)
14
  # On the wireless, use gstreamer_no_video to optimise CPU usage if the app does not use video streaming
15
  request_media_backend: str | None = None
 
8
 
9
  class Test(ReachyMiniApp):
10
  # Optional: URL to a custom configuration page for the app
11
+ # eg. "http://localhost:8042"
12
+ custom_app_url: str | None = "http://0.0.0.0:8042"
13
  # Optional: specify a media backend ("gstreamer", "gstreamer_no_video", "default", etc.)
14
  # On the wireless, use gstreamer_no_video to optimise CPU usage if the app does not use video streaming
15
  request_media_backend: str | None = None
coquille/programme2.py CHANGED
@@ -8,8 +8,8 @@ from pydantic import BaseModel
8
 
9
  class Test(ReachyMiniApp):
10
  # Optional: URL to a custom configuration page for the app
11
- # eg. "http://localhost:8043"
12
- custom_app_url: str | None = "http://0.0.0.0:8043"
13
  # Optional: specify a media backend ("gstreamer", "gstreamer_no_video", "default", etc.)
14
  # On the wireless, use gstreamer_no_video to optimise CPU usage if the app does not use video streaming
15
  request_media_backend: str | None = None
 
8
 
9
  class Test(ReachyMiniApp):
10
  # Optional: URL to a custom configuration page for the app
11
+ # eg. "http://localhost:8042"
12
+ custom_app_url: str | None = "http://0.0.0.0:8042"
13
  # Optional: specify a media backend ("gstreamer", "gstreamer_no_video", "default", etc.)
14
  # On the wireless, use gstreamer_no_video to optimise CPU usage if the app does not use video streaming
15
  request_media_backend: str | None = None
coquille/static/index.html CHANGED
@@ -66,6 +66,6 @@
66
  <div class="progress-bar" id="progress-bar"></div>
67
  </footer>
68
 
69
- <script src="/static/main.js"></script>
70
  </body>
71
  </html>
 
66
  <div class="progress-bar" id="progress-bar"></div>
67
  </footer>
68
 
69
+ <script src="/static/main.js?v=3"></script>
70
  </body>
71
  </html>
coquille/static/main.js CHANGED
@@ -30,6 +30,18 @@ document.addEventListener("DOMContentLoaded", () => {
30
  if (!nom) return updateStatus("Donnez un nom à l'enregistrement", 'error');
31
  if (!selectedFile) return updateStatus("Chargez une vidéo d'abord", 'error');
32
 
 
 
 
 
 
 
 
 
 
 
 
 
33
  runBtn.disabled = true;
34
  updateStatus("Extraction des landmarks et génération des mouvements...", 'active');
35
  setProgress(30);
@@ -61,32 +73,26 @@ document.addEventListener("DOMContentLoaded", () => {
61
 
62
  // Petit délai pour s'assurer que le fichier vidéo est bien écrit
63
  setTimeout(() => {
64
- // Chargement et lecture de la vidéo landmarks générée
65
  preview.src = `/video/${encodeURIComponent(nom)}?t=${Date.now()}`;
66
  preview.style.display = 'block';
67
  placeholder.style.display = 'none';
68
 
69
- // Fonction pour lancer la vidéo
70
  const playVideo = () => {
71
  preview.play().catch(() => {
72
- // Autoplay bloqué : on affiche un bouton play natif
73
  preview.controls = true;
74
  });
75
  };
76
 
77
- // Si la vidéo est déjà prête, on lance directement
78
- if (preview.readyState >= 3) { // HAVE_FUTURE_DATA ou mieux
79
  playVideo();
80
  } else {
81
- // Sinon, on attend
82
  preview.addEventListener('canplay', playVideo, { once: true });
83
  }
84
 
85
- // Nettoyage des inputs pour le prochain enregistrement
86
  nameInput.value = '';
87
  selectedFile = null;
88
  chargerBibliotheque();
89
- }, 500); // Délai d'attente pour la génération vidéo
90
  } else {
91
  setProgress(0);
92
  updateStatus(`Erreur : ${data.message}`, 'error');
@@ -116,10 +122,12 @@ async function chargerBibliotheque() {
116
  data.noms.forEach((nom, i) => {
117
  const item = document.createElement('li');
118
  item.className = 'library-item';
 
119
  item.innerHTML = `
120
  <span class="item-index">${String(i + 1).padStart(2, '0')}</span>
121
  <span class="item-name">${nom}</span>
122
- <button class="btn btn-play" onclick="playRecording('${nom}', this)">play</button>
 
123
  `;
124
  liste.appendChild(item);
125
  });
@@ -130,72 +138,117 @@ async function chargerBibliotheque() {
130
  }
131
 
132
  async function playRecording(nom, btn) {
133
- // Nettoyage visuel des anciens boutons en cours de lecture
134
  document.querySelectorAll('.library-item').forEach(el => el.classList.remove('playing'));
135
  document.querySelectorAll('.btn-play').forEach(b => {
136
  b.classList.remove('active');
137
  b.textContent = 'play';
138
  });
139
 
140
- // Activation visuelle de la ligne sélectionnée
141
  btn.classList.add('active');
142
  btn.textContent = '...';
143
  btn.closest('.library-item').classList.add('playing');
144
- updateStatus(`Synchronisation vidéo & simulation pour : ${nom}`, 'active');
145
  setProgress(50);
146
 
147
- // Lancement de la vidéo annotée contenant les repères faciaux (landmarks)
148
  const preview = document.getElementById('video-preview');
149
  const placeholder = document.getElementById('video-placeholder');
150
- preview.src = `/video/${encodeURIComponent(nom)}`;
151
- preview.style.display = 'block';
152
- placeholder.style.display = 'none';
153
 
154
- // Fonction pour lancer la vidéo
155
- const playVideo = () => {
156
- preview.play().catch(err => {
157
- console.error('Erreur lors de la lecture:', err);
158
- updateStatus(`Erreur de lecture vidéo : ${err.message}`, 'error');
159
- btn.classList.remove('active');
160
- btn.textContent = 'play';
161
- btn.closest('.library-item').classList.remove('playing');
162
- });
163
- };
164
 
165
- // Attendre que la vidéo soit chargée
166
- if (preview.readyState >= 3) {
167
- playVideo();
168
- } else {
169
- preview.addEventListener('canplay', playVideo, { once: true });
170
- }
171
-
172
- // Envoi synchrone de la commande pour faire réagir la simulation en direct
173
  try {
174
- const response = await fetch('/play', {
175
  method: 'POST',
176
  headers: { 'Content-Type': 'application/json' },
177
  body: JSON.stringify({ nom: nom })
178
  });
179
- const data = await response.json();
180
-
181
- if (data.succes) {
182
- setProgress(100);
183
- updateStatus(`Lecture synchronisée en cours...`, 'done');
184
- } else {
185
  setProgress(0);
186
- updateStatus(`Erreur simulation : ${data.message}`, 'error');
 
 
 
 
187
  }
188
  } catch (err) {
189
  setProgress(0);
190
- updateStatus("Erreur de communication de mouvements", 'error');
 
 
 
 
191
  }
192
 
193
- // Reset automatique de l'état du bouton dès que l'animation vidéo prend fin
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
194
  preview.onended = () => {
195
  btn.classList.remove('active');
196
  btn.textContent = 'play';
197
  btn.closest('.library-item').classList.remove('playing');
198
  updateStatus(`Séquence "${nom}" terminée`, 'done');
 
199
  };
200
  }
201
 
@@ -207,6 +260,7 @@ function updateStatus(msg, type) {
207
  }
208
  }
209
 
 
210
  function setProgress(pct) {
211
  const bar = document.getElementById('progress-bar');
212
  if (bar) {
@@ -215,4 +269,38 @@ function setProgress(pct) {
215
  setTimeout(() => { bar.style.width = '0%'; }, 1200);
216
  }
217
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218
  }
 
30
  if (!nom) return updateStatus("Donnez un nom à l'enregistrement", 'error');
31
  if (!selectedFile) return updateStatus("Chargez une vidéo d'abord", 'error');
32
 
33
+ // [AJOUT DU COLLÈGUE] : Vérification anti-doublon avant de lancer le traitement
34
+ try {
35
+ const responsescv = await fetch('/liste');
36
+ if (!responsescv.ok) throw new Error();
37
+ const listescv = await responsescv.json();
38
+ if (listescv.noms.includes(nom)) {
39
+ return updateStatus("Ce nom est déjà dans la bibliothèque", 'error');
40
+ }
41
+ } catch (err) {
42
+ console.error("Erreur de vérification de la bibliothèque:", err);
43
+ }
44
+
45
  runBtn.disabled = true;
46
  updateStatus("Extraction des landmarks et génération des mouvements...", 'active');
47
  setProgress(30);
 
73
 
74
  // Petit délai pour s'assurer que le fichier vidéo est bien écrit
75
  setTimeout(() => {
 
76
  preview.src = `/video/${encodeURIComponent(nom)}?t=${Date.now()}`;
77
  preview.style.display = 'block';
78
  placeholder.style.display = 'none';
79
 
 
80
  const playVideo = () => {
81
  preview.play().catch(() => {
 
82
  preview.controls = true;
83
  });
84
  };
85
 
86
+ if (preview.readyState >= 3) {
 
87
  playVideo();
88
  } else {
 
89
  preview.addEventListener('canplay', playVideo, { once: true });
90
  }
91
 
 
92
  nameInput.value = '';
93
  selectedFile = null;
94
  chargerBibliotheque();
95
+ }, 500);
96
  } else {
97
  setProgress(0);
98
  updateStatus(`Erreur : ${data.message}`, 'error');
 
122
  data.noms.forEach((nom, i) => {
123
  const item = document.createElement('li');
124
  item.className = 'library-item';
125
+ // [AJOUT DU COLLÈGUE] : Intégration des deux boutons côte à côte avec ses classes CSS
126
  item.innerHTML = `
127
  <span class="item-index">${String(i + 1).padStart(2, '0')}</span>
128
  <span class="item-name">${nom}</span>
129
+ <button class="btn btn-play button-in-list" onclick="playRecording('${nom}', this)">play</button>
130
+ <button class="btn btn-delete button-in-list" onclick="deleteRecording('${nom}')">delete</button>
131
  `;
132
  liste.appendChild(item);
133
  });
 
138
  }
139
 
140
  async function playRecording(nom, btn) {
 
141
  document.querySelectorAll('.library-item').forEach(el => el.classList.remove('playing'));
142
  document.querySelectorAll('.btn-play').forEach(b => {
143
  b.classList.remove('active');
144
  b.textContent = 'play';
145
  });
146
 
 
147
  btn.classList.add('active');
148
  btn.textContent = '...';
149
  btn.closest('.library-item').classList.add('playing');
150
+ updateStatus(`Chargement pour : ${nom}`, 'active');
151
  setProgress(50);
152
 
 
153
  const preview = document.getElementById('video-preview');
154
  const placeholder = document.getElementById('video-placeholder');
 
 
 
155
 
156
+ // [TA MODIF] : Masquer la vidéo pendant l'attente, on retire le src
157
+ preview.pause();
158
+ preview.removeAttribute('src');
159
+ preview.load();
 
 
 
 
 
 
160
 
161
+ // 1. Lancer apply_signals côté serveur
162
+ let simData;
 
 
 
 
 
 
163
  try {
164
+ const simResp = await fetch('/play', {
165
  method: 'POST',
166
  headers: { 'Content-Type': 'application/json' },
167
  body: JSON.stringify({ nom: nom })
168
  });
169
+ simData = await simResp.json();
170
+ if (!simData.succes) {
 
 
 
 
171
  setProgress(0);
172
+ updateStatus(`Erreur simulation : ${simData.message}`, 'error');
173
+ btn.classList.remove('active');
174
+ btn.textContent = 'play';
175
+ btn.closest('.library-item').classList.remove('playing');
176
+ return;
177
  }
178
  } catch (err) {
179
  setProgress(0);
180
+ updateStatus(`Erreur : ${err.message}`, 'error');
181
+ btn.classList.remove('active');
182
+ btn.textContent = 'play';
183
+ btn.closest('.library-item').classList.remove('playing');
184
+ return;
185
  }
186
 
187
+ console.log('[PLAY] Polling /is-ready — ' + new Date().toISOString());
188
+ updateStatus(`En attente du signal robot...`, 'active');
189
+
190
+ // 2. [TA MODIF] : Polling de sécurité /is-ready jusqu'à ce que Reachy soit prêt (timeout 60s)
191
+ const POLL_INTERVAL = 250;
192
+ const POLL_TIMEOUT = 60000;
193
+ const pollStart = Date.now();
194
+
195
+ try {
196
+ await new Promise((resolve, reject) => {
197
+ const poll = async () => {
198
+ if (Date.now() - pollStart > POLL_TIMEOUT) {
199
+ return reject(new Error('Timeout : le robot n\'a pas répondu dans les 60s'));
200
+ }
201
+ try {
202
+ const r = await fetch('/is-ready');
203
+ const d = await r.json();
204
+ const elapsed = ((Date.now() - pollStart) / 1000).toFixed(1);
205
+ updateStatus(`En attente du signal robot\u2026 ${elapsed}s`, 'active');
206
+ if (d.ready) {
207
+ console.log(`[PLAY] Robot prêt après ${d.delay_ms}ms`);
208
+ resolve();
209
+ } else {
210
+ setTimeout(poll, POLL_INTERVAL);
211
+ }
212
+ } catch (e) {
213
+ setTimeout(poll, POLL_INTERVAL);
214
+ }
215
+ };
216
+ poll();
217
+ });
218
+ } catch (err) {
219
+ setProgress(0);
220
+ updateStatus(`Erreur : ${err.message}`, 'error');
221
+ btn.classList.remove('active');
222
+ btn.textContent = 'play';
223
+ btn.closest('.library-item').classList.remove('playing');
224
+ return;
225
+ }
226
+
227
+ console.log('[PLAY] Signal reçu, chargement vidéo — ' + new Date().toISOString());
228
+
229
+ // Attribution finale de la source après le feu vert du serveur
230
+ preview.src = `/video/${encodeURIComponent(nom)}`;
231
+ preview.style.display = 'block';
232
+ placeholder.style.display = 'none';
233
+
234
+ await new Promise((resolve, reject) => {
235
+ preview.addEventListener('canplay', resolve, { once: true });
236
+ preview.addEventListener('error', () => reject(new Error('Erreur chargement vidéo')), { once: true });
237
+ preview.load();
238
+ });
239
+
240
+ console.log('[PLAY] Lancement preview.play() — ' + new Date().toISOString());
241
+ preview.play().catch(e => { console.error('[PLAY] play() refusé:', e); preview.controls = true; });
242
+ setProgress(100);
243
+ updateStatus(`Lecture en cours...`, 'done');
244
+
245
+ // Reset quand la vidéo se termine + arrêt du daemon
246
  preview.onended = () => {
247
  btn.classList.remove('active');
248
  btn.textContent = 'play';
249
  btn.closest('.library-item').classList.remove('playing');
250
  updateStatus(`Séquence "${nom}" terminée`, 'done');
251
+ fetch('/stop', { method: 'POST' }).catch(() => {});
252
  };
253
  }
254
 
 
260
  }
261
  }
262
 
263
+ // Gestion de la barre de progression
264
  function setProgress(pct) {
265
  const bar = document.getElementById('progress-bar');
266
  if (bar) {
 
269
  setTimeout(() => { bar.style.width = '0%'; }, 1200);
270
  }
271
  }
272
+ }
273
+
274
+ // [AJOUT DU COLLÈGUE] : Fonction globale pour supprimer un enregistrement
275
+ async function deleteRecording(nom) {
276
+ const preview = document.getElementById('video-preview');
277
+ const placeholder = document.getElementById('video-placeholder');
278
+
279
+ // Si la vidéo en train d'être jouée est supprimée, on réinitialise l'affichage
280
+ if (preview.src.includes(encodeURIComponent(nom))) {
281
+ preview.pause();
282
+ preview.src = '';
283
+ preview.load();
284
+ preview.style.display = 'none';
285
+ placeholder.style.display = 'block';
286
+ }
287
+
288
+ await new Promise(r => setTimeout(r, 300));
289
+
290
+ try {
291
+ const response = await fetch('/supprimer', {
292
+ method: 'POST',
293
+ headers: { 'Content-Type': 'application/json' },
294
+ body: JSON.stringify({ nom: nom })
295
+ });
296
+ const data = await response.json();
297
+ if (data.succes) {
298
+ updateStatus('"' + nom + '" supprimé.', 'done');
299
+ chargerBibliotheque();
300
+ } else {
301
+ updateStatus('Erreur suppression : ' + data.message, 'error');
302
+ }
303
+ } catch (err) {
304
+ updateStatus('Erreur : ' + err.message, 'error');
305
+ }
306
  }
coquille/static/style.css CHANGED
@@ -283,7 +283,7 @@ input[type="text"]::placeholder {
283
  color: var(--text-muted);
284
  }
285
 
286
- .btn-play {
287
  padding: 0 10px;
288
  height: 26px;
289
  font-size: 10px;
@@ -291,12 +291,19 @@ input[type="text"]::placeholder {
291
  flex-shrink: 0;
292
  }
293
 
294
- .btn-play.active {
295
  border-color: var(--accent-dim);
296
  color: var(--accent);
297
  background: var(--accent-bg);
298
  }
299
 
 
 
 
 
 
 
 
300
  footer {
301
  height: 48px;
302
  border-top: 1px solid var(--border);
 
283
  color: var(--text-muted);
284
  }
285
 
286
+ .button-in-list {
287
  padding: 0 10px;
288
  height: 26px;
289
  font-size: 10px;
 
291
  flex-shrink: 0;
292
  }
293
 
294
+ .button-in-list.active {
295
  border-color: var(--accent-dim);
296
  color: var(--accent);
297
  background: var(--accent-bg);
298
  }
299
 
300
+ /* Petit ajout pour différencier le bouton de suppression au survol */
301
+ .btn-delete:hover {
302
+ border-color: #f56565;
303
+ color: #f56565;
304
+ background: rgba(245, 101, 101, 0.1);
305
+ }
306
+
307
  footer {
308
  height: 48px;
309
  border-top: 1px solid var(--border);