drenayaz commited on
Commit
6b77c3c
·
1 Parent(s): 03117be

get sound from emotions lib

Browse files
hand_tracker_v2/main.py CHANGED
@@ -9,6 +9,7 @@ from hand_tracker import HandTracker
9
  from scipy.spatial.transform import Rotation as R
10
  from utils import finger_orientation_deg, angle_diff, allow_multiturn
11
  import os
 
12
 
13
  DEBUG = True
14
  FREQUENCY = 50 # Hz
@@ -100,7 +101,7 @@ class HandTrackerV2(ReachyMiniApp):
100
  cv2.waitKey(1)
101
 
102
 
103
- def update_hand_pos(self, im, reachy_mini):
104
  if self.hand_tracker is None:
105
  self.hand_tracker = HandTracker(model_complexity=0)
106
  hands = self.hand_tracker.get_hands_positions(im)
@@ -130,11 +131,7 @@ class HandTrackerV2(ReachyMiniApp):
130
  def play_sound(self,reachy_mini, sound_name: str):
131
  if time.time() - self.last_play_sound < 2.0:
132
  return
133
- sound_path = os.path.join(
134
- os.path.dirname(__file__),
135
- "sounds",
136
- f"{sound_name}.wav"
137
- )
138
  reachy_mini.media.play_sound(sound_path)
139
  self.last_play_sound = time.time()
140
 
@@ -167,9 +164,9 @@ class HandTrackerV2(ReachyMiniApp):
167
  if all(count == stable_hand_count for count in self.hand_count_history):
168
  # Le nombre de mains est stable, comparer avec précédent
169
  if stable_hand_count > self.previous_number_hands:
170
- self.play_sound(reachy_mini, "happy")
171
  elif stable_hand_count < self.previous_number_hands:
172
- self.play_sound(reachy_mini, "sad")
173
 
174
  self.previous_number_hands = stable_hand_count
175
 
@@ -183,7 +180,7 @@ class HandTrackerV2(ReachyMiniApp):
183
  if time_since_last_hand > self.idle_timeout:
184
  # Enter idle once
185
  if not self.is_idle:
186
- print(">>> ENTERING IDLE MODE")
187
  self.previous_antenna_angles = reachy_mini.get_present_antenna_joint_positions()
188
 
189
  self.is_idle = True
@@ -285,6 +282,8 @@ class HandTrackerV2(ReachyMiniApp):
285
  self.hand_count_history = [] # buffer des derniers counts
286
  self.hand_count_buffer_size = 3 # nombre de frames consécutives pour valider
287
 
 
 
288
 
289
 
290
  tracking_thread = threading.Thread(
@@ -301,7 +300,7 @@ class HandTrackerV2(ReachyMiniApp):
301
  if self.width is None or self.height is None:
302
  self.height, self.width = im.shape[:2]
303
 
304
- self.update_hand_pos(im, reachy_mini)
305
  if DEBUG:
306
  self.draw(im)
307
  # time.sleep(0.02)
 
9
  from scipy.spatial.transform import Rotation as R
10
  from utils import finger_orientation_deg, angle_diff, allow_multiturn
11
  import os
12
+ from recorded_moves import RecordedMoves
13
 
14
  DEBUG = True
15
  FREQUENCY = 50 # Hz
 
101
  cv2.waitKey(1)
102
 
103
 
104
+ def update_hand_pos(self, im):
105
  if self.hand_tracker is None:
106
  self.hand_tracker = HandTracker(model_complexity=0)
107
  hands = self.hand_tracker.get_hands_positions(im)
 
131
  def play_sound(self,reachy_mini, sound_name: str):
132
  if time.time() - self.last_play_sound < 2.0:
133
  return
134
+ sound_path = self.recorded_moves.sounds[sound_name]
 
 
 
 
135
  reachy_mini.media.play_sound(sound_path)
136
  self.last_play_sound = time.time()
137
 
 
164
  if all(count == stable_hand_count for count in self.hand_count_history):
165
  # Le nombre de mains est stable, comparer avec précédent
166
  if stable_hand_count > self.previous_number_hands:
167
+ self.play_sound(reachy_mini, "success2")
168
  elif stable_hand_count < self.previous_number_hands:
169
+ self.play_sound(reachy_mini, "irritated1")
170
 
171
  self.previous_number_hands = stable_hand_count
172
 
 
180
  if time_since_last_hand > self.idle_timeout:
181
  # Enter idle once
182
  if not self.is_idle:
183
+ print("Entering IDLE mode")
184
  self.previous_antenna_angles = reachy_mini.get_present_antenna_joint_positions()
185
 
186
  self.is_idle = True
 
282
  self.hand_count_history = [] # buffer des derniers counts
283
  self.hand_count_buffer_size = 3 # nombre de frames consécutives pour valider
284
 
285
+ self.recorded_moves = RecordedMoves("pollen-robotics/reachy-mini-emotions-library")
286
+
287
 
288
 
289
  tracking_thread = threading.Thread(
 
300
  if self.width is None or self.height is None:
301
  self.height, self.width = im.shape[:2]
302
 
303
+ self.update_hand_pos(im)
304
  if DEBUG:
305
  self.draw(im)
306
  # time.sleep(0.02)
hand_tracker_v2/recorded_moves.py ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import json
2
+ from glob import glob
3
+ from pathlib import Path
4
+ from typing import Any, Dict
5
+ from huggingface_hub import snapshot_download
6
+
7
+ class RecordedMoves:
8
+ """Load a library of recorded moves from a HuggingFace dataset."""
9
+
10
+ def __init__(self, hf_dataset_name: str):
11
+ """Initialize RecordedMoves."""
12
+ self.hf_dataset_name = hf_dataset_name
13
+ self.local_path = snapshot_download(self.hf_dataset_name, repo_type="dataset")
14
+ # self.local_path = Path(__file__).parent.parent.parent / hf_dataset_name
15
+ # print(self.local_path)
16
+ self.moves: Dict[str, Any] = {}
17
+ self.sounds: Dict[str, Any] = {}
18
+
19
+ self.process()
20
+
21
+ def process(self) -> None:
22
+ """Populate recorded moves and sounds."""
23
+ move_paths_tmp = glob(f"{self.local_path}/*.json")
24
+ move_paths = [Path(move_path) for move_path in move_paths_tmp]
25
+ for move_path in move_paths:
26
+ move_name = move_path.stem
27
+
28
+ move = json.load(open(move_path, "r"))
29
+ self.moves[move_name] = move
30
+
31
+ sound_paths_tmp = glob(f"{self.local_path}/*.wav")
32
+ sound_paths = [Path(sound_path) for sound_path in sound_paths_tmp]
33
+ for sound_path in sound_paths:
34
+ sound_name = sound_path.stem
35
+ self.sounds[sound_name] = str(sound_path)