<jupyter_start><jupyter_text>Import Modules<jupyter_code>import warnings
warnings.filterwarnings('ignore')
from src import detect_faces, show_bboxes
from PIL import Image

import torch
from torchvision import transforms, datasets
import numpy as np
import os<jupyter_output><empty_output><jupyter_text>Path Definitions<jupyter_code>dataset_path = '../Dataset/emotiw/'

processed_dataset_path = '../Dataset/FaceCoordinates/'<jupyter_output><empty_output><jupyter_text>Load Train and Val Dataset<jupyter_code>image_datasets = {x : datasets.ImageFolder(os.path.join(dataset_path, x))
                    for x in ['train', 'val']}

class_names = image_datasets['train'].classes

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
class_names
training_dataset = image_datasets['train']
validation_dataset = image_datasets['val']
neg_train = sorted(os.listdir(dataset_path + 'train/Negative/'))
neu_train = sorted(os.listdir(dataset_path + 'train/Neutral/'))
pos_train = sorted(os.listdir(dataset_path + 'train/Positive/'))

neg_val = sorted(os.listdir(dataset_path + 'val/Negative/'))
neu_val = sorted(os.listdir(dataset_path + 'val/Neutral/'))
pos_val = sorted(os.listdir(dataset_path + 'val/Positive/'))
neg_train_filelist = [x.split('.')[0] for x in neg_train]
neu_train_filelist = [x.split('.')[0] for x in neu_train]
pos_train_filelist = [x.split('.')[0] for x in pos_train]

neg_val_filelist = [x.split('.')[0] for x in neg_val]
neu_val_filelist = [x.split('.')[0] for x in neu_val]
pos_val_filelist = [x.split('.')[0] for x in pos_val]

print(neg_train_filelist[:10])
print(neu_train_filelist[:10])
print(pos_train_filelist[:10])

print(neg_val_filelist[:10])
print(neu_val_filelist[:10])
print(pos_val_filelist[:10])
train_filelist = neg_train_filelist + neu_train_filelist + pos_train_filelist
val_filelist = neg_val_filelist + neu_val_filelist + pos_val_filelist
print(len(training_dataset))
print(len(validation_dataset))<jupyter_output>9815
4346<jupyter_text>Extract Faces from Image using MTCNN<jupyter_code>for i in range(len(training_dataset)):
    image, label = training_dataset[i]
    print(train_filelist[i])
    try:
        if label == 0:
            if os.path.isfile(processed_dataset_path + 'train/Negative/' + train_filelist[i] + '.npz'):
                print(train_filelist[i] + ' Already present')
                continue
            bounding_boxes, landmarks = detect_faces(image)
            bounding_boxes = np.asarray(bounding_boxes)
            if bounding_boxes.size == 0:
                print('MTCNN model handling empty face condition at ' + train_filelist[i])
            np.savez(processed_dataset_path + 'train/Negative/' + train_filelist[i] , a=bounding_boxes, b=landmarks)

        elif label == 1:
            if os.path.isfile(processed_dataset_path + 'train/Neutral/' + train_filelist[i] + '.npz'):
                print(train_filelist[i] + ' Already present')
                continue
            bounding_boxes, landmarks = detect_faces(image)
            bounding_boxes = np.asarray(bounding_boxes)
            if bounding_boxes.size == 0:
                print('MTCNN model handling empty face condition at ' + train_filelist[i]) 
            np.savez(processed_dataset_path + 'train/Neutral/' + train_filelist[i] , a=bounding_boxes, b=landmarks)

        else:
            if os.path.isfile(processed_dataset_path + 'train/Positive/' + train_filelist[i] + '.npz'):
                print(train_filelist[i] + ' Already present')
                continue
            bounding_boxes, landmarks = detect_faces(image)
            bounding_boxes = np.asarray(bounding_boxes)
            if bounding_boxes.size == 0:
                print('MTCNN model handling empty face condition at ' + train_filelist[i])
            np.savez(processed_dataset_path + 'train/Positive/' + train_filelist[i] , a=bounding_boxes, b=landmarks)
                      
    except ValueError:
        print('No faces detected for ' + train_filelist[i] + ". Also MTCNN failed.")
        if label == 0:
            np.savez(processed_dataset_path + 'train/Negative/' + train_filelist[i] , a=np.zeros(1), b=np.zeros(1))
        elif label == 1:
            np.savez(processed_dataset_path + 'train/Neutral/' + train_filelist[i] , a=np.zeros(1), b=np.zeros(1))
        else:
            np.savez(processed_dataset_path + 'train/Positive/' + train_filelist[i] , a=np.zeros(1), b=np.zeros(1))
        continue
for i in range(len(validation_dataset)):
    image, label = validation_dataset[i]
    print(val_filelist[i])
    try:
        if label == 0:
            if os.path.isfile(processed_dataset_path + 'val/Negative/' + val_filelist[i] + '.npz'):
                print(val_filelist[i] + ' Already present')
                continue
            bounding_boxes, landmarks = detect_faces(image)
            bounding_boxes = np.asarray(bounding_boxes)
            if bounding_boxes.size == 0:
                print('MTCNN model handling empty face condition at ' + val_filelist[i])
            np.savez(processed_dataset_path + 'val/Negative/' + val_filelist[i] , a=bounding_boxes, b=landmarks)

        elif label == 1:
            if os.path.isfile(processed_dataset_path + 'val/Neutral/' + val_filelist[i] + '.npz'):
                print(val_filelist[i] + ' Already present')
                continue
            bounding_boxes, landmarks = detect_faces(image)
            bounding_boxes = np.asarray(bounding_boxes)
            if bounding_boxes.size == 0:
                print('MTCNN model handling empty face condition at ' + val_filelist[i]) 
            np.savez(processed_dataset_path + 'val/Neutral/' + val_filelist[i] , a=bounding_boxes, b=landmarks)

        else:
            if os.path.isfile(processed_dataset_path + 'val/Positive/' + val_filelist[i] + '.npz'):
                print(val_filelist[i] + ' Already present')
                continue
            bounding_boxes, landmarks = detect_faces(image)
            bounding_boxes = np.asarray(bounding_boxes)
            if bounding_boxes.size == 0:
                print('MTCNN model handling empty face condition at ' + val_filelist[i])
            np.savez(processed_dataset_path + 'val/Positive/' + val_filelist[i] , a=bounding_boxes, b=landmarks)
                      
    except ValueError:
        print('No faces detected for ' + val_filelist[i] + ". Also MTCNN failed.")
        if label == 0:
            np.savez(processed_dataset_path + 'val/Negative/' + val_filelist[i] , a=np.zeros(1), b=np.zeros(1))
        elif label == 1:
            np.savez(processed_dataset_path + 'val/Neutral/' + val_filelist[i] , a=np.zeros(1), b=np.zeros(1))
        else:
            np.savez(processed_dataset_path + 'val/Positive/' + val_filelist[i] , a=np.zeros(1), b=np.zeros(1))
        continue<jupyter_output>neg_1
neg_1 Already present
neg_10
neg_10 Already present
neg_100
neg_100 Already present
neg_1000
neg_1000 Already present
neg_1001
neg_1001 Already present
neg_1002
neg_1002 Already present
neg_1003
neg_1003 Already present
neg_1004
neg_1004 Already present
neg_1005
neg_1005 Already present
neg_1006
neg_1006 Already present
neg_1007
neg_1007 Already present
neg_1008
neg_1008 Already present
neg_1009
neg_1009 Already present
neg_101
neg_101 Already present
neg_1010
neg_1010 Already present
neg_1011
neg_1011 Already present
neg_1012
neg_1012 Already present
neg_1013
neg_1013 Already present
neg_1014
neg_1014 Already present
neg_1015
neg_1015 Already present
neg_1016
neg_1016 Already present
neg_1017
neg_1017 Already present
neg_1018
neg_1018 Already present
neg_1019
neg_1019 Already present
neg_102
neg_102 Already present
neg_1020
neg_1020 Already present
neg_1021
neg_1021 Already present
neg_1022
neg_1022 Already present
neg_1023
neg_1023 Already present
neg_1024
neg_1024 Already pres[...]
{'fraction_non_alphanumeric': 0.10708802456499489, 'fraction_numerical': 0.035312180143295804, 'mean_word_length': 3.3965129358830146, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '602133', 'license': 'MIT', 'n_tokens_mistral': 2793, 'n_tokens_neox': 2510, 'n_words': 527, 'path': 'MTCNN/.ipynb_checkpoints/Face_Extractor_BB_Landmarks_Train-checkpoint.ipynb', 'repo_name': 'aarushgupta/Group-Level-Emotion-Recognition', 'size': '7816'}