23 import matplotlib.pyplot
as plt
25 from os.path
import expanduser
26 from app_error_exception
import AppError
32 from rapp_platform_ros_communications.srv
import (
33 imageClassificationSrv,
34 imageClassificationSrvResponse,
35 registerImageToOntologySrv,
36 registerImageToOntologySrvResponse,
37 registerImageToOntologySrvRequest,
38 ontologyClassBridgeSrv,
39 ontologyClassBridgeSrvResponse,
40 ontologyClassBridgeSrvRequest
50 self.
caffe_root = expanduser(
"~")+
'/rapp_platform/caffe/'
53 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'):
54 print(
"Downloading pre-trained CaffeNet model...")
55 os.system(self.
caffe_root+
"scripts/download_model_binary.py \
56 ../models/bvlc_reference_caffenet")
61 'models/bvlc_reference_caffenet/deploy.prototxt',\
63 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel',\
67 caffe.io.Transformer({
'data': self.net.blobs[
'data'].data.shape})
68 self.transformer.set_transpose(
'data', (2, 0, 1))
69 self.transformer.set_mean(
'data', \
71 'python/caffe/imagenet/ilsvrc_2012_mean.npy').mean(1).mean(1))
72 self.transformer.set_raw_scale(
'data', 255)
73 self.transformer.set_channel_swap(
'data', (2, 1, 0))
74 self.net.blobs[
'data'].reshape(1, 3, 227, 227)
75 imagenet_labels_filename = self.
caffe_root +
'data/ilsvrc12/synset_words.txt'
76 self.
labels = np.loadtxt(imagenet_labels_filename, str, delimiter=
'\t')
88 res = imageClassificationSrvResponse()
89 start_time = time.time()
91 res.objectClass = caffeObjectClass
93 if(req.registerToOntology):
96 caffeObjectClass, ontologyClass, req.objectFileUrl)
97 res.ontologyNameOfImage = ontologyNameOfImage
101 except IndexError, e:
102 res.trace.append(
"IndexError: " + str(e))
103 res.error =
"IndexError: "+ str(e)
107 res.trace.append(
"IOError: " + str(e))
108 res.error =
"IOError: " + str(e)
111 res.trace.append(
'"KeyError (probably invalid cfg/.yaml parameter) \
113 res.error =
'"KeyError (probably invalid cfg/.yaml parameter) "%s"' \
115 except AppError
as e:
116 AppError.passErrorToRosSrv(e,res)
125 serv_topic = rospy.get_param(\
126 'rapp_caffe_wrapper_get_ontology_class_equivalent')
127 knowrob_service = rospy.ServiceProxy(serv_topic, ontologyClassBridgeSrv)
128 ontologyClassBridgeReq = ontologyClassBridgeSrvRequest()
129 ontologyClassBridgeReq.caffeClass = caffeObjectClass
130 ontologyClassBridgeResponse = knowrob_service(ontologyClassBridgeReq)
131 if(ontologyClassBridgeResponse.success !=
True):
132 raise AppError(ontologyClassBridgeResponse.error, \
133 ontologyClassBridgeResponse.trace)
134 return ontologyClassBridgeResponse.ontologyClass
146 serv_topic = rospy.get_param(
'rapp_caffe_wrapper_register_image_to_ontology')
147 knowrob_service = rospy.ServiceProxy(serv_topic, registerImageToOntologySrv)
148 registerImageToOntologyReq = registerImageToOntologySrvRequest()
149 registerImageToOntologyReq.username = req.username
150 registerImageToOntologyReq.ontologyClass = ontologyClass
151 registerImageToOntologyReq.caffeClass = caffeObjectClass
152 registerImageToOntologyReq.imagePath = currentImagePath
153 registerImageToOntologyResponse = knowrob_service(registerImageToOntologyReq)
154 if(registerImageToOntologyResponse.success !=
True):
155 raise AppError(registerImageToOntologyResponse.error, \
156 registerImageToOntologyResponse.trace)
157 return registerImageToOntologyResponse.object_entry
164 self.net.blobs[
'data'].data[...] = \
165 self.transformer.preprocess(
'data', \
166 caffe.io.load_image(req.objectFileUrl))
167 out = self.net.forward()
168 top_k = self.net.blobs[
'prob'].data[0].flatten().argsort()[-1:-6:-1]
169 objectClass = self.
labels[top_k][0]
Exception compliant with the ros error and trace srvs.
def getImageClass
Obtains the name of the class of the image from the caffe image classes file.
Contains the necessary functions for classifying an images and requesting registration to the ontolog...
def classifyImage
Implements the imageClassificationSrv service main function.
def getOntologyClass
Calls the caffe wrapper service that registers the image to the ontology.
def registerToOntology
Calls the caffe wrapper service that registers the image to the ontology.
def __init__
Initializes Caffe.