Sei sulla pagina 1di 5

from gpiozero import DistanceSensor

from picamera.array import PiRGBArray


from picamera import PiCamera
from time import sleep
import cv2
import pygame.mixer
from pygame.mixer import Sound
from signal import pause
pygame.mixer.init()

#songs
khings = Sound("/home/pi/Robotics II/Q3 Project/songs/khingsong.wav")
kaos = Sound("/home/pi/Robotics II/Q3 Project/songs/kaosong.wav")
vans = Sound("/home/pi/Robotics II/Q3 Project/songs/vansong.wav")
fais = Sound("/home/pi/Robotics II/Q3 Project/songs/faisong.wav")
mints = Sound("/home/pi/Robotics II/Q3 Project/songs/mintsong.wav")
#fords = Sound("/home/pi/Robotics II/Q3 Project/songs/fordsong.wav")
nuts = Sound("/home/pi/Robotics II/Q3 Project/songs/nutsong.wav")
pats = Sound("/home/pi/Robotics II/Q3 Project/songs/patsong.wav")
mrtims = Sound("/home/pi/Robotics II/Q3 Project/songs/mrtimsong.wav")
unknowns = Sound("/home/pi/Robotics II/Q3 Project/songs/unknown.wav")

try:
#mrtims.play()
# use the trainer.yml file that was created with face_trainer.py
print("recognizer")
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('trainer/trainer.yml')

# set the Haar Cascade used for detecting faces


face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')

# set the font for displaying names


font = cv2.FONT_HERSHEY_SIMPLEX

# id counter
id = 0

# names related to ids: example ==> Marcelo: id=1, etc.


names = ['None', 'Van', 'Ichi', 'Fai', 'Pat', 'Mint', 'Khing', 'Ford', 'Nut',
'Kao', 'Mr.Tim']
# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
camera.resolution = (1280, 720)
camera.framerate = 24
camera.rotation = 180
​ (1280, 720))
rawCapture = PiRGBArray(camera, ​size=

print("Starting distance sensor")


#sensor
​ 6, ​trigger​=5)
sensor1 = DistanceSensor(​echo=

numtest = 0
#while True:

#pygame.mixer.music.load('/home/pi/Robotics II/Q3 Project/songs/patsong.wav')


pygame.mixer.music.load('/home/pi/Robotics II/Q3 Project/songs/piano.wav')
pygame.mixer.music.play(-1)
print("piano loop")
sleep(2)
print("piano 2 s sleep")
#pygame.mixer.music.queue('')
#pygamer.mixer.music.queue('')

# capture frames from the camera


print("camera frames")
for frame in camera.capture_continuous(rawCapture, ​format​="bgr",
use_video_port​=True):
#print("check point 2")
if (sensor1.distance * 100 < 15) or (sensor1.distance * 100 > 10):
#print('Face detected')
sleep(0.1)

#print("Test naaaa")
# grab the raw NumPy array representing the image
img = frame.array

# convert to gray scale for the face detect algorithm


gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# use the haar cascade to detect faces
faces = face_cascade.detectMultiScale(gray, 1.3, 5)

# loop through all the faces found and draw rectangles around them
for (x, y, w, h) in faces:
cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)

# save just the face roi on the gray image


roi_gray = gray[y:y + h, x:x + w]

id, confidence = recognizer.predict(roi_gray)

# Check if confidence is less them 100 ==> "0" is perfect match


if (confidence < 48):
id = names[id]
confidence = " {0}%".format(round(100 - confidence))
print("Distance: " + ​str​(sensor1.distance))
​ id) + "|" + "Confidence: " +
print("User: " + ​str(
​ confidence))
str(

if id == "Khing":

pygame.mixer.music.stop()
sleep(2)
print("Khing's song plays.")
khings.play()
sleep(32)
print("Sleep without wake up again :P")
pygame.mixer.music.play(-1)

elif id == "Kao":

pygame.mixer.music.stop()
sleep(2)
print("Kao's song plays.")
kaos.play()
sleep(39)
pygame.mixer.music.play(-1)

elif id == "Van":
pygame.mixer.music.stop()
sleep(2)
print("Van's song plays.")
vans.play()
sleep(38)
pygame.mixer.music.play(-1)

elif id == "Fai":

pygame.mixer.music.stop()
sleep(2)
print("Fai's song plays.")
fais.play()
sleep(31)
pygame.mixer.music.play(-1)

elif id == "Mint":

pygame.mixer.music.stop()
sleep(2)
print("Mint's song plays.")
mints.play()
sleep(32)
pygame.mixer.music.play(-1)

elif id == "Nut":

pygame.mixer.music.stop()
sleep(2)
print("Nut's song plays.")
nuts.play()
sleep(25)
pygame.mixer.music.play(-1)

elif id == "Pat":

pygame.mixer.music.stop()
sleep(2)
print("Pat's song plays.")
pats.play()
sleep(35)
pygame.mixer.music.play(-1)

elif id == "Mr.Tim":

pygame.mixer.music.stop()
sleep(2)
print("Bacon song plays.")
mrtims.play()
sleep(21)
pygame.mixer.music.play(-1)

break
else:
id = "unknown", names[id]

confidence = " {0}%".format(round(100 - confidence))


print("Distance: " + ​str​(sensor1.distance))
​ id) + "|" + "Confidence: " +
print("User: " + ​str(
​ confidence))
str(

print("Beep Sound plays")


unknowns.play()
#cv2.putText(img, str(id), (x + 5, y - 5), font, 1, (255, 255, 255),
2)
#cv2.putText(img, str(confidence), (x + 5, y + h - 5), font, 1,
(255, 255, 0), 1)

#cv2.imshow('camera',img)

k = cv2.waitKey(10) & ​0x​ff # Press 'ESC' for exiting video


if k == 27:
break

# clear the stream in preparation for the next frame


rawCapture.truncate(0)

finally:
cv2.destroyAllWindows(

Potrebbero piacerti anche