apirrone commited on
Commit
f337e6b
·
1 Parent(s): bfa6ede

first test

Browse files
.gitattributes CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
 
 
33
  *.zip filter=lfs diff=lfs merge=lfs -text
34
  *.zst filter=lfs diff=lfs merge=lfs -text
35
  *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ *.task filter=lfs diff=lfs merge=lfs -text
pyproject.toml CHANGED
@@ -4,15 +4,16 @@ build-backend = "setuptools.build_meta"
4
 
5
 
6
  [project]
7
- name = "reachy_mini_app_example"
8
  version = "0.1.0"
9
- description = "Add your description here"
10
  readme = "README.md"
11
  requires-python = ">=3.8"
12
  # dependencies = ["reachy-mini"]
13
  dependencies = [
14
  "reachy-mini@git+https://github.com/pollen-robotics/reachy_mini",
 
15
  ] # TODO open
16
 
17
  [project.entry-points."reachy_mini_apps"]
18
- reachy_mini_app_example = "reachy_mini_app_example.main:ExampleApp"
 
4
 
5
 
6
  [project]
7
+ name = "reachy_mini_red_light_green_light"
8
  version = "0.1.0"
9
+ description = "Red green light game for Reachy Mini"
10
  readme = "README.md"
11
  requires-python = ">=3.8"
12
  # dependencies = ["reachy-mini"]
13
  dependencies = [
14
  "reachy-mini@git+https://github.com/pollen-robotics/reachy_mini",
15
+ "mediapipe",
16
  ] # TODO open
17
 
18
  [project.entry-points."reachy_mini_apps"]
19
+ reachy_mini_red_light_green_light = "reachy_mini_red_light_green_light.main:RedLightGreenLightApp"
reachy_mini_app_example/main.py DELETED
@@ -1,28 +0,0 @@
1
- import threading
2
- import time
3
-
4
- import numpy as np
5
- from reachy_mini import ReachyMiniApp
6
- from reachy_mini.reachy_mini import ReachyMini
7
- from scipy.spatial.transform import Rotation as R
8
-
9
-
10
- class ExampleApp(ReachyMiniApp):
11
- def run(self, reachy_mini: ReachyMini, stop_event: threading.Event):
12
- t0 = time.time()
13
- while not stop_event.is_set():
14
- pose = np.eye(4)
15
- pose[:3, 3][2] = 0.005 * np.sin(2 * np.pi * 0.3 * time.time() + np.pi)
16
- euler_rot = [
17
- 0,
18
- 0,
19
- 0.5 * np.sin(2 * np.pi * 0.3 * time.time() + np.pi),
20
- ]
21
- rot_mat = R.from_euler("xyz", euler_rot, degrees=False).as_matrix()
22
- pose[:3, :3] = rot_mat
23
- pose[:3, 3][2] += 0.01 * np.sin(2 * np.pi * 0.5 * time.time())
24
- antennas = np.array([1, 1]) * np.sin(2 * np.pi * 0.5 * time.time())
25
-
26
- reachy_mini.set_target(head=pose, antennas=antennas)
27
-
28
- time.sleep(0.02)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
{reachy_mini_app_example → reachy_mini_red_light_green_light}/__init__.py RENAMED
File without changes
reachy_mini_red_light_green_light/main.py ADDED
@@ -0,0 +1,210 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import threading
2
+ import time
3
+
4
+ import numpy as np
5
+ from reachy_mini import ReachyMiniApp
6
+ from reachy_mini.reachy_mini import ReachyMini
7
+ from reachy_mini.io.cam_utils import find_camera
8
+ from reachy_mini.utils import create_head_pose
9
+ from people_detector import PeopleDetector
10
+ import cv2
11
+ from reachy_mini.reachy_mini import (
12
+ SLEEP_HEAD_JOINT_POSITIONS,
13
+ SLEEP_ANTENNAS_JOINT_POSITIONS,
14
+ )
15
+
16
+
17
+ class RedLightGreenLightApp(ReachyMiniApp):
18
+ def run(self, reachy_mini: ReachyMini, stop_event: threading.Event):
19
+ cap = find_camera()
20
+ if cap is None:
21
+ raise RuntimeError("No camera found. Please connect a camera.")
22
+
23
+ # arbitrary 3 maximum people are tracked. Maybe make it a parameter?
24
+ people_detector = PeopleDetector(
25
+ model_path="pose_landmarker_full.task", max_poses=3
26
+ )
27
+
28
+ def sequence():
29
+ time.sleep(1)
30
+ sleep_head_joint_positions = SLEEP_HEAD_JOINT_POSITIONS.copy()
31
+ sleep_head_joint_positions[0] = np.deg2rad(170)
32
+ reachy_mini._goto_joint_positions(
33
+ head_joint_positions=sleep_head_joint_positions,
34
+ antennas_joint_positions=SLEEP_ANTENNAS_JOINT_POSITIONS,
35
+ duration=2.0,
36
+ )
37
+ time.sleep(1)
38
+ sleep_1_time = np.random.rand() + 0.4 * 1.3
39
+ sleep_2_time = np.random.rand() + 0.4 * 1.3
40
+ sleep_3_time = np.random.rand() + 0.3 * 3.0
41
+ print("1...")
42
+ reachy_mini.play_sound("count.wav")
43
+ sleep_head_joint_positions[0] = np.deg2rad(160)
44
+ reachy_mini._goto_joint_positions(
45
+ head_joint_positions=sleep_head_joint_positions,
46
+ antennas_joint_positions=SLEEP_ANTENNAS_JOINT_POSITIONS,
47
+ duration=0.2,
48
+ )
49
+ sleep_head_joint_positions[0] = np.deg2rad(170)
50
+ reachy_mini._goto_joint_positions(
51
+ head_joint_positions=sleep_head_joint_positions,
52
+ antennas_joint_positions=SLEEP_ANTENNAS_JOINT_POSITIONS,
53
+ duration=sleep_1_time,
54
+ )
55
+ print("2...")
56
+ reachy_mini.play_sound("count.wav")
57
+ sleep_head_joint_positions[0] = np.deg2rad(150)
58
+ reachy_mini._goto_joint_positions(
59
+ head_joint_positions=sleep_head_joint_positions,
60
+ antennas_joint_positions=SLEEP_ANTENNAS_JOINT_POSITIONS,
61
+ duration=0.2,
62
+ )
63
+ sleep_head_joint_positions[0] = np.deg2rad(170)
64
+ reachy_mini._goto_joint_positions(
65
+ head_joint_positions=sleep_head_joint_positions,
66
+ antennas_joint_positions=SLEEP_ANTENNAS_JOINT_POSITIONS,
67
+ duration=sleep_2_time,
68
+ )
69
+ print("3...")
70
+ reachy_mini.play_sound("count.wav")
71
+ sleep_head_joint_positions[0] = np.deg2rad(0)
72
+ reachy_mini._goto_joint_positions(
73
+ head_joint_positions=sleep_head_joint_positions,
74
+ antennas_joint_positions=SLEEP_ANTENNAS_JOINT_POSITIONS,
75
+ duration=sleep_3_time,
76
+ )
77
+ # sleep_head_joint_positions[0] = np.deg2rad(170)
78
+ # reachy_mini._goto_joint_positions(
79
+ # head_joint_positions=sleep_head_joint_positions,
80
+ # antennas_joint_positions=SLEEP_ANTENNAS_JOINT_POSITIONS,
81
+ # duration=sleep_3_time,
82
+ # )
83
+ print("SOLEIL !!!")
84
+ reachy_mini.play_sound("dance1.wav")
85
+ reachy_mini.goto_target(np.eye(4), antennas=[0.0, 0.0], duration=0.5)
86
+
87
+ def scan():
88
+ print("Scanning ...")
89
+ time.sleep(0.5)
90
+ t0 = time.time()
91
+ scan_for = (
92
+ np.random.rand() * 3 + 4
93
+ ) # Random scan duration between 4 and 7 seconds
94
+ buffer = []
95
+ moved = []
96
+ reachy_mini.play_sound("confused1.wav")
97
+ last_watched = time.time()
98
+ while time.time() - t0 < scan_for:
99
+ ret, frame = cap.read()
100
+ if not ret:
101
+ break
102
+
103
+ # Look at a random player
104
+ if len(buffer) > 0:
105
+ if time.time() - last_watched >= (scan_for / (len(buffer) + 2)):
106
+ print("LOOK")
107
+ # TODO replace with reachy_mini.look_at_image
108
+ selected_to_watch = np.random.randint(0, len(buffer))
109
+ left_shoulder = buffer[selected_to_watch][-1][0]
110
+ r_shoulder = buffer[selected_to_watch][-1][1]
111
+ look_at_px = (
112
+ (left_shoulder[0] + r_shoulder[0]) // 2,
113
+ (left_shoulder[1] + r_shoulder[1]) // 2,
114
+ )
115
+
116
+ normalized_look_at = (
117
+ ((look_at_px[0] / frame.shape[1]) - 0.5) * 2,
118
+ ((look_at_px[1] / frame.shape[0]) - 0.5) * 2,
119
+ ) # Normalize to [-1, 1]
120
+ yaw_range = [-80, 80]
121
+ target_yaw = (
122
+ normalized_look_at[0] * (yaw_range[1] - yaw_range[0]) / 2
123
+ + (yaw_range[1] + yaw_range[0]) / 2
124
+ )
125
+ pose = create_head_pose(
126
+ yaw=-target_yaw,
127
+ )
128
+ reachy_mini.goto_target(
129
+ head=pose, antennas=[0.5, 0.0], duration=0.3
130
+ )
131
+
132
+ last_watched = time.time()
133
+ buffer = []
134
+ moved = []
135
+
136
+ frame, detected_people = people_detector.detect(frame)
137
+ if len(detected_people) > 0:
138
+ if len(detected_people) > len(buffer):
139
+ for i in range(len(detected_people) - len(buffer)):
140
+ # Initialize buffer with empty lists for new detected people
141
+ buffer.append([])
142
+
143
+ if len(detected_people) > len(moved):
144
+ for i in range(len(detected_people) - len(moved)):
145
+ moved.append(0)
146
+
147
+ scores = np.zeros(len(buffer))
148
+ for i, person in enumerate(detected_people):
149
+ landmarks = person["landmarks"]
150
+ buffer[i].append(landmarks)
151
+ buffer[i] = buffer[i][-5:]
152
+ if len(buffer[i]) >= 5: # if the buffer is full
153
+ scores[i] = np.mean(np.std(buffer[i], axis=0))
154
+
155
+ for i in range(len(scores)):
156
+ if scores[i] > 3.0:
157
+ # If the score is high, mark the person as moving
158
+ cv2.putText(
159
+ frame,
160
+ f"Person {i + 1} Moving",
161
+ (10, 30 + i * 20),
162
+ cv2.FONT_HERSHEY_SIMPLEX,
163
+ 0.5,
164
+ (0, 0, 255),
165
+ 2,
166
+ )
167
+ moved[i] += 1
168
+
169
+ stop = False
170
+ look_at_px = None
171
+ for i in range(len(moved)):
172
+ if moved[i] > 10:
173
+ stop = True
174
+ left_shoulder = buffer[i][-1][0]
175
+ r_shoulder = buffer[i][-1][1]
176
+ look_at_px = (
177
+ (left_shoulder[0] + r_shoulder[0]) // 2,
178
+ (left_shoulder[1] + r_shoulder[1]) // 2,
179
+ )
180
+
181
+ if stop:
182
+ print("STOP !!!")
183
+ normalized_look_at = (
184
+ ((look_at_px[0] / frame.shape[1]) - 0.5) * 2,
185
+ ((look_at_px[1] / frame.shape[0]) - 0.5) * 2,
186
+ ) # Normalize to [-1, 1]
187
+ yaw_range = [-80, 80]
188
+ target_yaw = (
189
+ normalized_look_at[0] * (yaw_range[1] - yaw_range[0]) / 2
190
+ + (yaw_range[1] + yaw_range[0]) / 2
191
+ )
192
+ pose = create_head_pose(
193
+ yaw=-target_yaw,
194
+ )
195
+ reachy_mini.goto_target(
196
+ head=pose, antennas=[-0.5, 0.5], duration=1.0
197
+ )
198
+ reachy_mini.play_sound("impatient1.wav")
199
+
200
+ time.sleep(2)
201
+ return
202
+
203
+ cv2.imshow("Pose Detection", frame)
204
+ cv2.waitKey(1)
205
+
206
+ cv2.destroyAllWindows()
207
+
208
+ while not stop_event.is_set():
209
+ sequence()
210
+ scan()
reachy_mini_red_light_green_light/people_detector.py ADDED
@@ -0,0 +1,45 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import mediapipe as mp
3
+
4
+ UPPER_BODY_LANDMARKS = [11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22]
5
+
6
+
7
+ class PeopleDetector:
8
+ def __init__(self, model_path="pose_landmarker_full.task", max_poses=5):
9
+ # Initialisation de MediaPipe Tasks
10
+ BaseOptions = mp.tasks.BaseOptions
11
+ PoseLandmarker = mp.tasks.vision.PoseLandmarker
12
+ PoseLandmarkerOptions = mp.tasks.vision.PoseLandmarkerOptions
13
+ VisionRunningMode = mp.tasks.vision.RunningMode
14
+
15
+ self.pose_landmarker = PoseLandmarker.create_from_options(
16
+ PoseLandmarkerOptions(
17
+ base_options=BaseOptions(model_asset_path=model_path),
18
+ running_mode=VisionRunningMode.IMAGE,
19
+ num_poses=max_poses,
20
+ )
21
+ )
22
+
23
+ def detect(self, frame, draw=True):
24
+ mp_image = mp.Image(
25
+ image_format=mp.ImageFormat.SRGB,
26
+ data=cv2.cvtColor(frame, cv2.COLOR_BGR2RGB),
27
+ )
28
+ result = self.pose_landmarker.detect(mp_image)
29
+
30
+ h, w, _ = frame.shape
31
+ detected_people = []
32
+
33
+ if result.pose_landmarks:
34
+ for person_landmarks in result.pose_landmarks:
35
+ person = {"landmarks": []}
36
+ for idx, lm in enumerate(person_landmarks):
37
+ if idx not in UPPER_BODY_LANDMARKS:
38
+ continue
39
+ cx, cy = int(lm.x * w), int(lm.y * h)
40
+ person["landmarks"].append((cx, cy))
41
+ if draw:
42
+ cv2.circle(frame, (cx, cy), 3, (0, 255, 0), -1)
43
+ detected_people.append(person)
44
+
45
+ return frame, detected_people
reachy_mini_red_light_green_light/pose_landmarker_full.task ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:4eaa5eb7a98365221087693fcc286334cf0858e2eb6e15b506aa4a7ecdcec4ad
3
+ size 9398198