Skip to content

Commit 36e8a5e

Browse files
authored
Merge pull request #1265 from J-B-Mugundh/file-locking
Added File Locking Mechanism
2 parents 2ab0b11 + e98f8c5 commit 36e8a5e

File tree

9 files changed

+26255
-0
lines changed

9 files changed

+26255
-0
lines changed
Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
import cv2
2+
import os
3+
import sys
4+
import numpy as np
5+
import FaceDetection
6+
import warnings
7+
from os import system
8+
import os
9+
warnings.filterwarnings("ignore")
10+
faces=[]
11+
labels=[]
12+
names={}
13+
dirpath = os.getcwd()
14+
training_folder = dirpath+"\\Face_Recognition_Script\\training-data"
15+
16+
def newUser():
17+
name = input("Enter Your Name: ")
18+
dirs = os.listdir(training_folder)
19+
os.makedirs(training_folder+'/'+name+'@'+str(len(dirs)+1))
20+
cap = cv2.VideoCapture(0)
21+
i=0
22+
while (True):
23+
ret, frame = cap.read()
24+
test = frame.copy()
25+
frame,frame_crop,rect = FaceDetection.detect_faces(FaceDetection.lbp_face_cascade,frame)
26+
cv2.imshow('Smile :) with different moods', frame)
27+
cv2.waitKey(50)
28+
if frame_crop!="None" and i<100:
29+
print(training_folder+"/" + name + '@' + str(len(dirs)+1) + '/' + str(i) + '.jpg')
30+
cv2.imwrite(training_folder+"/" + name + '@' + str(len(dirs)+1) + '/' + str(i) + '.jpg', frame_crop)
31+
#cv2.imwrite("sample.jpg",test)
32+
i+=1
33+
elif i>=100:
34+
break
35+
36+
cap.release()
37+
cv2.destroyAllWindows()
38+
39+
40+
41+
def createLables():
42+
dirs = os.listdir(training_folder)
43+
for users in dirs:
44+
lable = int(users[users.find("@")+1:len(users)])
45+
names[lable] = users[0:users.find("@")]
46+
subfolders = training_folder + "/" + users
47+
imagesName = os.listdir(subfolders)
48+
for image in imagesName:
49+
imagePath = subfolders + "/" + image
50+
face = cv2.imread(imagePath)
51+
face = cv2.cvtColor(face,cv2.COLOR_BGR2GRAY)
52+
#cv2.imshow("Training on this image...",face)
53+
#cv2.waitKey(10)
54+
#cv2.destroyAllWindows()
55+
faces.append(face)
56+
labels.append(lable)
57+
#print("Labels: "+ str(labels))
58+
#print("Total Number of Faces: "+str(len(faces)))
59+
#print(names)
60+
61+
face_recognizer = object
62+
def trainDataLBPH():
63+
# create our LBPH face recognizer
64+
#face_recognizer = cv2.
65+
global face_recognizer
66+
if len(labels)>0:
67+
face_recognizer = cv2.face.createLBPHFaceRecognizer()
68+
face_recognizer.train(faces, np.array(labels))
69+
else:
70+
print("No train data is present. Add train data using -train flag.")
71+
sys.exit()
72+
def trainDataEigen():
73+
# or use EigenFaceRecognizer by replacing above line with
74+
if len(labels)>0:
75+
face_recognizer = cv2.face.createEigenFaceRecognizer()
76+
face_recognizer.train(faces, np.array(labels))
77+
else:
78+
print("No train data is present. Add train data using -train flag.")
79+
sys.exit()
80+
def trainDataFisher():
81+
# or use FisherFaceRecognizer by replacing above line with
82+
if len(labels)>0:
83+
face_recognizer = cv2.face.createFisherFaceRecognizer()
84+
face_recognizer.train(faces, np.array(labels))
85+
else:
86+
print("No train data is present. Add train data using -train flag.")
87+
sys.exit()
88+
89+
90+
def draw_rectangle(img, rect):
91+
(x, y, w, h) = rect
92+
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
93+
def draw_text(img, text, x, y):
94+
cv2.putText(img, text, (x, y), cv2.FONT_HERSHEY_PLAIN, 1.5, (0, 255, 0), 2)
95+
96+
97+
def predict(test_img):
98+
img = test_img
99+
img, face, rect = FaceDetection.detect_faces(FaceDetection.haar_face_cascade,img,1.1)
100+
if face=="None":
101+
pass
102+
else:
103+
face = cv2.cvtColor(np.array(face,dtype=np.uint16),cv2.COLOR_BGR2GRAY)
104+
label,conf = face_recognizer.predict(np.array(face,dtype=np.uint16))
105+
if label==-1:
106+
label_text = "unknown"
107+
else:
108+
label_text = names[label]
109+
draw_rectangle(img, rect)
110+
draw_text(img, label_text, rect[0], rect[1] - 5)
111+
112+
return img
113+
114+
def newUserTest():
115+
cap = cv2.VideoCapture(0)
116+
os.system('cls')
117+
previous_label = ""
118+
while (True):
119+
ret, frame = cap.read()
120+
#test = frame.copy()
121+
frame,frame_crop,rect = FaceDetection.detect_faces(FaceDetection.haar_face_cascade,frame,1.1)
122+
if frame_crop == "None":
123+
pass
124+
else:
125+
126+
frame_crop = cv2.cvtColor(np.array(frame_crop, dtype=np.uint16), cv2.COLOR_BGR2GRAY)
127+
label, conf = face_recognizer.predict(np.array(frame_crop, dtype=np.uint16))
128+
if label == -1:
129+
label_text = "unknown"
130+
else:
131+
label_text = names[label]
132+
#label_text = names[label]
133+
# print(face)
134+
draw_rectangle(frame, rect)
135+
global pass_name
136+
if previous_label!=label_text:
137+
os.system('cls')
138+
previous_label = label_text
139+
print(label_text)
140+
if label_text==pass_name and pass_name!='':
141+
sys.exit()
142+
draw_text(frame, label_text, rect[0], rect[1] - 5)
143+
cv2.imshow('Smile :) with different moods', frame)
144+
if cv2.waitKey(1) & 0xFF == ord('q'):
145+
#cv2.imwrite("sample.jpg",test)
146+
break
147+
148+
cap.release()
149+
cv2.destroyAllWindows()
150+
151+
if __name__ == '__main__':
152+
if len(sys.argv)>1:
153+
if str(sys.argv[1]) == '-train':
154+
newUser()
155+
elif str(sys.argv[1]) == '-run':
156+
pass_name=''
157+
createLables()
158+
os.system('cls')
159+
trainDataLBPH()
160+
os.system('cls')
161+
newUserTest()
162+
else:
163+
pass_name = sys.argv[1]
164+
createLables()
165+
os.system('cls')
166+
trainDataLBPH()
167+
os.system('cls')
168+
newUserTest()
169+
else:
170+
createLables()
171+
os.system('cls')
172+
trainDataLBPH()
173+
os.system('cls')
174+
newUserTest()
175+
176+
newUser()
177+
createLables()
178+
os.system('cls')
179+
trainDataLBPH()
180+
os.system('cls')
181+
newUserTest()
182+
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
'''
2+
detectMultiScale(image, scaleFactor, minNeighbors):
3+
This is a general function to detect objects, in this case, it'll detect faces since we called in the face cascade.
4+
If it finds a face, it returns a list of positions of said face in the form “Rect(x,y,w,h).”, if not, then returns “None”.
5+
Image:
6+
The first input is the grayscale image. So make sure the image is in grayscale.
7+
scaleFactor:
8+
This function compensates a false perception in size that occurs when one face appears to be bigger than the other simply because it is closer to the camera.
9+
minNeighbors:
10+
This is a detection algorithm that uses a moving window to detect objects,
11+
it does so by defining how many objects are found near the current one before it can declare the face found.
12+
'''
13+
14+
import cv2
15+
16+
haar_face_cascade = cv2.CascadeClassifier('E://PYTHON//Windows-Folder-Unlock-Using-Face-Recognition-master//Face_Recognition_Script//haarcascade_frontalface_alt.xml')
17+
lbp_face_cascade = cv2.CascadeClassifier('E://PYTHON//Windows-Folder-Unlock-Using-Face-Recognition-master//Face_Recognition_Script//lbpcascade_frontalface.xml')
18+
19+
20+
def detect_faces(f_cascade, colored_img, scaleFactor=1.1):
21+
img_copy = colored_img
22+
# convert the test image to gray image as opencv face detector expects gray images
23+
gray = cv2.cvtColor(img_copy, cv2.COLOR_BGR2GRAY)
24+
# let's detect multiscale (some i
25+
# mages may be closer to camera than others) images
26+
faces = f_cascade.detectMultiScale(gray, scaleFactor=scaleFactor, minNeighbors=5);
27+
# go over list of faces and draw them as rectangles on original colored img
28+
x=0
29+
y=0
30+
z=0
31+
w = 0
32+
if len(faces)==0:
33+
return img_copy,"None","None"
34+
for (x, y, w, h) in faces:
35+
cv2.rectangle(img_copy, (x, y), (x + w, y + h), (0, 255, 0), 2)
36+
return img_copy,img_copy[y:y+w, x:x+h], faces[0]
37+
38+
def staticFaceDetectHaar(img):
39+
test1 = cv2.imread(img)
40+
test1 = detect_faces(haar_face_cascade,test1)
41+
cv2.imshow('finanl',test1)
42+
cv2.waitKey(0)
43+
cv2.destroyAllWindows()
44+
45+
def staticFaceDetectLbp(img):
46+
test1 = cv2.imread(img)
47+
test1 = detect_faces(lbp_face_cascade,test1)
48+
cv2.imshow('finanl',test1)
49+
cv2.waitKey(0)
50+
cv2.destroyAllWindows()
51+
52+
def liveFaceDetectLbp():
53+
cap = cv2.VideoCapture(0)
54+
while(True):
55+
ret, frame = cap.read()
56+
frame = detect_faces(lbp_face_cascade,frame,1.1)
57+
cv2.imshow("frame",frame)
58+
if cv2.waitKey(1) & 0xFF == ord('q'):
59+
break
60+
cap.release()
61+
cv2.destroyAllWindows()
62+
63+
def liveFaceDetectHaar():
64+
cap = cv2.VideoCapture(0)
65+
while(True):
66+
ret, frame = cap.read()
67+
frame = detect_faces(haar_face_cascade,frame,1.1)
68+
cv2.imshow("frame",frame)
69+
if cv2.waitKey(1) & 0xFF == ord('q'):
70+
break
71+
cap.release()
72+
cv2.destroyAllWindows()
73+
74+
75+
#liveFaceDetectHaar()
76+
#liveFaceDetectLbp()

0 commit comments

Comments
 (0)