File size: 2,813 Bytes
ea908d6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import os
import pickle
import warnings
import numpy as np


warnings.filterwarnings("ignore")


class GenderIdentifier:

    def __init__(self, females_files_path,males_model_path, females_model_path ,males_files_path):
        self.females_training_path  = females_files_path
        self.males_model_path = males_model_path
        self.error  = 0
        self.total_sample =0
        self.features_extractor = FeaturesExtractor()
        #load models
        self.females_gmm = pickle.load(open(females_model_path, 'rb'))
        self.males_gmm = pickle.load(open(males_model_path, 'rb'))

    def process(self):
        files = self.get_file_path(self.females_training_path, self.males_training_path)
        #read the test directory and get the list of test audio files
        for file in files:
            self.total_sample +=1
            print("%10s %8s %1s" % ("--> TESTING",":", os.path.basename(file)))

            vector = self.features_extractor.extract_features(file)
            winner = self.identity_gender(vector)
            expected_gender = file.split("/")[1][:-1]


            print("%10s %6s %1s" % ("+ EXPECTATION",":", expected_gender))
            print("%10s %3s %1s" % ("+ IDENTIFICATION",":", winner))

            if winner != expected_gender: self.error +=1

            print("--------------------------")


        accuracy  =(float(self.total_sample - self.error) / float(self.total_sample)) * 100
        accuracy_msg = "*** Accuracy = " + str(round(accuracy ,3)) + "%  ***"
        print(accuracy_msg)


    def get_file_paths(self, females_training_path, males_training_path):
        females = [ os.path.join(females_training_path, f) for f in os.listdir(females_training_path) ]
        males   = [ os.path.join(males_training_path, f) for f in os.listdir(males_training_path) ]
        files   = females + males
        return files

    def identify_gender(self, vector):
        # female hypothesis scoring
        is_female_scores         = np.array(self.females_gmm.score(vector))
        is_female_log_likelihood = is_female_scores.sum()
        # male hypothesis scoring
        is_male_scores         = np.array(self.males_gmm.score(vector))
        is_male_log_likelihood = is_male_scores.sum()

        print("%10s %5s %1s" % ("+ FEMALE SCORE",":", str(round(is_female_log_likelihood, 3))))
        print("%10s %7s %1s" % ("+ MALE SCORE", ":", str(round(is_male_log_likelihood,3))))

        if is_male_log_likelihood > is_female_log_likelihood: winner = "male"
        else                                                : winner = "female"
        return winner


if __name__== "__main__":
    gender_identifier = GenderIdentifier("TestingData/females", "TestingData/males", "females.gmm", "males.gmm")
    gender_identifier.process()