-
Notifications
You must be signed in to change notification settings - Fork 0
/
svm.py
55 lines (45 loc) · 1.69 KB
/
svm.py
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
import csv
import math
import os
import face_recognition
import numpy
from sklearn import svm
encodings = []
names = []
with open("encodings.csv") as csvfile:
reader = csv.reader(csvfile, quoting=csv.QUOTE_NONNUMERIC)
for row in reader:
encodings.append(row)
with open("names.csv") as csvfile:
reader = csv.reader(csvfile, quoting=csv.QUOTE_MINIMAL)
for row in reader:
names.append(row[0])
clf = svm.SVC(gamma='scale', probability=True)
clf.fit(encodings,names)
# Load the test image with unknown faces into a numpy array
test_image = face_recognition.load_image_file('./assets/bts.jpeg')
# Find all the faces in the test image using the default HOG-based model
face_locations = face_recognition.face_locations(test_image)
face_encodings = face_recognition.face_encodings(test_image, face_locations)
face_names = []
probabilites = []
valid_names = ["IU", "RM", "Jin", "Suga", "J-Hope", "Jimin", "V", "Jungkook"]
for face_encoding in face_encodings:
name = clf.predict([face_encoding])
face_names.append(*name)
# print(*name)
results = clf.predict_proba([face_encoding])[0]
results = [str(int(element * 100)) for element in results]
prob_per_class_dictionary = dict(zip(clf.classes_, results))
probabilites.append(prob_per_class_dictionary)
# print("probability pre class dictionary: ")
# print(prob_per_class_dictionary)
for prob, name in zip(probabilites, face_names):
print('\n')
print("Prediction: " + name)
for label in valid_names:
if(len(prob[label]) == 1):
prob[label] = "0" + prob[label]
prob[label] = prob[label] + "%: " + label
for i in range(len(valid_names)):
print(prob[valid_names[i]])