├── .gitignore ├── README.md ├── imgs ├── ex1.jpg ├── ex2.jpg ├── ex3.jpg └── ex4.jpg └── src └── main.py /.gitignore: -------------------------------------------------------------------------------- 1 | .venv -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Gender Detection with DeepFace and OpenCV 2 | 3 | This project detects the gender (male or female) in images using the **DeepFace** library for facial recognition and **OpenCV** for face detection. The system scans a folder of images, detects faces, and classifies the gender of each person in the images. 4 | 5 | ## Requirements 6 | 7 | Before running the project, install the necessary dependencies: 8 | 9 | pip install opencv-python 10 | pip install deepface 11 | pip install tensorflow 12 | If you prefer to use PyTorch as the backend for DeepFace, install it as well: 13 | 14 | 15 | pip install torch torchvision 16 | How to Use 17 | Clone the repository or download the files to your local machine. 18 | 19 | Place the images you want to process inside a folder. 20 | 21 | Modify the folder_path variable in the main.py file to point to the directory containing your images: 22 | 23 | 24 | 25 | folder_path = 'path/to/your/folder' 26 | Run the main.py script: 27 | 28 | 29 | python src/main.py 30 | The script will iterate over all images in the specified folder, detect faces, and display the detected gender (male or female) for each face found. The processed images will be saved with the prefix output_ in the same directory. 31 | 32 | Example Output 33 | After execution, you will see an output similar to this: 34 | 35 | Image: path/to/image.jpg -> Detected gender: Female 36 | Image: path/to/another_image.jpg -> Detected gender: Male 37 | Processed images will be saved in the same folder with the prefix output_. 38 | 39 | License 40 | This project is licensed under the MIT License - see the LICENSE file for details. 41 | 42 | Acknowledgements 43 | Special thanks to: 44 | 45 | OpenCV: Open Source Computer Vision Library. 46 | DeepFace: A Python library for deep learning facial recognition. 47 | TensorFlow: Backend for facial recognition. 48 | -------------------------------------------------------------------------------- /imgs/ex1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GabrielNat1/Face-Gender-Detection/1c34b01406a6993fa6b2a67c001693d1ffaa52fa/imgs/ex1.jpg -------------------------------------------------------------------------------- /imgs/ex2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GabrielNat1/Face-Gender-Detection/1c34b01406a6993fa6b2a67c001693d1ffaa52fa/imgs/ex2.jpg -------------------------------------------------------------------------------- /imgs/ex3.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GabrielNat1/Face-Gender-Detection/1c34b01406a6993fa6b2a67c001693d1ffaa52fa/imgs/ex3.jpg -------------------------------------------------------------------------------- /imgs/ex4.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/GabrielNat1/Face-Gender-Detection/1c34b01406a6993fa6b2a67c001693d1ffaa52fa/imgs/ex4.jpg -------------------------------------------------------------------------------- /src/main.py: -------------------------------------------------------------------------------- 1 | import cv2 2 | import os 3 | from deepface import Deepface 4 | 5 | face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') 6 | 7 | def detect_gender(image_path): 8 | img = cv2.imread(image_path) 9 | gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) 10 | 11 | faces = face_cascade.detectMultiScale(gray, 1.1, 4) 12 | 13 | for (x, y, w, h) in faces: 14 | face = img[y:y+h, x:x+w] 15 | result = DeepFace.analyze(face, actions=['gender']) 16 | gender = result[0]['gender'] 17 | print(f'Image: {image_path} -> Detected gender: {gender}') 18 | cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2) 19 | cv2.putText(img, f'Gender: {gender}', (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2) 20 | 21 | cv2.imwrite(f'output_{os.path.basename(image_path)}', img) 22 | cv2.imshow('Image with Detected Gender', img) 23 | cv2.waitKey(0) 24 | cv2.destroyAllWindows() 25 | 26 | def process_folder(folder_path): 27 | files = [f for f in os.listdir(folder_path) if f.lower().endswith(('.png', '.jpg', '.jpeg'))] 28 | 29 | for file in files: 30 | image_path = os.path.join(folder_path, file) 31 | detect_gender(image_path) 32 | 33 | folder_path = '../imgs/' 34 | process_folder(folder_path) 35 | --------------------------------------------------------------------------------