示例#1
0
def callback(data):
	global previous_faces
	current_faces = {};
	for user in data.tf_bundles:
		user_id = int(re.sub("user_","",user.user_id))	
		location = user.head.translation
		current_faces[user_id] = location
	
	new_face = findDiff(list(current_faces.keys()), list(previous_faces.keys()))
	lost_face = findDiff(list(previous_faces.keys()), list(current_faces.keys()))

	# Publishers 
	# if new face is found
	if (new_face):
		face_event_msg = FaceEvent()
		face_event_msg.face_event = "new_face"
		face_event_msg.face_id = new_face
		event_publisher.publish(face_event_msg)
	# if face is lost
	if (lost_face):
		face_event_msg = FaceEvent()
		face_event_msg.face_event = "lost_face"
		face_event_msg.face_id = lost_face
		event_publisher.publish(face_event_msg)

	# Face Location
	faces_msg = Faces()
	faces_msg.faces = []
	for user, location in current_faces.items():
		face_msg = Face()
		face_msg.id = user
		face_msg.point = location
		face_msg.attention = 1
		faces_msg.faces.append(face_msg)

	face_publisher.publish(faces_msg)

	previous_faces = current_faces
示例#2
0
    def callback(self,data, cmt, face,temp):
        try:
            cv_image = self.bridge.imgmsg_to_cv2(data, "bgr8")
        except CvBridgeError as e:
            self.logger.error(e)

        ttp = cmt
        for val in temp.tracker_results:
            ttp.tracker_results.append(val)

        not_covered_faces,covered_faces = self.returnOverlapping(face,ttp)

        for face,cmt in covered_faces:
            tupl = []
            for pts in face.feature_point.points:
                x,y = pts.x, pts.y
                tupl.append((x,y))
            key = cmt.tracker_name.data

            #TODO Can we Query state only:
            query_only = rospy.get_param('query_only', True)
            # print("Confidence: %f" % self.confidence)
            if not query_only:
                # This is to avoid adding faces sample size by saving while querying.
                self.cmt_tracker_instances[key] = self.cmt_tracker_instances.get(key,{'count': 0, 'state':self.state['query_save']})
            else:
                self.cmt_tracker_instances[key] = self.cmt_tracker_instances.get(key, {'count': 0, 'state': self.state[
                    'query_only']})
            #print(self.cmt_tracker_instances)
            if self.cmt_tracker_instances[key]['state'] == self.state['query_save']:
                self.queryAddResults(cv_image, tupl, key, self.confidence)
                # print(self.face_recognizer.face_results_aggregator[cmt.tracker_name.data]['results'])


            elif not query_only and (self.cmt_tracker_instances[key]['state'] == self.state['save_only'] or self.cmt_tracker_instances[key]['state'] == self.state['query_save']):
                self.face_recognizer.save_faces(cv_image, tupl, key, str(self.cmt_tracker_instances[key]['count']))

            elif self.cmt_tracker_instances[key]['state'] == self.state['query_only']:
                self.queryAddResults(cv_image, tupl, key, self.confidence)
                # print(self.face_recognizer.face_results_aggregator[cmt.tracker_name.data]['results'])
                if key not in self.google_query:
                    self.face_recognizer.temp_save_faces(cv_image, tupl, key)
                    self.google_query.append(key)
                    # print "Image Saved"

            elif self.cmt_tracker_instances[key]['state'] == self.state['ignore']:
                pass

            #Change state now;

            if self.cmt_tracker_instances[key]['count'] == self.sample_size:

                if self.cmt_tracker_instances[key]['state'] == self.state['query_save'] or self.cmt_tracker_instances[key]['state'] == self.state['query_only']:
                    max_index = max(self.face_recognizer.face_results_aggregator[cmt.tracker_name.data]['results'], key=self.face_recognizer.face_results_aggregator[cmt.tracker_name.data]['results'].get)
                    # print("openface output results: ")
                    # print(self.face_recognizer.face_results_aggregator[cmt.tracker_name.data]['results'])
                    if self.face_recognizer.face_results_aggregator[cmt.tracker_name.data]['results'][max_index] > self.num_positive:
                        try:
                            self.upt = rospy.ServiceProxy('recognition', TrackerNames)
                            indication = self.upt(names=str(max_index), index=int(cmt.tracker_name.data))
                            recognized_event = FaceEvent()
                            recognized_event.face_event = "recognized_face"
                            recognized_event.face_id = int(cmt.tracker_name.data)
                            recognized_event.recognized_id = str(max_index)

                            self.event_pub.publish(recognized_event)

                            if not indication:
                                self.logger.info("there was the same id in the id chamber.....")
                            self.cmt_tracker_instances[key]['state'] = self.state['ignore']
                        except rospy.ServiceException, e:
                            self.logger.error("Service call failed: %s" % e)

                    else:
                        recognized_event = FaceEvent()
                        recognized_event.face_event = "recognized_face"
                        recognized_event.face_id = int(cmt.tracker_name.data)
                        recognized_event.recognized_id = str(0)

                        self.event_pub.publish(recognized_event)
                        if not query_only:
                            self.cmt_tracker_instances[key]['state'] = self.state['save_only']
                            self.cmt_tracker_instances[key]['count'] = 0
                        else:
                            self.cmt_tracker_instances[key]['state'] = self.state['ignore']
                            self.cmt_tracker_instances[key]['count'] = 0

                    # Google Scrapper
                    if self.image_scraper_enable != 0:
                        try:
                            print "Upload Imgur and Query Google Scrapper: "
                            results = self.image_scraper.image_scraper("/tmp/" + key + ".png")
                            try:
                                self.gog = rospy.ServiceProxy('google_scraper', TrackerNames)

                                indication = self.gog(names=str(results), index=int(cmt.tracker_name.data))

                                if not indication:
                                    self.logger.info("there was the same id in the id chamber.....")
                            except rospy.ServiceException, e:
                                self.logger.error("Service call failed: %s" % e)

                            print  (results)
                            print "Finished Query of Google Images"
                        except Exception, e:
                            print e