forked from anki/vector-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path18_annotate.py
More file actions
executable file
·98 lines (75 loc) · 3.36 KB
/
18_annotate.py
File metadata and controls
executable file
·98 lines (75 loc) · 3.36 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
93
94
95
96
97
98
#!/usr/bin/env python3
# Copyright (c) 2019 Anki, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License in the file LICENSE.txt or at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
'''Display a GUI window showing an annotated camera view.
Note:
This example requires Python to have Tkinter installed to display the GUI.
This example uses tkinter to display the annotated camera feed on the screen
and adds a couple of custom annotations of its own using two different methods.
'''
import asyncio
import sys
import time
from PIL import ImageDraw
import anki_vector
from anki_vector import annotate
# Define an annotator using the annotator decorator
@annotate.annotator
def clock(image, scale, annotator=None, world=None, **kw):
d = ImageDraw.Draw(image)
bounds = (0, 0, image.width, image.height)
text = annotate.ImageText(time.strftime("%H:%m:%S"),
position=annotate.AnnotationPosition.TOP_LEFT,
outline_color="black")
text.render(d, bounds)
# Define a new annotator by inheriting the base annotator class
class Battery(annotate.Annotator):
def __init__(self, img_annotator, box_color=None):
super().__init__(img_annotator)
self.battery_state = None
self.battery_state_task = None
if box_color is not None:
self.box_color = box_color
def apply(self, image, scale):
d = ImageDraw.Draw(image)
bounds = (0, 0, image.width, image.height)
if not self.battery_state_task:
self.battery_state_task = self.world.robot.get_battery_state()
if asyncio.isfuture(self.battery_state_task) and self.battery_state_task.done():
self.battery_state = self.battery_state_task.result()
self.battery_state_task = self.world.robot.get_battery_state()
if self.battery_state:
batt = self.battery_state.battery_volts
text = annotate.ImageText(f"BATT {batt:.1f}v", color="green", outline_color="black")
text.render(d, bounds)
def main():
args = anki_vector.util.parse_command_args()
with anki_vector.Robot(args.serial, show_viewer=True, enable_face_detection=True) as robot:
robot.camera.image_annotator.add_static_text("text", "Vec-Cam", position=annotate.AnnotationPosition.TOP_RIGHT)
robot.camera.image_annotator.add_annotator("clock", clock)
robot.camera.image_annotator.add_annotator("battery", Battery)
time.sleep(10)
print("Turning off all annotations for 5 seconds")
robot.camera.image_annotator.annotation_enabled = False
time.sleep(5)
print("Re-enabling all annotations")
robot.camera.image_annotator.annotation_enabled = True
print("------ Press ctrl+c to exit early ------")
try:
# Shutdown the program after 30 seconds
time.sleep(30)
except KeyboardInterrupt:
pass
if __name__ == "__main__":
main()