-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopenCVfaceCheckLearn.py
More file actions
72 lines (68 loc) · 2.82 KB
/
openCVfaceCheckLearn.py
File metadata and controls
72 lines (68 loc) · 2.82 KB
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
import cv2
import numpy as np
from os import listdir
from os.path import isfile, join
data_path = 'faces/'
onlyfiles = [f for f in listdir(data_path) if isfile(join(data_path,f))]
Training_Data, Labels = [], []
for i, files in enumerate(onlyfiles):
image_path = data_path + onlyfiles[i]
images = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
if images is None:
continue
Training_Data.append(np.asarray(images, dtype=np.uint8))
Labels.append(i)
if len(Labels) == 0:
print("There is no data to train.")
exit()
Labels = np.asarray(Labels, dtype=np.int32)
model = cv2.face.LBPHFaceRecognizer_create()
model.train(np.asarray(Training_Data), np.asarray(Labels))
print("Model Training Complete!!!!!")
face_classifier = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
def face_detector(img, size = 0.5):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(gray,1.3,5)
if faces is():
return img,[]
for(x,y,w,h) in faces:
cv2.rectangle(img, (x,y),(x+w,y+h),(0,255,255),2)
roi = img[y:y+h, x:x+w]
roi = cv2.resize(roi, (200,200))
return img,roi #검출된 좌표에 사각 박스 그리고(img), 검출된 부위를 잘라(roi) 전달
#카메라 열기
cap = cv2.VideoCapture(0)
while True:
#카메라로 부터 사진 한장 읽기
ret, frame = cap.read()
# 얼굴 검출 시도
image, face = face_detector(frame)
try:
#검출된 사진을 흑백으로 변환
face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
#위에서 학습한 모델로 예측시도
result = model.predict(face)
#result[1]은 신뢰도이고 0에 가까울수록 자신과 같다는 뜻이다.
if result[1] < 500:
# 0~100으로 유사도 변경
confidence = int(100*(1-(result[1])/300))
# 유사도 화면에 표시
display_string = str(confidence)+'% Confidence it is user'
cv2.putText(image,display_string,(100,120), cv2.FONT_HERSHEY_COMPLEX,1,(250,120,255),2)
#75 보다 크면 동일 인물로 간주해 UnLocked!
if confidence > 75:
cv2.putText(image, "Unlocked", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 255, 0), 2)
cv2.imshow('Face Cropper', image)
else:
#75 이하면 타인.. Locked!!!
cv2.putText(image, "Locked", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (0, 0, 255), 2)
cv2.imshow('Face Cropper', image)
except:
#얼굴 검출 안됨
cv2.putText(image, "Face Not Found", (250, 450), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 0, 0), 2)
cv2.imshow('Face Cropper', image)
pass
if cv2.waitKey(1)==13:
break
cap.release()
cv2.destroyAllWindows()