Repository navigation
Expand file tree
/
Copy pathlook_at_image.py
More file actions
92 lines (70 loc) · 2.87 KB
/
Copy pathlook_at_image.py
File metadata and controls
92 lines (70 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""Demonstrate how to make Reachy Mini look at a point in an image.
When you click on the image, Reachy Mini will look at the point you clicked on.
It uses OpenCV to capture video from a camera and display it, and Reachy Mini's
look_at_image method to make the robot look at the specified point.
Note:
This example requires the OpenCV optional dependency.
Install with: pip install reachy_mini[opencv]
The daemon must be running before executing this script.
"""
# START doc_example
import argparse
import sys
from typing import Any
try:
import cv2
except ImportError:
print("Error: OpenCV is required for this example but not installed.")
print("Install it with: pip install reachy_mini[opencv]")
sys.exit(1)
from reachy_mini import ReachyMini
# from reachy_mini.media.camera_constants import CameraResolution
def click(event: int, x: int, y: int, flags: int, param: Any) -> None:
"""Handle mouse click events to get the coordinates of the click."""
if event == cv2.EVENT_LBUTTONDOWN:
param["just_clicked"] = True
param["x"] = x
param["y"] = y
def main(backend: str) -> None:
"""Show the camera feed from Reachy Mini and make it look at clicked points."""
state = {"x": 0, "y": 0, "just_clicked": False}
cv2.namedWindow("Reachy Mini Camera")
cv2.setMouseCallback("Reachy Mini Camera", click, param=state)
print("Click on the image to make ReachyMini look at that point.")
print("Press 'q' to quit the camera feed.")
with ReachyMini(media_backend=backend) as reachy_mini:
# Uncomment these three lines to change resolution
# reachy_mini.media.camera.close()
# reachy_mini.media.camera.set_resolution(CameraResolution.R3072x1728at10fps)
# reachy_mini.media.camera.open()
try:
while True:
frame = reachy_mini.media.get_frame()
if frame is None:
print("Failed to grab frame.")
continue
cv2.imshow("Reachy Mini Camera", frame)
if cv2.waitKey(1) & 0xFF == ord("q"):
print("Exiting...")
break
if state["just_clicked"]:
reachy_mini.look_at_image(state["x"], state["y"], duration=0.3)
state["just_clicked"] = False
except KeyboardInterrupt:
print("Interrupted. Closing viewer...")
finally:
cv2.destroyAllWindows()
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Display Reachy Mini's camera feed and make it look at clicked points."
)
parser.add_argument(
"--backend",
type=str,
choices=["default", "local", "webrtc"],
default="default",
help="Media backend to use.",
)
args = parser.parse_args()
main(backend=args.backend)
# END doc_example