-
-
Notifications
You must be signed in to change notification settings - Fork 596
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1789 from UppuluriKalyani/master
Face_Orientation_Predictor
- Loading branch information
Showing
6 changed files
with
1,758 additions
and
0 deletions.
There are no files selected for viewing
753 changes: 753 additions & 0 deletions
753
Awesome-face-operations/Face_Orientation_Predictor/Face_Orientations_Predictor.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
126 changes: 126 additions & 0 deletions
126
Awesome-face-operations/Face_Orientation_Predictor/README.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
Sure! Below is a sample description for your README file on GitHub, detailing the task and providing instructions on how to run the code. | ||
|
||
--- | ||
|
||
# Neural Network for Face Orientation Prediction | ||
|
||
This repository contains a neural network implementation for predicting the orientation of faces in grayscale images. The model is trained on a dataset of face images, and it can predict four possible orientations: straight, up, left, and right. | ||
|
||
## Project Overview | ||
|
||
This project involves the following steps: | ||
|
||
1. **Data Collection**: Download face images from a specified URL and save them to a local directory. | ||
2. **Preprocessing**: Resize the images to 32x30 pixels and normalize the pixel values to the range [0,1]. | ||
3. **Model Training**: Train a neural network to classify the orientation of the faces. | ||
4. **Model Saving**: Save the trained model to a file for future use. | ||
5. **Model Loading and Prediction**: Load the saved model and use it to predict the orientation of new face images. | ||
6. **Visualization**: Display the images along with their predicted labels. | ||
|
||
## Getting Started | ||
|
||
### Prerequisites | ||
|
||
- Python 3.x | ||
- Required Python libraries: `numpy`, `requests`, `beautifulsoup4`, `opencv-python`, `matplotlib`, `Pillow` | ||
|
||
Install the required libraries using pip: | ||
|
||
```bash | ||
pip install numpy requests beautifulsoup4 opencv-python matplotlib Pillow | ||
``` | ||
|
||
### Data Collection | ||
|
||
The script downloads face images from the following URL: [Carnegie Mellon University Face Images](https://www.cs.cmu.edu/afs/cs.cmu.edu/project/theo-8/faceimages/faces/). | ||
|
||
```python | ||
# URL of the webpage | ||
main_url = "https://www.cs.cmu.edu/afs/cs.cmu.edu/project/theo-8/faceimages/faces/" | ||
|
||
# Directory to save downloaded files | ||
base_directory = "/content/faces" | ||
|
||
# Downloading function | ||
# [Include the code for downloading files here] | ||
``` | ||
|
||
### Preprocessing | ||
|
||
Resize images to 32x30 pixels and normalize them to the range [0,1]. | ||
|
||
```python | ||
def resize_image(image_path): | ||
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) | ||
resized_img = cv2.resize(img, (32, 30)) | ||
return resized_img / 255.0 | ||
``` | ||
|
||
### Model Training | ||
|
||
Train the neural network on the preprocessed images and save the model. | ||
|
||
```python | ||
# Define and train the neural network | ||
input_size = 960 # 32 * 30 | ||
hidden_size = 100 | ||
output_size = 4 | ||
|
||
nn = NeuralNetwork(input_size, hidden_size, output_size) | ||
nn.train(X_train, y_train_encoded) | ||
|
||
# Save the model | ||
save_model(nn, '/content/models', 'neural_network_model.pkl') | ||
``` | ||
|
||
### Model Loading and Prediction | ||
|
||
Load the saved model and predict the orientation of new images. | ||
|
||
|
||
# Load the model | ||
nn_loaded = load_model('/content/models', 'neural_network_model.pkl') | ||
|
||
# Perform predictions | ||
predictions = nn_loaded.forward(predict_images) | ||
``` | ||
### Visualization | ||
Display the images along with their predicted labels. | ||
display_images_with_predictions(name_list, predictions, reverse_label_map, path=data_path, max_images=20) | ||
``` | ||
|
||
## Usage | ||
|
||
1. **Download the dataset**: | ||
- Update the `main_url` to the URL containing the face images. | ||
- Run the script to download and save the images to a specified directory. | ||
|
||
2. **Preprocess the dataset**: | ||
- Use the `resize_image` function to preprocess the images. | ||
|
||
3. **Train the model**: | ||
- Define the neural network structure. | ||
- Train the model using the preprocessed images and labels. | ||
|
||
4. **Save the trained model**: | ||
- Use the `save_model` function to save the trained model. | ||
|
||
5. **Load the model and predict**: | ||
- Use the `load_model` function to load the saved model. | ||
- Predict the orientation of new face images using the loaded model. | ||
|
||
6. **Display predictions**: | ||
- Use the `display_images_with_predictions` function to visualize the predictions. | ||
|
||
## License | ||
|
||
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. | ||
|
||
## Acknowledgments | ||
|
||
- The face images are sourced from [Carnegie Mellon University](https://www.cs.cmu.edu/afs/cs.cmu.edu/project/theo-8/faceimages/faces/). | ||
|
Binary file added
BIN
+911 KB
Awesome-face-operations/Face_Orientation_Predictor/neural_network_model.pkl
Binary file not shown.
753 changes: 753 additions & 0 deletions
753
Face_Orientation_Predictor/Face_Orientations_Predictor.ipynb
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
Sure! Below is a sample description for your README file on GitHub, detailing the task and providing instructions on how to run the code. | ||
|
||
--- | ||
|
||
# Neural Network for Face Orientation Prediction | ||
|
||
This repository contains a neural network implementation for predicting the orientation of faces in grayscale images. The model is trained on a dataset of face images, and it can predict four possible orientations: straight, up, left, and right. | ||
|
||
## Project Overview | ||
|
||
This project involves the following steps: | ||
|
||
1. **Data Collection**: Download face images from a specified URL and save them to a local directory. | ||
2. **Preprocessing**: Resize the images to 32x30 pixels and normalize the pixel values to the range [0,1]. | ||
3. **Model Training**: Train a neural network to classify the orientation of the faces. | ||
4. **Model Saving**: Save the trained model to a file for future use. | ||
5. **Model Loading and Prediction**: Load the saved model and use it to predict the orientation of new face images. | ||
6. **Visualization**: Display the images along with their predicted labels. | ||
|
||
## Getting Started | ||
|
||
### Prerequisites | ||
|
||
- Python 3.x | ||
- Required Python libraries: `numpy`, `requests`, `beautifulsoup4`, `opencv-python`, `matplotlib`, `Pillow` | ||
|
||
Install the required libraries using pip: | ||
|
||
```bash | ||
pip install numpy requests beautifulsoup4 opencv-python matplotlib Pillow | ||
``` | ||
|
||
### Data Collection | ||
|
||
The script downloads face images from the following URL: [Carnegie Mellon University Face Images](https://www.cs.cmu.edu/afs/cs.cmu.edu/project/theo-8/faceimages/faces/). | ||
|
||
```python | ||
# URL of the webpage | ||
main_url = "https://www.cs.cmu.edu/afs/cs.cmu.edu/project/theo-8/faceimages/faces/" | ||
|
||
# Directory to save downloaded files | ||
base_directory = "/content/faces" | ||
|
||
# Downloading function | ||
# [Include the code for downloading files here] | ||
``` | ||
|
||
### Preprocessing | ||
|
||
Resize images to 32x30 pixels and normalize them to the range [0,1]. | ||
|
||
```python | ||
def resize_image(image_path): | ||
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE) | ||
resized_img = cv2.resize(img, (32, 30)) | ||
return resized_img / 255.0 | ||
``` | ||
|
||
### Model Training | ||
|
||
Train the neural network on the preprocessed images and save the model. | ||
|
||
```python | ||
# Define and train the neural network | ||
input_size = 960 # 32 * 30 | ||
hidden_size = 100 | ||
output_size = 4 | ||
|
||
nn = NeuralNetwork(input_size, hidden_size, output_size) | ||
nn.train(X_train, y_train_encoded) | ||
|
||
# Save the model | ||
save_model(nn, '/content/models', 'neural_network_model.pkl') | ||
``` | ||
|
||
### Model Loading and Prediction | ||
|
||
Load the saved model and predict the orientation of new images. | ||
|
||
|
||
# Load the model | ||
nn_loaded = load_model('/content/models', 'neural_network_model.pkl') | ||
|
||
# Perform predictions | ||
predictions = nn_loaded.forward(predict_images) | ||
``` | ||
### Visualization | ||
Display the images along with their predicted labels. | ||
display_images_with_predictions(name_list, predictions, reverse_label_map, path=data_path, max_images=20) | ||
``` | ||
|
||
## Usage | ||
|
||
1. **Download the dataset**: | ||
- Update the `main_url` to the URL containing the face images. | ||
- Run the script to download and save the images to a specified directory. | ||
|
||
2. **Preprocess the dataset**: | ||
- Use the `resize_image` function to preprocess the images. | ||
|
||
3. **Train the model**: | ||
- Define the neural network structure. | ||
- Train the model using the preprocessed images and labels. | ||
|
||
4. **Save the trained model**: | ||
- Use the `save_model` function to save the trained model. | ||
|
||
5. **Load the model and predict**: | ||
- Use the `load_model` function to load the saved model. | ||
- Predict the orientation of new face images using the loaded model. | ||
|
||
6. **Display predictions**: | ||
- Use the `display_images_with_predictions` function to visualize the predictions. | ||
|
||
## License | ||
|
||
This project is licensed under the MIT License - see the [LICENSE](LICENSE) file for details. | ||
|
||
## Acknowledgments | ||
|
||
- The face images are sourced from [Carnegie Mellon University](https://www.cs.cmu.edu/afs/cs.cmu.edu/project/theo-8/faceimages/faces/). | ||
|
Binary file not shown.