Skip to content

Commit

Permalink
(face) small refactor/cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
MatthijsBurgh committed Mar 13, 2024
1 parent 314c8c8 commit e9abd10
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
75 changes: 44 additions & 31 deletions image_recognition_face_recognition/scripts/get_face_recognition
Original file line number Diff line number Diff line change
Expand Up @@ -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)

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)))
17 changes: 6 additions & 11 deletions image_recognition_face_recognition/scripts/train_from_images
Original file line number Diff line number Diff line change
Expand Up @@ -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...')
Expand All @@ -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))

Expand All @@ -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()
Expand Down

0 comments on commit e9abd10

Please sign in to comment.