forked from surajitsaikia27/Vector_robot_ObjectDetection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvector_objectDetection.py
More file actions
99 lines (72 loc) · 2.6 KB
/
vector_objectDetection.py
File metadata and controls
99 lines (72 loc) · 2.6 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
99
#!/usr/bin/env python3
"""Making Vector to detect 600 objects from Google OpenImageDataset"""
import time
import anki_vector
from model.detect_odr import object_detection
from PIL import Image
from anki_vector.util import degrees
import anki_vector.camera
robot = anki_vector.Robot(anki_vector.util.parse_command_args().serial)#, enable_camera_feed=True)
screen_dimensions = anki_vector.screen.SCREEN_WIDTH, anki_vector.screen.SCREEN_HEIGHT
image_name = "detect.jpg"
def get_classnames(image_path):
"""
This function calls the object detection library to detect 600 objects
:param image_path:
:return: class labels
"""
try:
classes = object_detection(image_path)
if len(classes) == 0:
return 'no objects'
class_list = []
for class_names in classes:
class_list.append(class_names)
print('Labels: {}'.format(classes))
return ', '.join(class_list)
except:
return 'no objects'
def connect_robot():
robot.connect()
print('Connecting vector')
def disconnect_robot():
robot.disconnect()
print('Disconnected vector')
def display_camera():
robot.camera.init_camera_feed()
robot.vision.enable_display_camera_feed_on_face(True)
def close_camera():
print('Close camera')
robot.vision.enable_display_camera_feed_on_face(False)
robot.camera.close_camera_feed()
def save_image(file_name):
print('Save image')
image = robot.camera.latest_image.raw_image.save(file_name, 'JPEG')
def display_image(file_name):
print('display image = {}'.format(file_name))
image = Image.open(file_name)
screen_data = anki_vector.screen.convert_image_to_screen_data(image.resize(screen_dimensions))
robot.screen.set_screen_with_image_data(screen_data, 5.0, True)
def vector_speaks(text):
print('Vector: {}'.format(text))
robot.behavior.say_text(text)
def detect():
display_camera()
vector_speaks('Hey, I am going to find some objects, and will tell you what I found')
vector_speaks('I will take a photo of this environment, and will analyze using my deep learning based brain')
time.sleep(1)
save_image(image_name)
display_image(image_name)
vector_speaks('Wait a minute. I am trying to find some objects, I will let you know now.')
text = get_classnames(image_name)
display_image(image_name)
vector_speaks('I can detect {}'.format(text))
close_camera()
if __name__ == "__main__":
while True:
connect_robot()
try:
detect()
except Exception as e:
print('Exception Handled', e)
disconnect_robot()