diff --git a/image_recognition_face_recognition/scripts/face_recognition_node b/image_recognition_face_recognition/scripts/face_recognition_node index dac863c9..eee211ad 100755 --- a/image_recognition_face_recognition/scripts/face_recognition_node +++ b/image_recognition_face_recognition/scripts/face_recognition_node @@ -29,6 +29,7 @@ class OpenfaceROS: ): """ OpenfaceROS class that wraps the FaceRecognizer in a ROS node + :param save_images_folder: path where to store the images :param topic_save_images: whether to save images originated from image topic callback :param service_save_images: whether to save images originated from a service call diff --git a/image_recognition_face_recognition/scripts/get_face_recognition b/image_recognition_face_recognition/scripts/get_face_recognition index 9123ccc5..360fa8f1 100755 --- a/image_recognition_face_recognition/scripts/get_face_recognition +++ b/image_recognition_face_recognition/scripts/get_face_recognition @@ -9,35 +9,48 @@ from image_recognition_msgs.msg import Recognition from image_recognition_util import image_writer from sensor_msgs.msg import RegionOfInterest -parser = argparse.ArgumentParser(description='Get face recognitions') - -# Add arguments -parser.add_argument('image', type=str, help='Image') -parser.add_argument( - '-v', '--verbose', help="Increase output verbosity", action="store_true") -args = parser.parse_args() - -# Read the image -img = cv2.imread(args.image) - -# Create openface interface -face_recognizer = FacenetRecognition() - -recognized_faces = face_recognizer.face_detection(img) -print(recognized_faces) - -recognitions = [] -for fr in recognized_faces: - face_recognition = [math.floor(xi) for xi in fr] - recognitions.append(Recognition( - roi=RegionOfInterest( - x_offset=face_recognition[0], - y_offset=face_recognition[1], - width=face_recognition[2] - face_recognition[0], - height=face_recognition[3] - face_recognition[1], + +def main(image, db: Optional[str] = None): + # Read the image + img = cv2.imread(image) + + # Create openface interface + face_recognizer = FaceRecognizer() + + if db: + face_recognizer.restore_trained_faces(db) + + recognized_faces = face_recognizer.face_detection(img) + print(recognized_faces) + + recognitions = [] + for fr in recognized_faces: + face_recognition = [math.floor(xi) for xi in fr] + recognitions.append( + Recognition( + roi=RegionOfInterest( + x_offset=face_recognition[0], + y_offset=face_recognition[1], + width=face_recognition[2] - face_recognition[0], + height=face_recognition[3] - face_recognition[1], + ) + ) ) - ) -) - annotated_original_image = image_writer.get_annotated_cv_image(img, recognitions) -cv2.imshow("result", annotated_original_image) -cv2.waitKey(1000) \ No newline at end of file + + annotated_original_image = image_writer.get_annotated_cv_image(img, recognitions) + cv2.imshow("result", annotated_original_image) + cv2.waitKey(1000) + + +if __name__ == "__main__": + import argparse + import sys + + parser = argparse.ArgumentParser(description="Get face recognitions") + + # Add arguments + parser.add_argument("image", type=str, help="Image") + parser.add_argument("-d", "--db", type=argparse.FileType("r"), help="Load already trained faces db from file") + args = parser.parse_args() + + sys.exit(main(**vars(args))) diff --git a/image_recognition_face_recognition/scripts/train_from_images b/image_recognition_face_recognition/scripts/train_from_images index 8743a42b..94cbd4b1 100755 --- a/image_recognition_face_recognition/scripts/train_from_images +++ b/image_recognition_face_recognition/scripts/train_from_images @@ -24,11 +24,11 @@ def main(modeldir, outfile, verbose: bool = False): path = os.path.join(modeldir, cat) logger.debug('loading images from %s', path) - for fname in os.listdir(path): - f = os.path.join(path, fname) + for filename in os.listdir(path): + f = os.path.join(path, filename) logger.debug('processing %s', f) - img = cv2.imread(f, 1) # load as color + img = cv2.imread(f, 1) # load as color try: logger.debug('training...') @@ -43,12 +43,12 @@ def main(modeldir, outfile, verbose: bool = False): class ReadableDir(Action): - def __call__(self,parser, namespace, values, option_string=None): - prospective_dir=values + def __call__(self, parser, namespace, values, option_string=None): + prospective_dir = values if not os.path.isdir(prospective_dir): raise ArgumentTypeError("readable_dir:{0} is not a valid path".format(prospective_dir)) if os.access(prospective_dir, os.R_OK): - setattr(namespace,self.dest,prospective_dir) + setattr(namespace, self.dest, prospective_dir) else: raise ArgumentTypeError("readable_dir:{0} is not a readable dir".format(prospective_dir)) @@ -59,11 +59,6 @@ if __name__ == '__main__': parser.add_argument('modeldir', action=ReadableDir, help='Directory with folders for each category') parser.add_argument('outfile', type=FileType('w'), help='Where to output the trained faces database') - parser.add_argument('-k', '--align_path', type=str, help='DLib Align path', required=False, - default="~/openface/models/dlib/shape_predictor_68_face_landmarks.dat") - parser.add_argument('-s', '--net_path', type=str, help='Openface neural network path', required=False, - default='~/openface/models/openface/nn4.small2.v1.t7') - parser.add_argument('-v', '--verbose', action='store_true') args = parser.parse_args()