Pracownia_programowania/src/smart_intercom.py

376 lines
11 KiB
Python

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:
global name_of_photo
print('taking photo... nr ' + str(name_of_photo))
if name_of_photo > 100:
name_of_photo = 1
img_item = "photos/" + str(name_of_photo) + ".png"
cv2.imwrite(img_item, frame)
name_of_photo = name_of_photo + 1
elif 620 <= y <= 703 and 965 <= x <= 1173:
print('pick up')
elif 620 <= y <= 703 and 1193 <= x <= 1267:
print('off')
with open("variable.pickle", "wb") as f:
pickle.dump(name_of_photo, f)
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_seconds2(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_seconds3(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_seconds1(time):
global temp1
if temp1 == None:
temp1 = datetime.datetime.now()
else:
live_time = datetime.datetime.now()
time_to_expire = datetime.timedelta(0, time)
if (live_time - temp1) < time_to_expire:
live_time = datetime.datetime.now()
else:
temp1 = None
return True
def wait_seconds2(time):
global temp2
if temp2 == None:
temp2 = datetime.datetime.now()
else:
live_time = datetime.datetime.now()
time_to_expire = datetime.timedelta(0, time)
if (live_time - temp2) < time_to_expire:
live_time = datetime.datetime.now()
else:
temp2 = None
return True
def wait_seconds3(time):
global temp3
if temp3 == None:
temp3 = datetime.datetime.now()
else:
live_time = datetime.datetime.now()
time_to_expire = datetime.timedelta(0, time)
if (live_time - temp3) < time_to_expire:
live_time = datetime.datetime.now()
else:
temp3 = None
return True
def most_frequent(List):
return max(set(List), key=List.count)
# Declaration
global displayString, wait_notification, temp1, temp2, temp3, name_of_photo
displayString = ""
wait_notification = 0
temp1 = None
temp2 = None
temp3 = None
name_of_photo = 1
global cam_status
cam_status = 0
List = []
count = 0
with open("variable.pickle", "rb") as f:
name_of_photo = pickle.load(f)
print(name_of_photo)
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:
frame = copy.deepcopy(frameprev)
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 wait_seconds1(10) == True:
List = []
###################
if ret:
# home_screen = copy.deepcopy(frame)
putTimeOnDisplay(frame)
putTimeOnDisplay(frameprev)
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
print("zapisuje " + str(name_of_photo))
with open("variable.pickle", "wb") as f:
pickle.dump(name_of_photo, f)
cap.release()
out.release()
cv2.destroyAllWindows()