I learned how to predict ages of people by the photo. In this situation, the age was the first filename number, and they used regular expression, like this:
import re
pattern = r'([^/] )_\d _\d .jpg$'
p = re.compile(pattern)
r = p.search('16_0_0.jpg')
print(r.group(1))
And the output is:
16
But in my case, I need to predict the second parameter. Not the age, but the gender (0 to male, 1 to female). In a image file name like '16_0_0.jpg' i need to take the second parameter, this 0. In '16_1_0.jpg' I have a woman that is 16 years old, in '16_0_0.jpg' is a man 16 year old. How can I take just the second parameter?
Also, this pattern is going to be used on traindata:
train_data, test_data, preproc) = vis.images_from_fname(DATADIR, pattern = pattern, is_regression = True, random_state = 42)
CodePudding user response:
You can modify the pattern to capture the second parameter:
import re
pattern = r'\d _(\d )_\d .jpg$'
p = re.compile(pattern)
r = p.search('16_0_0.jpg')
print(r.group(1))
In this pattern, \d matches one or more digits, and the second set of parentheses (\d ) captures the second parameter in the image filename. The final regular expression .jpg$ matches the extension of the filename.
With this pattern, when you run the following code:
r = p.search('16_0_0.jpg')
print(r.group(1))
You will get the output 0, which is the second parameter of the image filename. I hope this info helps you.
CodePudding user response:
Another approach that may work for you:
filename = “16_0_0.jpg”
data = filename.split(“_”)
age = data[0]
gender = data[1]