-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.py
49 lines (41 loc) · 1.17 KB
/
example.py
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
#!/usr/bin/env python3
import time, sys
import dlib
import numpy as np
import cozmo
# Load trained detector model
detector = dlib.simple_object_detector("detector.svm")
# Initialize window for results
win = dlib.image_window()
def displayImage(image):
# Convert to numpy array
img = np.array(image.raw_image)
startTime = time.time()
# Detect
dets = detector(img)
print('Took {:5.0f}'.format((time.time() - startTime) * 1000))
win.clear_overlay()
# Display raw Cozmo image
win.set_image(img)
# Display bounding rectangles
win.add_overlay(dets)
def run(sdk_conn):
'''The run method runs once Cozmo is connected.'''
robot = sdk_conn.wait_for_robot()
robot.camera.image_stream_enabled = True
while(True):
image = robot.world.latest_image
# Cozmo may not immediately start video feed
while image is None:
image = robot.world.latest_image
time.sleep(0.1)
# Detect cubes and render image with bounding boxes
displayImage(image)
time.sleep(0.1)
if __name__ == '__main__':
cozmo.setup_basic_logging()
cozmo.robot.Robot.drive_off_charger_on_connect = False
try:
cozmo.connect(run)
except cozmo.ConnectionError as e:
sys.exit("A connection error occurred: %s" % e)