コード例 #1
0
ファイル: node.py プロジェクト: arnthorm/Fable
  def __init__(self, command=None, args=[]):
    self.manager = PostureManager('%s/db.p' %
                                  roslib.packages.get_pkg_dir('posture_detection'))
    self.subscriptions = []
    self.command = command
    self.args = args

    rospy.init_node('posture', anonymous=True)

    if command == 'add':
      self.subscriptions.append(
        rospy.Subscriber('user_ang', UserAng, self.snapshot_callback)
      )
    elif command == 'remove':
      self.manager.remove_posture(self.args[0])
      rospy.signal_shutdown('')
    elif command == 'list':
      for key in self.manager.database:
        print key
      rospy.signal_shutdown('')
    else:
      rospy.Subscriber('user_ang', UserAng, self.userAng_callback)

    self.pub = rospy.Publisher('posture_detected', PostureDetected)
    rospy.spin()
コード例 #2
0
ファイル: node.py プロジェクト: arnthorm/Fable
class PostureNode:
  def __init__(self, command=None, args=[]):
    self.manager = PostureManager('%s/db.p' %
                                  roslib.packages.get_pkg_dir('posture_detection'))
    self.subscriptions = []
    self.command = command
    self.args = args

    rospy.init_node('posture', anonymous=True)

    if command == 'add':
      self.subscriptions.append(
        rospy.Subscriber('user_ang', UserAng, self.snapshot_callback)
      )
    elif command == 'remove':
      self.manager.remove_posture(self.args[0])
      rospy.signal_shutdown('')
    elif command == 'list':
      for key in self.manager.database:
        print key
      rospy.signal_shutdown('')
    else:
      rospy.Subscriber('user_ang', UserAng, self.userAng_callback)

    self.pub = rospy.Publisher('posture_detected', PostureDetected)
    rospy.spin()

  def _remove_subscriptions(self):
    for sub in self.subscriptions:
      sub.unregister()

  def snapshot_callback(self, message):
    rospy.loginfo('Snapshot')
    self.manager.add_posture(self.args[0], message)
    self.manager.remove_angles_from_posture(self.args[0], body)
    if len(self.args) > 1:
      to_remove = legs if self.args[1] == 'arms' else arms
      self.manager.remove_angles_from_posture(self.args[0], to_remove)
    self._remove_subscriptions()
    rospy.signal_shutdown('')

  def userAng_callback(self, message):
    posture_detected = self.manager.posture_event(message)
    if posture_detected:
      rospy.loginfo("Posture detected: %s", posture_detected)
      p = PostureDetected()
      p.user_id = message.user_id
      p.posture = posture_detected
      self.pub.publish(p)
コード例 #3
0
ファイル: posture_test.py プロジェクト: arnthorm/Fable
 def __init__(self, database_path='testdp.p'):
   rospy.init_node('posture_test', anonymous=True)
   self.database_path = database_path
   self.subscriptions = []
   self.args = {}
   self.package_path = roslib.packages.get_pkg_dir('posture_detection')
   self.manager = PostureManager(
     '%s/db.p' %
     roslib.packages.get_pkg_dir('posture_detection')
   )
   self._load_database()
コード例 #4
0
ファイル: posture_test.py プロジェクト: arnthorm/Fable
class PostureTest:
  def __init__(self, database_path='testdp.p'):
    rospy.init_node('posture_test', anonymous=True)
    self.database_path = database_path
    self.subscriptions = []
    self.args = {}
    self.package_path = roslib.packages.get_pkg_dir('posture_detection')
    self.manager = PostureManager(
      '%s/db.p' %
      roslib.packages.get_pkg_dir('posture_detection')
    )
    self._load_database()

  def _load_database(self):
    if os.path.exists(self.database_path):
      self.database = pickle.load(open(self.database_path, "rb"))
    else:
      self.database = []
    rospy.loginfo("Loaded %d test postures" % len(self.database))

  def _save_database(self):
    """Save current database to file (using Pickle)."""
    pickle.dump(self.database, open(self.database_path, "wb"))

  def _remove_subscriptions(self):
    for sub in self.subscriptions:
      sub.unregister()

  def add(self, *args, **kwargs):
    for i, item in enumerate(self.database):
      if item['username'] == kwargs['username'] and \
         item['posture'] == kwargs['posture'] and \
         item['set'] == kwargs['set']:
        print 'This has already been added to the database!'
        return

    self.args = kwargs
    self.subscriptions.append(
      rospy.Subscriber('user_ang', UserAng, self.add_cb)
    )
    rospy.spin()

  def list(self):
    for item in self.database:
      item['detected'] = self.manager.detect_posture(item['message'])
      del item['message']
      print item

  def remove(self, username, posture, set):
    for i, item in enumerate(self.database):
      if item['username'] == username and item['posture'] == posture and \
         item['set'] == set:
        rospy.loginfo("Deleted");
        del self.database[i]
      self._save_database()
      rospy.signal_shutdown('')

  def add_cb(self, data):
    self.args.update({'message': data})
    self.database.append(self.args)
    self._save_database()
    self._remove_subscriptions()
    rospy.signal_shutdown('')

  def edit(self):
    edit = False
    for i, item in enumerate(self.database):
      if item['username'] == 'arnthor' and item['posture'] == 'hands_up' and \
         item['set'] == '1':
        #self.database[i]['set'] = '2'
        edit = True
        rospy.loginfo("Edited.");
        break
    if edit:
      self._save_database()

  def compare(self, group_by='posture', set=None, label=''):
    results = {}
    if label:
      label = label + '_'
    for item in self.database:
      if set is not None and item['set'] == set or set is None:
        detected = self.manager.detect_posture(item['message'])
        detected = detected[0]
        value = 0
        if item['posture'] == detected:
          value = 1
          #print 'True positive'
        elif detected is not None:
          print 'False positive'
        else:
          pass
          #print 'No detection'
        if not results.has_key(item[group_by]):
          results[item[group_by]] = []
        results[item[group_by]].append(value)
    success_rate = {}
    for key in results:
      success_rate[key] = 100.0*sum_list(results[key])/len(results[key]) 
      print("%s: %f (%d/%d)" % (
        key,
        success_rate[key],
        sum_list(results[key]),
        len(results[key])
      ))
    pylab.ion()
    pylab.bar(range(0, len(success_rate)), success_rate.values())
    pylab.xticks(range(0, len(success_rate)), success_rate.keys(), rotation=17)
    pylab.ylabel("Success rate [%]")
    pylab.show()
    pylab.savefig('%s/img/%s%s_%s.pdf' % (self.package_path, label, group_by, set))
    ble = raw_input('Press enter to continue')