adding face recognition

This commit is contained in:
s449273 2020-01-17 00:23:07 +01:00
parent 45ce7aa935
commit 769eabce7a
74 changed files with 418542 additions and 0 deletions

View File

@ -0,0 +1,3 @@
import os
haarcascades = os.path.join(os.path.dirname(__file__), '')

Binary file not shown.

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

File diff suppressed because it is too large Load Diff

57
src/faces-train.py Normal file
View File

@ -0,0 +1,57 @@
import cv2
import os
import numpy as np
from PIL import Image
import pickle
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
image_dir = os.path.join(BASE_DIR, "images")
face_cascade = cv2.CascadeClassifier(
'cascades/data/haarcascade_frontalface_alt2.xml')
recognizer = cv2.face.LBPHFaceRecognizer_create()
current_id = 0
label_ids = {}
y_labels = []
x_train = []
for root, dirs, files in os.walk(image_dir):
# print(root)
# print(dirs)
# print(files)
for file in files:
if file.endswith("png") or file.endswith("jpg"):
path = os.path.join(root, file)
label = os.path.basename(root).replace(" ", "-").lower()
#print(label, path)
if not label in label_ids:
label_ids[label] = current_id
current_id += 1
id_ = label_ids[label]
# print(label_ids)
# y_labels.append(label) #some number
# x_train.append(path) # verify this image turn into a numpy array ,gray
pil_image = Image.open(path).convert("L") # gray scale
size = (550, 550)
final_image = pil_image.resize(size, Image.ANTIALIAS)
image_array = np.array(final_image, "uint8")
# print(image_array)
faces = face_cascade.detectMultiScale(
image_array, scaleFactor=1.5, minNeighbors=5)
for (x, y, w, h) in faces:
roi = image_array[y:y + h, x:x + w]
x_train.append(roi)
y_labels.append(id_)
# print(y_labels)
# print(x_train)
with open("labels.pickle", "wb") as f:
pickle.dump(label_ids, f)
recognizer.train(x_train, np.array(y_labels))
recognizer.save("trainner.yml")

BIN
src/images/bartek/1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 59 KiB

BIN
src/images/bartek/2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 334 KiB

BIN
src/images/bartek/3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 379 KiB

BIN
src/images/bartek/4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 320 KiB

BIN
src/images/bartek/5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

BIN
src/images/bartek/6.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 28 KiB

BIN
src/images/black.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.2 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 329 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 79 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 40 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 57 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 5.9 KiB

BIN
src/images/fun/glasses.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 6.8 KiB

BIN
src/images/fun/mustache.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 11 KiB

BIN
src/images/is_coming.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 578 B

BIN
src/images/justin/1.jpg Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 133 KiB

BIN
src/images/justin/2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 MiB

BIN
src/images/justin/3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 164 KiB

BIN
src/images/justin/4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 779 KiB

BIN
src/images/justin/5.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 285 KiB

BIN
src/images/justin/6.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 212 KiB

BIN
src/images/justin/7.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 41 KiB

BIN
src/images/justin/8.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 307 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 111 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 480 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 33 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 135 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 88 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 54 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 29 KiB

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 203 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 38 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 76 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 47 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.1 KiB

BIN
src/images/paulina/1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 320 KiB

BIN
src/images/paulina/2.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 224 KiB

BIN
src/images/paulina/3.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 299 KiB

BIN
src/images/paulina/4.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 198 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 293 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 63 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 21 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 16 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 25 KiB

32
src/labels.pickle Normal file
View File

@ -0,0 +1,32 @@
(dp0
S'fun'
p1
I9
sS'paulina'
p2
I3
sS'kit-harington'
p3
I1
sS'justin'
p4
I4
sS'peter-dinklage'
p5
I6
sS'emilia-clarke'
p6
I8
sS'nikolaj-coster-waldau'
p7
I2
sS'images'
p8
I0
sS'logo'
p9
I5
sS'bartek'
p10
I7
s.

BIN
src/my-image.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.2 KiB

322
src/smart_intercom.py Normal file
View File

@ -0,0 +1,322 @@
import cv2
import datetime
import time
import numpy as np
from pygame import mixer
import pickle
import copy
def alert():
mixer.init()
alert = mixer.Sound('noises/beep-07.wav')
for i in range(5):
alert.play()
time.sleep(0.1)
alert.play()
def click_event(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
if 620 <= y <= 703 and 20 <= x <= 169:
print('cam')
global cam_status
if cam_status == 1:
cam_status = 0
elif cam_status == 0:
cam_status = 1
elif 620 <= y <= 703 and 189 <= x <= 448:
print('doorbell')
alert()
elif 620 <= y <= 703 and 468 <= x <= 734:
print('open gate')
global wait_notification
wait_notification = 1
elif 620 <= y <= 703 and 754 <= x <= 945:
print('listen')
elif 620 <= y <= 703 and 965 <= x <= 1173:
print('pick up')
elif 620 <= y <= 703 and 1193 <= x <= 1267:
print('off')
cap.release()
out.release()
cv2.destroyAllWindows()
exit()
def click_event2(event, x, y, flags, param):
if event == cv2.EVENT_LBUTTONDOWN:
global displayString
if 348 <= y <= 370 and 59 <= x <= 86:
print("1 outdoor")
displayString = displayString + '1'
print(displayString)
if 348 <= y <= 370 and 100 <= x <= 128:
print("2 outdoor")
displayString = displayString + '2'
print(displayString)
if 348 <= y <= 370 and 142 <= x <= 169:
print("3 outdoor")
displayString = displayString + '3'
print(displayString)
if 390 <= y <= 412 and 59 <= x <= 86:
print("4 outdoor")
displayString = displayString + '4'
print(displayString)
if 390 <= y <= 412 and 100 <= x <= 128:
print("5 outdoor")
displayString = displayString + '5'
print(displayString)
if 390 <= y <= 412 and 142 <= x <= 169:
print("6 outdoor")
displayString = displayString + '6'
print(displayString)
if 431 <= y <= 453 and 59 <= x <= 86:
print("7 outdoor")
displayString = displayString + '7'
print(displayString)
if 431 <= y <= 453 and 100 <= x <= 128:
print("8 outdoor")
displayString = displayString + '8'
print(displayString)
if 431 <= y <= 453 and 142 <= x <= 169:
print("9 outdoor")
displayString = displayString + '9'
print(displayString)
if 472 <= y <= 495 and 59 <= x <= 86:
print("* outdoor")
displayString = displayString + '*'
print(displayString)
print("0 outdoor")
displayString = displayString + '0'
print(displayString)
if 472 <= y <= 495 and 142 <= x <= 169:
print("# outdoor")
displayString = displayString + '#'
print(displayString)
display_work(len(displayString))
def display_work(length):
global displayString
if length == 1:
onestar = cv2.imread('images/*.png')
outDoor[220:304, 54:214] = onestar
elif length == 2:
twostar = cv2.imread('images/**.png')
outDoor[220:304, 54:214] = twostar
elif length == 3:
threestar = cv2.imread('images/***.png')
outDoor[220:304, 54:214] = threestar
elif length == 4:
if displayString == "1234":
goodpass = cv2.imread('images/goodpass.png')
outDoor[220:304, 54:214] = goodpass
if wait_seconds(3) == True:
displayString = displayString + '#'
else:
badpass = cv2.imread('images/badpass.png')
outDoor[220:304, 54:214] = badpass
elif length == 5:
displayString = ''
def notification():
not1 = cv2.imread('notification/not1.png')
frame[50:50 + 139, 800:800 + 406] = not1
if wait_seconds(3) == True:
global wait_notification
wait_notification = 0
def putOnDisplay(frame, fheight, sheight, fwidth,
swidth, src):
img = cv2.imread(src)
frame[fheight: sheight, fwidth: swidth] = img
def putTimeOnDisplay(frame):
font = cv2.FONT_HERSHEY_SIMPLEX
actual_time = str(
datetime.datetime.now().strftime("%b %d %Y %H:%M:%S"))
cv2.putText(frame, actual_time, (10, 50),
font, 1, (0, 255, 255), 2, cv2.LINE_AA)
def wait_3_sec(start):
live_time = datetime.datetime.now()
time_to_expire = datetime.timedelta(0, 3)
if (live_time - start) < time_to_expire:
live_time = datetime.datetime.now()
else:
return True
def wait_seconds(time):
global pusto
if pusto == None:
pusto = datetime.datetime.now()
else:
live_time = datetime.datetime.now()
time_to_expire = datetime.timedelta(0, time)
if (live_time - pusto) < time_to_expire:
live_time = datetime.datetime.now()
else:
pusto = None
return True
def most_frequent(List):
return max(set(List), key=List.count)
# Declaration
global displayString, wait_notification, pusto
displayString = ""
wait_notification = 0
pusto = None
global cam_status
cam_status = 1
List = []
count = 0
cap = cv2.VideoCapture(0)
four_cc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('recordings/output.avi', four_cc, 10.0, (1280, 720))
cap.set(3, 1280)
cap.set(4, 720)
# 3
face_cascade = cv2.CascadeClassifier(
'cascades/data/haarcascade_frontalface_alt2.xml')
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read("trainner.yml")
labels = {}
with open("labels.pickle", "rb") as f:
og_labels = pickle.load(f)
labels = {v: k for k, v in og_labels.items()}
###
################################################################################
while True:
if not cap.isOpened():
print("Camera error")
break
ret, frameprev = cap.read()
if cam_status == 1:
frame = copy.deepcopy(frameprev)
if cam_status == 0:
putOnDisplay(frame, 0, 720, 0, 1280, "images/black.png")
# 3
gray = cv2.cvtColor(frameprev, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(
gray, scaleFactor=1.5, minNeighbors=5)
if len(List) < 11:
for (x, y, w, h) in faces:
# print(x, y, w, h)
roi_gray = gray[y:y+h, x:x+w] # (ycord_start, ycord_end)
roi_color = frameprev[y:y + h, x:x + w]
id_, conf = recognizer.predict(roi_gray)
if conf >= 1: # and conf <= 85:
# print(id_)
count = count + 1
print(str(count) + " " + labels[id_])
font = cv2.FONT_HERSHEY_SIMPLEX
name = labels[id_]
color = (255, 255, 255)
cv2.putText(frame, name, (x, y), font,
1, color, 2, cv2.LINE_AA)
cam_status = 1
img_item = "my-image.png"
cv2.imwrite(img_item, roi_gray)
color = (255, 0, 0) # BGR 0-255
stroke = 2
end_cord_x = x + w
end_cord_y = y + h
cv2.rectangle(frame, (x, y), (end_cord_x,
end_cord_y), color, stroke)
List.append(labels[id_])
if len(List) == 11:
font = cv2.FONT_HERSHEY_SIMPLEX
putOnDisplay(frame, 120, 255, 60, 430, "images/is_coming.png")
cv2.putText(frame, most_frequent(List), (80, 170),
font, 1, (0, 255, 255), 2, cv2.LINE_AA)
cv2.putText(frame, "is coming!", (250, 225),
font, 1, (0, 255, 255), 2, cv2.LINE_AA)
###################
if ret:
# home_screen = copy.deepcopy(frame)
putTimeOnDisplay(frame)
putOnDisplay(frame, 620, 720, 0, 1280, 'images/menu.png')
putOnDisplay(frame, 640, 640 + 63, 20, 169, 'icons/icon1.png')
putOnDisplay(frame, 640, 640 + 63, 189, 448, 'icons/icon2.png')
putOnDisplay(frame, 640, 640 + 63, 468, 734, 'icons/icon3.png')
putOnDisplay(frame, 640, 640 + 63, 754, 945, 'icons/icon4.png')
putOnDisplay(frame, 640, 640 + 63, 965, 1173, 'icons/icon5.png')
putOnDisplay(frame, 640, 640 + 63, 1193, 1267, 'icons/icon6.png')
if wait_notification == 1:
notification()
cv2.imshow('frame', frame)
outDoor = cv2.imread('images/outDoorPanel.png')
display_work(len(displayString))
#cv2.imshow('outDoor', outDoor)
out.write(frameprev)
cv2.setMouseCallback('frame', click_event)
#cv2.setMouseCallback('outDoor', click_event2)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()

19
src/test.py Normal file
View File

@ -0,0 +1,19 @@
def most_frequent(List):
return max(set(List), key = List.count)
List = []
List = ["1", "2", "3", "4", "5", "2"]
print(List)
List.append("3")
print(List)
print(len(List))
#List = [2, 1, 2, 2, 1, 3]
#print(most_frequent(List))s

66138
src/trainner.yml Normal file

File diff suppressed because it is too large Load Diff