Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
ML_mini_challenge/mp.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
64 lines (54 sloc)
2.05 KB
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
from mtcnn.mtcnn import MTCNN | |
import cv2 | |
from PIL import Image | |
import numpy as np | |
from sklearn.model_selection import train_test_split | |
from sklearn.preprocessing import LabelEncoder | |
from tensorflow.keras.utils import to_categorical | |
import numpy as np | |
import pandas as pd | |
import os | |
detector = MTCNN() | |
def extract_face(filename, required_size=(160, 160)): | |
# Load the image | |
image = cv2.imread(filename) | |
if image is None: | |
print(f"Could not load image at {image_path}") | |
return None | |
# Convert the image to RGB (MTCNN expects RGB) | |
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) | |
# Detect faces in the image | |
results = detector.detect_faces(image) | |
# Extract the bounding box from the first face | |
x1, y1, width, height = results[0]['box'] | |
x2, y2 = x1 + width, y1 + height | |
# Extract the face | |
face = image[y1:y2, x1:x2] | |
# Resize pixels to the model size | |
image = Image.fromarray(face) | |
image = image.resize(required_size) | |
face_array = np.asarray(image) | |
return face_array | |
csv_path = 'train_small.csv' | |
data = pd.read_csv(csv_path) | |
# Assuming your images are stored in a directory | |
image_dir = 'train_small' | |
# Initialize lists to hold processed images and labels | |
processed_images = [] | |
labels = [] | |
for _, row in data.iterrows(): | |
image_path = os.path.join(image_dir, row['File Name']) | |
print(image_path) | |
face = extract_face(image_path) # Make sure this function returns an appropriately resized image | |
if face is not None: # Ensure a face was detected | |
processed_images.append(face) | |
labels.append(row['Category']) | |
# Convert the processed images and labels into numpy arrays | |
processed_images = np.array(processed_images) | |
labels = np.array(labels) | |
# Encode the labels | |
label_encoder = LabelEncoder() | |
encoded_labels = label_encoder.fit_transform(labels) | |
encoded_labels = to_categorical(encoded_labels) | |
# Split the dataset | |
X_train, X_test, y_train, y_test = train_test_split(processed_images, encoded_labels, test_size=0.2, random_state=42) |