<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 matplotlib.pyplot as plt
import matplotlib.image as mpimg
import numpy as np
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/test/'
face_coordinates_directory = '../Dataset/FaceCoordinates/'
processed_face_ex = '../Dataset/CroppedFaces/test/'<jupyter_output><empty_output><jupyter_text>Loading Test Dataset<jupyter_code>test_dataset = datasets.ImageFolder(dataset_path)

class_names = test_dataset.classes

device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
class_names
test_filelist = sorted(os.listdir(dataset_path + 'test_shared/'))
test_filelist = [x.split('.')[0] for x in test_filelist]
print(test_filelist[:10])
print(len(test_dataset))<jupyter_output>3011<jupyter_text>Crop Faces<jupyter_code>for i in range(len(test_dataset)):
    print(test_filelist[i])
    image, label = test_dataset[i]

    face_list = []
    landmarks_new_coordinates = []

    if os.path.isfile(processed_face_ex + 'test/' + test_filelist[i] + '.npz'):
        print(test_filelist[i] + ' Already present')
        continue

    bbox_lm = np.load(face_coordinates_directory + 'test/' + test_filelist[i] +'.npz')
    bounding_boxes = bbox_lm['a']
    if bounding_boxes.size == 0 or (bounding_boxes[0] == 0).all():
        print("No bounding boxes for " + test_filelist[i] + ". Adding empty file for the same")
        np.savez(processed_face_ex + test_filelist[i], a = np.zeros(1), b = np.zeros(1))
        continue
    landmarks = bbox_lm['b']

    for j in range(len(bounding_boxes)):
        bbox_coordinates = bounding_boxes[j]
        landmark = landmarks[j]
        img_face = image.crop((bbox_coordinates[0], bbox_coordinates[1], bbox_coordinates[2], bbox_coordinates[3]))

        x = bbox_coordinates[0]
        y = bbox_coordinates[1]
        for k in range(5):
            landmark[k] -= x
            landmark[k+5] -= y
        img_face = np.array(img_face)
        landmark = np.array(landmark)


        if len(face_list) != 0:
            if img_face.shape[0] == face_list[-1].shape[0]:
                img_face = image.crop((bbox_coordinates[0] - 1, bbox_coordinates[1] - 1, bbox_coordinates[2], bbox_coordinates[3]))
                img_face = np.array(img_face)
                landmark +=1

        face_list.append(img_face)
        landmarks_new_coordinates.append(landmark)
    face_list = np.asarray(face_list)
    landmarks_new_coordinates = np.asarray(landmarks_new_coordinates)
    np.savez(processed_face_ex + test_filelist[i], a = face_list, b = landmarks_new_coordinates)

    print(i)<jupyter_output>test_1
0
test_10
1
test_100
2
test_1000
3
test_1001
4
test_1002
5
test_1003
6
test_1004
7
test_1005
8
test_1006
9
test_1007
10
test_1008
11
test_1009
12
test_101
13
test_1010
14
test_1011
15
test_1012
No bounding boxes for test_1012. Adding empty file for the same
test_1013
17
test_1014
No bounding boxes for test_1014. Adding empty file for the same
test_1015
19
test_1016
20
test_1017
21
test_1018
22
test_1019
23
test_102
24
test_1020
25
test_1021
26
test_1022
27
test_1023
28
test_1024
29
test_1025
30
test_1026
31
test_1027
32
test_1028
33
test_1029
34
test_103
35
test_1030
36
test_1031
37
test_1032
38
test_1033
39
test_1034
40
test_1035
41
test_1036
42
test_1037
43
test_1038
44
test_1039
No bounding boxes for test_1039. Adding empty file for the same
test_104
46
test_1040
47
test_1041
48
test_1042
49
test_1043
50
test_1044
51
test_1045
52
test_1046
53
test_1047
54
test_1048
55
test_1049
56
test_105
57
test_1050
58
test_1051
59
test_1052
60
test_1053
61
test_1054
62
test_1055
63
test_1[...]
{'fraction_non_alphanumeric': 0.09018294254058233, 'fraction_numerical': 0.10332388559649575, 'mean_word_length': 4.260162601626016, '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': '498032', 'license': 'MIT', 'n_tokens_mistral': 1702, 'n_tokens_neox': 1469, 'n_words': 340, 'path': 'MTCNN/.ipynb_checkpoints/Face_Cropper_TestDataset-checkpoint.ipynb', 'repo_name': 'aarushgupta/Group-Level-Emotion-Recognition', 'size': '3881'}