RAPP Platform  v0.6.0
RAPP Platform is a collection of ROS nodes and back-end processes that aim to deliver ready-to-use generic services to robots
 All Classes Namespaces Files Functions Variables Macros
ontology_class_bridge.py
Go to the documentation of this file.
1 #!/usr/bin/env python
2 
3 #Copyright 2015 RAPP
4 
5 #Licensed under the Apache License, Version 2.0 (the "License");
6 #you may not use this file except in compliance with the License.
7 #You may obtain a copy of the License at
8 
9  #http://www.apache.org/licenses/LICENSE-2.0
10 
11 #Unless required by applicable law or agreed to in writing, software
12 #distributed under the License is distributed on an "AS IS" BASIS,
13 #WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 #See the License for the specific language governing permissions and
15 #limitations under the License.
16 
17 # Author: Athanassios Kintsakis
18 # contact: akintsakis@issel.ee.auth.gr
19 
20 import rospy
21 import sys
22 from os.path import expanduser
23 from app_error_exception import AppError
24 from rapp_platform_ros_communications.srv import (
25  ontologyClassBridgeSrv,
26  ontologyClassBridgeSrvResponse
27  )
28 
29 ## @class OntologyClassBridge
30 # @brief Contains the necessary functions for translating caffe classes to ontology classes
32 
33  ## @brief Implements the getOntologyClassEquivalent service main function
34  # @param req [rapp_platform_ros_communications::ontologyClassBridgeSrvRequest::Request&] The ROS service request
35  #
36  # @return res [rapp_platform_ros_communications::ontologyClassBridgeSrvResponse::Response&] The ROS service response
37  # @exception Exception KeyError
39  try:
40  res=ontologyClassBridgeSrvResponse()
41  mapFile = home = expanduser("~")+rospy.get_param("ontology_class_bridge_file")
42  caffeToOntologyClassesDict=self.loadMappingIntoDictionary(mapFile)
43  if (req.caffeClass in caffeToOntologyClassesDict):
44  res.ontologyClass=caffeToOntologyClassesDict[req.caffeClass]
45  res.existsInOntology=True
46  else:
47  res.existsInOntology=False
48  res.ontologyClass="SpatialThing-Localized-Unspecified"
49  res.success=True
50 
51  except KeyError, e:
52  res.success=False
53  res.trace.append('"KeyError, probably caffe class does not exist or no ontology equivalent exists for "%s"' % str(e))
54  res.error='"KeyError, probably caffe class does not exist or no ontology equivalent exists for "%s"' % str(e)
55  return res
56 
57  ## @brief Loads the caffeToOntologyClasses file into a dictionary
58  # @param mapFilePath [string] The path to the file containing the caffe to ontology classes equivalencies
59  #
60  # @return caffeToOntologyClassesDict [dictionary] The dictionary containing the caffe to ontology classes
61  def loadMappingIntoDictionary(self,mapFilePath):
62  with open(mapFilePath) as f:
63  lines = f.read().splitlines()
64  caffeToOntologyClassesDict=dict()
65  for s in lines:
66  currentList=s.split("\t")
67  if(len(currentList)>=3):
68  caffeToOntologyClassesDict[currentList[1]]=currentList[2]
69  return caffeToOntologyClassesDict
Contains the necessary functions for translating caffe classes to ontology classes.
def loadMappingIntoDictionary
Loads the caffeToOntologyClasses file into a dictionary.
def getOntologyClassEquivalent
Implements the getOntologyClassEquivalent service main function.