-
Notifications
You must be signed in to change notification settings - Fork 0
/
trainImage.py
38 lines (34 loc) · 1.19 KB
/
trainImage.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
import csv
import os, cv2
import numpy as np
import pandas as pd
import datetime
import time
from PIL import ImageTk, Image
# Train Image
def TrainImage(haarcasecade_path, trainimage_path, trainimagelabel_path, message,text_to_speech):
recognizer = cv2.face.LBPHFaceRecognizer_create()
detector = cv2.CascadeClassifier(haarcasecade_path)
faces, Id = getImagesAndLables(trainimage_path)
recognizer.train(faces, np.array(Id))
recognizer.save(trainimagelabel_path)
res = "Image Trained successfully" # +",".join(str(f) for f in Id)
message.configure(text=res)
text_to_speech(res)
def getImagesAndLables(path):
# imagePath = [os.path.join(path, f) for d in os.listdir(path) for f in d]
newdir = [os.path.join(path, d) for d in os.listdir(path)]
imagePath = [
os.path.join(newdir[i], f)
for i in range(len(newdir))
for f in os.listdir(newdir[i])
]
faces = []
Ids = []
for imagePath in imagePath:
pilImage = Image.open(imagePath).convert("L")
imageNp = np.array(pilImage, "uint8")
Id = int(os.path.split(imagePath)[-1].split("_")[1])
faces.append(imageNp)
Ids.append(Id)
return faces, Ids