Skip to content

Commit

Permalink
Merge pull request #1789 from UppuluriKalyani/master
Browse files Browse the repository at this point in the history
Face_Orientation_Predictor
  • Loading branch information
akshitagupta15june authored Jun 4, 2024
2 parents 6c2fbf6 + fe00318 commit 369144b
Show file tree
Hide file tree
Showing 6 changed files with 1,758 additions and 0 deletions.

Large diffs are not rendered by default.

126 changes: 126 additions & 0 deletions Awesome-face-operations/Face_Orientation_Predictor/README.md
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.
753 changes: 753 additions & 0 deletions Face_Orientation_Predictor/Face_Orientations_Predictor.ipynb

Large diffs are not rendered by default.

126 changes: 126 additions & 0 deletions Face_Orientation_Predictor/README.md
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.

0 comments on commit 369144b

Please sign in to comment.