-
Notifications
You must be signed in to change notification settings - Fork 0
/
identify.py
137 lines (90 loc) · 3.49 KB
/
identify.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import cv2
import os
import numpy as np
import tkinter as tk
import tkinter.font as font
def collect_data():
name = input("Enter name of person : ")
count = 1
ids = input("Enter ID: ")
cap = cv2.VideoCapture(0)
filename = "haarcascade_frontalface_default.xml"
cascade = cv2.CascadeClassifier(filename)
while True:
_, frm = cap.read()
gray = cv2.cvtColor(frm, cv2.COLOR_BGR2GRAY)
faces = cascade.detectMultiScale(gray, 1.4, 1)
for x, y, w, h in faces:
cv2.rectangle(frm, (x, y), (x + w, y + h), (0, 255, 0), 2)
roi = gray[y:y + h, x:x + w]
cv2.imwrite(f"persons/{name}-{count}-{ids}.jpg", roi)
count = count + 1
cv2.putText(frm, f"{count}", (20, 20), cv2.FONT_HERSHEY_PLAIN, 2, (0, 255, 0), 3)
cv2.imshow("new", roi)
cv2.imshow("identify", frm)
if cv2.waitKey(1) == 27 or count > 300:
cv2.destroyAllWindows()
cap.release()
train()
break
def train():
print("training part initiated !")
recog = cv2.face.LBPHFaceRecognizer_create()
dataset = 'persons'
paths = [os.path.join(dataset, im) for im in os.listdir(dataset)]
faces = []
ids = []
labels = []
for path in paths:
labels.append(path.split('/')[-1].split('-')[0])
ids.append(int(path.split('/')[-1].split('-')[2].split('.')[0]))
faces.append(cv2.imread(path, 0))
recog.train(faces, np.array(ids))
recog.save('model.yml')
return
def identify():
cap = cv2.VideoCapture(0)
filename = "haarcascade_frontalface_default.xml"
paths = [os.path.join("persons", im) for im in os.listdir("persons")]
labelslist = {}
for path in paths:
labelslist[path.split('/')[-1].split('-')[2].split('.')[0]] = path.split('/')[-1].split('-')[0]
print(labelslist)
recog = cv2.face.LBPHFaceRecognizer_create()
recog.read('model.yml')
cascade = cv2.CascadeClassifier(filename)
while True:
_, frm = cap.read()
gray = cv2.cvtColor(frm, cv2.COLOR_BGR2GRAY)
faces = cascade.detectMultiScale(gray, 1.3, 2)
for x, y, w, h in faces:
cv2.rectangle(frm, (x, y), (x + w, y + h), (0, 255, 0), 2)
roi = gray[y:y + h, x:x + w]
label = recog.predict(roi)
if label[1] < 100:
cv2.putText(frm, f"{labelslist[str(label[0])]} + {int(label[1])}", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1,
(0, 0, 255), 3)
else:
cv2.putText(frm, "unkown", (x, y), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 3)
cv2.imshow("identify", frm)
if cv2.waitKey(1) == 27:
cv2.destroyAllWindows()
cap.release()
break
def maincall():
root = tk.Tk()
root.geometry("480x100")
root.title("identify")
label = tk.Label(root, text="Select below buttons ")
label.grid(row=0, columnspan=2)
label_font = font.Font(size=35, weight='bold', family='Helvetica')
label['font'] = label_font
btn_font = font.Font(size=25)
button1 = tk.Button(root, text="Add Member ", command=collect_data, height=2, width=20)
button1.grid(row=1, column=0, pady=(10, 10), padx=(5, 5))
button1['font'] = btn_font
button2 = tk.Button(root, text="Start with known ", command=identify, height=2, width=20)
button2.grid(row=1, column=1, pady=(10, 10), padx=(5, 5))
button2['font'] = btn_font
root.mainloop()
return