forked from udacity/RoboND-Perception-Project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcapture_features.py
95 lines (83 loc) · 3.02 KB
/
capture_features.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
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
#!/usr/bin/env python
import numpy as np
import pickle
import rospy
import time
from gazebo_msgs.srv import GetModelState
from sensor_stick.pcl_helper import *
from sensor_stick.training_helper import spawn_model
from sensor_stick.training_helper import delete_model
from sensor_stick.training_helper import initial_setup
from sensor_stick.training_helper import capture_sample
from sensor_stick.features import compute_color_histograms
from sensor_stick.features import compute_normal_histograms
from sensor_stick.srv import GetNormals
from geometry_msgs.msg import Pose
from sensor_msgs.msg import PointCloud2
def get_normals(cloud):
get_normals_prox = rospy.ServiceProxy('/feature_extractor/get_normals', GetNormals)
return get_normals_prox(cloud).cluster
def get_model_state():
get_model_state_prox = rospy.ServiceProxy('gazebo/get_model_state', GetModelState)
model_state = get_model_state_prox('training_model', 'world')
print 'model_state.pose:', model_state.pose
print 'model_state.success:', model_state.success
print 'model_state.status_message:', model_state.status_message
if __name__ == '__main__':
rospy.init_node('capture_node')
models = [
'sticky_notes',
'book',
'snacks',
'biscuits',
'eraser',
'soap2',
'soap',
'glue']
# Disable gravity and delete the ground plane
initial_setup()
labeled_features = []
for model_name in models:
spawn_model(model_name)
for i in range(300):
print '* Model|iteration: {} ({})'.format(model_name, i)
sample_was_good = False
try_count = 0
while not sample_was_good and try_count < 20:
sample_cloud = capture_sample()
sample_cloud_arr = ros_to_pcl(sample_cloud).to_array()
# Check for invalid clouds.
if sample_cloud_arr.shape[0] == 0:
print('Invalid cloud detected')
try_count += 1
else:
sample_was_good = True
# Extract histogram features
chists = compute_color_histograms(sample_cloud, using_hsv=True)
normals = get_normals(sample_cloud)
nhists = compute_normal_histograms(normals)
feature = np.concatenate((chists, nhists))
labeled_features.append([feature, model_name])
try:
get_model_state()
delete_model()
get_model_state()
except Exception as e:
print 'error:', e.message
try:
delete_model()
get_model_state()
except Exception as e:
print 'error:', e.message
try:
delete_model()
get_model_state()
except Exception as e:
print 'error:', e.message
try:
delete_model()
get_model_state()
except Exception as e:
print 'error:', e.message
time.sleep(5)
pickle.dump(labeled_features, open('training_set.sav', 'wb'))