-
Notifications
You must be signed in to change notification settings - Fork 0
/
train.py
58 lines (50 loc) · 2.19 KB
/
train.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
56
57
58
import module.face_detection as face_detection
import module.face_embedding as face_embedding
import os
from sklearn.model_selection import train_test_split
from sklearn.svm import LinearSVC
from sklearn.metrics import accuracy_score
import pickle
def train_main():
dir_cute="./photo/cute/"
dir_normal="./photo/normal/"
face_vectors = []
face_cute_or_normal_txt = []
# 画像のパスを指定して顔検出を実行
files_cute = os.listdir(dir_cute)
files_normal = os.listdir(dir_normal)
for file in files_cute:
if file.endswith(('.jpg', '.jpeg', '.png')):
file_path = os.path.join(dir_cute, file)
faces = face_detection.detect_faces(file_path)
if faces:
face = faces[0]
# 顔ベクトルの取得
face_vector = face_embedding.face_embedding(face,file)
face_vectors.append(face_vector["face_vector"])
face_cute_or_normal_txt.append(0)
else:
print("No faces detected.")
for file in files_normal:
if file.endswith(('.jpg', '.jpeg', '.png')):
file_path = os.path.join(dir_normal, file)
faces = face_detection.detect_faces(file_path)
if faces:
face = faces[0]
# 顔ベクトルの取得
face_vector = face_embedding.face_embedding(face,file)
face_vectors.append(face_vector["face_vector"])
face_cute_or_normal_txt.append(1)
else:
print("No faces detected.")
print("face_vectors length = ", len(face_vectors), "face_cute_or_normal_txt length = ", len(face_cute_or_normal_txt))
x_train, x_test, y_train, y_test = train_test_split(face_vectors, face_cute_or_normal_txt, test_size = 0.2, train_size = 0.8, shuffle = True)
print("train length = ", len(x_train), "test length = ", len(x_test))
model_svc=LinearSVC()
model_svc.fit(x_train,y_train)
y_pred = model_svc.predict(x_test)
print("正解率 = " , accuracy_score(y_test, y_pred))
with open('model.pickle', mode='wb') as f:
pickle.dump(model_svc,f,protocol=2)
if __name__ == "__main__":
train_main()