Skip to content

Commit

Permalink
Fix image size (#152)
Browse files Browse the repository at this point in the history
I think we should merge this one over
#151
  • Loading branch information
MatthijsBurgh authored Mar 6, 2024
2 parents 2f809b5 + 44feba3 commit e807d54
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 11 deletions.
8 changes: 2 additions & 6 deletions image_recognition_age_gender/scripts/face_properties_node
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ from image_recognition_util import image_writer


class FacePropertiesNode:
def __init__(self, weights_file_path, img_size, depth, width, save_images_folder, use_gpu):
def __init__(self, weights_file_path, img_size, save_images_folder, use_gpu):
"""
ROS node that wraps the PyTorch age gender estimator
"""
self._bridge = CvBridge()
self._properties_srv = rospy.Service('get_face_properties', GetFaceProperties, self._get_face_properties_srv)
self._estimator = AgeGenderEstimator(weights_file_path, img_size, depth, width, use_gpu)
self._estimator = AgeGenderEstimator(weights_file_path, img_size, use_gpu)

if save_images_folder:
self._save_images_folder = os.path.expanduser(save_images_folder)
Expand All @@ -30,8 +30,6 @@ class FacePropertiesNode:
rospy.loginfo("PytorchFaceProperties node initialized:")
rospy.loginfo(" - weights_file_path=%s", weights_file_path)
rospy.loginfo(" - img_size=%s", img_size)
rospy.loginfo(" - depth=%s", depth)
rospy.loginfo(" - width=%s", width)
rospy.loginfo(" - save_images_folder=%s", save_images_folder)
rospy.loginfo(" - use_gpu=%s", use_gpu)

Expand Down Expand Up @@ -82,8 +80,6 @@ if __name__ == '__main__':
default_weights_path = os.path.expanduser('~/data/pytorch_models/best-epoch47-0.9314.onnx')
weights_file_path = rospy.get_param("~weights_file_path", default_weights_path)
img_size = rospy.get_param("~image_size", 64)
depth = rospy.get_param("~depth", 16)
width = rospy.get_param("~width", 8)
save_images = rospy.get_param("~save_images", True)
use_gpu = rospy.get_param("~use_gpu", False)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@


class AgeGenderEstimator(object):
def __init__(self, weights_file_path, img_size=64, depth=16, width=8, use_gpu=False):
def __init__(self, weights_file_path, img_size=64, use_gpu=False):
"""
Estimate the age and gender of the incoming image
:param weights_file_path: path to a pre-trained network in onnx format
:param img_size: Images are resized to a square image of (img_size X img_size)
:param use_gpu: Use GPU or CPU
"""
weights_file_path = os.path.expanduser(weights_file_path)

Expand All @@ -22,8 +24,6 @@ def __init__(self, weights_file_path, img_size=64, depth=16, width=8, use_gpu=Fa
self._model = None
self._weights_file_path = weights_file_path
self._img_size = img_size
self._depth = depth
self._width = width
self._use_gpu = use_gpu

def estimate(self, np_images):
Expand Down Expand Up @@ -52,7 +52,7 @@ def estimate(self, np_images):

results = []
for np_image in np_images:
inputs = np.transpose(cv2.resize(np_image, (64, 64)), (2, 0, 1))
inputs = np.transpose(cv2.resize(np_image, (self._img_size, self._img_size)), (2, 0, 1))
inputs = np.expand_dims(inputs, 0).astype(np.float32) / 255.
predictions = self._model.run(['output'], input_feed={'input': inputs})[0][0]
# age p(male) p(female)
Expand Down
2 changes: 1 addition & 1 deletion image_recognition_age_gender/test/test_face_properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def age_is_female_from_asset_name(asset_name):
images_gt = [(cv2.imread(os.path.join(assets_path, asset)), age_is_female_from_asset_name(asset))
for asset in os.listdir(assets_path)]

estimations = AgeGenderEstimator(local_path, 64, 16, 8).estimate([image for image, _ in images_gt])
estimations = AgeGenderEstimator(local_path, 64).estimate(image for image, _ in images_gt)
for (_, (age_gt, is_female_gt)), (age, gender) in zip(images_gt, estimations):
age = int(age)
is_female = gender[0] > 0.5
Expand Down

0 comments on commit e807d54

Please sign in to comment.