face_recognition_trainer.py
Go to the documentation of this file.
00001 #! /usr/bin/env python
00002 # Software License Agreement (BSD License)
00003 #
00004 # Copyright (c) 2017, Yuki Furuta.
00005 # All rights reserved.
00006 #
00007 # Redistribution and use in source and binary forms, with or without
00008 # modification, are permitted provided that the following conditions
00009 # are met:
00010 #
00011 #  * Redistributions of source code must retain the above copyright
00012 #    notice, this list of conditions and the following disclaimer.
00013 #  * Redistributions in binary form must reproduce the above
00014 #    copyright notice, this list of conditions and the following
00015 #    disclaimer in the documentation and/or other materials provided
00016 #    with the distribution.
00017 #  * Neither the name of Kei Okada nor the names of its
00018 #    contributors may be used to endorse or promote products derived
00019 #    from this software without specific prior written permission.
00020 #
00021 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00022 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00023 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00024 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00025 # COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00026 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00027 # BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00028 # LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00029 # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00030 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00031 # ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00032 # POSSIBILITY OF SUCH DAMAGE.
00033 
00034 
00035 from __future__ import print_function
00036 
00037 try:
00038     input = raw_input
00039 except:
00040     pass
00041 
00042 import rospy
00043 import message_filters
00044 from sensor_msgs.msg import Image
00045 from opencv_apps.msg import FaceArrayStamped
00046 from opencv_apps.srv import FaceRecognitionTrain, FaceRecognitionTrainRequest
00047 
00048 class FaceRecognitionTrainer(object):
00049     def __init__(self):
00050         self.queue_size = rospy.get_param("~queue_size", 100)
00051         
00052         self.img_sub = message_filters.Subscriber("image", Image)
00053         self.face_sub = message_filters.Subscriber("faces", FaceArrayStamped)
00054         
00055         self.req = FaceRecognitionTrainRequest()
00056         self.label = ""
00057         self.ok = False
00058 
00059         self.sync = message_filters.TimeSynchronizer([self.img_sub, self.face_sub],
00060                                                      self.queue_size)
00061         self.sync.registerCallback(self.callback)
00062     def callback(self, img, faces):
00063         if len(faces.faces) <= 0:
00064             return
00065         if self.ok:
00066             faces.faces.sort(key=lambda f: f.face.width * f.face.height)
00067             self.req.images.append(img)
00068             self.req.rects.append(faces.faces[0].face)
00069             self.req.labels.append(self.label)
00070             self.ok = False
00071     def run(self):
00072         rospy.wait_for_service("train")
00073         train = rospy.ServiceProxy("train", FaceRecognitionTrain)
00074         self.label = input("Please input your name and press Enter: ")
00075         while len(self.label) <= 0 or input("Your name is %s. Correct? [y/n]: " % self.label) not in ["", "y", "Y"]:
00076             self.label = input("Please input your name and press Enter: ")
00077 
00078         input("Please stand at the center of the camera and press Enter: ")
00079         while True:
00080             self.ok = True
00081             while self.ok:
00082                 print("taking picture...")
00083                 rospy.sleep(1)
00084             if input("One more picture? [y/n]: ") not in ["", "y", "Y"]:
00085                 break
00086         print("sending to trainer...")
00087         
00088         res = train(self.req)
00089         if res.ok:
00090             print("OK. Trained successfully!")
00091         else:
00092             print("NG. Error: %s" % res.error)
00093 
00094 if __name__ == '__main__':
00095     rospy.init_node("face_recognition_trainer")
00096     t = FaceRecognitionTrainer()
00097     t.run()


opencv_apps
Author(s): Kei Okada
autogenerated on Mon Apr 22 2019 02:18:26