This project provides a FastAPI-based API for detecting the gender of a person in an uploaded image. It uses RetinaFace for face detection and OpenAI's CLIP model for gender classification. The API ensures high accuracy for real human faces and handles errors gracefully for non-human or low-quality images.
- Face Detection: Uses RetinaFace to detect and validate human faces in the image.
- Gender Classification: Uses OpenAI's CLIP model to classify the gender as "male" or "female".
- Error Handling: Handles cases like no face detected, multiple faces, or non-human images.
- FastAPI: Provides a fast and scalable API for gender detection.
Before running the project, ensure you have the following installed:
- Python 3.8 or higher
- pip (Python package manager)
git clone https://github.com/your-username/gender-detection-api.git
cd gender-detection-apipython -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activatepip install -r requirements.txtuvicorn final:app --reloadThe API will start running at http://127.0.0.1:8000.
Open your browser and navigate to:
- Swagger UI: http://127.0.0.1:8000/docs
- ReDoc: http://127.0.0.1:8000/redoc
URL: GET /
Description: Welcome message and usage instructions.
Response:
{
"message": "Welcome to the Gender Detection API",
"usage": "Send a POST request with an image file to /predict-gender endpoint"
}URL: POST /predict_gender/
Description: Predicts the gender of the person in the uploaded image.
Parameters:
selected_gender(Query Parameter): Expected gender (male or female).file(Form Data): Image file to analyze.
Response:
Success:
{
"gender": "male" "or" "female",
}Error:
{
"message": "No faces detected in the image. Please upload an image with a clear human face."
}curl -X POST -F "file=@test_image.jpg" "http://127.0.0.1:8000/predict_gender/?selected_gender=male"import requests
url = "http://127.0.0.1:8000/predict_gender/?selected_gender=male"
files = {"file": open("test_image.jpg", "rb")}
response = requests.post(url, files=files)
print(response.json())gender-detection-api/
├── final.py # Main FastAPI application
├── requirements.txt # List of dependencies
└── README.md # Project documentation
The project uses the following Python libraries:
- fastapi: For building the API.
- uvicorn: For running the FastAPI server.
- retina-face: For face detection.
- clip: For gender classification using OpenAI's CLIP model.
- torch: For deep learning operations.
- Pillow: For image processing.
- numpy: For numerical operations.
- opencv-python: For image manipulation.
All dependencies are listed in requirements.txt.
The API handles the following errors gracefully:
{
"message": "No faces detected in the image. Please upload an image with a clear human face."
}{
"message": "Multiple faces detected. Please upload an image with only one face."
}{
"message": "Non-human image detected. Please upload a real human face."
}{
"message": "Gender mismatch. Detected: male, Expected: female"
}{
"message": "Internal server error: <error details>"
}If you'd like to contribute to this project, please follow these steps:
- Fork the repository.
- Create a new branch (
git checkout -b feature/YourFeatureName). - Commit your changes (
git commit -m 'Add some feature'). - Push to the branch (
git push origin feature/YourFeatureName). - Open a pull request.
- RetinaFace: For robust face detection.
- OpenAI CLIP: For accurate gender classification.
- FastAPI: For building a fast and scalable API.
For any questions or feedback, feel free to reach out:
- Name: Nitin Singh
- Email: ni3.singh.r@gmail.com
- GitHub: NI3singh
Enjoy using the Gender Detection API!