|
| 1 | +from deepface import DeepFace |
| 2 | +from PIL import Image, ImageDraw, ImageFont |
| 3 | +import os |
| 4 | +import json |
| 5 | + |
| 6 | +def extract_gender_and_age(input_image_path, output_directory): |
| 7 | + try: |
| 8 | + # Load the input image using Pillow |
| 9 | + input_img = Image.open(input_image_path) |
| 10 | + |
| 11 | + # Extract gender and age predictions |
| 12 | + result = DeepFace.analyze(input_image_path, actions=['gender', 'age'], enforce_detection=False) |
| 13 | + gender_stats = result[0]['gender'] |
| 14 | + age = int(result[0]['age']) |
| 15 | + print("Predicted Gender:", gender_stats) |
| 16 | + print("Predicted Age:", age) |
| 17 | + |
| 18 | + # Select the dominant gender label |
| 19 | + gender = max(gender_stats, key=gender_stats.get) |
| 20 | + |
| 21 | + # Create a drawing object |
| 22 | + draw = ImageDraw.Draw(input_img) |
| 23 | + font = ImageFont.load_default() |
| 24 | + position_gender = (10, 10) # Top-left corner for gender |
| 25 | + position_age = (10, 30) # Below gender information |
| 26 | + |
| 27 | + # Write gender and age information on the image |
| 28 | + draw.text(position_gender, f"Gender: {gender} - {gender_stats[gender]:.2f}%", font=font, fill=(255, 255, 255)) |
| 29 | + draw.text(position_age, f"Age: {age} years", font=font, fill=(255, 255, 255)) |
| 30 | + |
| 31 | + # Create the output directory if it doesn't exist |
| 32 | + os.makedirs(output_directory, exist_ok=True) |
| 33 | + |
| 34 | + # Save the output image as JPEG with a filename based on predicted gender |
| 35 | + output_filename = f"{gender.lower()}_age_{age}.jpg" |
| 36 | + output_path = os.path.join(output_directory, output_filename) |
| 37 | + input_img.save(output_path, format='JPEG') |
| 38 | + |
| 39 | + except Exception as e: |
| 40 | + print(f"Error: {e}") |
| 41 | + |
| 42 | + |
| 43 | +if __name__ == "__main__": |
| 44 | + # Get the directory of the current script |
| 45 | + script_directory = os.path.dirname(os.path.abspath(__file__)) |
| 46 | + |
| 47 | + # Load settings from the JSON file |
| 48 | + with open("settings.json", "r") as file: |
| 49 | + settings = json.load(file) |
| 50 | + |
| 51 | + # Specify the paths for input and output directories based on the loaded settings |
| 52 | + input_directory = os.path.join(script_directory, settings["directories"]["input"]) |
| 53 | + output_directory = os.path.join(script_directory, settings["directories"]["output"]) |
| 54 | + |
| 55 | + # Get a list of all image files in the input directory |
| 56 | + image_files = [f for f in os.listdir(input_directory) if os.path.isfile(os.path.join(input_directory, f))] |
| 57 | + |
| 58 | + # Process each image file in the input directory |
| 59 | + for image_file in image_files: |
| 60 | + input_path = os.path.join(input_directory, image_file) |
| 61 | + |
| 62 | + extract_gender_and_age(input_path, output_directory) |
| 63 | + |
| 64 | + print("Gender and age extraction complete. Output saved as JPEG to", output_directory) |
0 commit comments