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
image_registration_to_ontology.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 import numpy as np
23 import matplotlib.pyplot as plt
24 import time
25 import os
26 import shutil
27 from os.path import expanduser
28 from app_error_exception import AppError
29 from rapp_platform_ros_communications.srv import (
30  createOntologyAliasSrv,
31  createOntologyAliasSrvRequest,
32  createOntologyAliasSrvResponse,
33  registerImageToOntologySrv,
34  registerImageToOntologySrvResponse,
35  registerImageToOntologySrvRequest,
36  registerImageObjectToOntologySrv,
37  registerImageObjectToOntologySrvRequest,
38  registerImageObjectToOntologySrvResponse
39  )
40 
41 
42 ## @class ImageRegistrationToOntology
43 # @brief Contains the necessary functions for registering images to the ontology
45 
46  ## @brief Implements the getCloudAgentServiceTypeAndHostPort service main function
47  # @param req [rapp_platform_ros_communications::registerImageToOntologySrvRequest::Request&] The ROS service request
48  #
49  # @return res [rapp_platform_ros_communications::registerImageToOntologySrvResponse::Response&] The ROS service response
50  # @exception Exception IndexError
51  # @exception Exception IOError
52  # @exception Exception AppError
53  # @exception Exception KeyError
54  def registerImage(self,req):
55  try:
56  res = registerImageToOntologySrvResponse()
57  ontologyAlias=self.getUserOntologyAlias(req.username)
58  baseDestinationFolder = rospy.get_param("user_images_folder")+req.username+"_"+ontologyAlias+"/"
59  ontologyName=self.registerImageToOntology(req,baseDestinationFolder)
60  ontologyName=ontologyName.split('#')[1]
61 
62  baseDestinationFolderUserAdjusted=expanduser("~")+baseDestinationFolder
63  print baseDestinationFolderUserAdjusted
64  if not os.path.exists(baseDestinationFolderUserAdjusted):
65  os.makedirs(baseDestinationFolderUserAdjusted)
66 
67  shutil.copy(req.imagePath, baseDestinationFolderUserAdjusted+ontologyName)
68  res.object_entry=ontologyName
69  res.success=True
70 
71  except IndexError, e:
72  res.trace.append("IndexError: " +str(e))
73  res.error="IndexError: "+str(e)
74  res.success=False
75  except IOError, e:
76  res.success=False
77  res.trace.append("IOError: "+str(e))
78  res.error="IOError: "+str(e)
79  except AppError as e:
80  AppError.passErrorToRosSrv(e,res)
81  except KeyError, e:
82  res.success=False
83  res.trace.append('"KeyError, probably caffe class does not exist or no ontology equivalent exists for "%s"' % str(e))
84  res.error='"KeyError, probably caffe class does not exist or no ontology equivalent exists for "%s"' % str(e)
85  return res
86 
87 
88  ## @brief Gets the users ontology alias and if it doesnt exist it creates it
89  # @param username [string] The user's username
90  #
91  # @return ontologyAlias [string] The user's ontology alias
92  # @exception Exception AppError
93  def getUserOntologyAlias(self,username):
94  serv_topic = rospy.get_param('rapp_knowrob_wrapper_create_ontology_alias')
95  knowrob_service = rospy.ServiceProxy(serv_topic, createOntologyAliasSrv)
96  createOntologyAliasReq = createOntologyAliasSrvRequest()
97  createOntologyAliasReq.username=username
98  createOntologyAliasResponse = knowrob_service(createOntologyAliasReq)
99  if(createOntologyAliasResponse.success!=True):
100  raise AppError(createOntologyAliasResponse.error, createOntologyAliasResponse.trace)
101  return createOntologyAliasResponse.ontology_alias
102 
103  ## @brief Calls the knowrob_wrapper service that registers the image to the ontology
104  # @param req [rapp_platform_ros_communications::registerImageToOntologySrvRequest::Request&] The ROS service request
105  # @param baseDestinationFolder [string] The path to the image
106  #
107  # @return registerImageObjectToOntologyResponse.object_entry [string] The ontology alias image entry
108  # @exception Exception AppError
109  def registerImageToOntology(self,req,baseDestinationFolder):
110  serv_topic = rospy.get_param('rapp_knowrob_wrapper_register_image_object_to_ontology')
111  knowrob_service = rospy.ServiceProxy(serv_topic, registerImageObjectToOntologySrv)
112  registerImageObjectToOntologyReq = registerImageObjectToOntologySrvRequest()
113  registerImageObjectToOntologyReq.user_ontology_alias=self.getUserOntologyAlias(req.username)
114  registerImageObjectToOntologyReq.image_path=baseDestinationFolder
115  registerImageObjectToOntologyReq.caffe_class=req.caffeClass
116  registerImageObjectToOntologyReq.timestamp=int(time.time())
117  registerImageObjectToOntologyReq.object_ontology_class=req.ontologyClass
118  registerImageObjectToOntologyResponse = knowrob_service(registerImageObjectToOntologyReq)
119  if(registerImageObjectToOntologyResponse.success!=True):
120  registerImageObjectToOntologyResponse.trace.append("Error in registering the image to the ontology")
121  raise AppError(registerImageObjectToOntologyResponse.error+"Error in registering the image to the ontology",registerImageObjectToOntologyResponse.trace)
122  return registerImageObjectToOntologyResponse.object_entry
123 
def registerImage
Implements the getCloudAgentServiceTypeAndHostPort service main function.
def getUserOntologyAlias
Gets the users ontology alias and if it doesnt exist it creates it.
def registerImageToOntology
Calls the knowrob_wrapper service that registers the image to the ontology.
Contains the necessary functions for registering images to the ontology.
Exception compliant with the ros error and trace srvs.