6 import roslib;roslib.load_manifest(
"ml_classifiers")
11 from sklearn.ensemble
import RandomForestClassifier
12 from sklearn.ensemble
import ExtraTreesClassifier
13 from sklearn.externals
import joblib
19 s = rospy.Service(
'predict', ClassifyData, self.
classifyData)
23 if len(data_x) != len(data_y):
24 rospy.logerr(
"Lenght of datas are different")
26 rospy.loginfo(
"InitWithData please wait..")
27 clf = RandomForestClassifier(
28 n_estimators=250, max_features=2, max_depth=29,
29 min_samples_split=2, random_state=0)
30 clf.fit(data_x, data_y)
35 rospy.loginfo(
"InitWithFileModel with%s please wait.."%filename)
36 clf = joblib.load(filename)
46 for predict_data
in self.clf.predict([data.point])]))
47 rospy.loginfo(
"req : " + str(data.point) +
"-> answer : " + str(ret))
48 return ClassifyDataResponse(ret)
52 rospy.loginfo(
"RandomForestServer is running!")
56 if __name__ ==
"__main__":
57 rospy.init_node(
'random_forest_cloth_classifier')
60 train_file = rospy.get_param(
'~random_forest_train_file')
62 rospy.logerr(
"Train File is not Set. Set train_data file or tree model file as ~random_forest_train_file.")
65 if train_file.endswith(
"pkl"):
66 node = RandomForestServer.initWithFileModel(train_file)
69 class_file = rospy.get_param(
'~random_forest_train_class_file')
73 for l
in open(train_file).readlines():
74 float_strings = l.split(
",");
75 data_x.append(map(
lambda x: float(x), float_strings))
77 for l
in open(class_file).readlines():
78 data_y.append(float(l))
81 node = RandomForestServer.initWithData(np.array(data_x), np.array(data_y))
84 rospy.logerr(
"Train Class File is not Set. Set train_data file or tree model file.")
85 rospy.logerr(
"Or Did you expect Extension to be pkl?.")
def initWithFileModel(cls, filename)
def initWithData(cls, data_x, data_y)
def classifyData(self, req)